Added basic channel support

This commit is contained in:
aymm 2021-04-18 12:19:54 +02:00
parent 9b64114325
commit 18eb80cb6f
Signed by: phlaym
GPG Key ID: A06651BAB6777237
3 changed files with 136 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace APnutI;
use APnutI\Entities\Post; use APnutI\Entities\Post;
use APnutI\Entities\Poll; use APnutI\Entities\Poll;
use APnutI\Entities\User; use APnutI\Entities\User;
use APnutI\Entities\Channel;
use APnutI\Exceptions\PnutException; use APnutI\Exceptions\PnutException;
use APnutI\Exceptions\NotFoundException; use APnutI\Exceptions\NotFoundException;
use APnutI\Exceptions\NotAuthorizedException; use APnutI\Exceptions\NotAuthorizedException;
@ -688,6 +689,29 @@ class APnutI
return new Post($this->post('/posts', $parameters, 'application/json'), $this); return new Post($this->post('/posts', $parameters, 'application/json'), $this);
} }
public function getChannel(int $channel_id): Channel
{
# Always include channel raw, it contains the channel name
$parameters = [
'include_channel_raw' => true
];
return new Channel($this->get('/channels/'.$channel_id, $parameters), $this);
}
public function getSubscribedChannel(): array
{
# Always include channel raw, it contains the channel name
$parameters = [
'include_channel_raw' => true
];
$channels = [];
$resp = $this->get('/users/me/channels/subscribed', $parameters);
foreach ($resp as $r) {
$channels[] = new Channel($r, $this);
}
return $channels;
}
protected function fetchPnutSystemConfig() protected function fetchPnutSystemConfig()
{ {
$config = $this->get('/sys/config'); $config = $this->get('/sys/config');

96
src/Entities/Channel.php Normal file
View File

@ -0,0 +1,96 @@
<?php
namespace APnutI\Entities;
use APnutI\Entities\User;
use APnutI\APnutI;
use APnutI\Entities\ChannelCounts;
class Channel
{
public \DateTime $created_at;
public int $id = 0;
public bool $has_sticky_messages = false;
public bool $has_unread = false;
public bool $is_active = true;
public bool $you_muted = false;
public bool $you_subscribed = false;
public string $type;
public ?User $owner = null;
public ?int $owner_id = null;
public ?string $name = null;
public array $raw = [];
public ?ChannelCounts $counts = null;
//TODO: recent_deleted_message, recent_message, avatar raw, acl, improved PMs
private APnutI $api;
protected static array $channel_types = [
'io.pnut.core.chat',
'io.pnut.core.pm'
];
public function __construct(array $data, APnutI $api)
{
$this->api = $api;
$this->created_at = new \DateTime($data['created_at']);
$this->id = (int)$data['id'];
$this->type = $data['type'];
$this->has_sticky_messages = array_key_exists('has_sticky_messages', $data)
? (bool)$data['has_sticky_messages']
: false;
$this->has_unread = array_key_exists('has_unread', $data)
? (bool)$data['has_unread']
: false;
$this->is_active = array_key_exists('is_active', $data)
? (bool)$data['is_active']
: true;
$this->you_muted = array_key_exists('you_muted', $data)
? (bool)$data['you_muted']
: false;
$this->you_subscribed = array_key_exists('you_subscribed', $data)
? (bool)$data['you_subscribed']
: false;
if (!empty($data['user'])) {
$this->owner = new User($data['user'], $this->api);
$this->owner_id = $this->owner->id;
} elseif (!empty($data['user_id'])) {
$this->owner_id = (int)$data['user_id'];
}
if (!empty($data['source'])) {
$this->source = new Source($data['source']);
}
if (!empty($data['counts'])) {
$this->counts = new ChannelCounts($data['counts']);
}
if (!empty($data['raw'])) {
$this->raw = $data['raw'];
foreach ($data['raw'] as $rawtype => $rawvalue) {
if ($rawtype === 'io.pnut.core.chat-settings') {
foreach ($rawvalue as $rawvalueentry) {
if (array_key_exists('name', $rawvalueentry)) {
$this->name = $rawvalueentry['name'];
break;
}
}
break;
}
}
}
}
public function __toString(): string
{
$str = '';
if (!empty($this->name)) {
$str = 'Channel "'.$this->name.'"';
} elseif ($this->type == 'io.pnut.core.chat') {
$str = 'Channel "'.$this->id.'"';
} elseif ($this->type == 'io.pnut.core.pm') {
$str = 'PM "'.$this->id.'"';
}
return $str;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace APnutI\Entities;
class ChannelCounts
{
public int $messages;
public ?int $subscribers = null;
public function __construct(array $data)
{
$this->messages = (int)$data['messages'];
if (!empty($data['subscribers'])) {
$this->subscribers = (int)$data['subscribers'];
}
}
}