Room.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Room 用于管理聊天室
  7. *
  8. * \~english
  9. * The `Room` is used to manage chat rooms
  10. */
  11. final class Room
  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. * 获取 app 中所有的聊天室(分页)
  28. *
  29. * @param int $limit 每页显示的数量,默认取 10 条
  30. * @param string $cursor 分页游标
  31. * @return array 聊天室列表信息或者错误
  32. *
  33. * \~english
  34. * \brief
  35. * Get all chat rooms in the app (paging)
  36. *
  37. * @param int $limit The number displayed on each page is 10 by default
  38. * @param string $cursor Paging cursor
  39. * @return array Chat room list information or error
  40. */
  41. public function listRooms($limit = 10, $cursor = '')
  42. {
  43. $limit = (int)$limit >= 0 ? (int)$limit : 10;
  44. $uri = $this->auth->getBaseUri() . '/chatrooms';
  45. $uri .= $limit ? '?limit=' . $limit : '';
  46. $uri .= ($limit && $cursor) ? '&cursor='.$cursor : '';
  47. $resp = Http::get($uri, $this->auth->headers());
  48. if (!$resp->ok()) {
  49. return \Easemob\error($resp);
  50. }
  51. $data = $resp->data();
  52. return array(
  53. 'cursor' => isset($data['cursor']) ? $data['cursor'] : '',
  54. 'data' => $data['data'],
  55. );
  56. }
  57. /**
  58. * \~chinese
  59. * \brief
  60. * 获取 app 中所有的聊天室
  61. *
  62. * @return array 聊天室列表信息或者错误
  63. *
  64. * \~english
  65. * \brief
  66. * Get all chat rooms in the app
  67. *
  68. * @return array Chat room list information or error
  69. */
  70. public function listAllRooms()
  71. {
  72. $result = $this->listRooms(0);
  73. return $result['data'];
  74. }
  75. /**
  76. * \~chinese
  77. * \brief
  78. * 获取用户加入的聊天室(分页)
  79. *
  80. * \details
  81. * 根据用户名称获取该用户加入的全部聊天室
  82. *
  83. * @param string $username 用户名
  84. * @param int $pageSize 每页获取的群组数量,默认取 10 条
  85. * @param int $pageNum 当前页码,默认取第 1 页
  86. * @return array 用户加入的聊天室列表或者错误
  87. *
  88. * \~english
  89. * \brief
  90. * Get the chat room that the user joined (paging)
  91. *
  92. * \details
  93. * Get all chat rooms joined by the user according to the user name
  94. *
  95. * @param string $username User name
  96. * @param int $pageSize The number of groups obtained per page is 10 by default
  97. * @param int $pageNum The current page number is page 1 by default
  98. * @return array The chat room list added by the user is incorrect
  99. */
  100. public function listRoomsUserJoined($username, $pageSize = 10, $pageNum = 1)
  101. {
  102. if (!trim($username)) {
  103. \Easemob\exception('Please pass the user name');
  104. }
  105. $pageSize = (int)$pageSize >= 0 ? (int)$pageSize : 10;
  106. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  107. $uri = $this->auth->getBaseUri() . '/users/' . trim($username) . '/joined_chatrooms';
  108. $uri .= $pageSize ? ('?pagesize=' . $pageSize . '&pagenum=' . $pageNum) : '';
  109. $resp = Http::get($uri, $this->auth->headers());
  110. if (!$resp->ok()) {
  111. return \Easemob\error($resp);
  112. }
  113. $data = $resp->data();
  114. return $data['data'];
  115. }
  116. /**
  117. * \~chinese
  118. * \brief
  119. * 获取用户加入的聊天室
  120. *
  121. * \details
  122. * 根据用户名称获取该用户加入的全部聊天室
  123. *
  124. * @param string $username 用户名
  125. * @return array 用户加入的聊天室列表或者错误
  126. *
  127. * \~english
  128. * \brief
  129. * Get the chat room that the user joined
  130. *
  131. * \details
  132. * Get all chat rooms joined by the user according to the user name
  133. *
  134. * @param string $username User name
  135. * @return array The chat room list added by the user is incorrect
  136. */
  137. public function listAllRoomsUserJoined($username)
  138. {
  139. return $this->listRoomsUserJoined($username, 0);
  140. }
  141. /**
  142. * \~chinese
  143. * \brief
  144. * 获取聊天室详情
  145. *
  146. * \details
  147. * 可以获取一个或多个聊天室的详情。当获取多个聊天室的详情时,可以直接填写多个 chatroom_id 并用 “,” 隔开,一次调用最多输入 100 个聊天室 ID,会返回所有存在的聊天室的详情,对于不存在的聊天室,response body 内返回 “chatroom id doesn’t exist”。
  148. *
  149. * @param string $roomId 聊天室 ID,多个之间用 “,” 分隔
  150. * @return array 聊天室详情或者错误
  151. *
  152. * \~english
  153. * \brief
  154. * Get chat room details
  155. *
  156. * \details
  157. * You can get details of one or more chat rooms. When obtaining the details of multiple chat rooms, you can directly fill in multiple chatrooms_ For all chat rooms that do not exist, enter "response ID" and return "response ID" at most once.
  158. *
  159. * @param string $roomId Chat room ID, separated by ","
  160. * @return array Chat room details or errors
  161. */
  162. public function getRoom($roomId)
  163. {
  164. if (!trim($roomId)) {
  165. \Easemob\exception('Please pass the chat room ID');
  166. }
  167. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId;
  168. $resp = Http::get($uri, $this->auth->headers());
  169. if (!$resp->ok()) {
  170. return \Easemob\error($resp);
  171. }
  172. $data = $resp->data();
  173. return strpos($roomId, ',') !== false ? $data['data'] : $data['data'][0];
  174. }
  175. /**
  176. * \~chinese
  177. * \brief
  178. * 创建聊天室
  179. *
  180. * \details
  181. * 创建一个聊天室,并设置聊天室名称、聊天室描述、公开聊天室/私有聊天室属性、聊天室成员最大人数(包括管理员)、加入公开聊天室是否需要批准、管理员、以及聊天室成员。
  182. *
  183. * @param array $name 聊天聊天室名称
  184. * @param string $description 聊天室描述
  185. * @param string $owner 聊天室的管理员
  186. * @param array $members 聊天室成员,此属性为可选的,但是如果加了此项,数组元素至少一个
  187. * @param int $maxusers 聊天室成员最大数(包括聊天室所有者),值为数值类型。
  188. * @return string|array 创建的聊天室 id 或者错误
  189. *
  190. * \~english
  191. * \brief
  192. * Create a chat room
  193. *
  194. * \details
  195. * Create a chat room, and set the chat room name, chat room description, public chat room / private chat room properties, the maximum number of chat room members (including administrators), whether approval is required to join the public chat room, administrators, and chat room members.
  196. *
  197. * @param array $name Chat room name
  198. * @param string $description Chat room description
  199. * @param string $owner Chat room administrator
  200. * @param array $members This attribute is optional for chat room members, but if this item is added, there must be at least one array element
  201. * @param int $maxusers Maximum number of chat room members (including chat room owners). The value is numeric.
  202. * @return string|array Created chat room ID or error
  203. */
  204. public function createRoom($name, $description, $owner, $members = array(), $maxusers = 0)
  205. {
  206. if (!trim($name)) {
  207. \Easemob\exception('Please pass the chat room name');
  208. }
  209. if (!trim($description)) {
  210. \Easemob\exception('Please pass the chat room description');
  211. }
  212. if (!trim($owner)) {
  213. \Easemob\exception('Please pass the chat room administrator');
  214. }
  215. if ($members && (!is_array($members) || empty($members))) {
  216. \Easemob\exception('Please pass chat room members');
  217. }
  218. $data = compact('name', 'description', 'owner', 'members');
  219. $maxusers = (int)$maxusers > 0 ? (int)$maxusers : 0;
  220. if ($maxusers) {
  221. $data['maxusers'] = $maxusers;
  222. }
  223. $uri = $this->auth->getBaseUri() . '/chatrooms';
  224. $resp = Http::post($uri, $data, $this->auth->headers());
  225. if (!$resp->ok()) {
  226. return \Easemob\error($resp);
  227. }
  228. $data = $resp->data();
  229. return $data['data']['id'];
  230. }
  231. /**
  232. * \~chinese
  233. * \brief
  234. * 修改聊天室信息
  235. *
  236. * \details
  237. * 修改成功的数据行会返回 true,失败为 false。请求 body 只接收 name、description、maxusers 三个属性。传其他字段,或者不能修改的字段会抛异常。
  238. *
  239. * @param array $data 聊天室信息
  240. * - `name` string 类型,聊天室名称,修改时值不能包含斜杠(“/”)。
  241. * - `description` string 类型,聊天室描述,修改时值不能包含斜杠(“/”)。
  242. * - `maxusers` int 类型,聊天室最大成员数(包括聊天室所有者),值为数值类型。
  243. * @return boolean|array 成功或者错误
  244. *
  245. * \~english
  246. * \brief
  247. * Modify chat room information
  248. *
  249. * \details
  250. * The modified data row will return true, and the failure will be false. The request body only receives three attributes: name, description and maxusers. Exceptions will be thrown if other fields are passed or fields that cannot be modified.
  251. *
  252. * @param array $data Chat room information
  253. * - `name` String type, chat room name. The value cannot contain slash ("/") when modified.
  254. * - `description` String type, chat room description. When modifying, the value cannot contain slash ("/").
  255. * - `maxusers` The type of chat room is int, and the value is the maximum number of chat room owners.
  256. * @return boolean|array Success or error
  257. */
  258. public function updateRoom($data)
  259. {
  260. if (!is_array($data) || empty($data)) {
  261. \Easemob\exception('Please pass the chat room information');
  262. }
  263. if (!isset($data['room_id']) || !trim($data['room_id'])) {
  264. \Easemob\exception('Please pass the chat room ID');
  265. }
  266. if (isset($data['name']) && preg_match('/\//', $data['name'])) {
  267. \Easemob\exception('Chat room names cannot contain slashes ("/")');
  268. }
  269. if (isset($data['description']) && preg_match('/\//', $data['description'])) {
  270. \Easemob\exception('Chat room description cannot contain slashes ("/")');
  271. }
  272. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $data['room_id'];
  273. unset($data['room_id']);
  274. if (isset($data['maxusers'])) {
  275. $data['maxusers'] = (int)$data['maxusers'];
  276. }
  277. $resp = Http::put($uri, $data, $this->auth->headers());
  278. if (!$resp->ok()) {
  279. return \Easemob\error($resp);
  280. }
  281. return true;
  282. }
  283. /**
  284. * \~chinese
  285. * \brief
  286. * 删除聊天室
  287. *
  288. * \details
  289. * 删除单个聊天室。如果被删除的聊天室不存在,会返回错误。
  290. *
  291. * @param string $roomId 聊天室 ID
  292. * @return boolean|array 成功或者错误
  293. *
  294. * \~english
  295. * \brief
  296. * Delete chat room
  297. *
  298. * \details
  299. * Delete a single chat room. If the deleted chat room does not exist, an error will be returned.
  300. *
  301. * @param string $roomId Chat room ID
  302. * @return boolean|array Success or error
  303. */
  304. public function destroyRoom($roomId)
  305. {
  306. if (!trim($roomId)) {
  307. \Easemob\exception('Please pass the chat room ID');
  308. }
  309. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId;
  310. $resp = Http::delete($uri, null, $this->auth->headers());
  311. if (!$resp->ok()) {
  312. return \Easemob\error($resp);
  313. }
  314. return true;
  315. }
  316. /**
  317. * \~chinese
  318. * \brief
  319. * 获取聊天室公告
  320. *
  321. * \details
  322. * 获取指定聊天室 ID 的聊天室公告。
  323. *
  324. * @param string $roomId 聊天室 id
  325. * @return array 公告信息或者错误
  326. *
  327. * \~english
  328. * \brief
  329. * Get chat announcements
  330. *
  331. * \details
  332. * Gets the chat announcement of the specified chat room ID.
  333. *
  334. * @param string $roomId Chat room ID
  335. * @return array Announcement information or error
  336. */
  337. public function getRoomAnnouncement($roomId)
  338. {
  339. if (!trim($roomId)) {
  340. \Easemob\exception('Please pass the chat room ID');
  341. }
  342. $uri = $this->auth->getBaseUri() . '/chatrooms/'. $roomId . '/announcement';
  343. $resp = Http::get($uri, $this->auth->headers());
  344. if (!$resp->ok()) {
  345. return \Easemob\error($resp);
  346. }
  347. $data = $resp->data();
  348. return $data['data'];
  349. }
  350. /**
  351. * \~chinese
  352. * \brief
  353. * 修改聊天室公告
  354. *
  355. * \details
  356. * 修改指定聊天室 ID 的聊天室公告。聊天室公告内容不能超过 512 个字符。
  357. * @param string $roomId 聊天室 ID
  358. * @param string $announcement 聊天室公告内容
  359. * @return boolean|array 成功或者错误
  360. *
  361. * \~english
  362. * \brief
  363. * Modify chat announcement
  364. *
  365. * \details
  366. * Modify the chat announcement of the specified chat ID. The content of chat room announcement cannot exceed 512 characters.
  367. * @param string $roomId Chat room ID
  368. * @param string $announcement Chat room announcement content
  369. * @return boolean|array Success or error
  370. */
  371. public function updateRoomAnnouncement($roomId, $announcement)
  372. {
  373. if (!trim($roomId) || !trim($announcement)) {
  374. \Easemob\exception('Please pass the chat room ID and announcement content');
  375. }
  376. if (mb_strlen($announcement) > 512) {
  377. \Easemob\exception('The content of the announcement room cannot exceed 512 characters');
  378. }
  379. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/announcement';
  380. $body = compact('announcement');
  381. $resp = Http::post($uri, $body, $this->auth->headers());
  382. if (!$resp->ok()) {
  383. return \Easemob\error($resp);
  384. }
  385. $data = $resp->data();
  386. return $data['data']['result'];
  387. }
  388. /**
  389. * \~chinese
  390. * \brief
  391. * 分页获取聊天室成员
  392. *
  393. * @param string $roomId 聊天室 ID
  394. * @param int $pageSize 每页获取的群组数量,默认取 10 条
  395. * @param int $pageNum 当前页码,默认取第 1 页
  396. * @return array 聊天室成员信息或者错误
  397. *
  398. * \~english
  399. * \brief
  400. * Paging to get chat room members
  401. *
  402. * @param string $roomId Chat room ID
  403. * @param int $pageSize The number of groups obtained per page is 10 by default
  404. * @param int $pageNum The current page number is page 1 by default
  405. * @return array Chat room member information or error
  406. */
  407. public function listRoomMembers($roomId, $pageSize = 10, $pageNum = 1)
  408. {
  409. if (!trim($roomId)) {
  410. \Easemob\exception('Please pass the chat room ID');
  411. }
  412. $pageSize = (int)$pageSize >= 0 ? (int)$pageSize : 10;
  413. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  414. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/users';
  415. $uri .= $pageSize ? ('?pagesize=' . $pageSize . '&pagenum=' . $pageNum) : '';
  416. $resp = Http::get($uri, $this->auth->headers());
  417. if (!$resp->ok()) {
  418. return \Easemob\error($resp);
  419. }
  420. $data = $resp->data();
  421. return $data['data'];
  422. }
  423. /**
  424. * \~chinese
  425. * \brief
  426. * 获取聊天室所有成员
  427. *
  428. * @param string $roomId 聊天室 ID
  429. * @return array 聊天室成员信息或者错误
  430. *
  431. * \~english
  432. * \brief
  433. * Get all members of the chat room
  434. *
  435. * @param string $roomId Chat room ID
  436. * @return array Chat room member information or error
  437. */
  438. public function listRoomMembersAll($roomId)
  439. {
  440. return $this->listRoomMembers($roomId, 0);
  441. }
  442. /**
  443. * \~chinese
  444. * \brief
  445. * 添加单个聊天室成员
  446. *
  447. * \details
  448. * 一次给聊天室添加一个成员,不能重复添加同一个成员。如果用户已经是聊天室成员,将添加失败,并返回错误。
  449. *
  450. * @param string $roomId 聊天室 ID
  451. * @param string $username 环信用户 ID
  452. * @return boolean|array 成功或者错误
  453. *
  454. * \~english
  455. * \brief
  456. * Add individual chat room members
  457. *
  458. * \details
  459. * Add one member to the chat room at a time. You cannot add the same member repeatedly. If the user is already a member of the chat room, the addition will fail with an error.
  460. *
  461. * @param string $roomId Chat room ID
  462. * @param string $username User name
  463. * @return boolean|array Success or error
  464. */
  465. public function addRoomMember($roomId, $username)
  466. {
  467. return $this->addUsers($roomId, $username);
  468. }
  469. /**
  470. * \~chinese
  471. * \brief
  472. * 批量添加聊天室成员
  473. *
  474. * \details
  475. * 向聊天室添加多位用户,一次性最多可添加 60 位用户。
  476. *
  477. * @param string $roomId 聊天室 ID
  478. * @param array $usernames 环信用户 ID 数组
  479. * @return boolean|array 成功或者错误
  480. *
  481. * \~english
  482. * \brief
  483. * Batch add chat members
  484. *
  485. * \details
  486. * Add more than 60 users to the chat room at one time.
  487. *
  488. * @param string $roomId Chat room ID
  489. * @param array $usernames User name array
  490. * @return boolean|array Success or error
  491. */
  492. public function addRoomMembers($roomId, $usernames)
  493. {
  494. return $this->addUsers($roomId, $usernames);
  495. }
  496. /**
  497. * \~chinese
  498. * \brief
  499. * 删除单个聊天室成员
  500. *
  501. * \details
  502. * 从聊天室删除一个成员。如果被删除用户不在聊天室中,或者聊天室不存在,将返回错误。
  503. *
  504. * @param string $roomId 聊天室 ID
  505. * @param string $username 环信用户 ID
  506. * @return boolean|array 成功或者错误
  507. *
  508. * \~english
  509. * \brief
  510. * Delete individual chat members
  511. *
  512. * \details
  513. * Delete a member from the chat room. If the deleted user is not in the chat room, or the chat room does not exist, an error will be returned.
  514. *
  515. * @param string $roomId Chat room ID
  516. * @param string $username User name
  517. * @return boolean|array Success or error
  518. */
  519. public function removeRoomMember($roomId, $username)
  520. {
  521. return $this->removeUsers($roomId, $username);
  522. }
  523. /**
  524. * \~chinese
  525. * \brief
  526. * 批量删除聊天室成员
  527. *
  528. * \details
  529. * 从聊天室删除多个成员。如果被删除用户不在聊天室中,或者聊天室不存在,将返回错误。
  530. *
  531. * 一次最多传 100 个用户 ID。
  532. *
  533. * @param string $roomId 聊天室 ID
  534. * @param array $usernames 环信用户 ID 数组
  535. * @return boolean|array 成功或者错误
  536. *
  537. * \~english
  538. * \brief
  539. * Batch delete chat room members
  540. *
  541. * \details
  542. * Delete multiple members from the chat room. If the deleted user is not in the chat room, or the chat room does not exist, an error will be returned.
  543. *
  544. * Up to 100 user IDs can be transmitted at a time.
  545. *
  546. * @param string $roomId Chat room ID
  547. * @param array $usernames User name array
  548. * @return boolean|array Success or error
  549. */
  550. public function removeRoomMembers($roomId, $usernames)
  551. {
  552. return $this->removeUsers($roomId, $usernames);
  553. }
  554. /**
  555. * \~chinese
  556. * \brief
  557. * 获取聊天室管理员列表
  558. *
  559. * @param string $roomId 聊天室 ID
  560. * @return array 聊天室管理员列表信息或者错误
  561. *
  562. * \~english
  563. * \brief
  564. * Get the list of chat room administrators
  565. *
  566. * @param string $roomId Chat room ID
  567. * @return array Chat room administrator list information or error
  568. */
  569. public function listRoomAdminsAll($roomId)
  570. {
  571. if (!trim($roomId)) {
  572. \Easemob\exception('Please pass the chat room ID');
  573. }
  574. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/admin';
  575. $resp = Http::get($uri, $this->auth->headers());
  576. if (!$resp->ok()) {
  577. return \Easemob\error($resp);
  578. }
  579. $data = $resp->data();
  580. return $data['data'];
  581. }
  582. /**
  583. * \~chinese
  584. * \brief
  585. * 添加聊天室管理员
  586. *
  587. * @param string $roomId 聊天室 ID
  588. * @param string $newadmin 添加的新管理员用户 ID
  589. * @return boolean|array 成功或者错误
  590. *
  591. * \~english
  592. * \brief
  593. * Add chat administrator
  594. *
  595. * @param string $roomId Chat room ID
  596. * @param string $newadmin New administrator user ID added
  597. * @return boolean|array Success or error
  598. */
  599. public function promoteRoomAdmin($roomId, $newadmin)
  600. {
  601. if (!trim($roomId) || !trim($newadmin)) {
  602. \Easemob\exception('Please pass the chat room ID and the new administrator user ID to be added');
  603. }
  604. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/admin';
  605. $body = compact('newadmin');
  606. $resp = Http::post($uri, $body, $this->auth->headers());
  607. if (!$resp->ok()) {
  608. return \Easemob\error($resp);
  609. }
  610. return true;
  611. }
  612. /**
  613. * \~chinese
  614. * \brief
  615. * 移除聊天室管理员
  616. *
  617. * \details
  618. * 将用户的角色从聊天室管理员降为普通聊天室成员。
  619. *
  620. * @param string $roomId 聊天室 ID
  621. * @param string $oldadmin 移除的管理员用户 ID
  622. * @return boolean|array 成功或者错误
  623. *
  624. * \~english
  625. * \brief
  626. * Remove chat admin
  627. *
  628. * \details
  629. * Reduce the user's role from chat room administrator to ordinary chat room member.
  630. *
  631. * @param string $roomId Chat room ID
  632. * @param string $oldadmin Removed administrator user ID
  633. * @return boolean|array Success or error
  634. */
  635. public function demoteRoomAdmin($roomId, $oldadmin)
  636. {
  637. if (!trim($roomId) || !trim($oldadmin)) {
  638. \Easemob\exception('Please pass the chat room ID and the administrator user ID to be removed');
  639. }
  640. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/admin/' . $oldadmin;
  641. $resp = Http::delete($uri, null, $this->auth->headers());
  642. if (!$resp->ok()) {
  643. return \Easemob\error($resp);
  644. }
  645. $data = $resp->data();
  646. return $data['data']['result'] === 'success' ? true : false;
  647. }
  648. /**
  649. * \~chinese
  650. * \brief
  651. * 分页获取聊天室超级管理员列表
  652. *
  653. * @param int $pageSize 每页获取的数量,默认取 10 条
  654. * @param int $pageNum 当前页码,默认取第 1 页
  655. * @return array 超级管理员列表信息或者错误
  656. *
  657. * \~english
  658. * \brief
  659. * Paging to get the list of chat room super administrators
  660. *
  661. * @param int $pageSize The quantity obtained per page is 10 by default
  662. * @param int $pageNum The current page number is page 1 by default
  663. * @return array Super administrator list information or error
  664. */
  665. public function listRoomSuperAdmins($pageSize = 10, $pageNum = 1)
  666. {
  667. return $this->superAdmins($pageSize, $pageNum);
  668. }
  669. /// @cond
  670. public function listRoomSuperAdminsAll()
  671. {
  672. return $this->superAdmins(0);
  673. }
  674. /// @endcond
  675. /**
  676. * \~chinese
  677. * \brief
  678. * 添加超级管理员
  679. *
  680. * \details
  681. * 给用户添加聊天室超级管理员身份,一次只能添加一个。
  682. *
  683. * @param string $superadmin 添加的用户名称
  684. * @return boolean|array 成功或者错误
  685. *
  686. * \~english
  687. * \brief
  688. * Add super administrator
  689. *
  690. * \details
  691. * Add the chat room super administrator identity to users. You can only add one at a time.
  692. *
  693. * @param string $superadmin User name
  694. * @return boolean|array Success or error
  695. */
  696. public function promoteRoomSuperAdmin($superadmin)
  697. {
  698. if (!is_string($superadmin) || !trim($superadmin)) {
  699. \Easemob\exception('Please pass the user name');
  700. }
  701. $uri = $this->auth->getBaseUri() . '/chatrooms/super_admin';
  702. $resp = Http::post($uri, compact('superadmin'), $this->auth->headers());
  703. if (!$resp->ok()) {
  704. return \Easemob\error($resp);
  705. }
  706. return true;
  707. }
  708. /**
  709. * \~chinese
  710. * \brief
  711. * 撤销超级管理员
  712. *
  713. * @param string $superadmin 需要移除的 IM 用户名
  714. * @return boolean|array 成功或者错误
  715. *
  716. * \~chinese
  717. * \brief
  718. * 撤销超级管理员
  719. *
  720. * @param string $superadmin User name
  721. * @return boolean|array Success or error
  722. */
  723. public function demoteRoomSuperAdmin($superadmin)
  724. {
  725. if (!trim($superadmin)) {
  726. \Easemob\exception('Please pass the user name');
  727. }
  728. $uri = $this->auth->getBaseUri() . '/chatrooms/super_admin/' . $superadmin;
  729. $resp = Http::delete($uri, null, $this->auth->headers());
  730. if (!$resp->ok()) {
  731. return \Easemob\error($resp);
  732. }
  733. return true;
  734. }
  735. /**
  736. * @ignore (批量)添加聊天室成员
  737. * @param string $roomId 聊天室 ID
  738. * @param string|array $usernames 环信用户 ID
  739. * @return boolean|array 成功或者错误
  740. */
  741. private function addUsers($roomId, $usernames)
  742. {
  743. if (!trim($roomId)) {
  744. \Easemob\exception('Please pass the chat room ID');
  745. }
  746. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  747. \Easemob\exception('Please pass the user name');
  748. }
  749. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/users';
  750. $uri .= is_array($usernames) ? '' : ('/' . $usernames);
  751. $body = is_array($usernames) ? compact('usernames') : null;
  752. $resp = Http::post($uri, $body, $this->auth->headers());
  753. if (!$resp->ok()) {
  754. return \Easemob\error($resp);
  755. }
  756. return true;
  757. }
  758. /**
  759. * @ignore (批量)移除聊天室成员
  760. * @param string $roomId 聊天室 ID
  761. * @param string|array $usernames 环信用户 ID,string: 移除单个成员;array: 批量移除成员
  762. * @return boolean|array 成功或者错误
  763. */
  764. private function removeUsers($roomId, $usernames)
  765. {
  766. if (!trim($roomId)) {
  767. \Easemob\exception('Please pass the chat room ID');
  768. }
  769. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  770. \Easemob\exception('Please pass the user name');
  771. }
  772. $uri = $this->auth->getBaseUri() . '/chatrooms/' . $roomId . '/users/';
  773. $uri .= is_array($usernames) ? implode(',', $usernames) : $usernames;
  774. $resp = Http::delete($uri, null, $this->auth->headers());
  775. if (!$resp->ok()) {
  776. return \Easemob\error($resp);
  777. }
  778. return true;
  779. }
  780. /**
  781. * @ignore 分页获取聊天室超级管理员列表
  782. * @param int $pageSize 每页获取的群组数量,默认取 10 条
  783. * @param int $pageNum 当前页码,默认取第 1 页
  784. * @return array 超级管理员列表信息或者错误
  785. */
  786. private function superAdmins($pageSize = 10, $pageNum = 1)
  787. {
  788. $pageSize = (int)$pageSize >= 0 ? (int)$pageSize : 10;
  789. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  790. $uri = $this->auth->getBaseUri() . '/chatrooms/super_admin';
  791. $uri .= $pageSize ? ('?pagesize=' . $pageSize . '&pagenum=' . $pageNum) : '';
  792. $resp = Http::get($uri, $this->auth->headers());
  793. if (!$resp->ok()) {
  794. return \Easemob\error($resp);
  795. }
  796. $data = $resp->data();
  797. return $data['data'];
  798. }
  799. }