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

View File

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

View File

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

View File

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