offlinePushInfoManager.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import TUIChatEngine, { IConversationModel, StoreName, TUIStore, TUITranslateService } from '@tencentcloud/chat-uikit-engine';
  2. import { transformTextWithKeysToEmojiNames } from '../emoji-config';
  3. import {
  4. IChatOfflinePushInfo,
  5. IOfflinePushInfoCreateParams,
  6. } from './interface';
  7. import { chatOfflinePushInfo, callOfflinePushInfo } from './info';
  8. import { DEFAULT_DESC, PUSH_SCENE } from './const';
  9. class OfflinePushInfoManager {
  10. private name = 'OfflinePushInfoManager';
  11. private static instance: OfflinePushInfoManager | null = null;
  12. private offlinePushInfo: any = {};
  13. private constructor() {
  14. this.offlinePushInfo = {
  15. [PUSH_SCENE.CHAT]: chatOfflinePushInfo,
  16. [PUSH_SCENE.CALL]: callOfflinePushInfo,
  17. };
  18. }
  19. public static getInstance(): OfflinePushInfoManager {
  20. if (!OfflinePushInfoManager.instance) {
  21. OfflinePushInfoManager.instance = new OfflinePushInfoManager();
  22. }
  23. return OfflinePushInfoManager.instance;
  24. }
  25. public getOfflinePushInfo(scene: PUSH_SCENE) {
  26. if (!Object.values(PUSH_SCENE).includes(scene)) {
  27. console.error(`${this.name} getOfflinePushInfo scene: ${scene} is invalid`);
  28. return null;
  29. }
  30. return this.offlinePushInfo[scene];
  31. }
  32. private genTitle(conversation: IConversationModel, userInfo: any) {
  33. let title = conversation?.getShowName();
  34. if (conversation.type === TUIChatEngine.TYPES.CONV_C2C) {
  35. title = userInfo?.nick || userInfo?.userID;
  36. }
  37. return title;
  38. }
  39. private genDesc(messageType: string, payload: any) {
  40. let desc = '';
  41. if (messageType === TUIChatEngine.TYPES.MSG_TEXT) {
  42. desc = transformTextWithKeysToEmojiNames(payload.text);
  43. }
  44. if (messageType === TUIChatEngine.TYPES.MSG_CUSTOM) {
  45. desc = payload.description;
  46. }
  47. return desc || TUITranslateService.t(`TUIChat.${DEFAULT_DESC[messageType]}`);
  48. }
  49. public create(options: IOfflinePushInfoCreateParams): IChatOfflinePushInfo {
  50. const { conversation, messageType = '', payload = {} } = options || {};
  51. const userInfo = TUIStore.getData(StoreName.USER, 'userProfile');
  52. const entity = {
  53. sender: conversation.type === TUIChatEngine.TYPES.CONV_GROUP ? conversation.groupProfile?.groupID : userInfo?.userID,
  54. nickName: userInfo?.nick,
  55. chatType: conversation.type === TUIChatEngine.TYPES.CONV_GROUP ? 2 : 1,
  56. version: 1,
  57. action: 1,
  58. };
  59. return {
  60. title: this.genTitle(conversation, userInfo),
  61. description: this.genDesc(messageType, payload),
  62. extension: JSON.stringify({ entity }),
  63. ...this.offlinePushInfo[PUSH_SCENE.CHAT],
  64. };
  65. }
  66. }
  67. export default OfflinePushInfoManager;