1 <?php
2 namespace Slack;
3
4 use React\Promise;
5
6 7 8
9 class Channel extends ClientObject implements ChannelInterface
10 {
11 12 13
14 public function getId()
15 {
16 return $this->data['id'];
17 }
18
19 20 21 22 23
24 public function getName()
25 {
26 return $this->data['name'];
27 }
28
29 30 31 32 33
34 public function getPurpose()
35 {
36 return $this->data['purpose']['value'];
37 }
38
39 40 41 42 43
44 public function getTopic()
45 {
46 return $this->data['topic']['value'];
47 }
48
49 50 51 52 53 54
55 public function getMembers()
56 {
57 $memberPromises = [];
58 foreach ($this->data['members'] as $memberId) {
59 $memberPromises[] = $this->client->getUserById($memberId);
60 }
61
62 return Promise\all($memberPromises);
63 }
64
65 66 67 68 69
70 public function getTimeCreated()
71 {
72 $time = new \DateTime();
73 $time->setTimestamp($this->data['created']);
74 return $time;
75 }
76
77 78 79 80 81
82 public function getCreator()
83 {
84 return $this->client->getUserById($this->data['creator']);
85 }
86
87 88 89 90 91
92 public function getUnreadCount()
93 {
94 return $this->data['unread_count'];
95 }
96
97 98 99 100 101
102 public function isArchived()
103 {
104 return $this->data['is_archived'];
105 }
106
107 108 109 110 111 112 113
114 public function rename($name)
115 {
116 return $this->client->apiCall('channels.rename', [
117 'channel' => $this->getId(),
118 'name' => $name,
119 ])->then(function () use ($name) {
120 $this->data['name'] = $name;
121 return $name;
122 });
123 }
124
125 126 127 128 129 130 131
132 public function setPurpose($text)
133 {
134 return $this->client->apiCall('channels.setPurpose', [
135 'channel' => $this->getId(),
136 'purpose' => $text,
137 ])->then(function () use ($text) {
138 $this->data['purpose']['value'] = $text;
139 return $text;
140 });
141 }
142
143 144 145 146 147 148 149
150 public function setTopic($text)
151 {
152 return $this->client->apiCall('channels.setTopic', [
153 'channel' => $this->getId(),
154 'topic' => $text,
155 ])->then(function () use ($text) {
156 $this->data['topic']['value'] = $text;
157 return $text;
158 });
159 }
160
161 162 163 164 165
166 public function archive()
167 {
168 return $this->client->apiCall('channels.archive', [
169 'channel' => $this->getId(),
170 ])->then(function () {
171 $this->data['is_archived'] = true;
172 });
173 }
174
175 176 177 178 179
180 public function unarchive()
181 {
182 return $this->client->apiCall('channels.unarchive', [
183 'channel' => $this->getId(),
184 ])->then(function () {
185 $this->data['is_archived'] = false;
186 });
187 }
188
189 190 191 192 193 194 195
196 public function inviteUser(User $user)
197 {
198 return $this->client->apiCall('channels.invite', [
199 'channel' => $this->getId(),
200 'user' => $user->getId(),
201 ])->then(function () use ($user) {
202 $this->data['members'][] = $user->getId();
203 });
204 }
205
206 207 208 209 210 211 212
213 public function kickUser(User $user)
214 {
215 return $this->client->apiCall('channels.kick', [
216 'channel' => $this->getId(),
217 'user' => $user->getId(),
218 ])->then(function () use ($user) {
219 unset($this->data['members'][$user->getId()]);
220 });
221 }
222
223 224 225
226 public function close()
227 {
228 return $this->client->apiCall('channels.close', [
229 'channel' => $this->getId(),
230 ])->then(function ($response) {
231 return !isset($response['no_op']);
232 });
233 }
234 }
235