Adding posts with polls

This commit is contained in:
aymm 2021-04-05 10:54:30 +02:00
parent 9f0c8ba3c6
commit 2b45c9fdd0
Signed by: phlaym
GPG Key ID: A06651BAB6777237
6 changed files with 85 additions and 38 deletions

View File

@ -648,16 +648,18 @@ class APnutI
string $text, string $text,
int $reply_to, int $reply_to,
bool $is_nsfw = false, bool $is_nsfw = false,
bool $auto_crop = false bool $auto_crop = false,
array $raw = []
): Post { ): Post {
return createPost($text, $reply_to, $is_nsfw, $auto_crop); return createPost($text, $reply_to, $is_nsfw, $auto_crop, $raw);
} }
public function createPost( public function createPost(
string $text, string $text,
bool $is_nsfw = false, bool $is_nsfw = false,
bool $auto_crop = false, bool $auto_crop = false,
?int $reply_to = null ?int $reply_to = null,
array $raw = []
): Post { ): Post {
$text = $auto_crop ? substr($text, 0, $this->getMaxPostLength()) : $text; $text = $auto_crop ? substr($text, 0, $this->getMaxPostLength()) : $text;
$parameters = [ $parameters = [
@ -665,7 +667,25 @@ 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), $this); if (!empty($raw)) {
$parameters['raw'] = $parameters;
}
return new Post($this->post('/posts', $parameters, 'application/json'), $this);
}
public function createPostWithParameters(
string $text,
array $params = [],
bool $auto_crop = false,
): Post {
$text = $auto_crop ? substr($text, 0, $this->getMaxPostLength()) : $text;
$parameters = [
'text' => $text,
];
$parameters = array_merge($parameters, $params);
$this->logger->debug('Post with params');
$this->logger->debug(json_encode($parameters));
return new Post($this->post('/posts', $parameters, 'application/json'), $this);
} }
protected function fetchPnutSystemConfig() protected function fetchPnutSystemConfig()
@ -713,7 +733,7 @@ class APnutI
'client_secret' => $this->client_secret, 'client_secret' => $this->client_secret,
'grant_type' => 'client_credentials' 'grant_type' => 'client_credentials'
]; ];
$resp = $this->post('oauth/access_token', $params); $resp = $this->post('/oauth/access_token', $params);
if (!empty($resp['access_token'])) { if (!empty($resp['access_token'])) {
$this->logger->info(json_encode($resp)); $this->logger->info(json_encode($resp));
return $resp['access_token']; return $resp['access_token'];

View File

@ -190,6 +190,29 @@ class Poll
return new Poll($resp, $api); return new Poll($resp, $api);
} }
public static function makePollNoticeRaw(int $poll_id, string $poll_token)
{
/*return [
'type' => 'io.pnut.core.poll-notice',
'value' => [
'+io.pnut.core.poll' => [
'poll_id' => $poll_id,
'poll_token' => $poll_token
]
]
];*/
return [
'io.pnut.core.poll-notice' => [
[
'+io.pnut.core.poll' => [
'poll_id' => $poll_id,
'poll_token' => $poll_token
]
]
]
];
}
public function __toString(): string public function __toString(): string
{ {
if (!empty($this->user)) { if (!empty($this->user)) {

View File

@ -9,7 +9,7 @@ use APnutI\Entities\PostContent;
class Post class Post
{ {
public DateTime $created_at; public \DateTime $created_at;
public int $id; public int $id;
public bool $is_deleted = false; public bool $is_deleted = false;
public bool $is_nsfw = false; public bool $is_nsfw = false;
@ -30,7 +30,11 @@ class Post
public function __construct(array $data, APnutI $api) public function __construct(array $data, APnutI $api)
{ {
$this->api = $api; $this->api = $api;
$this->api->logger->debug('constructing post');
$this->api->logger->debug(json_encode($data));
$this->api->logger->debug('Created string: ' . $data['created_at']);
$this->created_at = new \DateTime($data['created_at']); $this->created_at = new \DateTime($data['created_at']);
$this->api->logger->debug(json_encode($this->created_at));
$this->id = (int)$data['id']; $this->id = (int)$data['id'];
if (!empty($data['is_deleted'])) { if (!empty($data['is_deleted'])) {
$this->is_deleted = (bool)$data['is_deleted']; $this->is_deleted = (bool)$data['is_deleted'];
@ -46,7 +50,7 @@ class Post
} }
#file_put_contents(__DIR__."/post_log.log", json_encode($data['user']), FILE_APPEND | LOCK_EX); #file_put_contents(__DIR__."/post_log.log", json_encode($data['user']), FILE_APPEND | LOCK_EX);
if (!empty($data['user'])) { if (!empty($data['user'])) {
$this->user = new User($data['user']); $this->user = new User($data['user'], $api);
} }
#file_put_contents(__DIR__."/post_log.log", json_encode($this->user), FILE_APPEND | LOCK_EX); #file_put_contents(__DIR__."/post_log.log", json_encode($this->user), FILE_APPEND | LOCK_EX);
$this->thread_id = (int)$data['thread_id']; $this->thread_id = (int)$data['thread_id'];

View File

@ -0,0 +1,22 @@
<?php
namespace APnutI\Entities;
class PostContent
{
public string $text;
public ?string $html = null;
public array $entities;
public bool $links_not_parsed = false;
public function __construct(array $data)
{
$this->text = $data['text'];
if (!empty($data['html'])) {
$this->html = $data['html'];
}
$this->entities = $data['entities'];
if (!empty($data['links_not_parsed'])) {
$this->links_not_parsed = (bool)$data['links_not_parsed'];
}
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace APnutI\Entities;
class PostCounts
{
public int $bookmarks;
public int $replies;
public int $reposts;
public int $threads;
public function __construct(array $data)
{
$this->bookmarks = (int)$data['bookmarks'];
$this->replies = (int)$data['replies'];
$this->reposts = (int)$data['reposts'];
$this->threads = (int)$data['threads'];
}
}

View File

@ -1,22 +1,18 @@
<?php <?php
namespace APnutI\Entities; namespace APnutI\Entities;
class PostContent class PostCounts
{ {
public string $text; public int $bookmarks;
public ?string $html = null; public int $replies;
public array $entities; public int $reposts;
public bool $links_not_parsed = false; public int $threads;
public function __construct(array $data) public function __construct(array $data)
{ {
$this->text = $data['text']; $this->bookmarks = (int)$data['bookmarks'];
if (!empty($data['html'])) { $this->replies = (int)$data['replies'];
$this->html = $data['html']; $this->reposts = (int)$data['reposts'];
} $this->threads = (int)$data['threads'];
$this->entities = $data['entities'];
if (!empty($data['links_not_parsed'])) {
$this->links_not_parsed = (bool)$data['links_not_parsed'];
}
} }
} }