UserMetadata.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * UserMetadata 用来管理用户属性
  7. *
  8. * \~english
  9. * The `UserMetadata` is used to manage user attribute
  10. */
  11. final class UserMetadata
  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. * 用户属性的内容为一个或多个纯文本键值对,默认单一用户的属性总长不得超过 2 KB,默认一个 app 下所有用户的所有属性总长不得超过 10 GB。
  31. *
  32. * @param string $username 要设置属性的用户名
  33. * @param array $metadata 要设置的属性(键:属性名;值:属性值)
  34. * @return boolean|array 成功或者错误
  35. *
  36. * \~english
  37. * \brief
  38. * Set user properties
  39. *
  40. * \details
  41. * The content of user attributes is one or more plain text key value pairs. By default, the total length of attributes of a single user shall not exceed 2 kb. By default, the total length of all attributes of all users under an app shall not exceed 10 GB.
  42. *
  43. * @param string $username User name
  44. * @param array $metadata Properties(key: attribute name; value: attribute value)
  45. * @return boolean|array Success or error
  46. */
  47. public function setMetadataToUser($username, $metadata)
  48. {
  49. if (!trim($username)) {
  50. \Easemob\exception('Please enter username');
  51. }
  52. if (!is_array($metadata) || empty($metadata)) {
  53. \Easemob\exception('Please enter metadata');
  54. }
  55. $uri = $this->auth->getBaseUri() . '/metadata/user/' . $username;
  56. $resp = Http::put($uri, http_build_query($metadata), $this->auth->headers());
  57. if (!$resp->ok()) {
  58. return \Easemob\error($resp);
  59. }
  60. return true;
  61. }
  62. /**
  63. * \~chinese
  64. * \brief
  65. * 获取用户属性
  66. *
  67. * \details
  68. * 获取指定用户的所有用户属性键值对。如果指定的用户或用户属性不存在,返回空数据 {}。
  69. *
  70. * @param string $username 要获取属性的用户名
  71. * @return array 用户属性或者错误
  72. *
  73. * \~english
  74. * \brief
  75. * Get user properties
  76. *
  77. * \details
  78. * Gets all user attribute key value pairs for the specified user. If the specified user or user attribute does not exist, null data {} is returned.
  79. *
  80. * @param string $username User name
  81. * @return array User properties or error
  82. */
  83. public function getMetadataFromUser($username)
  84. {
  85. if (!trim($username)) {
  86. \Easemob\exception('Please enter username');
  87. }
  88. $uri = $this->auth->getBaseUri() . '/metadata/user/' . $username;
  89. $resp = Http::get($uri, $this->auth->headers());
  90. if (!$resp->ok()) {
  91. return \Easemob\error($resp);
  92. }
  93. $data = $resp->data();
  94. return $data['data'];
  95. }
  96. /**
  97. * \~chinese
  98. * \brief
  99. * 批量获取用户属性
  100. *
  101. * \details
  102. * 根据指定的用户名列表和属性列表,查询用户属性。如果指定的用户或用户属性不存在,返回空数据 {}。 每次最多指定 100 个用户。
  103. *
  104. * @param array $targets 用户名列表,最多 100 个用户名。
  105. * @param array $properties 属性名列表,查询结果只返回该列表中包含的属性,不在该列表中的属性将被忽略。
  106. * @return array 用户属性(数组键是用户名,数组值是用户对应的属性)或者错误
  107. *
  108. * \~english
  109. * \brief
  110. * Get user attributes in batch
  111. *
  112. * \details
  113. * Query user attributes according to the specified user name list and attribute list. If the specified user or user attribute does not exist, null data {} is returned. Specify up to 100 users at a time.
  114. *
  115. * @param array $targets User name list, up to 100 user names.
  116. * @param array $properties Attribute name list. The query result only returns the attributes contained in the list, and the attributes not in the list will be ignored.
  117. * @return array User attribute (the array key is the user name, and the array value is the attribute corresponding to the user) or error
  118. */
  119. public function batchGetMetadataFromUser($targets, $properties)
  120. {
  121. if (!is_array($targets) || empty($targets) || !is_array($properties) || empty($properties)) {
  122. \Easemob\exception('Parameters error');
  123. }
  124. // 最多 100 个用户
  125. $limitNums = 100;
  126. if (count($targets) > $limitNums) {
  127. // 截取前 100 个用户
  128. $targets = array_slice($targets, 0, $limitNums);
  129. }
  130. $uri = $this->auth->getBaseUri() . '/metadata/user/get';
  131. $body = compact('targets', 'properties');
  132. $resp = Http::post($uri, $body, $this->auth->headers());
  133. if (!$resp->ok()) {
  134. return \Easemob\error($resp);
  135. }
  136. $data = $resp->data();
  137. return $data['data'];
  138. }
  139. /**
  140. * \~chinese
  141. * \brief
  142. * 获取用户属性总量大小
  143. *
  144. * \details
  145. * 获取该 app 下所有用户的属性数据大小,单位为 byte。
  146. *
  147. * @return float|array 用户属性总量大小(单位:byte)或者错误
  148. *
  149. * \~english
  150. * \brief
  151. * Get the total size of user attributes
  152. *
  153. * \details
  154. * Get the attribute data size of all users under the app, in bytes.
  155. *
  156. * @return float|array Total size of user attributes (unit: byte) or error
  157. */
  158. public function getUsage()
  159. {
  160. $uri = $this->auth->getBaseUri() . '/metadata/user/capacity';
  161. $resp = Http::get($uri, $this->auth->headers());
  162. if (!$resp->ok()) {
  163. return \Easemob\error($resp);
  164. }
  165. $data = $resp->data();
  166. return $data['data'];
  167. }
  168. /**
  169. * \~chinese
  170. * \brief
  171. * 删除用户属性
  172. *
  173. * \details
  174. * 删除指定用户的所有属性。如果指定的用户或用户属性不存在(可能已删除),也视为删除成功。
  175. *
  176. * @param string $username 用户名
  177. * @return boolean|array 成功或者错误
  178. *
  179. * \~english
  180. * \brief
  181. * Delete user attributes
  182. *
  183. * \details
  184. * Deletes all properties of the specified user. If the specified user or user attribute does not exist (may have been deleted), the deletion is also regarded as successful.
  185. *
  186. * @param string $username User name
  187. * @return boolean|array Success or error
  188. */
  189. public function deleteMetadataFromUser($username)
  190. {
  191. if (!trim($username)) {
  192. \Easemob\exception('Please enter username');
  193. }
  194. $uri = $this->auth->getBaseUri() . '/metadata/user/' . $username;
  195. $resp = Http::delete($uri, null, $this->auth->headers());
  196. if (!$resp->ok()) {
  197. return \Easemob\error($resp);
  198. }
  199. $data = $resp->data();
  200. return $data['data'];
  201. }
  202. }