Contact.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Contact 用来管理联系人(添加好友等)
  7. *
  8. * \~english
  9. * The `Contact` is used to manage contacts (add friends, etc.)
  10. */
  11. final class Contact
  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. * \details
  30. * 好友必须是和自己在一个 App Key 下的用户。免费版 App Key 下的每个用户的好友数量上限为 1000,不同版本 App Key 上限不同,具体可参考:<a href="https://www.easemob.com/pricing/im" target="_blank">版本功能介绍</a>。
  31. *
  32. * @param string $username 要添加好友的用户名
  33. * @param string $contact 好友用户名
  34. * @return boolean|array 成功或者错误
  35. *
  36. * \~english
  37. * \brief
  38. * Add friends
  39. *
  40. * \details
  41. * Friends must be users under the same app key with themselves. The maximum number of friends of each user under the free version of APP key is 1000. The maximum number of friends of different versions of APP key is different. For details, please refer to:<a href="https://www.easemob.com/pricing/im" target="_blank">version function introduction</a>.
  42. *
  43. * @param string $username User name
  44. * @param string $contact Friend user name
  45. * @return boolean|array Success or error
  46. */
  47. public function add($username, $contact)
  48. {
  49. if (!trim($username) || !trim($contact)) {
  50. \Easemob\exception('Please enter username and friend username');
  51. }
  52. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/contacts/users/' . $contact;
  53. $resp = Http::post($uri, array(), $this->auth->headers());
  54. if (!$resp->ok()) {
  55. return \Easemob\error($resp);
  56. }
  57. return true;
  58. }
  59. /**
  60. * \~chinese
  61. * \brief
  62. * 移除好友
  63. *
  64. * \details
  65. * 从用户的好友列表中移除一个用户。
  66. *
  67. * @param string $username 要移除好友的用户名
  68. * @param string $contact 好友用户名
  69. * @return boolean|array 成功或者错误
  70. *
  71. * \~english
  72. * \brief
  73. * Remove friends
  74. *
  75. * \details
  76. * Removes a user from the user's friends list.
  77. *
  78. * @param string $username User name
  79. * @param string $contact Friend user name
  80. * @return boolean|array Success or error
  81. */
  82. public function remove($username, $contact)
  83. {
  84. if (!trim($username) || !trim($contact)) {
  85. \Easemob\exception('Please enter username and friend username');
  86. }
  87. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/contacts/users/' . $contact;
  88. $resp = Http::delete($uri, null, $this->auth->headers());
  89. if (!$resp->ok()) {
  90. return \Easemob\error($resp);
  91. }
  92. return true;
  93. }
  94. /**
  95. * \~chinese
  96. * \brief
  97. * 获取好友列表
  98. *
  99. * @param string $username 要获取好友列表的用户名
  100. * @return array 好友列表或者错误
  101. *
  102. * \~english
  103. * \brief
  104. * Get friends list
  105. *
  106. * @param string $username User name
  107. * @return array Friends list or error
  108. */
  109. public function get($username)
  110. {
  111. if (!trim($username)) {
  112. \Easemob\exception('Please enter username');
  113. }
  114. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/contacts/users';
  115. $resp = Http::get($uri, $this->auth->headers());
  116. if (!$resp->ok()) {
  117. return \Easemob\error($resp);
  118. }
  119. $data = $resp->data();
  120. return $data['data'];
  121. }
  122. }