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
|
|
|
* |
92
|
|
|
* @return Channel|ResponseInterface |
93
|
|
|
*/ |
94
|
8 |
|
public function createChannel(array $params) |
95
|
|
|
{ |
96
|
8 |
|
$response = $this->httpPost('/channels', $params); |
97
|
|
|
|
98
|
8 |
|
return $this->handleResponse($response, Channel::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Creates and returns a direct channel between two users. |
103
|
|
|
* |
104
|
|
|
* @return Channel|ResponseInterface |
105
|
|
|
*/ |
106
|
9 |
|
public function createDirectChannel(string $userId1, string $userId2) |
107
|
|
|
{ |
108
|
9 |
|
if (empty($userId1) || empty($userId2)) { |
109
|
1 |
|
throw new InvalidArgumentException('Two user IDs must be provided'); |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
$response = $this->httpPost('/channels/direct', [$userId1, $userId2]); |
113
|
|
|
|
114
|
8 |
|
return $this->handleResponse($response, Channel::class); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Delete a Channel. |
119
|
|
|
* |
120
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fdelete |
121
|
|
|
* |
122
|
|
|
* @return Status|ResponseInterface |
123
|
|
|
*/ |
124
|
9 |
|
public function deleteChannel(string $channelId) |
125
|
|
|
{ |
126
|
9 |
|
if (empty($channelId)) { |
127
|
1 |
|
throw new InvalidArgumentException('Channel ID can not be empty'); |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
$response = $this->httpDelete(sprintf('/channels/%s', $channelId)); |
131
|
|
|
|
132
|
8 |
|
return $this->handleResponse($response, Status::class); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Restore channel from the provided channel id string. |
137
|
|
|
* |
138
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1restore%2Fpost |
139
|
|
|
* |
140
|
|
|
* @return Channel|ResponseInterface |
141
|
|
|
*/ |
142
|
9 |
|
public function restoreChannel(string $channelId) |
143
|
|
|
{ |
144
|
9 |
|
if (empty($channelId)) { |
145
|
1 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
146
|
|
|
} |
147
|
|
|
|
148
|
8 |
|
$response = $this->httpPost(sprintf('/channels/%s/restore', $channelId)); |
149
|
|
|
|
150
|
8 |
|
return $this->handleResponse($response, Channel::class); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Patch a channel. |
155
|
|
|
* |
156
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1patch%2Fput |
157
|
|
|
* |
158
|
|
|
* @return Channel|ResponseInterface |
159
|
|
|
*/ |
160
|
9 |
|
public function patchChannel(string $channelId, array $params) |
161
|
|
|
{ |
162
|
9 |
|
if (empty($channelId)) { |
163
|
1 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
164
|
|
|
} |
165
|
|
|
|
166
|
8 |
|
$response = $this->httpPut(sprintf('/channels/%s/patch', $channelId), $params); |
167
|
|
|
|
168
|
8 |
|
return $this->handleResponse($response, Channel::class); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Update a channel. |
173
|
|
|
* |
174
|
|
|
* @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fput |
175
|
|
|
* |
176
|
|
|
* @return Channel|ResponseInterface |
177
|
|
|
*/ |
178
|
9 |
|
public function updateChannel(string $channelId, array $params) |
179
|
|
|
{ |
180
|
9 |
|
if (empty($channelId)) { |
181
|
1 |
|
throw new InvalidArgumentException('ChannelId can not be empty'); |
182
|
|
|
} |
183
|
|
|
|
184
|
8 |
|
$response = $this->httpPut(sprintf('/channels/%s', $channelId), $params); |
185
|
|
|
|
186
|
8 |
|
return $this->handleResponse($response, Channel::class); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Add a user to a channel, with specific roles. |
191
|
|
|
* |
192
|
|
|
* |
193
|
|
|
* @return ChannelMember|ResponseInterface |
194
|
|
|
*/ |
195
|
11 |
|
public function addChannelMember(string $channelId, string $userId, string $roles = '') |
196
|
|
|
{ |
197
|
11 |
|
if (empty($channelId) || empty($userId)) { |
198
|
3 |
|
throw new InvalidArgumentException('Channel ID or user ID can not be empty'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$body = [ |
202
|
8 |
|
'channel_id' => $channelId, |
203
|
8 |
|
'user_id' => $userId, |
204
|
8 |
|
'roles' => $roles, |
205
|
|
|
]; |
206
|
|
|
|
207
|
8 |
|
$response = $this->httpPost(sprintf('/channels/%s/members', $channelId), $body); |
208
|
|
|
|
209
|
8 |
|
return $this->handleResponse($response, ChannelMember::class); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Remove a user from a channel. |
214
|
|
|
* |
215
|
|
|
* |
216
|
|
|
* @return Status|ResponseInterface |
217
|
|
|
*/ |
218
|
11 |
|
public function removeChannelMember(string $channelId, string $userId) |
219
|
|
|
{ |
220
|
11 |
|
if (empty($channelId) || empty($userId)) { |
221
|
3 |
|
throw new InvalidArgumentException('Channel ID or user ID can not be empty'); |
222
|
|
|
} |
223
|
|
|
|
224
|
8 |
|
$response = $this->httpDelete(sprintf('/channels/%s/members/%s', $channelId, $userId)); |
225
|
|
|
|
226
|
8 |
|
return $this->handleResponse($response, Status::class); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get members of a channel. |
231
|
|
|
* |
232
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
233
|
|
|
* |
234
|
|
|
* @return ChannelMembers|ResponseInterface |
235
|
|
|
*/ |
236
|
9 |
|
public function getChannelMembers(string $channelId, array $params = []) |
237
|
|
|
{ |
238
|
9 |
|
if (empty($channelId)) { |
239
|
1 |
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
240
|
|
|
} |
241
|
|
|
|
242
|
8 |
|
$response = $this->httpGet(sprintf('/channels/%s/members', $channelId), $params); |
243
|
|
|
|
244
|
8 |
|
return $this->handleResponse($response, ChannelMembers::class); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the posts for a channel. |
249
|
|
|
* |
250
|
|
|
* @param array $params The listing params: 'page', 'per_page', 'before', 'after', 'since' |
251
|
|
|
* |
252
|
|
|
* @see: https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1posts%2Fget |
253
|
|
|
* |
254
|
|
|
* @return Posts|ResponseInterface |
255
|
|
|
*/ |
256
|
10 |
|
public function getChannelPosts(string $channelId, array $params = []) |
257
|
|
|
{ |
258
|
10 |
|
if (empty($channelId)) { |
259
|
1 |
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
260
|
|
|
} |
261
|
|
|
|
262
|
9 |
|
$response = $this->httpGet(sprintf('/channels/%s/posts', $channelId), $params); |
263
|
|
|
|
264
|
9 |
|
return $this->handleResponse($response, Posts::class); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|