1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pnz\MattermostClient\Api; |
6
|
|
|
|
7
|
|
|
use Pnz\MattermostClient\Exception\InvalidArgumentException; |
8
|
|
|
use Pnz\MattermostClient\Model\Channel\Channel; |
9
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelMember; |
10
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelMembers; |
11
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelStats; |
12
|
|
|
use Pnz\MattermostClient\Model\Post\Posts; |
13
|
|
|
use Pnz\MattermostClient\Model\Status; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
final class ChannelsApi extends HttpApi |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns an channel by its ID. |
20
|
|
|
* |
21
|
|
|
* @return Channel|ResponseInterface |
22
|
|
|
*/ |
23
|
9 |
|
public function getChannelById(string $channelId) |
24
|
|
|
{ |
25
|
9 |
|
if (empty($channelId)) { |
26
|
1 |
|
throw new InvalidArgumentException('Id can not be empty'); |
27
|
|
|
} |
28
|
|
|
|
29
|
8 |
|
$response = $this->httpGet(sprintf('/channels/%s', $channelId)); |
30
|
|
|
|
31
|
8 |
|
return $this->handleResponse($response, Channel::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns a channel given the team ID and the channel name. |
36
|
|
|
* |
37
|
|
|
* @param array $parameters Associative array of additional parameters for the request: |
38
|
|
|
* - 'include_deleted'=>'true': allow to fetch deleted channels too |
39
|
|
|
* |
40
|
|
|
* @return Channel|ResponseInterface |
41
|
|
|
*/ |
42
|
11 |
|
public function getChannelByName(string $teamId, string $channelName, array $parameters = []) |
43
|
|
|
{ |
44
|
11 |
|
if (empty($teamId) || empty($channelName)) { |
45
|
3 |
|
throw new InvalidArgumentException('Team ID and channel name can not be empty'); |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
$response = $this->httpGet( |
49
|
8 |
|
sprintf('/teams/%s/channels/name/%s', $teamId, $channelName), |
50
|
8 |
|
$parameters |
51
|
|
|
); |
52
|
|
|
|
53
|
8 |
|
return $this->handleResponse($response, Channel::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns an channel by the team name and the chanel name. |
58
|
|
|
* |
59
|
|
|
* @return Channel|ResponseInterface |
60
|
|
|
*/ |
61
|
11 |
|
public function getChannelByNameAndTeamName(string $teamName, string $channelName) |
62
|
|
|
{ |
63
|
11 |
|
if (empty($teamName) || empty($channelName)) { |
64
|
3 |
|
throw new InvalidArgumentException('Team ID and channel name can not be empty'); |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
$response = $this->httpGet(sprintf('/teams/name/%s/channels/name/%s', $teamName, $channelName)); |
68
|
|
|
|
69
|
8 |
|
return $this->handleResponse($response, Channel::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve the channel statistics of the given channelId. |
74
|
|
|
* |
75
|
|
|
* @return ChannelStats|ResponseInterface |
76
|
|
|
*/ |
77
|
9 |
|
public function getChannelStats(string $channelId) |
78
|
|
|
{ |
79
|
9 |
|
if (empty($channelId)) { |
80
|
1 |
|
throw new InvalidArgumentException('Channel ID can not be empty'); |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
$response = $this->httpGet(sprintf('/channels/%s/stats', $channelId)); |
84
|
|
|
|
85
|
8 |
|
return $this->handleResponse($response, ChannelStats::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a Channel. Required parameters: 'team_id', 'name', 'display_name' and 'type'. |
90
|
|
|
* |
91
|
|
|
* @return Channel|ResponseInterface |
92
|
|
|
*/ |
93
|
8 |
|
public function createChannel(array $params) |
94
|
|
|
{ |
95
|
8 |
|
$response = $this->httpPost('/channels', $params); |
96
|
|
|
|
97
|
8 |
|
return $this->handleResponse($response, Channel::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates and returns a direct channel between two users. |
102
|
|
|
* |
103
|
|
|
* @return Channel|ResponseInterface |
104
|
|
|
*/ |
105
|
9 |
|
public function createDirectChannel(string $userId1, string $userId2) |
106
|
|
|
{ |
107
|
9 |
|
if (empty($userId1) || empty($userId2)) { |
108
|
1 |
|
throw new InvalidArgumentException('Two user IDs must be provided'); |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
$response = $this->httpPost('/channels/direct', [$userId1, $userId2]); |
112
|
|
|
|
113
|
8 |
|
return $this->handleResponse($response, Channel::class); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Delete a Channel. |
118
|
|
|
* |
119
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fdelete |
120
|
|
|
* |
121
|
|
|
* @param array $params Query parameters |
122
|
|
|
* - permanent => "true"|"false" (Mattermost >= 5.28) |
123
|
9 |
|
* |
124
|
|
|
* @return Status|ResponseInterface |
125
|
9 |
|
*/ |
126
|
1 |
|
public function deleteChannel(string $channelId, array $params = []) |
127
|
|
|
{ |
128
|
|
|
if (empty($channelId)) { |
129
|
8 |
|
throw new InvalidArgumentException('Channel ID can not be empty'); |
130
|
|
|
} |
131
|
8 |
|
|
132
|
|
|
$response = $this->httpDelete(sprintf('/channels/%s', $channelId), [], $params); |
133
|
|
|
|
134
|
|
|
return $this->handleResponse($response, Status::class); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Restore channel from the provided channel id string. |
139
|
|
|
* |
140
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1restore%2Fpost |
141
|
9 |
|
* |
142
|
|
|
* @return Channel|ResponseInterface |
143
|
9 |
|
*/ |
144
|
1 |
|
public function restoreChannel(string $channelId) |
145
|
|
|
{ |
146
|
|
|
if (empty($channelId)) { |
147
|
8 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
148
|
|
|
} |
149
|
8 |
|
|
150
|
|
|
$response = $this->httpPost(sprintf('/channels/%s/restore', $channelId)); |
151
|
|
|
|
152
|
|
|
return $this->handleResponse($response, Channel::class); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Patch a channel. |
157
|
|
|
* |
158
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1patch%2Fput |
159
|
9 |
|
* |
160
|
|
|
* @return Channel|ResponseInterface |
161
|
9 |
|
*/ |
162
|
1 |
|
public function patchChannel(string $channelId, array $params) |
163
|
|
|
{ |
164
|
|
|
if (empty($channelId)) { |
165
|
8 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
166
|
|
|
} |
167
|
8 |
|
|
168
|
|
|
$response = $this->httpPut(sprintf('/channels/%s/patch', $channelId), $params); |
169
|
|
|
|
170
|
|
|
return $this->handleResponse($response, Channel::class); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Update a channel. |
175
|
|
|
* |
176
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fput |
177
|
9 |
|
* |
178
|
|
|
* @return Channel|ResponseInterface |
179
|
9 |
|
*/ |
180
|
1 |
|
public function updateChannel(string $channelId, array $params) |
181
|
|
|
{ |
182
|
|
|
if (empty($channelId)) { |
183
|
8 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
184
|
|
|
} |
185
|
8 |
|
|
186
|
|
|
$response = $this->httpPut(sprintf('/channels/%s', $channelId), $params); |
187
|
|
|
|
188
|
|
|
return $this->handleResponse($response, Channel::class); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add a user to a channel. |
193
|
11 |
|
* |
194
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1members%2Fpost |
195
|
11 |
|
* |
196
|
3 |
|
* @return ChannelMember|ResponseInterface |
197
|
|
|
*/ |
198
|
|
|
public function addChannelMember(string $channelId, string $userId, string $postRootId = '') |
199
|
|
|
{ |
200
|
8 |
|
if (empty($channelId) || empty($userId)) { |
201
|
8 |
|
throw new InvalidArgumentException('Channel ID or user ID can not be empty'); |
202
|
8 |
|
} |
203
|
|
|
|
204
|
|
|
$body = [ |
205
|
8 |
|
'channel_id' => $channelId, |
206
|
|
|
'user_id' => $userId, |
207
|
8 |
|
'post_root_id' => $postRootId, |
208
|
|
|
]; |
209
|
|
|
|
210
|
|
|
$response = $this->httpPost(sprintf('/channels/%s/members', $channelId), $body); |
211
|
|
|
|
212
|
|
|
return $this->handleResponse($response, ChannelMember::class); |
213
|
|
|
} |
214
|
|
|
|
215
|
11 |
|
/** |
216
|
|
|
* Remove a user from a channel. |
217
|
11 |
|
* |
218
|
3 |
|
* @return Status|ResponseInterface |
219
|
|
|
*/ |
220
|
|
|
public function removeChannelMember(string $channelId, string $userId) |
221
|
8 |
|
{ |
222
|
|
|
if (empty($channelId) || empty($userId)) { |
223
|
8 |
|
throw new InvalidArgumentException('Channel ID or user ID can not be empty'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$response = $this->httpDelete(sprintf('/channels/%s/members/%s', $channelId, $userId)); |
227
|
|
|
|
228
|
|
|
return $this->handleResponse($response, Status::class); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get members of a channel. |
233
|
9 |
|
* |
234
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
235
|
9 |
|
* |
236
|
1 |
|
* @return ChannelMembers|ResponseInterface |
237
|
|
|
*/ |
238
|
|
|
public function getChannelMembers(string $channelId, array $params = []) |
239
|
8 |
|
{ |
240
|
|
|
if (empty($channelId)) { |
241
|
8 |
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$response = $this->httpGet(sprintf('/channels/%s/members', $channelId), $params); |
245
|
|
|
|
246
|
|
|
return $this->handleResponse($response, ChannelMembers::class); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get the posts for a channel. |
251
|
|
|
* |
252
|
|
|
* @param array $params The listing params: 'page', 'per_page', 'before', 'after', 'since' |
253
|
10 |
|
* |
254
|
|
|
* @see: https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1posts%2Fget |
255
|
10 |
|
* |
256
|
1 |
|
* @return Posts|ResponseInterface |
257
|
|
|
*/ |
258
|
|
|
public function getChannelPosts(string $channelId, array $params = []) |
259
|
9 |
|
{ |
260
|
|
|
if (empty($channelId)) { |
261
|
9 |
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$response = $this->httpGet(sprintf('/channels/%s/posts', $channelId), $params); |
265
|
|
|
|
266
|
|
|
return $this->handleResponse($response, Posts::class); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|