Block.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Block 用于限制访问(将用户加入黑名单、群组/聊天室禁言等)
  7. *
  8. * \~english
  9. * The `Block` is used to restrict access (add users to blacklists, group / chat room prohibitions, etc.)
  10. */
  11. final class Block
  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. /**
  25. * \~chinese
  26. * \brief
  27. * 获取用户黑名单
  28. *
  29. * @param string $username 要获取黑名单的用户
  30. * @return array 黑名单用户名列表或者错误
  31. *
  32. * \~english
  33. * \brief
  34. * Get user blacklist
  35. *
  36. * @param string $username User name
  37. * @return array Blacklist user name list or error
  38. */
  39. public function getUsersBlockedFromSendMsgToUser($username)
  40. {
  41. if (!trim($username)) {
  42. \Easemob\exception('Please enter username');
  43. }
  44. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/blocks/users';
  45. $resp = Http::get($uri, $this->auth->headers());
  46. if (!$resp->ok()) {
  47. return \Easemob\error($resp);
  48. }
  49. $data = $resp->data();
  50. return $data['data'];
  51. }
  52. /**
  53. * \~chinese
  54. * \brief
  55. * 添加用户黑名单
  56. *
  57. * \details
  58. * 向用户的黑名单列表中添加一个或者多个用户,黑名单中的用户无法给该用户发送消息,每个用户的黑名单人数上限为 500。
  59. *
  60. * @param string $username 要添加黑名单的用户名
  61. * @param array $usernames 需要加入到黑名单中的用户名,以数组方式提交
  62. * @return boolean|array 成功或者错误
  63. *
  64. * \~english
  65. * \brief
  66. * Add user blacklist
  67. *
  68. * \details
  69. * Add one or more users to the user's blacklist. Users in the blacklist cannot send messages to the user. The maximum number of blacklists for each user is 500.
  70. *
  71. * @param string $username User name
  72. * @param array $usernames The user names that need to be added to the blacklist shall be submitted in the form of array
  73. * @return boolean|array Success or error
  74. */
  75. public function blockUserSendMsgToUser($username, $usernames = array())
  76. {
  77. if (!trim($username)) {
  78. \Easemob\exception('Please enter username');
  79. }
  80. if (!is_array($usernames)) {
  81. \Easemob\exception('Please pass in the user array to be blacklisted');
  82. }
  83. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/blocks/users';
  84. $body = compact('usernames');
  85. $resp = Http::post($uri, $body, $this->auth->headers());
  86. if (!$resp->ok()) {
  87. return \Easemob\error($resp);
  88. }
  89. return true;
  90. }
  91. /**
  92. * \~chinese
  93. * \brief
  94. * 移除用户黑名单
  95. *
  96. * \details
  97. * 从用户的黑名单中移除用户。将用户从黑名单移除后,恢复到好友,或者未添加好友的用户关系。可以正常的进行消息收发。
  98. *
  99. * @param string $username 要移除黑名单的用户名
  100. * @param string $friend_username 好友用户名
  101. * @return boolean|array 成功或者错误
  102. *
  103. * \~english
  104. * \brief
  105. * Remove user blacklist
  106. *
  107. * \details
  108. * Remove the user from the user's blacklist. After the user is removed from the blacklist, it can be restored to friends, or the user relationship without adding friends. Can send and receive messages normally.
  109. *
  110. * @param string $username User name
  111. * @param string $friend_username Friends user name
  112. * @return boolean|array Success or error
  113. */
  114. public function unblockUserSendMsgToUser($username, $blocked_username)
  115. {
  116. if (!trim($username) || !trim($blocked_username)) {
  117. \Easemob\exception('Please enter the user name and the user name to remove the blacklist');
  118. }
  119. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/blocks/users/' . $blocked_username;
  120. $resp = Http::delete($uri, null, $this->auth->headers());
  121. if (!$resp->ok()) {
  122. return \Easemob\error($resp);
  123. }
  124. return true;
  125. }
  126. /**
  127. * \~chinese
  128. * \brief
  129. * 账号封禁
  130. *
  131. * \details
  132. * 用户若被禁用将立即下线并无法登录,直到被解禁后才能恢复登录。常用在对异常用户的即时处理场景使用。
  133. *
  134. * @param string $username 要封禁的用户名
  135. * @return boolean|array 成功或者错误
  136. *
  137. * \~english
  138. * \brief
  139. * Account number ban
  140. *
  141. * \details
  142. * If the user is disabled, he will be offline immediately and cannot log in until he is lifted. It is often used in the immediate processing of abnormal users.
  143. *
  144. * @param string $username User name
  145. * @return boolean|array Success or error
  146. */
  147. public function blockUserLogin($username)
  148. {
  149. return $this->activate($username, 0);
  150. }
  151. /**
  152. * \~chinese
  153. * \brief
  154. * 账号解禁
  155. *
  156. * \details
  157. * 用户若被禁用将立即下线并无法登录,直到被解禁后才能恢复登录。常用在对异常用户的即时处理场景使用。
  158. *
  159. * @param string $username 要解禁的用户名
  160. * @return boolean|array 成功或者错误
  161. *
  162. * \~english
  163. * \brief
  164. * Account lifting
  165. *
  166. * \details
  167. * If the user is disabled, he will be offline immediately and cannot log in until he is lifted. It is often used in the immediate processing of abnormal users.
  168. *
  169. * @param string $username User name
  170. * @return boolean|array Success or error
  171. */
  172. public function unblockUserLogin($username)
  173. {
  174. return $this->activate($username);
  175. }
  176. /**
  177. * \~chinese
  178. * \brief
  179. * 设置用户全局禁言
  180. *
  181. * \details
  182. * 设置单个用户 ID 的单聊、群组、聊天室消息全局禁言。
  183. *
  184. * @param string $username 用户名
  185. * @param int $chatMuteDuration 单聊消息禁言时间,单位为秒,非负整数,最大值为 2147483647,`0` 表示取消该帐号的单聊消息禁言,`-1` 表示该帐号被设置永久禁言,其它值表示该帐号的具体禁言时间,负值为非法值。
  186. * @param int $groupchatMuteDuration 群组消息禁言时间,单位为秒,规则同上。
  187. * @param int $chatroomMuteDuration 聊天室消息禁言时间,单位为秒,规则同上。
  188. * @return boolean|array 成功或者错误
  189. *
  190. * \~english
  191. * \brief
  192. * Set user global prohibitions
  193. *
  194. * \details
  195. * Set the global prohibition of single chat, group and chat room messages of a single user ID.
  196. *
  197. * @param string $username User name
  198. * @param int $chatMuteDuration Single chat message forbidden time, in seconds, is a non negative integer. The maximum value is 2147483647, ` 0 'means to cancel the forbidden time of single chat messages of the account, ` - 1' means that the account is set with permanent forbidden time, other values mean the specific forbidden time of the account, and negative values are illegal values.
  199. * @param int $groupchatMuteDuration Group message forbidden time, in seconds, and the rules are the same as above.
  200. * @param int $chatroomMuteDuration Chat room message forbidden time, in seconds, and the rules are the same as above.
  201. * @return boolean|array Success or error
  202. */
  203. public function blockUserSendMsg($username, $chatMuteDuration = -1, $groupchatMuteDuration = -1, $chatroomMuteDuration = -1)
  204. {
  205. return $this->blockUserSendMsgGlobal($username, $chatMuteDuration, $groupchatMuteDuration, $chatroomMuteDuration);
  206. }
  207. /**
  208. * \~chinese
  209. * \brief
  210. * 解除用户全局禁言
  211. *
  212. * @param string $username 解除禁言的用户名
  213. * @return boolean|array 成功或者错误
  214. *
  215. * \~english
  216. * \brief
  217. * Lifting the user's global prohibition
  218. *
  219. * @param string $username User name
  220. * @return boolean|array Success or error
  221. */
  222. public function unblockUserSendMsg($username)
  223. {
  224. return $this->blockUserSendMsgGlobal($username, 0, 0, 0);
  225. }
  226. /**
  227. * \~chinese
  228. * \brief
  229. * 查询单个用户 ID 全局禁言
  230. *
  231. * \details
  232. * 查询单个用户的单聊/群聊/聊天室消息禁言。
  233. *
  234. * @param string $username 查询禁言信息的用户名
  235. * @return array 用户全局禁言信息或者错误
  236. *
  237. * \~english
  238. * \brief
  239. * Query single user ID global prohibitions
  240. *
  241. * \details
  242. * Query the forbidden words of single chat / group chat / chat room messages of a single user.
  243. *
  244. * @param string $username User name
  245. * @return array User global forbidden information or error
  246. */
  247. public function getUserBlocked($username)
  248. {
  249. if (!trim($username)) {
  250. \Easemob\exception('Please enter username');
  251. }
  252. $uri = $this->auth->getBaseUri() . '/mutes/' . $username;
  253. $resp = Http::get($uri, $this->auth->headers());
  254. if (!$resp->ok()) {
  255. return \Easemob\error($resp);
  256. }
  257. $data = $resp->data();
  258. return $data['data'];
  259. }
  260. /**
  261. * \~chinese
  262. * \brief
  263. * 查询APPKEY的用户禁言
  264. *
  265. * \details
  266. * 查询 App Key 下的用户禁言剩余时间的集合。
  267. *
  268. * @param int $pageSize 请求查询每页显示的禁言用户的数量,默认取 10 条
  269. * @param int $pageNum 请求查询的页码,默认取第 1 页
  270. * @return array 用户禁言信息或者错误
  271. *
  272. * \~english
  273. * \brief
  274. * User prohibitions for querying appkey
  275. *
  276. * \details
  277. * Query the collection of the remaining time of the user's forbidden words under the app key.
  278. *
  279. * @param int $pageSize Request to query the number of forbidden users displayed on each page. The default is 10
  280. * @param int $pageNum The page number requested for query is page 1 by default
  281. * @return array User forbidden information or error
  282. */
  283. public function getAppBlocked($pageSize = 10, $pageNum = 1)
  284. {
  285. $pageSize = (int)$pageSize > 0 ? (int)$pageSize : 10;
  286. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  287. $uri = $this->auth->getBaseUri() . '/mutes';
  288. $uri .= $pageSize ? ('?pagesize=' . $pageSize . '&pagenum=' . $pageNum) : '';
  289. $resp = Http::get($uri, $this->auth->headers());
  290. if (!$resp->ok()) {
  291. return \Easemob\error($resp);
  292. }
  293. $data = $resp->data();
  294. return isset($data['data']) ? $data['data'] : $data;
  295. }
  296. // Group
  297. /**
  298. * \~chinese
  299. * \brief
  300. * 查询群组黑名单
  301. *
  302. * \details
  303. * 查询一个群组黑名单中的用户列表。位于黑名单中的用户查看不到该群组的信息,也无法收到该群组的消息。
  304. *
  305. * @param string $groupId 群组 ID
  306. * @return array 群组黑名单信息或者错误
  307. *
  308. *
  309. * \~english
  310. * \brief
  311. * Query group blacklist
  312. *
  313. * \details
  314. * Query the list of users in a group blacklist. Users in the blacklist cannot view the information of the group or receive the message of the group.
  315. *
  316. * @param string $groupId Group ID
  317. * @return array Group blacklist information or error
  318. */
  319. public function getUsersBlockedJoinGroup($groupId)
  320. {
  321. if (!trim($groupId)) {
  322. \Easemob\exception('Please pass the group ID');
  323. }
  324. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/blocks/users';
  325. $resp = Http::get($uri, $this->auth->headers());
  326. if (!$resp->ok()) {
  327. return \Easemob\error($resp);
  328. }
  329. $data = $resp->data();
  330. return $data['data'];
  331. }
  332. /**
  333. * \~chinese
  334. * \brief
  335. * 添加单个用户至群组黑名单
  336. *
  337. * \details
  338. * 添加一个用户进入一个群组的黑名单。群主无法被加入群组的黑名单。
  339. *
  340. * 用户进入群组黑名单后,会收到消息:You are kicked out of the group xxx。之后,该用户查看不到该群组的信息,也收不到该群组的消息。
  341. *
  342. * @param string $groupId 群组 ID
  343. * @param string $username 要添加的 IM 用户名
  344. * @return boolean|array 成功或者错误
  345. *
  346. * \~english
  347. * \brief
  348. * Add a single user to the group blacklist
  349. *
  350. * \details
  351. * Add a user to the blacklist of a group. The group leader cannot be added to the blacklist of the group.
  352. *
  353. * After entering the group blacklist, the user will receive a message: you are kicked out of the group XXX. After that, the user cannot view the information of the group or receive the message of the group.
  354. *
  355. * @param string $groupId Group ID
  356. * @param string $username User name
  357. * @return boolean|array Success or error
  358. */
  359. public function blockUserJoinGroup($groupId, $username)
  360. {
  361. return $this->addGroupBlocks($groupId, $username);
  362. }
  363. /**
  364. * \~chinese
  365. * \brief
  366. * 批量添加用户至群组黑名单
  367. *
  368. * \details
  369. * 将多个用户添加一个群组的黑名单。你一次最多可以添加 60 个用户至群组黑名单。群主无法被加入群组的黑名单。
  370. *
  371. * 用户进入群组黑名单后,会收到消息:You are kicked out of the group xxx。之后,该用户查看不到该群组的信息,也收不到该群组的消息。
  372. *
  373. * @param string $groupId 群组 ID
  374. * @param array $usernames 要添加的 IM 用户名数组
  375. * @return boolean|array 成功或者错误
  376. *
  377. * \~english
  378. * \brief
  379. * Batch add users to group blacklist
  380. *
  381. * \details
  382. * Add multiple users to the blacklist of a group. You can add up to 60 users to the group blacklist at a time. The group leader cannot be added to the blacklist of the group.
  383. *
  384. * After entering the group blacklist, the user will receive a message: you are kicked out of the group XXX. After that, the user cannot view the information of the group or receive the message of the group.
  385. *
  386. * @param string $groupId Group ID
  387. * @param array $usernames User name array
  388. * @return boolean|array Success or error
  389. */
  390. public function blockUsersJoinGroup($groupId, $usernames)
  391. {
  392. return $this->addGroupBlocks($groupId, $usernames);
  393. }
  394. /**
  395. * \~chinese
  396. * \brief
  397. * 从群组黑名单移除单个用户
  398. *
  399. * \details
  400. * 将指定用户移出群组黑名单。对于群组黑名单中的用户,如果需要将其再次加入群组,需要先将其从群组黑名单中移除。
  401. *
  402. * @param string $groupId 群组 ID
  403. * @param string $username 要移除的用户名
  404. * @return boolean|array 成功或者错误
  405. *
  406. * \~english
  407. * \brief
  408. * Remove a single user from the group blacklist
  409. *
  410. * \details
  411. * Remove the specified user from the group blacklist. For users in the group blacklist, if they need to join the group again, they need to be removed from the group blacklist first.
  412. *
  413. * @param string $groupId Group ID
  414. * @param string $username User name
  415. * @return boolean|array Success or error
  416. */
  417. public function unblockUserJoinGroup($groupId, $username)
  418. {
  419. return $this->removeGroupBlocks($groupId, $username);
  420. }
  421. /**
  422. * \~chinese
  423. * \brief
  424. * 从群组黑名单批量移除用户
  425. *
  426. * \details
  427. * 将多名指定用户从群组黑名单中移除。对于群组黑名单中的用户,如果需要将其再次加入群组,需要先将其从群组黑名单中移除。
  428. *
  429. * @param string $groupId 群组 ID
  430. * @param array $usernames 要添加的 IM 用户名数组
  431. * @return boolean|array 成功或者错误
  432. *
  433. * \~english
  434. * \brief
  435. * Batch remove users from group blacklist
  436. *
  437. * \details
  438. * Remove multiple designated users from the group blacklist. For users in the group blacklist, if they need to join the group again, they need to be removed from the group blacklist first.
  439. *
  440. * @param string $groupId Group ID
  441. * @param array $usernames User name array
  442. * @return boolean|array Success or error
  443. */
  444. public function unblockUsersJoinGroup($groupId, $usernames)
  445. {
  446. return $this->removeGroupBlocks($groupId, $usernames);
  447. }
  448. /**
  449. * \~chinese
  450. * \brief
  451. * 添加群组禁言
  452. *
  453. * \details
  454. * 对指定群成员禁言。群成员被禁言后,将无法在群中发送消息。
  455. *
  456. * @param string $groupId 群组 ID
  457. * @param array $usernames 要被添加禁言的用户 ID 数组
  458. * @param int $mute_duration 禁言时长,单位为毫秒。
  459. * @return boolean|array 成功或者错误
  460. *
  461. *
  462. * \~english
  463. * \brief
  464. * Add group forbidden words
  465. *
  466. * \details
  467. * No speaking to designated group members. After group members are banned, they will not be able to send messages in the group.
  468. *
  469. * @param string $groupId Group ID
  470. * @param array $usernames User name array
  471. * @param int $mute_duration Forbidden speech duration, in milliseconds.
  472. * @return boolean|array Success or error
  473. */
  474. public function blockUserSendMsgToGroup($groupId, $usernames, $mute_duration = -1)
  475. {
  476. if (!trim($groupId)) {
  477. \Easemob\exception('Please pass the group ID');
  478. }
  479. if (!is_array($usernames) || empty($usernames)) {
  480. \Easemob\exception('Please pass the user name');
  481. }
  482. $mute_duration = (int)$mute_duration > 0 ? (int)$mute_duration : -1;
  483. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/mute';
  484. $resp = Http::post($uri, compact('usernames', 'mute_duration'), $this->auth->headers());
  485. if (!$resp->ok()) {
  486. return \Easemob\error($resp);
  487. }
  488. return true;
  489. }
  490. /**
  491. * \~chinese
  492. * \brief
  493. * 解除群组成员禁言
  494. *
  495. * \details
  496. * 将一个或多个群成员移除禁言列表。移除后,群成员可以在群组中正常发送消息。
  497. *
  498. * @param string $groupId 群组 ID
  499. * @param array $usernames 要移除禁言的用户 ID 数组
  500. * @return boolean|array 成功或者错误
  501. *
  502. * \~english
  503. * \brief
  504. * Release group members from prohibition
  505. *
  506. * \details
  507. * Remove one or more group members from the forbidden list. After removal, group members can send messages normally in the group.
  508. *
  509. * @param string $groupId Group ID
  510. * @param array $usernames User name array
  511. * @return boolean|array Success or error
  512. */
  513. public function unblockUserSendMsgToGroup($groupId, $usernames)
  514. {
  515. if (!trim($groupId)) {
  516. \Easemob\exception('Please pass the group ID');
  517. }
  518. if (!is_array($usernames) || empty($usernames)) {
  519. \Easemob\exception('Please pass the user name');
  520. }
  521. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/mute/' . implode(',', $usernames);
  522. $resp = Http::delete($uri, null, $this->auth->headers());
  523. if (!$resp->ok()) {
  524. return \Easemob\error($resp);
  525. }
  526. return true;
  527. }
  528. /**
  529. * \~chinese
  530. * \brief
  531. * 获取禁言列表
  532. *
  533. * \details
  534. * 获取当前群组的禁言用户列表。
  535. *
  536. * @param string $groupId 群组 ID
  537. * @return array 禁言列表信息或者错误
  538. *
  539. * \~english
  540. * \brief
  541. * Get forbidden list
  542. *
  543. * \details
  544. * Get the list of forbidden users in the current group.
  545. *
  546. * @param string $groupId Group ID
  547. * @return array Forbidden list information or error
  548. */
  549. public function getUsersBlockedSendMsgToGroup($groupId)
  550. {
  551. if (!trim($groupId)) {
  552. \Easemob\exception('Please pass the group ID');
  553. }
  554. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/mute';
  555. $resp = Http::get($uri, $this->auth->headers());
  556. if (!$resp->ok()) {
  557. return \Easemob\error($resp);
  558. }
  559. $data = $resp->data();
  560. return $data['data'];
  561. }
  562. /**
  563. * \~chinese
  564. * \brief
  565. * 禁言群组全体成员
  566. *
  567. * \details
  568. * 对所有群组成员一键禁言,即将群组的所有成员均加入禁言列表。设置群组全员禁言后,仅群组白名单中的用户可在群组内发消息
  569. *
  570. * @param string $groupId 群组 ID
  571. * @param int $mute_duration 禁言时长,单位为毫秒。
  572. * @return boolean|array 成功或者错误
  573. *
  574. * \~english
  575. * \brief
  576. * Forbidden all members of the group
  577. *
  578. * \details
  579. * One click prohibition for all group members, that is, all members of the group will be added to the prohibition list. After setting the prohibition of all members of the group, only users in the group white list can send messages in the group
  580. *
  581. * @param string $groupId Group ID
  582. * @param int $mute_duration Forbidden speech duration, in milliseconds.
  583. * @return boolean|array Success or error
  584. */
  585. public function blockAllUserSendMsgToGroup($groupId, $mute_duration = -1)
  586. {
  587. if (!trim($groupId)) {
  588. \Easemob\exception('Please pass the group ID');
  589. }
  590. $mute_duration = (int)$mute_duration > 0 ? (int)$mute_duration : -1;
  591. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/ban';
  592. $resp = Http::post($uri, compact('mute_duration'), $this->auth->headers());
  593. if (!$resp->ok()) {
  594. return \Easemob\error($resp);
  595. }
  596. return true;
  597. }
  598. /**
  599. * \~chinese
  600. * \brief
  601. * 解除群组全员禁言
  602. *
  603. * \details
  604. * 一键取消对群组全体成员的禁言。移除后,群成员可以在群组中正常发送消息。
  605. *
  606. * @param string $groupId 群组 ID
  607. * @return boolean|array 成功或者错误
  608. *
  609. * \~english
  610. * \brief
  611. * Lifting the ban on all members of the group
  612. *
  613. * \details
  614. * One click to cancel the ban on all members of the group. After removal, group members can send messages normally in the group.
  615. *
  616. * @param string $groupId Group ID
  617. * @return boolean|array Success or error
  618. */
  619. public function unblockAllUserSendMsgToGroup($groupId)
  620. {
  621. if (!trim($groupId)) {
  622. \Easemob\exception('Please pass the group ID');
  623. }
  624. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/ban';
  625. $resp = Http::delete($uri, null, $this->auth->headers());
  626. if (!$resp->ok()) {
  627. return \Easemob\error($resp);
  628. }
  629. return true;
  630. }
  631. // Room
  632. /**
  633. * \~chinese
  634. * \brief
  635. * 查询聊天室黑名单
  636. *
  637. * \details
  638. * 查询一个聊天室黑名单中的用户列表。黑名单中的用户无法查看或收到该聊天室的信息。
  639. *
  640. * @param string $roomId 聊天室 ID
  641. * @return array 聊天室黑名单信息或者错误
  642. *
  643. * \~english
  644. * \brief
  645. * Query chat room blacklist
  646. *
  647. * \details
  648. * Query the list of users in a chat room blacklist. Users in the blacklist cannot view or receive information from this chat room.
  649. *
  650. * @param string $roomId Chat room ID
  651. * @return array Chat room blacklist information or error
  652. */
  653. public function getUsersBlockedJoinRoom($roomId)
  654. {
  655. if (!trim($roomId)) {
  656. \Easemob\exception('Please pass the chat room ID');
  657. }
  658. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/blocks/users';
  659. $resp = Http::get($uri, $this->auth->headers());
  660. if (!$resp->ok()) {
  661. return \Easemob\error($resp);
  662. }
  663. $data = $resp->data();
  664. return $data['data'];
  665. }
  666. /**
  667. * \~chinese
  668. * \brief
  669. * 添加单个用户至聊天室黑名单
  670. *
  671. * \details
  672. * 添加一个用户进入一个聊天室的黑名单。聊天室所有者无法被加入聊天室的黑名单。
  673. *
  674. * 用户进入聊天室黑名单后,会收到消息:“You are kicked out of the chatroom xxx”。之后,该用户无法查看和收发该聊天室的信息。
  675. *
  676. * @param string $roomId 聊天室 ID
  677. * @param string $username 要添加的 IM 用户名
  678. * @return boolean|array 成功或者错误
  679. *
  680. * \~english
  681. * \brief
  682. * Add a single user to the chat room blacklist
  683. *
  684. * \details
  685. * Add a user to the blacklist of a chat room. Chat room owners cannot be blacklisted.
  686. *
  687. * After entering the chat room blacklist, users will receive a message: "you are kicked out of the chat room XXX". After that, the user cannot view and send the information of the chat room.
  688. *
  689. * @param string $roomId Chat room ID
  690. * @param string $username User name
  691. * @return boolean|array Success or error
  692. */
  693. public function blockUserJoinRoom($roomId, $username)
  694. {
  695. return $this->addRoomBlocks($roomId, $username);
  696. }
  697. /**
  698. * \~chinese
  699. * \brief
  700. * 批量添加用户至聊天室黑名单
  701. *
  702. * \details
  703. * 将多个用户加入一个聊天室的黑名单。你一次最多可以添加 60 个用户至聊天室黑名单。聊天室所有者无法被加入聊天室的黑名单。
  704. *
  705. * 用户进入聊天室黑名单后,会收到消息:“You are kicked out of the chatroom xxx”。之后,这些用户无法查看和收发该聊天室的信息。
  706. *
  707. * @param string $roomId 聊天室 ID
  708. * @param array $usernames 要添加的 IM 用户名数组
  709. * @return boolean|array 成功或者错误
  710. *
  711. * \~english
  712. * \brief
  713. * Batch add users to chat room blacklist
  714. *
  715. * \details
  716. * Add multiple users to the blacklist of a chat room. You can add up to 60 users to the chat room blacklist at a time. Chat room owners cannot be blacklisted.
  717. *
  718. * After entering the chat room blacklist, users will receive a message: "you are kicked out of the chat room XXX". After that, these users cannot view and send the information of the chat room.
  719. *
  720. * @param string $roomId Chat room ID
  721. * @param array $usernames User name array
  722. * @return boolean|array Success or error
  723. */
  724. public function blockUsersJoinRoom($roomId, $usernames)
  725. {
  726. return $this->addRoomBlocks($roomId, $usernames);
  727. }
  728. /**
  729. * \~chinese
  730. * \brief
  731. * 从聊天室黑名单移除单个用户
  732. *
  733. * \details
  734. * 将指定用户移出聊天室黑名单。对于聊天室黑名单中的用户,如果需要将其再次加入聊天室,需要先将其从聊天室黑名单中移除。
  735. *
  736. * @param string $roomId 聊天室 ID
  737. * @param string $username 要添加的 IM 用户名
  738. * @return boolean|array 成功或者错误
  739. *
  740. * \~english
  741. * \brief
  742. * Remove a single user from the chat room blacklist
  743. *
  744. * \details
  745. * Remove the specified user from the chat room blacklist. For users in the chat room blacklist, if they need to join the chat room again, they need to be removed from the chat room blacklist first.
  746. *
  747. * @param string $roomId Chat room ID
  748. * @param string $username User name
  749. * @return boolean|array Success or error
  750. */
  751. public function unblockUserJoinRoom($roomId, $username)
  752. {
  753. return $this->removeRoomBlocks($roomId, $username);
  754. }
  755. /**
  756. * \~chinese
  757. * \brief
  758. * 从聊天室黑名单批量移除用户
  759. *
  760. * \details
  761. * 将多名指定用户从聊天室黑名单中移除。你每次最多可移除 60 个用户。对于聊天室黑名单中的用户,如果需要将其再次加入聊天室,需要先将其从聊天室黑名单中移除。
  762. *
  763. * @param string $roomId 聊天室 ID
  764. * @param array $usernames 要添加的 IM 用户名数组
  765. * @return boolean|array 成功或者错误
  766. *
  767. * \~english
  768. * \brief
  769. * Batch remove users from chat room blacklist
  770. *
  771. * \details
  772. * Remove multiple designated users from the chat room blacklist. You can remove up to 60 users at a time. For users in the chat room blacklist, if they need to join the chat room again, they need to be removed from the chat room blacklist first.
  773. *
  774. * @param string $roomId Chat room ID
  775. * @param array $usernames User name array
  776. * @return boolean|array Success or error
  777. */
  778. public function unblockUsersJoinRoom($roomId, $usernames)
  779. {
  780. return $this->removeRoomBlocks($roomId, $usernames);
  781. }
  782. /**
  783. * \~chinese
  784. * \brief
  785. * 禁言聊天室成员
  786. *
  787. * \details
  788. * 将用户禁言。用户被禁言后,将无法在聊天室中发送消息。
  789. *
  790. * @param string $roomId 聊天室 ID
  791. * @param array $usernames 要被添加禁言的用户 ID 数组
  792. * @param int $mute_duration 禁言的时间,单位毫秒,如果是“-1”代表永久(实际的到期时间为固定时间戳4638873600000,即2117-01-01 00:00:00)
  793. * @return boolean|array 成功或者错误
  794. *
  795. * \~english
  796. * \brief
  797. * Forbidden chat room members
  798. *
  799. * \details
  800. * Forbid users from speaking. After being banned, users will not be able to send messages in the chat room.
  801. *
  802. * @param string $roomId Chat room ID
  803. * @param array $usernames User name array
  804. * @param int $mute_duration Forbidden time, in milliseconds. If "- 1" means permanent (the actual expiration time is the fixed timestamp 4638873600000, i.e. 2117-01-01 00:00:00)
  805. * @return boolean|array Success or error
  806. */
  807. public function blockUserSendMsgToRoom($roomId, $usernames, $mute_duration = -1)
  808. {
  809. if (!trim($roomId)) {
  810. \Easemob\exception('Please pass the chat room ID');
  811. }
  812. if (!is_array($usernames) || empty($usernames)) {
  813. \Easemob\exception('Please pass the user name');
  814. }
  815. $mute_duration = (int)$mute_duration > 0 ? (int)$mute_duration : -1;
  816. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/mute';
  817. $resp = Http::post($uri, compact('usernames', 'mute_duration'), $this->auth->headers());
  818. if (!$resp->ok()) {
  819. return \Easemob\error($resp);
  820. }
  821. return true;
  822. }
  823. /**
  824. * \~chinese
  825. * \brief
  826. * 解除聊天室禁言成员
  827. *
  828. * \details
  829. * 将用户从禁言列表中移除,可以移除多个 member。移除后,用户可以正常在聊天室中发送消息。
  830. *
  831. * @param string $roomId 聊天室 ID
  832. * @param array $usernames 要移除禁言的用户 ID 数组
  833. * @return boolean|array 成功或者错误
  834. *
  835. * \~english
  836. * \brief
  837. * Unblock chat room members
  838. *
  839. * \details
  840. * You can remove multiple members by removing users from the forbidden list. After removal, users can send messages in the chat room normally.
  841. *
  842. * @param string $roomId Chat room ID
  843. * @param array $usernames User name array
  844. * @return boolean|array Success or error
  845. */
  846. public function unblockUserSendMsgToRoom($roomId, $usernames)
  847. {
  848. if (!trim($roomId)) {
  849. \Easemob\exception('Please pass the chat room ID');
  850. }
  851. if (!is_array($usernames) || empty($usernames)) {
  852. \Easemob\exception('Please pass the user name');
  853. }
  854. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/mute/' . implode(',', $usernames);
  855. $resp = Http::delete($uri, null, $this->auth->headers());
  856. if (!$resp->ok()) {
  857. return \Easemob\error($resp);
  858. }
  859. return true;
  860. }
  861. /**
  862. * \~chinese
  863. * \brief
  864. * 获取聊天室禁言列表
  865. *
  866. * @param string $roomId 聊天室 ID
  867. * @return array 禁言列表信息或者错误
  868. *
  869. * \~english
  870. * \brief
  871. * Get chat room forbidden list
  872. *
  873. * @param string $roomId Chat room ID
  874. * @return array Forbidden list information or error
  875. */
  876. public function getUsersBlockedSendMsgToRoom($roomId)
  877. {
  878. if (!trim($roomId)) {
  879. \Easemob\exception('Please pass the chat room ID');
  880. }
  881. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/mute';
  882. $resp = Http::get($uri, $this->auth->headers());
  883. if (!$resp->ok()) {
  884. return \Easemob\error($resp);
  885. }
  886. $data = $resp->data();
  887. return isset($data['data']) ? $data['data'] : $data;
  888. }
  889. /**
  890. * \~chinese
  891. * \brief
  892. * 禁言聊天室全体成员
  893. *
  894. * \details
  895. * 对所有聊天室成员一键禁言,即将聊天室的所有成员均加入禁言列表。设置聊天室全员禁言后,仅聊天室白名单中的用户可在聊天室内发消息。
  896. * @param string $roomId 聊天室 ID
  897. * @param int $mute_duration 禁言时长,单位为毫秒。
  898. * @return boolean|array 成功或者错误
  899. *
  900. * \~english
  901. * \brief
  902. * Forbidden all members of the room
  903. *
  904. * \details
  905. * One click prohibition for all members of the chat room, that is, all members of the chat room will be added to the prohibition list. After setting the prohibition of all members in the chat room, only users in the chat room white list can send messages in the chat room.
  906. *
  907. * @param string $roomId Chat room ID
  908. * @param int $mute_duration Forbidden speech duration, in milliseconds.
  909. * @return boolean|array Success or error
  910. */
  911. public function blockAllUserSendMsgToRoom($roomId, $mute_duration = -1)
  912. {
  913. if (!trim($roomId)) {
  914. \Easemob\exception('Please pass the chat room ID');
  915. }
  916. $mute_duration = (int)$mute_duration > 0 ? (int)$mute_duration : -1;
  917. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/ban';
  918. $resp = Http::post($uri, compact('mute_duration'), $this->auth->headers());
  919. if (!$resp->ok()) {
  920. return \Easemob\error($resp);
  921. }
  922. return true;
  923. }
  924. /**
  925. * \~chinese
  926. * \brief
  927. * 解除聊天室全员禁言
  928. *
  929. * \details
  930. * 一键取消对聊天室全体成员的禁言。移除后,聊天室成员可以在聊天室中正常发送消息。
  931. *
  932. * @param string $roomId 聊天室 ID
  933. * @return boolean|array 成功或者错误
  934. *
  935. * \~english
  936. * \brief
  937. * Lifting the ban on all members of the chat room
  938. *
  939. * \details
  940. * One click to cancel the ban on all members of the chat room. After removal, chat room members can send messages normally in the chat room.
  941. *
  942. * @param string $roomId Chat room ID
  943. * @return boolean|array Success or error
  944. */
  945. public function unblockAllUserSendMsgToRoom($roomId)
  946. {
  947. if (!trim($roomId)) {
  948. \Easemob\exception('Please pass the chat room ID');
  949. }
  950. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/ban';
  951. $resp = Http::delete($uri, null, $this->auth->headers());
  952. if (!$resp->ok()) {
  953. return \Easemob\error($resp);
  954. }
  955. return true;
  956. }
  957. /**
  958. * @ignore 设置用户全局禁言
  959. * @param string $username 用户名
  960. * @param int $chatMuteDuration 单聊消息禁言时间,单位为秒,非负整数,最大值为 2147483647,`0` 表示取消该帐号的单聊消息禁言,`-1` 表示该帐号被设置永久禁言,其它值表示该帐号的具体禁言时间,负值为非法值。
  961. * @param int $groupchatMuteDuration 群组消息禁言时间,单位为秒,规则同上。
  962. * @param int $chatroomMuteDuration 聊天室消息禁言时间,单位为秒,规则同上。
  963. * @return boolean|array 成功或者错误
  964. */
  965. private function blockUserSendMsgGlobal($username, $chatMuteDuration = -1, $groupchatMuteDuration = -1, $chatroomMuteDuration = -1)
  966. {
  967. if (!trim($username)) {
  968. \Easemob\exception('Please enter username');
  969. }
  970. $chatMuteDuration = (int)$chatMuteDuration >= 0 ? (int)$chatMuteDuration : -1;
  971. $groupchatMuteDuration = (int)$groupchatMuteDuration >= 0 ? (int)$groupchatMuteDuration : -1;
  972. $chatroomMuteDuration = (int)$chatroomMuteDuration >= 0 ? (int)$chatroomMuteDuration : -1;
  973. $data = array(
  974. 'username' => $username,
  975. 'chat' => $chatMuteDuration,
  976. 'groupchat' => $groupchatMuteDuration,
  977. 'chatroom' => $chatroomMuteDuration,
  978. );
  979. $uri = $this->auth->getBaseUri() . '/mutes';
  980. $resp = Http::post($uri, $data, $this->auth->headers());
  981. if (!$resp->ok()) {
  982. return \Easemob\error($resp);
  983. }
  984. $data = $resp->data();
  985. $data = $data['data'];
  986. if (isset($data['result']) && $data['result'] == 'ok') {
  987. return true;
  988. }
  989. return false;
  990. }
  991. /**
  992. * @ignore 账号封禁与解禁
  993. * @param string $username 要操作用户的用户名
  994. * @param int $status 0:禁用;1:解禁
  995. * @return boolean|array 成功或者错误
  996. */
  997. private function activate($username, $status = 1)
  998. {
  999. if (!trim($username)) {
  1000. \Easemob\exception('Please enter your username');
  1001. }
  1002. $status = (int)$status ? 1 : 0;
  1003. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  1004. $uri .= $status ? '/activate' : '/deactivate';
  1005. $resp = Http::post($uri, array(), $this->auth->headers());
  1006. if (!$resp->ok()) {
  1007. return \Easemob\error($resp);
  1008. }
  1009. return true;
  1010. }
  1011. /**
  1012. * @ignore 添加用户至群组黑名单
  1013. * @param string $groupId 群组 ID
  1014. * @param array|string $usernames 要添加的 IM 用户名,string: 添加单个用户;array: 批量添加
  1015. * @return boolean|array 成功或者错误
  1016. */
  1017. private function addGroupBlocks($groupId, $usernames)
  1018. {
  1019. if (!trim($groupId)) {
  1020. \Easemob\exception('Please pass the group ID');
  1021. }
  1022. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  1023. \Easemob\exception('Please pass the user name');
  1024. }
  1025. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/blocks/users';
  1026. $uri .= is_string($usernames) ? ('/' . $usernames) : '';
  1027. $body = is_array($usernames) ? compact('usernames') : null;
  1028. $resp = Http::post($uri, $body, $this->auth->headers());
  1029. if (!$resp->ok()) {
  1030. return \Easemob\error($resp);
  1031. }
  1032. return true;
  1033. }
  1034. /**
  1035. * @ignore 从群组黑名单移除用户
  1036. * @param string $groupId 群组 ID
  1037. * @param array|string $usernames 要移除的 IM 用户名,string: 移除单个用户;array: 批量移除
  1038. * @return boolean|array 成功或者错误
  1039. */
  1040. private function removeGroupBlocks($groupId, $usernames)
  1041. {
  1042. if (!trim($groupId)) {
  1043. \Easemob\exception('Please pass the group ID');
  1044. }
  1045. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  1046. \Easemob\exception('Please pass the user name');
  1047. }
  1048. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/blocks/users/';
  1049. $uri .= is_array($usernames) ? implode(',', $usernames) : $usernames;
  1050. $resp = Http::delete($uri, null, $this->auth->headers());
  1051. if (!$resp->ok()) {
  1052. return \Easemob\error($resp);
  1053. }
  1054. return true;
  1055. }
  1056. /**
  1057. * @ignore 添加用户至聊天室黑名单
  1058. * @param string $roomId 聊天室 ID
  1059. * @param array|string $usernames 要添加的 IM 用户名,string: 添加单个用户;array: 批量添加
  1060. * @return boolean|array 成功或者错误
  1061. */
  1062. private function addRoomBlocks($roomId, $usernames)
  1063. {
  1064. if (!trim($roomId)) {
  1065. \Easemob\exception('Please pass the chat room ID');
  1066. }
  1067. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  1068. \Easemob\exception('Please pass the user name');
  1069. }
  1070. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/blocks/users';
  1071. $uri .= is_string($usernames) ? ('/' . $usernames) : '';
  1072. $body = is_array($usernames) ? compact('usernames') : null;
  1073. $resp = Http::post($uri, $body, $this->auth->headers());
  1074. if (!$resp->ok()) {
  1075. return \Easemob\error($resp);
  1076. }
  1077. return true;
  1078. }
  1079. /**
  1080. * @ignore 从聊天室黑名单移除用户
  1081. * @param string $roomId 聊天室 ID
  1082. * @param array|string $usernames 要移除的 IM 用户名,string: 移除单个用户;array: 批量移除
  1083. * @return boolean|array 成功或者错误
  1084. */
  1085. private function removeRoomBlocks($roomId, $usernames)
  1086. {
  1087. if (!trim($roomId)) {
  1088. \Easemob\exception('Please pass the chat room ID');
  1089. }
  1090. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  1091. \Easemob\exception('Please pass the user name');
  1092. }
  1093. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/blocks/users/';
  1094. $uri .= is_array($usernames) ? implode(',', $usernames) : $usernames;
  1095. $resp = Http::delete($uri, null, $this->auth->headers());
  1096. if (!$resp->ok()) {
  1097. return \Easemob\error($resp);
  1098. }
  1099. return true;
  1100. }
  1101. }