UNPKG

react-native-agora-chat

Version:
93 lines (89 loc) 3.28 kB
import { MTfetchUserInfoById, MTupdateOwnUserInfo } from './__internal__/Consts'; import { Native } from './__internal__/Native'; import { ChatClient } from './ChatClient'; import { chatlog } from './common/ChatConst'; import { ChatUserInfo } from './common/ChatUserInfo'; /** * The user information manager for updating and getting user attributes. */ export class ChatUserInfoManager extends Native { static TAG = 'ChatUserInfoManager'; constructor() { super(); } /** * Modifies the user attributes of the current user. * * @params The parameter set. * - [nickName] The nickname of the user. * - [avatarUrl] The avatar URL of the user. * - [mail] The email address of the user. * - [phone] The phone number of the user. * - [gender] The gender of the user. The value can only be `0`, `1`, or `2`. Other values are invalid. * - `0`: (Default) Unknown; * - `1`: Male; * - `2`: Female. * - [sign] The signature of the user. * - [birth] The birthday of the user. * - [ext] The custom extension information of the user. You can set it to an empty string or type custom information and encapsulate them as a JSON string. * * @throws A description of the exception. See {@link ChatError}. */ async updateOwnUserInfo(params) { chatlog.log(`${ChatUserInfoManager.TAG}: updateOwnUserInfo: `, params); const userId = await ChatClient.getInstance().getCurrentUsername(); const ret = await this.fetchUserInfoById([userId]); if (ret.has(userId)) { let userInfo = new ChatUserInfo(ret.get(userId)); userInfo = Object.assign(userInfo, params); let r = await Native._callMethod(MTupdateOwnUserInfo, { [MTupdateOwnUserInfo]: { userInfo } }); ChatUserInfoManager.checkErrorFromResult(r); } } /** * Gets the user attributes of the specified users. * * @param userIds The user ID array. * @returns A map that contains key-value pairs where the key is the user ID and the value is user attributes,see {@link ChatUserInfo}. * * @throws A description of the exception. See {@link ChatError}. */ async fetchUserInfoById(userIds) { chatlog.log(`${ChatUserInfoManager.TAG}: fetchUserInfoById: `, userIds); let r = await Native._callMethod(MTfetchUserInfoById, { [MTfetchUserInfoById]: { userIds: userIds } }); ChatUserInfoManager.checkErrorFromResult(r); const ret = new Map(); Object.entries(r === null || r === void 0 ? void 0 : r[MTfetchUserInfoById]).forEach(value => { const userInfo = new ChatUserInfo(value[1]); ret.set(value[0], userInfo); }); return ret; } /** * Gets attributes of the current user from the server. * * @returns The obtained user attributes. See {@link ChatUserInfo}. * * @throws A description of the exception. See {@link ChatError}. */ async fetchOwnInfo() { chatlog.log(`${ChatUserInfoManager.TAG}: fetchOwnInfo: `); const id = await ChatClient.getInstance().getCurrentUsername(); if (id) { const ret = await this.fetchUserInfoById([id]); if (ret.size > 0) { return ret.get(id); } } return undefined; } } //# sourceMappingURL=ChatUserInfoManager.js.map