From 18eb80cb6feeaeae77c71526cded238bbfe42d68 Mon Sep 17 00:00:00 2001 From: aymm Date: Sun, 18 Apr 2021 12:19:54 +0200 Subject: [PATCH] Added basic channel support --- src/APnutI.php | 24 +++++++++ src/Entities/Channel.php | 96 ++++++++++++++++++++++++++++++++++ src/Entities/ChannelCounts.php | 16 ++++++ 3 files changed, 136 insertions(+) create mode 100644 src/Entities/Channel.php create mode 100644 src/Entities/ChannelCounts.php diff --git a/src/APnutI.php b/src/APnutI.php index c0caf29..91403de 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -5,6 +5,7 @@ namespace APnutI; use APnutI\Entities\Post; use APnutI\Entities\Poll; use APnutI\Entities\User; +use APnutI\Entities\Channel; use APnutI\Exceptions\PnutException; use APnutI\Exceptions\NotFoundException; use APnutI\Exceptions\NotAuthorizedException; @@ -688,6 +689,29 @@ class APnutI 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() { $config = $this->get('/sys/config'); diff --git a/src/Entities/Channel.php b/src/Entities/Channel.php new file mode 100644 index 0000000..e86c91d --- /dev/null +++ b/src/Entities/Channel.php @@ -0,0 +1,96 @@ +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; + } +} diff --git a/src/Entities/ChannelCounts.php b/src/Entities/ChannelCounts.php new file mode 100644 index 0000000..84dc184 --- /dev/null +++ b/src/Entities/ChannelCounts.php @@ -0,0 +1,16 @@ +messages = (int)$data['messages']; + if (!empty($data['subscribers'])) { + $this->subscribers = (int)$data['subscribers']; + } + } +}