WhiteList.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * WhiteList 用于管理群组、聊天室白名单
  7. *
  8. * \~english
  9. * The `WhiteList` used to manage groups, chat rooms white lists
  10. */
  11. final class WhiteList
  12. {
  13. /**
  14. * @ignore
  15. * @var Auth $auth 授权对象
  16. */
  17. private $auth;
  18. /// @cond
  19. public function __construct($auth)
  20. {
  21. $this->auth = $auth;
  22. }
  23. /// @endcond
  24. // Group
  25. /**
  26. * \~chinese
  27. * \brief
  28. * 查询群组白名单
  29. *
  30. * \details
  31. * 查询一个群组白名单中的用户列表。
  32. *
  33. * @param string $groupId 群组 id
  34. * @return array 白名单列表或者错误
  35. *
  36. * \~english
  37. * \brief
  38. * Query group whitelist
  39. *
  40. * \details
  41. * Query the list of users in a group white list.
  42. *
  43. * @param string $groupId group ID
  44. * @return array Whitelist or error
  45. */
  46. public function getGroupWhiteList($groupId)
  47. {
  48. if (!trim($groupId)) {
  49. \Easemob\exception('Please pass the group ID');
  50. }
  51. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/white/users';
  52. $resp = Http::get($uri, $this->auth->headers());
  53. if (!$resp->ok()) {
  54. return \Easemob\error($resp);
  55. }
  56. $data = $resp->data();
  57. return $data['data'];
  58. }
  59. /**
  60. * \~chinese
  61. * \brief
  62. * 添加单个用户至群组白名单
  63. *
  64. * \details
  65. * 查询一个群组将指定的单个用户添加至群组白名单。用户在添加至群组白名单后,当群组全员被禁言时,仍可以在群组中发送消息。白名单中的用户列表。
  66. *
  67. * @param string $groupId 群组 id
  68. * @param string $username 用户名
  69. * @return boolean|array 成功或者错误
  70. *
  71. * \~english
  72. * \brief
  73. * Add a single user to the group whitelist
  74. *
  75. * \details
  76. * Query a group and add the specified individual user to the group white list. After being added to the group white list, users can still send messages in the group when all members of the group are banned. List of users in the whitelist.
  77. *
  78. * @param string $groupId Group id
  79. * @param string $username user name
  80. * @return boolean|array Success or error
  81. */
  82. public function addUserToGroupWhiteList($groupId, $username)
  83. {
  84. if (!trim($groupId)) {
  85. \Easemob\exception('Please pass the group ID');
  86. }
  87. if (!trim($username)) {
  88. \Easemob\exception('Please pass the user name');
  89. }
  90. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/white/users/' . $username;
  91. $resp = Http::post($uri, null, $this->auth->headers());
  92. if (!$resp->ok()) {
  93. return \Easemob\error($resp);
  94. }
  95. return true;
  96. }
  97. /**
  98. * \~chinese
  99. * \brief
  100. * 批量添加用户至群组白名单
  101. *
  102. * \details
  103. * 添加多个用户至群组白名单。你一次最多可添加 60 个用户。用户添加至白名单后在群组全员禁言时仍可以在群组中发送消息。
  104. *
  105. * @param string $groupId 群组 id
  106. * @param array $usernames 用户名数组
  107. * @return boolean|array 成功或者错误
  108. *
  109. * \~english
  110. * \brief
  111. * Batch add users to the group whitelist
  112. *
  113. * \details
  114. * Add multiple users to the group whitelist. You can add up to 60 users at a time. After users are added to the white list, they can still send messages in the group when all members of the group are forbidden.
  115. *
  116. * @param string $groupId Group id
  117. * @param array $usernames User name array
  118. * @return boolean|array Success or error
  119. */
  120. public function addUsersToGroupWhiteList($groupId, $usernames)
  121. {
  122. if (!trim($groupId)) {
  123. \Easemob\exception('Please pass the group ID');
  124. }
  125. if (!is_array($usernames) || empty($usernames)) {
  126. \Easemob\exception('Please pass the user name');
  127. }
  128. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/white/users';
  129. $body = compact('usernames');
  130. $resp = Http::post($uri, $body, $this->auth->headers());
  131. if (!$resp->ok()) {
  132. return \Easemob\error($resp);
  133. }
  134. return true;
  135. }
  136. /**
  137. * \~chinese
  138. * \brief
  139. * 将用户移除群组白名单
  140. *
  141. * \details
  142. * 将指定用户从群组白名单中移除。你每次最多可移除 60 个用户。
  143. *
  144. * @param string $groupId 群组 id
  145. * @param string $username 用户名,可以以逗号分隔传递多个用户名
  146. * @return boolean|array 成功或者错误
  147. *
  148. * \~english
  149. * \brief
  150. * Remove user from group whitelist
  151. *
  152. * \details
  153. * Removes the specified user from the group whitelist. You can remove up to 60 users at a time.
  154. *
  155. * @param string $groupId Group ID
  156. * @param string $username User name. Multiple user names can be passed in comma separated form
  157. * @return boolean|array Success or error
  158. */
  159. public function removeUsersFromGroupWhiteList($groupId, $username)
  160. {
  161. if (!trim($groupId)) {
  162. \Easemob\exception('Please pass the group ID');
  163. }
  164. if (!trim($username)) {
  165. \Easemob\exception('Please pass the user name');
  166. }
  167. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/white/users/' . $username;
  168. $resp = Http::delete($uri, null, $this->auth->headers());
  169. if (!$resp->ok()) {
  170. return \Easemob\error($resp);
  171. }
  172. return true;
  173. }
  174. // Room
  175. /**
  176. * \~chinese
  177. * \brief
  178. * 查询聊天室白名单
  179. *
  180. * \details
  181. * 查询一个聊天室白名单中的用户列表。
  182. *
  183. * @param string $roomId 聊天室 id
  184. * @return array 白名单列表或者错误
  185. *
  186. * \~english
  187. * \brief
  188. * Query chat room white list
  189. *
  190. * \details
  191. * Query the list of users in a chat room white list.
  192. *
  193. * @param string $roomId Chat room ID
  194. * @return array Whitelist or error
  195. */
  196. public function getRoomWhiteList($roomId)
  197. {
  198. if (!trim($roomId)) {
  199. \Easemob\exception('Please pass the chat room ID');
  200. }
  201. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/white/users';
  202. $resp = Http::get($uri, $this->auth->headers());
  203. if (!$resp->ok()) {
  204. return \Easemob\error($resp);
  205. }
  206. $data = $resp->data();
  207. return $data['data'];
  208. }
  209. /**
  210. * \~chinese
  211. * \brief
  212. * 添加单个用户至聊天室白名单
  213. *
  214. * \details
  215. * 将指定的单个用户添加至聊天室白名单。用户添加至聊天室白名单后,当聊天室全员禁言时,仍可以在聊天室中发送消息。
  216. *
  217. * @param string $roomId 聊天室 id
  218. * @param string $username 用户名
  219. * @return boolean|array 成功或者错误
  220. *
  221. * \~english
  222. * \brief
  223. * Add a single user to the chat room whitelist
  224. *
  225. * \details
  226. * Adds the specified individual user to the chat room whitelist. After the user is added to the chat room white list, when the chat room is forbidden, he can still send messages in the chat room.
  227. *
  228. * @param string $roomId Chat room id
  229. * @param string $username user name
  230. * @return boolean|array Success or error
  231. */
  232. public function addUserToRoomWhiteList($roomId, $username)
  233. {
  234. if (!trim($roomId)) {
  235. \Easemob\exception('Please pass the chat room ID');
  236. }
  237. if (!trim($username)) {
  238. \Easemob\exception('Please pass the user name');
  239. }
  240. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/white/users/' . $username;
  241. $resp = Http::post($uri, null, $this->auth->headers());
  242. if (!$resp->ok()) {
  243. return \Easemob\error($resp);
  244. }
  245. return true;
  246. }
  247. /**
  248. * \~chinese
  249. * \brief
  250. * 批量添加用户至聊天室白名单
  251. *
  252. * \details
  253. * 添加多个用户至聊天室白名单。你一次最多可添加 60 个用户。用户添加至聊天室白名单后,在聊天室全员禁言时,仍可以在聊天室中发送消息。
  254. *
  255. * @param string $roomId 聊天室 id
  256. * @param array $usernames 用户名数组
  257. * @return boolean|array 成功或者错误
  258. *
  259. * \~english
  260. * \brief
  261. * Batch add users to chat room white list
  262. *
  263. * \details
  264. * Add multiple users to the chat room whitelist. You can add up to 60 users at a time. After users are added to the chat room white list, they can still send messages in the chat room when all members of the chat room are forbidden to speak.
  265. *
  266. * @param string $roomId Chat room id
  267. * @param array $usernames User name array
  268. * @return boolean|array Success or error
  269. */
  270. public function addUsersToRoomWhiteList($roomId, $usernames)
  271. {
  272. if (!trim($roomId)) {
  273. \Easemob\exception('Please pass the chat room ID');
  274. }
  275. if (!is_array($usernames) || empty($usernames)) {
  276. \Easemob\exception('Please pass the user name');
  277. }
  278. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/white/users';
  279. $body = compact('usernames');
  280. $resp = Http::post($uri, $body, $this->auth->headers());
  281. if (!$resp->ok()) {
  282. return \Easemob\error($resp);
  283. }
  284. return true;
  285. }
  286. /**
  287. * \~chinese
  288. * \brief
  289. * 将用户移除聊天室白名单
  290. *
  291. * \details
  292. * 将指定用户从聊天室白名单移除。你每次最多可移除 60 个用户。
  293. *
  294. * @param string $roomId 聊天室 id
  295. * @param string $username 用户名,可以以逗号分隔传递多个用户名
  296. * @return boolean|array 成功或者错误
  297. *
  298. * \~english
  299. * \brief
  300. * Remove user from chat room whitelist
  301. *
  302. * \details
  303. * Remove the specified user from the chat room whitelist. You can remove up to 60 users at a time.
  304. *
  305. * @param string $roomId Chat room ID
  306. * @param string $username User name. Multiple user names can be passed in comma separated form
  307. * @return boolean|array Success or error
  308. */
  309. public function removeUsersFromRoomWhiteList($roomId, $username)
  310. {
  311. if (!trim($roomId)) {
  312. \Easemob\exception('Please pass the chat room ID');
  313. }
  314. if (!trim($username)) {
  315. \Easemob\exception('Please pass the user name');
  316. }
  317. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/white/users/' . $username;
  318. $resp = Http::delete($uri, null, $this->auth->headers());
  319. if (!$resp->ok()) {
  320. return \Easemob\error($resp);
  321. }
  322. return true;
  323. }
  324. }