@rongcloud/imkit-store
Version:
RongCloud IM UIKit Store
714 lines (703 loc) • 17.5 kB
TypeScript
import * as mobx from 'mobx';
import { ObservableMap } from 'mobx';
import * as _rongcloud_engine from '@rongcloud/engine';
import { FileType, IAsyncRes, UploadMethod, ILogger, PluginContext, IConversationOption, ConversationType as ConversationType$1, RCConnectionStatus } from '@rongcloud/engine';
import { LogL, ConversationType, IAReceivedMessage, NotificationLevel, BaseMessage, ISendMessageOptions, IAsyncRes as IAsyncRes$1, GetHistoryMessageResult } from '@rongcloud/imlib-next';
/**
* 上传信息
* @category Interface
*/
interface IKitUploadInfo {
file?: File;
path?: string;
fileSize: number;
fileType: FileType;
duration?: number;
onProgress?: (progress: number) => void;
onComplete?: (res: IAsyncRes<IKitUploadCompleteData>) => void;
}
/**
* 上传请求数据接口
* 用于外部调用者发起请求
* @category Interface
*/
interface IKitUploadRequestData {
url: string;
headers: Record<string, string>;
body: Record<string, string>;
fileName: string;
uniqueName?: string;
contentType?: string;
uploadMethod?: UploadMethod;
}
/**
* @hidden
*/
interface IKitUploadResult {
name: string;
downloadUrl: string;
uniqueName: string;
size: number;
type: string;
/**
* 音频时长,单位秒
*/
duration?: number;
length?: number;
/**
* 图片缩略图
*/
thumbnail?: string;
/**
* 图片宽度
*/
width?: number;
/**
* 图片高度
*/
height?: number;
}
/**
* 图片信息
* @category Interface
*/
interface IKitImageInfo {
/**
* 图片缩略图的 base64 数据
*/
thumbnail?: string;
/**
* 图片宽度
*/
width?: number;
/**
* 图片高度
*/
height?: number;
}
/**
* 缩略图配置
* @category Interface
*/
interface IKitThumbnailConfig {
/**
* 最大高度
*/
maxHeight?: number;
/**
* 最大宽度
*/
maxWidth?: number;
/**
* 图片质量
*/
quality?: number;
/**
* 缩放比例
*/
scale?: number;
}
/**
* 上传完成返回数据信息
* @category Interface
*/
interface IKitUploadCompleteData {
downloadUrl: string;
}
/**
* 上传请求协议接口
* 由外部实现,用于执行上传请求
* @category class
*/
declare abstract class AKitUploadRequest {
uploadInfo: IKitUploadInfo;
protected _abortStatus: boolean;
protected _requestData?: IKitUploadRequestData;
/**
* @hidden
*/
constructor(info: IKitUploadInfo);
/**
* 设置请求数据
* @param data 上传请求数据
*/
setRequestData(data: IKitUploadRequestData): void;
/**
* 执行上传请求
* @returns 上传结果
*/
abstract execute(): Promise<IAsyncRes<IKitUploadResult>>;
/**
* 取消上传请求
*/
abort(): void;
isAbort(): boolean;
}
/**
* store 初始化参数
* @category Interface
*/
interface IRCStoreInitOpts {
/**
* 日志输出等级,默认值 `LogL.WARN(2)`
*/
logLevel?: LogL.DEBUG | LogL.INFO | LogL.WARN | LogL.ERROR;
/**
* 业务数据模块钩子,用于向 SDK 注入用户信息、群组信息等。
*/
hooks?: IKitServiceHooks;
}
/**
* 会话信息
* @category Interface
*/
interface IKitConversation {
/**
* 根据 targetId、conversationType、channelId 生成的唯一标识
*/
key: string;
targetId: string;
conversationType: ConversationType;
channelId: string;
name: string;
nickName: string;
portraitUri: string;
draft: string;
latestMessage: IAReceivedMessage | null;
isTop: boolean;
notificationLevel: NotificationLevel;
unreadCount: number;
updateTime: number;
memberCount: number;
}
/**
* @hidden
*/
type CacheType<T> = ObservableMap<string, {
data: T;
expires: number;
pending: boolean;
}>;
/**
* Profile 基础信息
* @category Interface
* @hidden
*/
interface IBaseProfile {
/**
* 唯一标识
*/
id: string;
/**
* 名称
*/
name: string;
/**
* 头像
*/
portraitUri?: string;
}
/**
* 用户信息
* @category Interface
*/
interface IUserProfile extends IBaseProfile {
/**
* 用户备注
*/
remark?: string;
}
/**
* 群组信息
* @category Interface
*/
interface IGroupProfile extends IBaseProfile {
/**
* 群组成员数量
*/
memberCount: number;
/**
* 群组备注
*/
remark?: string;
}
/**
* 系统信息
* @category Interface
*/
interface ISystemProfile extends IBaseProfile {
}
/**
* 群组成员信息
* @category Interface
*/
interface IGroupMemberProfile {
/**
* 群成员昵称
*/
groupNickname: string;
/**
* 用户信息
*/
user: {
/**
* 用户 ID
*/
id: string;
/**
* 用户昵称
*/
nickname: string;
/**
* 用户头像
*/
portraitUri?: string;
};
}
/**
* 数据源接口约束
* @hidden
*/
interface IDataSource {
getUserProfile(userIds: string[]): Promise<IUserProfile[]>;
getGroupProfile(groupIds: string[]): Promise<IGroupProfile[]>;
getSystemProfile(systemIds: string[]): Promise<ISystemProfile[]>;
getGroupMembers(groupId: string): Promise<IGroupMemberProfile[]>;
}
/**
* 用户信息回调钩子
* @category Interface
*/
interface IKitServiceHooks {
/**
* 根据 userId 批量获取用户信息
* @param userIds - 用户 ID 列表
*/
reqUserProfiles?(userIds: string[]): Promise<IUserProfile[]>;
/**
* 根据 groupId 批量获取群组信息
* @param groupIds - 群组 ID 列表
*/
reqGroupProfiles?(groupIds: string[]): Promise<IGroupProfile[]>;
/**
* 批量获取系统会话信息,当会话列表中存在 ConversationType.SYSTEM 类型时,会调用此方法获取系统会话信息
* @param targetIds - 系统会话 ID 列表
*/
reqSystemProfiles?(targetIds: string[]): Promise<ISystemProfile[]>;
/**
* 根据 groupId 获取群组成员列表信息
* @param groupId - 群组 ID
*/
reqGroupMembers?(groupId: string): Promise<IGroupMemberProfile[]>;
}
/**
* 消息信息
* @category Interface
*/
interface IKitMessage extends IKitMessageCache {
user: {
nickname: string;
portraitUri: string;
groupNickname?: string;
};
}
/**
* @category Interface
*/
interface IKitMessageCache extends IAReceivedMessage {
/**
* 媒体消息本地临时路径
*/
localTmpPath?: string;
}
/**
* 内存态会话更新信息
* @category Interface
*/
interface IConversationUpdate {
/**
* 置顶变更
*/
isTop?: boolean;
/**
* 免打扰状态变更
*/
notificationLevel?: NotificationLevel;
/**
* 未读数变更
*/
unreadCount?: number;
/**
* 会话最后一条消息变更
*/
latestMessage?: IAReceivedMessage | null;
/**
* 会话草稿变更
*/
draft?: string;
}
/**
* 媒体消息参数
* @category Interface
*/
interface IKitMediaMessageOptions {
/**
* 会话 key
*/
conversationKey: string;
/**
* 消息
*/
message: BaseMessage;
/**
* 发送消息选项
*/
sendOptions?: ISendMessageOptions;
/**
* 上传信息
*/
info: IKitUploadInfo;
/**
* 上传请求器
*/
request: AKitUploadRequest;
}
/**
* 消息列表状态管理
* @category class
*/
declare class RCKitMessageStore {
#private;
private readonly _logger;
private rootStore;
private _context;
/**
* @hidden
*/
constructor(_logger: ILogger, rootStore: RCKitStore, _context: PluginContext);
getMessages(conversationKey: string): IKitMessage[];
/**
* 获取历史消息
* @param conversationKey 会话ID
* @param endTime 结束时间戳, 精确到 ms
* @param lastMsgUId 上次查询的最后一条消息的 messageUId, 第一次不填
* @param limit 查询的数量, 默认 20
* @returns
*/
getHistoryMsgList(conversationKey: string, endTime: number, lastMsgUId?: string, limit?: number): Promise<IAsyncRes$1<GetHistoryMessageResult>>;
/**
* 从消息队列尾部插入一条消息,如果缓存中不存在消息所在会话,则不做任何操作
* @param message 消息对象
* @param isTrim 是否需要移除超出队列缓存上限部分,默认为 true
*/
insertMessage(message: IKitMessageCache, isTrim?: boolean, isLog?: boolean): IKitMessageCache;
/**
* 更新缓存中的消息,如果缓存中不存在则不做任何操作
* @param message 消息对象
* @returns
*/
updateCatchedMessage(message: IKitMessageCache): void;
/**
* 移除一条缓存中的消息
* @param message 消息对象
*/
removeCatchedMessage(message: Pick<IAReceivedMessage, 'conversationType' | 'targetId' | 'channelId' | 'messageUId' | 'messageId'>): void;
/**
* 撤回消息
* @param message 消息对象
*/
recallMessage(message: IAReceivedMessage): Promise<_rongcloud_engine.RCResult<IAReceivedMessage>>;
/**
* 发送消息
* @param message 消息实例
*/
sendMessage(conversationKey: string, message: BaseMessage, options: ISendMessageOptions): Promise<IAsyncRes$1<IAReceivedMessage>>;
/**
* 发送媒体消息
*/
sendMediaMessage(mediaOptions: IKitMediaMessageOptions): Promise<IAsyncRes$1<IAReceivedMessage>>;
/**
* 转发消息
* @param conversationKey 会话ID
* @param message 消息列表
*/
forwardMessage(conversationKey: string, message: IAReceivedMessage): Promise<{
code: number;
data?: IAReceivedMessage | undefined;
}>;
/**
* 取消指定消息的上传请求
* @param messageId 消息ID
*/
cancelSendMediaMessage(messageId: number): void;
/**
* 获取选中的消息
*/
getSelectedMessages(): IKitMessage[];
/**
* 设置选中的消息
* @param messages 消息列表
*/
setSelectedMessages(messages: IKitMessage[]): void;
/**
* 清理超出缓存上限的消息
* @param conversationKey 会话ID
* @hidden
*/
clearExcessMessage(conversationKey: string): void;
destroy(): void;
/**
* 清理内存数据
*/
clearMemoryData(): void;
}
/**
* 会话管理模块
* @category class
*/
declare class RCKitConversationStore {
#private;
private _kitStore;
/**
* 当前打开的会话信息
*/
openedConversation: IKitConversation | null;
/**
* 内存太缓存会话列表
*/
conversations: IKitConversation[];
/**
* @hidden
*/
constructor(_kitStore: RCKitStore, _logger: ILogger);
/**
* 获取更多会话(分页)
*
* 根据会话对象获取更多会话,若传入 null 则获取首页
* @param conversation 类型为 IKitConversation 的会话对象
* @param count 获取数量
* @returns 会话列表
*/
getConversations(conversation?: IKitConversation | null, count?: number): {
hasMore: boolean;
code: _rongcloud_engine.ErrorCode;
list: IKitConversation[];
isFirstScreen: boolean;
} | undefined;
/**
* 打开会话
*
* 传递 null 为清空当前选中会话
* @param conversation 会话对象
*/
openConversation(conversation: IKitConversation | null): void;
/**
* 创建会话缓存数据对象,若已存在则直接返回
* @param conversation 会话对象
*/
createCachedConversation(conversation: IConversationOption): IKitConversation;
/**
* 删除会话
* 可通过 trans2ConversationKey 生成会话 key
* @param conversationKey 会话 key
*/
removeConversation(conversationKey: string): void;
/**
* 更新会话缓存数据
* @param conversationKey 会话 key
* @param data 会话更新数据
*/
updateCacheConversation(conversationKey: string, data: IConversationUpdate): void;
/**
* 获取所有会话的未读消息总数
*/
getTotalUnreadCount(): number;
/**
* 清除会话未读消息
* 可通过 trans2ConversationKey 生成会话 key
* @param conversationKey 会话 key
*/
clearUnreadCount(conversationKey: string): Promise<void>;
/**
* 清理内存数据
*/
clearMemoryData(): void;
/**
* 销毁
*/
destroy(): void;
}
/**
* 群组成员缓存
* @category class
*/
declare class GroupMembersCache {
#private;
private logger;
private dataSource;
/**
* @hidden
*/
constructor(logger: ILogger, dataSource: IDataSource);
/**
* 初始化群组成员信息
* @param groupId 群组 ID
* @hidden
*/
initGroupMembers(groupId: string): Promise<void>;
/**
* 获取群组成员信息
* @param groupId 群组 ID
*/
getGroupMembers(groupId: string): IGroupMemberProfile[];
/**
* 获取群组成员信息
* @param groupId 群组 ID
* @param userId 用户 ID
*/
getGroupMember(groupId: string, userId: string): IGroupMemberProfile | undefined;
/**
* 获取群组成员信息 map
* @param groupId 群组 ID
* @hidden
*/
getGroupMemberMap(groupId: string): Map<string, IGroupMemberProfile>;
/**
* 清理内存数据
*/
clearMemoryData(): void;
}
/**
* 用户数据模块
* @category class
*/
declare class AppDataModule {
#private;
private logger;
groupMembersCache: GroupMembersCache;
/**
* @hidden
*/
constructor(logger: ILogger, hooks?: IKitServiceHooks);
/**
* 获取用户信息
* @param userId - 用户 ID
* @returns 用户信息
*/
getUserProfile(userId: string): IUserProfile | undefined;
/**
* 获取群组信息
* @param groupId - 群组 ID
* @returns 群组信息
*/
getGroupProfile(groupId: string): IGroupProfile | undefined;
/**
* 获取系统信息
* @param systemId - 系统 ID
* @returns 系统信息
*/
getSystemProfile(systemId: string): ISystemProfile | undefined;
/**
* 获取本地用户信息
* @param userId - 用户 ID
* @returns 用户信息
*/
getUserInfo(userId: string): IUserProfile | undefined;
/**
* @hidden
*/
getUsersCache(): CacheType<IUserProfile>;
/**
* @hidden
*/
getGroupsCache(): CacheType<IGroupProfile>;
/**
* @hidden
*/
getSystemsCache(): CacheType<ISystemProfile>;
/**
* 更新用户信息
* @param userId - 用户 ID
* @param profile - 用户信息
*/
updateUserProfile(userId: string, profile: IUserProfile): void;
/**
* 更新群组信息
* @param groupId - 群组 ID
* @param profile - 群组信息
*/
updateGroupProfile(groupId: string, profile: IGroupProfile): void;
/**
* 更新系统信息
* @param systemId - 系统 ID
* @param profile - 系统信息
*/
updateSystemProfile(systemId: string, profile: ISystemProfile): void;
/**
* 获取会话信息
* @param id - 会话 ID
* @param type - 类型
* @returns 会话信息
*/
getProfileSync(id: string, type: ConversationType$1): Promise<IUserProfile | IGroupProfile | ISystemProfile | undefined>;
/**
* 清理内存数据
*/
clearMemoryData(): void;
}
/**
* @category class
*/
declare class RCKitStore {
private _context;
private readonly _logger;
private _option;
/**
* 消息状态管理器
*/
messageStore: RCKitMessageStore;
/**
* 会话状态管理器
*/
conversationStore: RCKitConversationStore;
/**
* 用户数据模块
*/
appData: AppDataModule;
/**
* 当前用户 Id
*/
currentUserId: string;
/**
* 当前连接状态
*/
connectStatus: mobx.IObservableValue<RCConnectionStatus>;
/**
* @hidden
*/
constructor(_context: PluginContext, _logger: ILogger, _option: IRCStoreInitOpts);
private _onConnected;
private _onDisconnected;
private _onSuspend;
private _onConnecting;
/**
* 销毁
*/
destroy(): void;
/**
* 清理内存数据
*/
clearMemoryData(): void;
}
/**
* kit store 初始化
* @param option 初始化参数
* @category function
*/
declare const RCKitStoreInstaller: (option: IRCStoreInitOpts) => RCKitStore | null;
/**
* 根据会话 targetId、conversationType、channelId 生成的唯一标识
* @category function
*/
declare const trans2ConversationKey: (conversation: IConversationOption) => string;
export { AKitUploadRequest, AppDataModule, CacheType, GroupMembersCache, IBaseProfile, IConversationUpdate, IDataSource, IGroupMemberProfile, IGroupProfile, IKitConversation, IKitImageInfo, IKitMediaMessageOptions, IKitMessage, IKitMessageCache, IKitServiceHooks, IKitThumbnailConfig, IKitUploadCompleteData, IKitUploadInfo, IKitUploadRequestData, IKitUploadResult, IRCStoreInitOpts, ISystemProfile, IUserProfile, RCKitConversationStore, RCKitMessageStore, RCKitStore, RCKitStoreInstaller, trans2ConversationKey };