Added avatar fetching to user

This commit is contained in:
aymm 2021-03-11 16:47:06 +00:00
parent 75200da1bc
commit 1f84767a67
4 changed files with 38 additions and 21 deletions

View File

@ -401,7 +401,7 @@ class APnutI
$posts = $this->get('/users/' . $username . '/posts', $params);
$p = [];
foreach ($posts as $post) {
$p[] = new Post($post);
$p[] = new Post($post, $this);
}
var_dump($p);
}
@ -429,7 +429,7 @@ class APnutI
$args['before_id'] = $this->meta->min_id;
}
foreach ($posts as $post) {
$post_obj[] = new Post($post);
$post_obj[] = new Post($post, $this);
}
} while ($this->meta != null
&& $this->meta->more
@ -475,7 +475,7 @@ class APnutI
{
try {
$res = $this->get('/polls/' . $poll_id);
return new Poll($res);
return new Poll($res, $this);
} catch (NotSupportedPollException $e) {
$this->logger->error('Poll not supported: '.json_encode($res));
throw $e;
@ -492,7 +492,7 @@ class APnutI
public function getUser(int $user_id, array $args = [])
{
return new User($this->get('/users/'.$user_id, $args));
return new User($this->get('/users/'.$user_id, $args), $this);
}
public function getPost(int $post_id, array $args = [])
@ -505,7 +505,7 @@ class APnutI
// Remove in production again
try {
$p = new Post($this->get('/posts/'.$post_id, $args));
$p = new Post($this->get('/posts/'.$post_id, $args), $this);
$this->logger->debug(json_encode($p));
return $p;
} catch (NotAuthorizedException $nae) {
@ -520,7 +520,7 @@ class APnutI
'application/json',
true
);
return new Post($r);
return new Post($r, $this);
}
}
@ -568,7 +568,8 @@ class APnutI
$cf = new \CURLFile($file_path, $content_type, $filename);
$parameters = ['avatar' => $cf];
return new User(
$this->post('/users/me/avatar', $parameters, 'multipart/form-data')
$this->post('/users/me/avatar', $parameters, 'multipart/form-data'),
$this
);
}
@ -601,7 +602,7 @@ class APnutI
'reply_to' => $reply_to,
'is_nsfw' => $is_nsfw,
];
return new Post($this->post('posts', $parameters));
return new Post($this->post('posts', $parameters), $this);
}
protected function fetchPnutSystemConfig()

View File

@ -3,6 +3,7 @@ namespace APnutI\Entities;
use APnutI\Entities\PollOption;
use APnutI\Entities\User;
use APnutI\Entities\APnutI;
use APnutI\Entities\Source;
use APnutI\Exceptions\NotSupportedPollException;
@ -20,6 +21,8 @@ class Poll
public ?Source $source = null;
public string $type;
private APnutI $api;
public static string $notice_type = 'io.pnut.core.poll-notice';
protected static array $poll_types = [
'general.poll',
@ -29,8 +32,9 @@ class Poll
'nl.chimpnut.quizbot.attachment.poll'
];
public function __construct(array $data)
public function __construct(array $data, APnutI $api)
{
$this->api = $api;
$this->options = [];
$this->type = $data['type'];
if ($data['type'] === Poll::$notice_type) {

View File

@ -3,6 +3,7 @@ namespace APnutI\Entities;
use APnutI\Entities\User;
use APnutI\Entities\Source;
use APnutI\Entities\APnutI;
use APnutI\Entities\PostCounts;
use APnutI\Entities\PostContent;
@ -24,8 +25,11 @@ class Post
public bool $you_bookmarked = false;
public bool $you_reposted = false;
public function __construct(array $data)
private APnutI $api;
public function __construct(array $data, APnutI $api)
{
$this->api = $api;
$this->created_at = new \DateTime($data['created_at']);
$this->id = (int)$data['id'];
if (!empty($data['is_deleted'])) {

View File

@ -3,6 +3,7 @@ namespace APnutI\Entities;
use APnutI\Entities\Image;
use APnutI\Entities\Badge;
use APnutI\Entities\APnutI;
class User
{
@ -13,11 +14,13 @@ class User
public string $username;
public ?string $name = null;
public ?bool $presence = null;
#private LoggerInterface $log;
public function __construct(array $data)
private APnutI $api;
public function __construct(array $data, APnutI $api)
{
#$this->log = new Logger('User');
$this->api = $api;
$this->id = (int)$data['id'];
$this->username = $data['username'];
if (!empty($data['badge'])) {
@ -49,14 +52,19 @@ class User
}
}
public function getPresenceString(): string
{
if ($this->presence === true) {
return "online";
} elseif ($this->presence === false) {
return "offline";
} else {
return "presence unknown";
public function getAvatarUrl(
?int $width = null,
?int $height = null
): string {
if (empty($this->avatar_image)) {
return $this->api->getAvatarUrl($this->id, $width, $height);
}
$query = '';
if (!empty($width)) {
$query = '?w='.$width;
} elseif (!empty($height)) {
$query = '?h='.$height;
}
return $this->avatar_image->link.$query;
}
}