@tencentcloud/lite-chat 1.6.5 → 1.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- function i(i,e,n,t){return new(n||(n=Promise))(function(s,a){function o(i){try{g(t.next(i))}catch(i){a(i)}}function r(i){try{g(t.throw(i))}catch(i){a(i)}}function g(i){var e;i.done?s(i.value):(e=i.value,e instanceof n?e:new n(function(i){i(e)})).then(o,r)}g((t=t.apply(i,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const e=1,n=2,t=3,s=4,a=5,o="TIMCustomElem",r="C2C",g="GROUP",l="invite",c="accept",v="cancel",u="reject",h="modifyInvitation",d="signaling",_=8010,I="signaling-timeout";function S(i){return i.filter(i=>{if(i.type===o){const{cloudCustomData:e="",payload:{data:n=""}={}}=i,t=e.match(/"type":"tsignaling"/),s=n.match(/inviteID/),a=n.match(/actionType/);return t||s&&a}return!1})}function p(i){const{data:e}=i.payload;try{return JSON.parse(e)}catch(i){return console.error(i),null}}function f(i,e){return i.toString(16).padStart(e,"0")}function D(i){if(i<0||i>53)throw new Error("Number of digits must be between 0 and 53");if(i<=30)return Math.floor(Math.random()*(1<<i));const e=i-30;return Math.floor(Math.random()*(1<<e))*(1<<30)+Math.floor(Math.random()*(1<<30))}var m=new class{constructor(){this._onlineSignalingMap=new Map,this._isSignalingListening=!1}init(i){this._core=i,i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}getOnlineSignalingMap(){return this._onlineSignalingMap}hasSignaling(i){return this._onlineSignalingMap.has(i)}getSignaling(i){return this._onlineSignalingMap.get(i)}saveSignaling(i,e){return this._onlineSignalingMap.set(i,e)}removeSignaling(i){return this._onlineSignalingMap.delete(i)}isInvitationValid(i){const e=`${d}.isInvitationValid`,{inviteID:n,groupID:t,inviter:s}=i,a=s===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(n))return!1;const{inviteeList:o}=this._onlineSignalingMap.get(n);if(console.log(`${e} inviteID:${n} inviteeList:${o} groupID:${t}`),t){if(a)return o.length>0;const i=this._core.common.getCurrentUserID();return o.includes(i)}return!0}updateSignaling(i){const e=`${d}.updateSignaling`,{inviteID:n,inviter:t,inviteeList:s,groupID:a}=i;if(console.log(`${e} inviteID:${n} inviter:${t} groupID:${a}`),a&&this.hasSignaling(n)){const i=s[0],{inviteeList:t}=this._onlineSignalingMap.get(n);t.includes(i)&&(t.splice(t.indexOf(i),1),console.log(`${e} remove ${i}. localInviteeList.length:${t.length}`)),0===t.length&&this.removeSignaling(n)}else this.removeSignaling(n)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}};var E=new class{init(i){this._core=i}createInviteSignaling(i){const e=this._generateInviteID(),n=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:t,inviteeList:s}=n,a=t||s[0];return{signaling:this._createSignaling(n,a),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){const e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){const e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,s=n||t[0];return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){const e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){const{isInviter:e=!1}=i,n=this._createTimeoutSignalingData(i),{groupID:t,inviteeList:s,inviter:a}=n,o=t||(e?s[0]:a);return{signaling:this._createSignaling(n,o),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(n)}}_createSignalingExtensionOptions(i){var e,n;const{data:t="",onlineUserOnly:s,inviteID:a="",offlinePushInfo:o,actionType:r}=i,g=(null===(n=null===(e=m.getSignaling(a))||void 0===e?void 0:e.signaling)||void 0===n?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:s||g,offlinePushInfo:o,messageControlInfo:this._createMessageControlInfo(t,r)}}_createMessageControlInfo(i,e){const n=e===a&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),t=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:n||t,excludedFromLastMessage:n||t}}_createInviteSignalingData(i){const n=`${d}._createInviteSignalingData`,{userID:t,timeout:s=0,groupID:a="",inviteeList:o=[]}=i,r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:e,inviter:r,inviteeList:a?o:[t],timeout:s});return console.log(`${n} signalingData:`,g),g}_createAcceptSignalingData(i){const e=`${d}._createAcceptSignalingData`,{inviteID:n}=i,s=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=m.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:t,groupID:o,inviter:a,inviteeList:[s]});return console.log(`${e} signalingData:`,r),r}_createCancelSignalingData(i){const e=`${d}._createCancelSignalingData`,{inviteID:t}=i,s=this._core.common.getCurrentUserID(),{inviteeList:a,groupID:o}=m.getSignaling(t),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:n,groupID:o,inviter:s,inviteeList:a});return console.log(`${e} signalingData:`,r),r}_createRejectSignalingData(i){const e=`${d}._createRejectSignalingData`,{inviteID:n}=i,t=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=m.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:s,groupID:o,inviter:a,inviteeList:[t]});return console.log(`${e} signalingData:`,r),r}_createTimeoutSignalingData(i){const e=`${d}._createTimeoutSignalingData`,{isInviter:n=!1,inviteID:t}=i,{inviteeList:s,inviter:o}=m.getSignaling(t),r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:a,inviter:o,inviteeList:n?s:[r]});return console.log(`${e} signalingData:`,g),g}_createSignaling(i,e){var n,t,s;const{groupID:a=""}=i,o={to:e,conversationType:a?g:r,priority:"High",payload:{data:JSON.stringify(i)}};return null===(s=null===(t=null===(n=this._core)||void 0===n?void 0:n.message)||void 0===t?void 0:t.messageFactory)||void 0===s?void 0:s.createCustomMessage(o)}_generateInviteID(){return[f(D(32),8),f(D(16),4),f(16384|D(12),4),f(32768|D(14),4),f(D(48),12)].join("-")}_generateBaseSignalData(i){const{data:e="",inviteID:n="",groupID:t=""}=i;return{businessID:1,timeout:0,data:e,inviteID:n,groupID:t}}};var O=new class{constructor(){this._isProcessingSignaling=!1}init(i){this._core=i,i.helper.registerApi({apiName:"invite",context:this}),i.helper.registerApi({apiName:"accept",context:this}),i.helper.registerApi({apiName:"cancel",context:this}),i.helper.registerApi({apiName:"reject",context:this}),i.helper.registerApi({apiName:"modifyInvitation",context:this}),i.helper.registerApi({apiName:"getSignalingInfo",context:this}),i.helper.registerApi({apiName:"addSignalingListener",context:this}),i.helper.registerApi({apiName:"removeSignalingListener",context:this}),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}invite(e){return i(this,void 0,void 0,function*(){var i;try{this._validateBeforeInvite(e);const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createInviteSignaling(e),a=yield this._sendSignaling(n,s);if(0===(null==a?void 0:a.code)){const{inviteID:e,timeout:s}=t;return m.saveSignaling(e,Object.assign(Object.assign({},t),{signaling:n})),s>0&&(null===(i=this._core)||void 0===i||i.helper.taskScheduler.addOnceTask({id:`${I}-${e}`,intervalMs:1e3*(s+5),callback:this.handleInvitationExpiryTimer.bind(this,e)})),Object.assign(Object.assign({},a),{inviteID:e})}return a}catch(i){throw i}})}accept(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeAccept(i),this._isProcessingSignaling=!0;const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createAcceptSignaling(e),a=yield this._sendSignaling(n,s);return 0===(null==a?void 0:a.code)?(m.updateSignaling(t),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}cancel(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeCancel(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createCancelSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(m.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}reject(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeReject(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createRejectSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(m.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}modifyInvitation(e){return i(this,void 0,void 0,function*(){var i,n;const{inviteID:t,data:s}=e;let a="";try{this._validateBeforeModifyInvitation(t);const e=m.getSignaling(t),{signaling:o}=e,r=function(i,e){var n={};for(var t in i)Object.prototype.hasOwnProperty.call(i,t)&&e.indexOf(t)<0&&(n[t]=i[t]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(t=Object.getOwnPropertySymbols(i);s<t.length;s++)e.indexOf(t[s])<0&&Object.prototype.propertyIsEnumerable.call(i,t[s])&&(n[t[s]]=i[t[s]])}return n}(e,["signaling"]);a=o.payload.data,r.data=s,o.payload.data=JSON.stringify(r);const g=yield null===(n=null===(i=this._core)||void 0===i?void 0:i.message.messageAction)||void 0===n?void 0:n.modifyMessage(o);return m.hasSignaling(t)&&m.saveSignaling(t,Object.assign(Object.assign({},r),{signaling:o})),g}catch(i){if(a){const{signaling:i}=m.getSignaling(t);i.payload.data=a}throw i}})}getSignalingInfo(i){const{ssoLog:n,utils:{safeStringify:t}}=this._core;if(0===S([i]).length)return;const s=p(i),a={businessID:s.businessID||1,inviteID:s.inviteID,groupID:s.groupID||"",inviter:s.inviter||"",inviteeList:s.inviteeList||[],data:s.data||"",actionType:s.actionType||e,timeout:s.timeout||0};return n.debug(`${d} getSignalingInfo ${t(a)}`),a}addSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!0),null===(s=this._core)||void 0===s||s.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!1),null===(s=this._core)||void 0===s||s.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){const e=m.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();if(!e.has(i))return;const t=e.get(i).inviter===n;this._sendTimeoutNotice({inviteID:i,isInviter:t})}_sendSignaling(e,n){return i(this,void 0,void 0,function*(){var i,t,s;return null===(s=null===(t=null===(i=this._core)||void 0===i?void 0:i.message)||void 0===t?void 0:t.messageSender)||void 0===s?void 0:s.sendMessage(e,n)})}_sendTimeoutNotice(e){return i(this,void 0,void 0,function*(){var i,n,t;this._core.ssoLog.debug("_sendTimeoutNotice",`${d}._sendTimeoutNotice params:${JSON.stringify(e)}`);const{isInviter:s,inviteID:a}=e,{signaling:o,signalingData:r,signalingExtensionOptions:g}=E.createTimeoutSignaling(e),l=yield this._sendSignaling(o,g);if(0===(null==l?void 0:l.code)){const{data:e,groupID:g,inviteeList:l,inviter:c}=r;null===(i=this._core)||void 0===i||i.notificationCenter.emitOuterEvent(null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_TIMEOUT,{name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.INVITATION_TIMEOUT,data:{data:e,groupID:g,inviteID:a,inviteeList:l,inviter:c,isSelfTimeout:!0,message:o}}),s?m.removeSignaling(a):m.updateSignaling(r)}})}_validateInviteId(i,e){if(!m.hasSignaling(e))throw new this._core.helper.ChatError({functionName:i,code:_})}_validateProcessStatus(i){if(this._isProcessingSignaling)throw new this._core.helper.ChatError({functionName:i,message:"processing other signaling operations"})}_validateBeforeInvite(i){const e=l,{userID:n}=i,t=this._core.common.getCurrentUserID();if(n===t)throw new this._core.helper.ChatError({functionName:e,message:`cannot invite yourself, currentUserId:${t}, inviteeId:${n}`})}_validateBeforeAccept(i){const e=c;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=m.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeCancel(i){const e=v;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviter:t}=m.getSignaling(i);if(t!==n){const i=`unmatched inviter:${t} and my userID:${n}`;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(i){const e=u;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=m.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeModifyInvitation(i){const e=h;this._validateInviteId(e,i)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}};var b=new class{constructor(){this._actionProcessor=new Map([[e,this._onNewInvitationReceived.bind(this)],[s,this._onInviteeRejected.bind(this)],[t,this._onInviteeAccepted.bind(this)],[n,this._onInvitationCancelled.bind(this)],[a,this._onInvitationTimeout.bind(this)]])}init(i){this._core=i,i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this)}handleActionSignaling(i){i.forEach(i=>{const e=p(i);if(e){const n=this._actionProcessor.get(e.actionType);null==n||n(e,i)}})}_handleMessageReceived(i){if(!m.getSignalingListenStatus())return;const e=S(i.data);0!==e.length&&this.handleActionSignaling(e)}_handleMessageModified(i){if(!m.getSignalingListenStatus())return;const e=S(i.data);e.length>0&&e.forEach(i=>{const e=p(i);e&&this._onInvitationModified(e,i)})}_onNewInvitationReceived(i,e){var n,t;const s=`${d}._onNewInvitationReceived`,{inviteID:a,inviteeList:o,groupID:r}=i,g=this._core.common.getCurrentUserID();this._core.ssoLog.debug("_onNewInvitationReceived",`${s} signalingData:${JSON.stringify(i)}}`);if(r&&!o.includes(g))return;let{timeout:l}=i;const c=Date.now()/1e3-e.time;l>0&&c>0&&l>c&&(l-=c);const v=m.getSignaling(a);v!==i&&(v||m.saveSignaling(a,Object.assign(Object.assign({},i),{signaling:e})),l>0&&(null===(n=this._core)||void 0===n||n.helper.taskScheduler.addOnceTask({id:`${I}-${a}`,intervalMs:1e3*l,callback:O.handleInvitationExpiryTimer.bind(O,a)})),this._emitEvent({name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:o})}))}_onInviteeRejected(i){var e;const n=`${d}._onInviteeRejected`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=m.hasSignaling(t);this._core.ssoLog.debug("_onInviteeRejected",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInviteeAccepted(i){var e;const n=`${d}._onInviteeAccepted`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=m.hasSignaling(t);this._core.ssoLog.debug("_onInviteeAccepted",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInvitationCancelled(i){var e;const n=`${d}._onInvitationCancelled`,{inviteID:t,inviter:s,groupID:a}=i,o=m.hasSignaling(t);this._core.ssoLog.debug("_onInvitationCancelled",`${n} inviteID:${t} hasInviteID:${o} inviter:${s} groupID:${a}`),o&&(m.removeSignaling(t),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){var e;const n=`${d}._onInvitationTimeout`,{inviteID:t,inviteeList:s}=i,a=m.hasSignaling(t);this._core.ssoLog.debug("_onInvitationTimeout",`${n} inviteID:${t} hasInviteID:${a} data:${i.data}`),a&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:s,isSelfTimeout:!1})}))}_onInvitationModified(i,e){var n;const t=`${d}._onInvitationModified`,{inviteID:s,data:a}=i,o=m.hasSignaling(s);this._core.ssoLog.debug("_onInvitationModified",`${t} inviteID:${s} data:${a}`),o&&(m.saveSignaling(s,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:s,data:a}}))}_emitEvent(i){var e;null===(e=this._core)||void 0===e||e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){const{inviteID:e,inviter:n,groupID:t,data:s}=i;return{inviteID:e,inviter:n,groupID:t,data:s||""}}_dispose(){var i,e,n;null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null===(n=this._core)||void 0===n||n.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}};var y=new class{constructor(){this._offlineSignalingMap=new Map}init(i){this._core=i;const{notificationCenter:e,helper:n,constants:{InnerEvent:t,WORKFLOW_STEP:s,WORKFLOW_NAME:a}}=i;e.subscribeInnerEvent(t.DESTROY,this._dispose,this),e.subscribeInnerEvent(t.LOGOUT,this._reset,this),n.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,s.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){const{result:{unreadMessageMap:e}={}}=i||{};if(!(0!==(null==e?void 0:e.size)&&m.getSignalingListenStatus()))return;const n=S([...e.values()]);if(0!==n.length&&(n.forEach(i=>{this._handleC2CActionType(i)}),this._offlineSignalingMap.size>0)){const i=this._sortOfflineSignalingByTime();b.handleActionSignaling(i)}}_handleC2CActionType(i){const n=p(i);if(!n)return;const{actionType:t}=n;t===e?this._saveValidOfflineInvite(n,i):this._removeOfflineInvite(n)}_saveValidOfflineInvite(i,e){const{inviteID:n,inviteeList:t=[],timeout:s=0}=i,a=this._core.common.getCurrentUserID();if(!t.includes(a))return;const o=Date.now()/1e3-e.time;s>0&&o>s&&0!==s||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]}))}_removeOfflineInvite(i){const{inviteID:e=""}=i;this._offlineSignalingMap.has(e)&&this._offlineSignalingMap.delete(e)}_sortOfflineSignalingByTime(){let i=[];return this._offlineSignalingMap.forEach(e=>{i=[...i,...e.signalingList]}),i.sort((i,e)=>i.time-e.time)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};const C={invite:{userID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0},timeout:{required:!1,rules:["number"],allowEmpty:!1},onlineUserOnly:{required:!1,rules:["boolean"],allowEmpty:!1},offlinePushInfo:{required:!1,rules:["object"],allowEmpty:!1}},cancel:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},accept:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},reject:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},modifyInvitation:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}}},T={invite:!0,cancel:!0,accept:!0,reject:!0,modifyInvitation:!0};var M=new class{constructor(){this.name="Signaling"}install(i){O.init(i),b.init(i),E.init(i),m.init(i),y.init(i),i.helper.registerValidateConfig({auth:T,params:C})}};export{M as default};
1
+ function i(i,e,n,t){return new(n||(n=Promise))((function(s,a){function o(i){try{g(t.next(i))}catch(i){a(i)}}function r(i){try{g(t.throw(i))}catch(i){a(i)}}function g(i){var e;i.done?s(i.value):(e=i.value,e instanceof n?e:new n((function(i){i(e)}))).then(o,r)}g((t=t.apply(i,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const e=1,n=2,t=3,s=4,a=5,o="TIMCustomElem",r="C2C",g="GROUP",l="invite",c="accept",v="cancel",u="reject",h="modifyInvitation",d="signaling",_=8010,I="signaling-timeout";function S(i){return i.filter((i=>{if(i.type===o){const{cloudCustomData:e="",payload:{data:n=""}={}}=i,t=e.match(/"type":"tsignaling"/),s=n.match(/inviteID/),a=n.match(/actionType/);return t||s&&a}return!1}))}function p(i){const{data:e}=i.payload;try{return JSON.parse(e)}catch(i){return console.error(i),null}}function f(i,e){return i.toString(16).padStart(e,"0")}function D(i){if(i<0||i>53)throw new Error("Number of digits must be between 0 and 53");if(i<=30)return Math.floor(Math.random()*(1<<i));const e=i-30;return Math.floor(Math.random()*(1<<e))*(1<<30)+Math.floor(Math.random()*(1<<30))}var m=new class{constructor(){this._onlineSignalingMap=new Map,this._isSignalingListening=!1}init(i){this._core=i,i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}getOnlineSignalingMap(){return this._onlineSignalingMap}hasSignaling(i){return this._onlineSignalingMap.has(i)}getSignaling(i){return this._onlineSignalingMap.get(i)}saveSignaling(i,e){return this._onlineSignalingMap.set(i,e)}removeSignaling(i){return this._onlineSignalingMap.delete(i)}isInvitationValid(i){const e=`${d}.isInvitationValid`,{inviteID:n,groupID:t,inviter:s}=i,a=s===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(n))return!1;const{inviteeList:o}=this._onlineSignalingMap.get(n);if(console.log(`${e} inviteID:${n} inviteeList:${o} groupID:${t}`),t){if(a)return o.length>0;const i=this._core.common.getCurrentUserID();return o.includes(i)}return!0}updateSignaling(i){const e=`${d}.updateSignaling`,{inviteID:n,inviter:t,inviteeList:s,groupID:a}=i;if(console.log(`${e} inviteID:${n} inviter:${t} groupID:${a}`),a&&this.hasSignaling(n)){const i=s[0],{inviteeList:t}=this._onlineSignalingMap.get(n);t.includes(i)&&(t.splice(t.indexOf(i),1),console.log(`${e} remove ${i}. localInviteeList.length:${t.length}`)),0===t.length&&this.removeSignaling(n)}else this.removeSignaling(n)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}};var E=new class{init(i){this._core=i}createInviteSignaling(i){const e=this._generateInviteID(),n=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:t,inviteeList:s}=n,a=t||s[0];return{signaling:this._createSignaling(n,a),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){const e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){const e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,s=n||t[0];return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){const e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){const{isInviter:e=!1}=i,n=this._createTimeoutSignalingData(i),{groupID:t,inviteeList:s,inviter:a}=n,o=t||(e?s[0]:a);return{signaling:this._createSignaling(n,o),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(n)}}_createSignalingExtensionOptions(i){var e,n;const{data:t="",onlineUserOnly:s,inviteID:a="",offlinePushInfo:o,actionType:r}=i,g=(null===(n=null===(e=m.getSignaling(a))||void 0===e?void 0:e.signaling)||void 0===n?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:s||g,offlinePushInfo:o,messageControlInfo:this._createMessageControlInfo(t,r)}}_createMessageControlInfo(i,e){const n=e===a&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),t=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:n||t,excludedFromLastMessage:n||t}}_createInviteSignalingData(i){const n=`${d}._createInviteSignalingData`,{userID:t,timeout:s=0,groupID:a="",inviteeList:o=[]}=i,r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:e,inviter:r,inviteeList:a?o:[t],timeout:s});return console.log(`${n} signalingData:`,g),g}_createAcceptSignalingData(i){const e=`${d}._createAcceptSignalingData`,{inviteID:n}=i,s=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=m.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:t,groupID:o,inviter:a,inviteeList:[s]});return console.log(`${e} signalingData:`,r),r}_createCancelSignalingData(i){const e=`${d}._createCancelSignalingData`,{inviteID:t}=i,s=this._core.common.getCurrentUserID(),{inviteeList:a,groupID:o}=m.getSignaling(t),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:n,groupID:o,inviter:s,inviteeList:a});return console.log(`${e} signalingData:`,r),r}_createRejectSignalingData(i){const e=`${d}._createRejectSignalingData`,{inviteID:n}=i,t=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=m.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:s,groupID:o,inviter:a,inviteeList:[t]});return console.log(`${e} signalingData:`,r),r}_createTimeoutSignalingData(i){const e=`${d}._createTimeoutSignalingData`,{isInviter:n=!1,inviteID:t}=i,{inviteeList:s,inviter:o}=m.getSignaling(t),r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:a,inviter:o,inviteeList:n?s:[r]});return console.log(`${e} signalingData:`,g),g}_createSignaling(i,e){var n,t,s;const{groupID:a=""}=i,o={to:e,conversationType:a?g:r,priority:"High",payload:{data:JSON.stringify(i)}};return null===(s=null===(t=null===(n=this._core)||void 0===n?void 0:n.message)||void 0===t?void 0:t.messageFactory)||void 0===s?void 0:s.createCustomMessage(o)}_generateInviteID(){return[f(D(32),8),f(D(16),4),f(16384|D(12),4),f(32768|D(14),4),f(D(48),12)].join("-")}_generateBaseSignalData(i){const{data:e="",inviteID:n="",groupID:t=""}=i;return{businessID:1,timeout:0,data:e,inviteID:n,groupID:t}}};var O=new class{constructor(){this._isProcessingSignaling=!1}init(i){this._core=i,i.helper.registerApi({apiName:"invite",context:this}),i.helper.registerApi({apiName:"accept",context:this}),i.helper.registerApi({apiName:"cancel",context:this}),i.helper.registerApi({apiName:"reject",context:this}),i.helper.registerApi({apiName:"modifyInvitation",context:this}),i.helper.registerApi({apiName:"getSignalingInfo",context:this}),i.helper.registerApi({apiName:"addSignalingListener",context:this}),i.helper.registerApi({apiName:"removeSignalingListener",context:this}),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}invite(e){return i(this,void 0,void 0,(function*(){var i;try{this._validateBeforeInvite(e);const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createInviteSignaling(e),a=yield this._sendSignaling(n,s);if(0===(null==a?void 0:a.code)){const{inviteID:e,timeout:s}=t;return m.saveSignaling(e,Object.assign(Object.assign({},t),{signaling:n})),s>0&&(null===(i=this._core)||void 0===i||i.helper.taskScheduler.addOnceTask({id:`${I}-${e}`,intervalMs:1e3*(s+5),callback:this.handleInvitationExpiryTimer.bind(this,e)})),Object.assign(Object.assign({},a),{inviteID:e})}return a}catch(i){throw i}}))}accept(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeAccept(i),this._isProcessingSignaling=!0;const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createAcceptSignaling(e),a=yield this._sendSignaling(n,s);return 0===(null==a?void 0:a.code)?(m.updateSignaling(t),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}cancel(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeCancel(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createCancelSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(m.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}reject(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeReject(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createRejectSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(m.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}modifyInvitation(e){return i(this,void 0,void 0,(function*(){var i,n;const{inviteID:t,data:s}=e;let a="";try{this._validateBeforeModifyInvitation(t);const e=m.getSignaling(t),{signaling:o}=e,r=function(i,e){var n={};for(var t in i)Object.prototype.hasOwnProperty.call(i,t)&&e.indexOf(t)<0&&(n[t]=i[t]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(t=Object.getOwnPropertySymbols(i);s<t.length;s++)e.indexOf(t[s])<0&&Object.prototype.propertyIsEnumerable.call(i,t[s])&&(n[t[s]]=i[t[s]])}return n}(e,["signaling"]);a=o.payload.data,r.data=s,o.payload.data=JSON.stringify(r);const g=yield null===(n=null===(i=this._core)||void 0===i?void 0:i.message.messageAction)||void 0===n?void 0:n.modifyMessage(o);return m.hasSignaling(t)&&m.saveSignaling(t,Object.assign(Object.assign({},r),{signaling:o})),g}catch(i){if(a){const{signaling:i}=m.getSignaling(t);i.payload.data=a}throw i}}))}getSignalingInfo(i){const{ssoLog:n,utils:{safeStringify:t}}=this._core;if(0===S([i]).length)return;const s=p(i),a={businessID:s.businessID||1,inviteID:s.inviteID,groupID:s.groupID||"",inviter:s.inviter||"",inviteeList:s.inviteeList||[],data:s.data||"",actionType:s.actionType||e,timeout:s.timeout||0};return n.debug(`${d} getSignalingInfo ${t(a)}`),a}addSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!0),null===(s=this._core)||void 0===s||s.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!1),null===(s=this._core)||void 0===s||s.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){const e=m.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();if(!e.has(i))return;const t=e.get(i).inviter===n;this._sendTimeoutNotice({inviteID:i,isInviter:t})}_sendSignaling(e,n){return i(this,void 0,void 0,(function*(){var i,t,s;return null===(s=null===(t=null===(i=this._core)||void 0===i?void 0:i.message)||void 0===t?void 0:t.messageSender)||void 0===s?void 0:s.sendMessage(e,n)}))}_sendTimeoutNotice(e){return i(this,void 0,void 0,(function*(){var i,n,t;this._core.ssoLog.debug("_sendTimeoutNotice",`${d}._sendTimeoutNotice params:${JSON.stringify(e)}`);const{isInviter:s,inviteID:a}=e,{signaling:o,signalingData:r,signalingExtensionOptions:g}=E.createTimeoutSignaling(e),l=yield this._sendSignaling(o,g);if(0===(null==l?void 0:l.code)){const{data:e,groupID:g,inviteeList:l,inviter:c}=r;null===(i=this._core)||void 0===i||i.notificationCenter.emitOuterEvent(null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_TIMEOUT,{name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.INVITATION_TIMEOUT,data:{data:e,groupID:g,inviteID:a,inviteeList:l,inviter:c,isSelfTimeout:!0,message:o}}),s?m.removeSignaling(a):m.updateSignaling(r)}}))}_validateInviteId(i,e){if(!m.hasSignaling(e))throw new this._core.helper.ChatError({functionName:i,code:_})}_validateProcessStatus(i){if(this._isProcessingSignaling)throw new this._core.helper.ChatError({functionName:i,message:"processing other signaling operations"})}_validateBeforeInvite(i){const e=l,{userID:n}=i,t=this._core.common.getCurrentUserID();if(n===t)throw new this._core.helper.ChatError({functionName:e,message:`cannot invite yourself, currentUserId:${t}, inviteeId:${n}`})}_validateBeforeAccept(i){const e=c;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=m.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeCancel(i){const e=v;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviter:t}=m.getSignaling(i);if(t!==n){const i=`unmatched inviter:${t} and my userID:${n}`;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(i){const e=u;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=m.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeModifyInvitation(i){const e=h;this._validateInviteId(e,i)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}};var b=new class{constructor(){this._actionProcessor=new Map([[e,this._onNewInvitationReceived.bind(this)],[s,this._onInviteeRejected.bind(this)],[t,this._onInviteeAccepted.bind(this)],[n,this._onInvitationCancelled.bind(this)],[a,this._onInvitationTimeout.bind(this)]])}init(i){this._core=i,i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this)}handleActionSignaling(i){i.forEach((i=>{const e=p(i);if(e){const n=this._actionProcessor.get(e.actionType);null==n||n(e,i)}}))}_handleMessageReceived(i){if(!m.getSignalingListenStatus())return;const e=S(i.data);0!==e.length&&this.handleActionSignaling(e)}_handleMessageModified(i){if(!m.getSignalingListenStatus())return;const e=S(i.data);e.length>0&&e.forEach((i=>{const e=p(i);e&&this._onInvitationModified(e,i)}))}_onNewInvitationReceived(i,e){var n,t;const s=`${d}._onNewInvitationReceived`,{inviteID:a,inviteeList:o,groupID:r}=i,g=this._core.common.getCurrentUserID();this._core.ssoLog.debug("_onNewInvitationReceived",`${s} signalingData:${JSON.stringify(i)}}`);if(r&&!o.includes(g))return;let{timeout:l}=i;const c=Date.now()/1e3-e.time;l>0&&c>0&&l>c&&(l-=c);const v=m.getSignaling(a);v!==i&&(v||m.saveSignaling(a,Object.assign(Object.assign({},i),{signaling:e})),l>0&&(null===(n=this._core)||void 0===n||n.helper.taskScheduler.addOnceTask({id:`${I}-${a}`,intervalMs:1e3*l,callback:O.handleInvitationExpiryTimer.bind(O,a)})),this._emitEvent({name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:o})}))}_onInviteeRejected(i){var e;const n=`${d}._onInviteeRejected`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=m.hasSignaling(t);this._core.ssoLog.debug("_onInviteeRejected",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInviteeAccepted(i){var e;const n=`${d}._onInviteeAccepted`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=m.hasSignaling(t);this._core.ssoLog.debug("_onInviteeAccepted",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInvitationCancelled(i){var e;const n=`${d}._onInvitationCancelled`,{inviteID:t,inviter:s,groupID:a}=i,o=m.hasSignaling(t);this._core.ssoLog.debug("_onInvitationCancelled",`${n} inviteID:${t} hasInviteID:${o} inviter:${s} groupID:${a}`),o&&(m.removeSignaling(t),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){var e;const n=`${d}._onInvitationTimeout`,{inviteID:t,inviteeList:s}=i,a=m.hasSignaling(t);this._core.ssoLog.debug("_onInvitationTimeout",`${n} inviteID:${t} hasInviteID:${a} data:${i.data}`),a&&(m.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:s,isSelfTimeout:!1})}))}_onInvitationModified(i,e){var n;const t=`${d}._onInvitationModified`,{inviteID:s,data:a}=i,o=m.hasSignaling(s);this._core.ssoLog.debug("_onInvitationModified",`${t} inviteID:${s} data:${a}`),o&&(m.saveSignaling(s,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:s,data:a}}))}_emitEvent(i){var e;null===(e=this._core)||void 0===e||e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){const{inviteID:e,inviter:n,groupID:t,data:s}=i;return{inviteID:e,inviter:n,groupID:t,data:s||""}}_dispose(){var i,e,n;null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null===(n=this._core)||void 0===n||n.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}};var y=new class{constructor(){this._offlineSignalingMap=new Map}init(i){this._core=i;const{notificationCenter:e,helper:n,constants:{InnerEvent:t,WORKFLOW_STEP:s,WORKFLOW_NAME:a}}=i;e.subscribeInnerEvent(t.DESTROY,this._dispose,this),e.subscribeInnerEvent(t.LOGOUT,this._reset,this),n.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,s.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){const{result:{unreadMessageMap:e}={}}=i||{};if(!(0!==(null==e?void 0:e.size)&&m.getSignalingListenStatus()))return;const n=S([...e.values()]);if(0!==n.length&&(n.forEach((i=>{this._handleC2CActionType(i)})),this._offlineSignalingMap.size>0)){const i=this._sortOfflineSignalingByTime();b.handleActionSignaling(i)}}_handleC2CActionType(i){const n=p(i);if(!n)return;const{actionType:t}=n;t===e?this._saveValidOfflineInvite(n,i):this._removeOfflineInvite(n)}_saveValidOfflineInvite(i,e){const{inviteID:n,inviteeList:t=[],timeout:s=0}=i,a=this._core.common.getCurrentUserID();if(!t.includes(a))return;const o=Date.now()/1e3-e.time;s>0&&o>s&&0!==s||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]}))}_removeOfflineInvite(i){const{inviteID:e=""}=i;this._offlineSignalingMap.has(e)&&this._offlineSignalingMap.delete(e)}_sortOfflineSignalingByTime(){let i=[];return this._offlineSignalingMap.forEach((e=>{i=[...i,...e.signalingList]})),i.sort(((i,e)=>i.time-e.time))}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};const C={invite:{userID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0},timeout:{required:!1,rules:["number"],allowEmpty:!1},onlineUserOnly:{required:!1,rules:["boolean"],allowEmpty:!1},offlinePushInfo:{required:!1,rules:["object"],allowEmpty:!1}},cancel:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},accept:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},reject:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},modifyInvitation:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}}},T={invite:!0,cancel:!0,accept:!0,reject:!0,modifyInvitation:!0};var M=new class{constructor(){this.name="Signaling"}install(i){O.init(i),b.init(i),E.init(i),m.init(i),y.init(i),i.helper.registerValidateConfig({auth:T,params:C})}};export{M as default};
@@ -1 +1 @@
1
- !function(i,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(i="undefined"!=typeof globalThis?globalThis:i||self).TencentCloudChatSignaling=e()}(this,function(){"use strict";function i(i,e,n,t){return new(n||(n=Promise))(function(s,a){function o(i){try{g(t.next(i))}catch(i){a(i)}}function r(i){try{g(t.throw(i))}catch(i){a(i)}}function g(i){var e;i.done?s(i.value):(e=i.value,e instanceof n?e:new n(function(i){i(e)})).then(o,r)}g((t=t.apply(i,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const e=1,n=2,t=3,s=4,a=5,o="TIMCustomElem",r="C2C",g="GROUP",l="invite",c="accept",v="cancel",u="reject",h="modifyInvitation",d="signaling",_=8010,I="signaling-timeout";function S(i){return i.filter(i=>{if(i.type===o){const{cloudCustomData:e="",payload:{data:n=""}={}}=i,t=e.match(/"type":"tsignaling"/),s=n.match(/inviteID/),a=n.match(/actionType/);return t||s&&a}return!1})}function p(i){const{data:e}=i.payload;try{return JSON.parse(e)}catch(i){return console.error(i),null}}function f(i,e){return i.toString(16).padStart(e,"0")}function m(i){if(i<0||i>53)throw new Error("Number of digits must be between 0 and 53");if(i<=30)return Math.floor(Math.random()*(1<<i));const e=i-30;return Math.floor(Math.random()*(1<<e))*(1<<30)+Math.floor(Math.random()*(1<<30))}var D=new class{constructor(){this._onlineSignalingMap=new Map,this._isSignalingListening=!1}init(i){this._core=i,i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}getOnlineSignalingMap(){return this._onlineSignalingMap}hasSignaling(i){return this._onlineSignalingMap.has(i)}getSignaling(i){return this._onlineSignalingMap.get(i)}saveSignaling(i,e){return this._onlineSignalingMap.set(i,e)}removeSignaling(i){return this._onlineSignalingMap.delete(i)}isInvitationValid(i){const e=`${d}.isInvitationValid`,{inviteID:n,groupID:t,inviter:s}=i,a=s===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(n))return!1;const{inviteeList:o}=this._onlineSignalingMap.get(n);if(console.log(`${e} inviteID:${n} inviteeList:${o} groupID:${t}`),t){if(a)return o.length>0;const i=this._core.common.getCurrentUserID();return o.includes(i)}return!0}updateSignaling(i){const e=`${d}.updateSignaling`,{inviteID:n,inviter:t,inviteeList:s,groupID:a}=i;if(console.log(`${e} inviteID:${n} inviter:${t} groupID:${a}`),a&&this.hasSignaling(n)){const i=s[0],{inviteeList:t}=this._onlineSignalingMap.get(n);t.includes(i)&&(t.splice(t.indexOf(i),1),console.log(`${e} remove ${i}. localInviteeList.length:${t.length}`)),0===t.length&&this.removeSignaling(n)}else this.removeSignaling(n)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}};var E=new class{init(i){this._core=i}createInviteSignaling(i){const e=this._generateInviteID(),n=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:t,inviteeList:s}=n,a=t||s[0];return{signaling:this._createSignaling(n,a),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){const e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){const e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,s=n||t[0];return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){const e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){const{isInviter:e=!1}=i,n=this._createTimeoutSignalingData(i),{groupID:t,inviteeList:s,inviter:a}=n,o=t||(e?s[0]:a);return{signaling:this._createSignaling(n,o),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(n)}}_createSignalingExtensionOptions(i){var e,n;const{data:t="",onlineUserOnly:s,inviteID:a="",offlinePushInfo:o,actionType:r}=i,g=(null===(n=null===(e=D.getSignaling(a))||void 0===e?void 0:e.signaling)||void 0===n?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:s||g,offlinePushInfo:o,messageControlInfo:this._createMessageControlInfo(t,r)}}_createMessageControlInfo(i,e){const n=e===a&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),t=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:n||t,excludedFromLastMessage:n||t}}_createInviteSignalingData(i){const n=`${d}._createInviteSignalingData`,{userID:t,timeout:s=0,groupID:a="",inviteeList:o=[]}=i,r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:e,inviter:r,inviteeList:a?o:[t],timeout:s});return console.log(`${n} signalingData:`,g),g}_createAcceptSignalingData(i){const e=`${d}._createAcceptSignalingData`,{inviteID:n}=i,s=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=D.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:t,groupID:o,inviter:a,inviteeList:[s]});return console.log(`${e} signalingData:`,r),r}_createCancelSignalingData(i){const e=`${d}._createCancelSignalingData`,{inviteID:t}=i,s=this._core.common.getCurrentUserID(),{inviteeList:a,groupID:o}=D.getSignaling(t),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:n,groupID:o,inviter:s,inviteeList:a});return console.log(`${e} signalingData:`,r),r}_createRejectSignalingData(i){const e=`${d}._createRejectSignalingData`,{inviteID:n}=i,t=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=D.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:s,groupID:o,inviter:a,inviteeList:[t]});return console.log(`${e} signalingData:`,r),r}_createTimeoutSignalingData(i){const e=`${d}._createTimeoutSignalingData`,{isInviter:n=!1,inviteID:t}=i,{inviteeList:s,inviter:o}=D.getSignaling(t),r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:a,inviter:o,inviteeList:n?s:[r]});return console.log(`${e} signalingData:`,g),g}_createSignaling(i,e){var n,t,s;const{groupID:a=""}=i,o={to:e,conversationType:a?g:r,priority:"High",payload:{data:JSON.stringify(i)}};return null===(s=null===(t=null===(n=this._core)||void 0===n?void 0:n.message)||void 0===t?void 0:t.messageFactory)||void 0===s?void 0:s.createCustomMessage(o)}_generateInviteID(){return[f(m(32),8),f(m(16),4),f(16384|m(12),4),f(32768|m(14),4),f(m(48),12)].join("-")}_generateBaseSignalData(i){const{data:e="",inviteID:n="",groupID:t=""}=i;return{businessID:1,timeout:0,data:e,inviteID:n,groupID:t}}};var O=new class{constructor(){this._isProcessingSignaling=!1}init(i){this._core=i,i.helper.registerApi({apiName:"invite",context:this}),i.helper.registerApi({apiName:"accept",context:this}),i.helper.registerApi({apiName:"cancel",context:this}),i.helper.registerApi({apiName:"reject",context:this}),i.helper.registerApi({apiName:"modifyInvitation",context:this}),i.helper.registerApi({apiName:"getSignalingInfo",context:this}),i.helper.registerApi({apiName:"addSignalingListener",context:this}),i.helper.registerApi({apiName:"removeSignalingListener",context:this}),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}invite(e){return i(this,void 0,void 0,function*(){var i;try{this._validateBeforeInvite(e);const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createInviteSignaling(e),a=yield this._sendSignaling(n,s);if(0===(null==a?void 0:a.code)){const{inviteID:e,timeout:s}=t;return D.saveSignaling(e,Object.assign(Object.assign({},t),{signaling:n})),s>0&&(null===(i=this._core)||void 0===i||i.helper.taskScheduler.addOnceTask({id:`${I}-${e}`,intervalMs:1e3*(s+5),callback:this.handleInvitationExpiryTimer.bind(this,e)})),Object.assign(Object.assign({},a),{inviteID:e})}return a}catch(i){throw i}})}accept(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeAccept(i),this._isProcessingSignaling=!0;const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createAcceptSignaling(e),a=yield this._sendSignaling(n,s);return 0===(null==a?void 0:a.code)?(D.updateSignaling(t),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}cancel(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeCancel(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createCancelSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(D.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}reject(e){return i(this,void 0,void 0,function*(){try{const{inviteID:i}=e;this._validateBeforeReject(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createRejectSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(D.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}modifyInvitation(e){return i(this,void 0,void 0,function*(){var i,n;const{inviteID:t,data:s}=e;let a="";try{this._validateBeforeModifyInvitation(t);const e=D.getSignaling(t),{signaling:o}=e,r=function(i,e){var n={};for(var t in i)Object.prototype.hasOwnProperty.call(i,t)&&e.indexOf(t)<0&&(n[t]=i[t]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(t=Object.getOwnPropertySymbols(i);s<t.length;s++)e.indexOf(t[s])<0&&Object.prototype.propertyIsEnumerable.call(i,t[s])&&(n[t[s]]=i[t[s]])}return n}(e,["signaling"]);a=o.payload.data,r.data=s,o.payload.data=JSON.stringify(r);const g=yield null===(n=null===(i=this._core)||void 0===i?void 0:i.message.messageAction)||void 0===n?void 0:n.modifyMessage(o);return D.hasSignaling(t)&&D.saveSignaling(t,Object.assign(Object.assign({},r),{signaling:o})),g}catch(i){if(a){const{signaling:i}=D.getSignaling(t);i.payload.data=a}throw i}})}getSignalingInfo(i){const{ssoLog:n,utils:{safeStringify:t}}=this._core;if(0===S([i]).length)return;const s=p(i),a={businessID:s.businessID||1,inviteID:s.inviteID,groupID:s.groupID||"",inviter:s.inviter||"",inviteeList:s.inviteeList||[],data:s.data||"",actionType:s.actionType||e,timeout:s.timeout||0};return n.debug(`${d} getSignalingInfo ${t(a)}`),a}addSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&D.setSignalingListenStatus(!0),null===(s=this._core)||void 0===s||s.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&D.setSignalingListenStatus(!1),null===(s=this._core)||void 0===s||s.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){const e=D.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();if(!e.has(i))return;const t=e.get(i).inviter===n;this._sendTimeoutNotice({inviteID:i,isInviter:t})}_sendSignaling(e,n){return i(this,void 0,void 0,function*(){var i,t,s;return null===(s=null===(t=null===(i=this._core)||void 0===i?void 0:i.message)||void 0===t?void 0:t.messageSender)||void 0===s?void 0:s.sendMessage(e,n)})}_sendTimeoutNotice(e){return i(this,void 0,void 0,function*(){var i,n,t;this._core.ssoLog.debug("_sendTimeoutNotice",`${d}._sendTimeoutNotice params:${JSON.stringify(e)}`);const{isInviter:s,inviteID:a}=e,{signaling:o,signalingData:r,signalingExtensionOptions:g}=E.createTimeoutSignaling(e),l=yield this._sendSignaling(o,g);if(0===(null==l?void 0:l.code)){const{data:e,groupID:g,inviteeList:l,inviter:c}=r;null===(i=this._core)||void 0===i||i.notificationCenter.emitOuterEvent(null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_TIMEOUT,{name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.INVITATION_TIMEOUT,data:{data:e,groupID:g,inviteID:a,inviteeList:l,inviter:c,isSelfTimeout:!0,message:o}}),s?D.removeSignaling(a):D.updateSignaling(r)}})}_validateInviteId(i,e){if(!D.hasSignaling(e))throw new this._core.helper.ChatError({functionName:i,code:_})}_validateProcessStatus(i){if(this._isProcessingSignaling)throw new this._core.helper.ChatError({functionName:i,message:"processing other signaling operations"})}_validateBeforeInvite(i){const e=l,{userID:n}=i,t=this._core.common.getCurrentUserID();if(n===t)throw new this._core.helper.ChatError({functionName:e,message:`cannot invite yourself, currentUserId:${t}, inviteeId:${n}`})}_validateBeforeAccept(i){const e=c;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=D.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeCancel(i){const e=v;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviter:t}=D.getSignaling(i);if(t!==n){const i=`unmatched inviter:${t} and my userID:${n}`;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(i){const e=u;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=D.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeModifyInvitation(i){const e=h;this._validateInviteId(e,i)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}};var b=new class{constructor(){this._actionProcessor=new Map([[e,this._onNewInvitationReceived.bind(this)],[s,this._onInviteeRejected.bind(this)],[t,this._onInviteeAccepted.bind(this)],[n,this._onInvitationCancelled.bind(this)],[a,this._onInvitationTimeout.bind(this)]])}init(i){this._core=i,i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this)}handleActionSignaling(i){i.forEach(i=>{const e=p(i);if(e){const n=this._actionProcessor.get(e.actionType);null==n||n(e,i)}})}_handleMessageReceived(i){if(!D.getSignalingListenStatus())return;const e=S(i.data);0!==e.length&&this.handleActionSignaling(e)}_handleMessageModified(i){if(!D.getSignalingListenStatus())return;const e=S(i.data);e.length>0&&e.forEach(i=>{const e=p(i);e&&this._onInvitationModified(e,i)})}_onNewInvitationReceived(i,e){var n,t;const s=`${d}._onNewInvitationReceived`,{inviteID:a,inviteeList:o,groupID:r}=i,g=this._core.common.getCurrentUserID();this._core.ssoLog.debug("_onNewInvitationReceived",`${s} signalingData:${JSON.stringify(i)}}`);if(r&&!o.includes(g))return;let{timeout:l}=i;const c=Date.now()/1e3-e.time;l>0&&c>0&&l>c&&(l-=c);const v=D.getSignaling(a);v!==i&&(v||D.saveSignaling(a,Object.assign(Object.assign({},i),{signaling:e})),l>0&&(null===(n=this._core)||void 0===n||n.helper.taskScheduler.addOnceTask({id:`${I}-${a}`,intervalMs:1e3*l,callback:O.handleInvitationExpiryTimer.bind(O,a)})),this._emitEvent({name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:o})}))}_onInviteeRejected(i){var e;const n=`${d}._onInviteeRejected`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=D.hasSignaling(t);this._core.ssoLog.debug("_onInviteeRejected",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInviteeAccepted(i){var e;const n=`${d}._onInviteeAccepted`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=D.hasSignaling(t);this._core.ssoLog.debug("_onInviteeAccepted",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInvitationCancelled(i){var e;const n=`${d}._onInvitationCancelled`,{inviteID:t,inviter:s,groupID:a}=i,o=D.hasSignaling(t);this._core.ssoLog.debug("_onInvitationCancelled",`${n} inviteID:${t} hasInviteID:${o} inviter:${s} groupID:${a}`),o&&(D.removeSignaling(t),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){var e;const n=`${d}._onInvitationTimeout`,{inviteID:t,inviteeList:s}=i,a=D.hasSignaling(t);this._core.ssoLog.debug("_onInvitationTimeout",`${n} inviteID:${t} hasInviteID:${a} data:${i.data}`),a&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:s,isSelfTimeout:!1})}))}_onInvitationModified(i,e){var n;const t=`${d}._onInvitationModified`,{inviteID:s,data:a}=i,o=D.hasSignaling(s);this._core.ssoLog.debug("_onInvitationModified",`${t} inviteID:${s} data:${a}`),o&&(D.saveSignaling(s,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:s,data:a}}))}_emitEvent(i){var e;null===(e=this._core)||void 0===e||e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){const{inviteID:e,inviter:n,groupID:t,data:s}=i;return{inviteID:e,inviter:n,groupID:t,data:s||""}}_dispose(){var i,e,n;null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null===(n=this._core)||void 0===n||n.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}};var y=new class{constructor(){this._offlineSignalingMap=new Map}init(i){this._core=i;const{notificationCenter:e,helper:n,constants:{InnerEvent:t,WORKFLOW_STEP:s,WORKFLOW_NAME:a}}=i;e.subscribeInnerEvent(t.DESTROY,this._dispose,this),e.subscribeInnerEvent(t.LOGOUT,this._reset,this),n.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,s.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){const{result:{unreadMessageMap:e}={}}=i||{};if(!(0!==(null==e?void 0:e.size)&&D.getSignalingListenStatus()))return;const n=S([...e.values()]);if(0!==n.length&&(n.forEach(i=>{this._handleC2CActionType(i)}),this._offlineSignalingMap.size>0)){const i=this._sortOfflineSignalingByTime();b.handleActionSignaling(i)}}_handleC2CActionType(i){const n=p(i);if(!n)return;const{actionType:t}=n;t===e?this._saveValidOfflineInvite(n,i):this._removeOfflineInvite(n)}_saveValidOfflineInvite(i,e){const{inviteID:n,inviteeList:t=[],timeout:s=0}=i,a=this._core.common.getCurrentUserID();if(!t.includes(a))return;const o=Date.now()/1e3-e.time;s>0&&o>s&&0!==s||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]}))}_removeOfflineInvite(i){const{inviteID:e=""}=i;this._offlineSignalingMap.has(e)&&this._offlineSignalingMap.delete(e)}_sortOfflineSignalingByTime(){let i=[];return this._offlineSignalingMap.forEach(e=>{i=[...i,...e.signalingList]}),i.sort((i,e)=>i.time-e.time)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};const C={invite:{userID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0},timeout:{required:!1,rules:["number"],allowEmpty:!1},onlineUserOnly:{required:!1,rules:["boolean"],allowEmpty:!1},offlinePushInfo:{required:!1,rules:["object"],allowEmpty:!1}},cancel:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},accept:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},reject:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},modifyInvitation:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}}},T={invite:!0,cancel:!0,accept:!0,reject:!0,modifyInvitation:!0};return new class{constructor(){this.name="Signaling"}install(i){O.init(i),b.init(i),E.init(i),D.init(i),y.init(i),i.helper.registerValidateConfig({auth:T,params:C})}}});
1
+ !function(i,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(i="undefined"!=typeof globalThis?globalThis:i||self).TencentCloudChatSignaling=e()}(this,(function(){"use strict";function i(i,e,n,t){return new(n||(n=Promise))((function(s,a){function o(i){try{g(t.next(i))}catch(i){a(i)}}function r(i){try{g(t.throw(i))}catch(i){a(i)}}function g(i){var e;i.done?s(i.value):(e=i.value,e instanceof n?e:new n((function(i){i(e)}))).then(o,r)}g((t=t.apply(i,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const e=1,n=2,t=3,s=4,a=5,o="TIMCustomElem",r="C2C",g="GROUP",l="invite",c="accept",v="cancel",u="reject",h="modifyInvitation",d="signaling",_=8010,I="signaling-timeout";function S(i){return i.filter((i=>{if(i.type===o){const{cloudCustomData:e="",payload:{data:n=""}={}}=i,t=e.match(/"type":"tsignaling"/),s=n.match(/inviteID/),a=n.match(/actionType/);return t||s&&a}return!1}))}function p(i){const{data:e}=i.payload;try{return JSON.parse(e)}catch(i){return console.error(i),null}}function f(i,e){return i.toString(16).padStart(e,"0")}function m(i){if(i<0||i>53)throw new Error("Number of digits must be between 0 and 53");if(i<=30)return Math.floor(Math.random()*(1<<i));const e=i-30;return Math.floor(Math.random()*(1<<e))*(1<<30)+Math.floor(Math.random()*(1<<30))}var D=new class{constructor(){this._onlineSignalingMap=new Map,this._isSignalingListening=!1}init(i){this._core=i,i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}getOnlineSignalingMap(){return this._onlineSignalingMap}hasSignaling(i){return this._onlineSignalingMap.has(i)}getSignaling(i){return this._onlineSignalingMap.get(i)}saveSignaling(i,e){return this._onlineSignalingMap.set(i,e)}removeSignaling(i){return this._onlineSignalingMap.delete(i)}isInvitationValid(i){const e=`${d}.isInvitationValid`,{inviteID:n,groupID:t,inviter:s}=i,a=s===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(n))return!1;const{inviteeList:o}=this._onlineSignalingMap.get(n);if(console.log(`${e} inviteID:${n} inviteeList:${o} groupID:${t}`),t){if(a)return o.length>0;const i=this._core.common.getCurrentUserID();return o.includes(i)}return!0}updateSignaling(i){const e=`${d}.updateSignaling`,{inviteID:n,inviter:t,inviteeList:s,groupID:a}=i;if(console.log(`${e} inviteID:${n} inviter:${t} groupID:${a}`),a&&this.hasSignaling(n)){const i=s[0],{inviteeList:t}=this._onlineSignalingMap.get(n);t.includes(i)&&(t.splice(t.indexOf(i),1),console.log(`${e} remove ${i}. localInviteeList.length:${t.length}`)),0===t.length&&this.removeSignaling(n)}else this.removeSignaling(n)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}};var E=new class{init(i){this._core=i}createInviteSignaling(i){const e=this._generateInviteID(),n=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:t,inviteeList:s}=n,a=t||s[0];return{signaling:this._createSignaling(n,a),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){const e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){const e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,s=n||t[0];return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){const e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e,s=n||t;return{signaling:this._createSignaling(e,s),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){const{isInviter:e=!1}=i,n=this._createTimeoutSignalingData(i),{groupID:t,inviteeList:s,inviter:a}=n,o=t||(e?s[0]:a);return{signaling:this._createSignaling(n,o),signalingData:n,signalingExtensionOptions:this._createSignalingExtensionOptions(n)}}_createSignalingExtensionOptions(i){var e,n;const{data:t="",onlineUserOnly:s,inviteID:a="",offlinePushInfo:o,actionType:r}=i,g=(null===(n=null===(e=D.getSignaling(a))||void 0===e?void 0:e.signaling)||void 0===n?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:s||g,offlinePushInfo:o,messageControlInfo:this._createMessageControlInfo(t,r)}}_createMessageControlInfo(i,e){const n=e===a&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),t=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:n||t,excludedFromLastMessage:n||t}}_createInviteSignalingData(i){const n=`${d}._createInviteSignalingData`,{userID:t,timeout:s=0,groupID:a="",inviteeList:o=[]}=i,r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:e,inviter:r,inviteeList:a?o:[t],timeout:s});return console.log(`${n} signalingData:`,g),g}_createAcceptSignalingData(i){const e=`${d}._createAcceptSignalingData`,{inviteID:n}=i,s=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=D.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:t,groupID:o,inviter:a,inviteeList:[s]});return console.log(`${e} signalingData:`,r),r}_createCancelSignalingData(i){const e=`${d}._createCancelSignalingData`,{inviteID:t}=i,s=this._core.common.getCurrentUserID(),{inviteeList:a,groupID:o}=D.getSignaling(t),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:n,groupID:o,inviter:s,inviteeList:a});return console.log(`${e} signalingData:`,r),r}_createRejectSignalingData(i){const e=`${d}._createRejectSignalingData`,{inviteID:n}=i,t=this._core.common.getCurrentUserID(),{inviter:a,groupID:o}=D.getSignaling(n),r=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:s,groupID:o,inviter:a,inviteeList:[t]});return console.log(`${e} signalingData:`,r),r}_createTimeoutSignalingData(i){const e=`${d}._createTimeoutSignalingData`,{isInviter:n=!1,inviteID:t}=i,{inviteeList:s,inviter:o}=D.getSignaling(t),r=this._core.common.getCurrentUserID(),g=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:a,inviter:o,inviteeList:n?s:[r]});return console.log(`${e} signalingData:`,g),g}_createSignaling(i,e){var n,t,s;const{groupID:a=""}=i,o={to:e,conversationType:a?g:r,priority:"High",payload:{data:JSON.stringify(i)}};return null===(s=null===(t=null===(n=this._core)||void 0===n?void 0:n.message)||void 0===t?void 0:t.messageFactory)||void 0===s?void 0:s.createCustomMessage(o)}_generateInviteID(){return[f(m(32),8),f(m(16),4),f(16384|m(12),4),f(32768|m(14),4),f(m(48),12)].join("-")}_generateBaseSignalData(i){const{data:e="",inviteID:n="",groupID:t=""}=i;return{businessID:1,timeout:0,data:e,inviteID:n,groupID:t}}};var O=new class{constructor(){this._isProcessingSignaling=!1}init(i){this._core=i,i.helper.registerApi({apiName:"invite",context:this}),i.helper.registerApi({apiName:"accept",context:this}),i.helper.registerApi({apiName:"cancel",context:this}),i.helper.registerApi({apiName:"reject",context:this}),i.helper.registerApi({apiName:"modifyInvitation",context:this}),i.helper.registerApi({apiName:"getSignalingInfo",context:this}),i.helper.registerApi({apiName:"addSignalingListener",context:this}),i.helper.registerApi({apiName:"removeSignalingListener",context:this}),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.LOGOUT,this._reset,this)}invite(e){return i(this,void 0,void 0,(function*(){var i;try{this._validateBeforeInvite(e);const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createInviteSignaling(e),a=yield this._sendSignaling(n,s);if(0===(null==a?void 0:a.code)){const{inviteID:e,timeout:s}=t;return D.saveSignaling(e,Object.assign(Object.assign({},t),{signaling:n})),s>0&&(null===(i=this._core)||void 0===i||i.helper.taskScheduler.addOnceTask({id:`${I}-${e}`,intervalMs:1e3*(s+5),callback:this.handleInvitationExpiryTimer.bind(this,e)})),Object.assign(Object.assign({},a),{inviteID:e})}return a}catch(i){throw i}}))}accept(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeAccept(i),this._isProcessingSignaling=!0;const{signaling:n,signalingData:t,signalingExtensionOptions:s}=E.createAcceptSignaling(e),a=yield this._sendSignaling(n,s);return 0===(null==a?void 0:a.code)?(D.updateSignaling(t),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}cancel(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeCancel(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createCancelSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(D.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}reject(e){return i(this,void 0,void 0,(function*(){try{const{inviteID:i}=e;this._validateBeforeReject(i),this._isProcessingSignaling=!0;const{signaling:n,signalingExtensionOptions:t}=E.createRejectSignaling(e),s=yield this._sendSignaling(n,t);return 0===(null==s?void 0:s.code)?(D.removeSignaling(i),Object.assign(Object.assign({},s),{inviteID:i})):s}catch(i){throw i}finally{this._isProcessingSignaling=!1}}))}modifyInvitation(e){return i(this,void 0,void 0,(function*(){var i,n;const{inviteID:t,data:s}=e;let a="";try{this._validateBeforeModifyInvitation(t);const e=D.getSignaling(t),{signaling:o}=e,r=function(i,e){var n={};for(var t in i)Object.prototype.hasOwnProperty.call(i,t)&&e.indexOf(t)<0&&(n[t]=i[t]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(t=Object.getOwnPropertySymbols(i);s<t.length;s++)e.indexOf(t[s])<0&&Object.prototype.propertyIsEnumerable.call(i,t[s])&&(n[t[s]]=i[t[s]])}return n}(e,["signaling"]);a=o.payload.data,r.data=s,o.payload.data=JSON.stringify(r);const g=yield null===(n=null===(i=this._core)||void 0===i?void 0:i.message.messageAction)||void 0===n?void 0:n.modifyMessage(o);return D.hasSignaling(t)&&D.saveSignaling(t,Object.assign(Object.assign({},r),{signaling:o})),g}catch(i){if(a){const{signaling:i}=D.getSignaling(t);i.payload.data=a}throw i}}))}getSignalingInfo(i){const{ssoLog:n,utils:{safeStringify:t}}=this._core;if(0===S([i]).length)return;const s=p(i),a={businessID:s.businessID||1,inviteID:s.inviteID,groupID:s.groupID||"",inviter:s.inviter||"",inviteeList:s.inviteeList||[],data:s.data||"",actionType:s.actionType||e,timeout:s.timeout||0};return n.debug(`${d} getSignalingInfo ${t(a)}`),a}addSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&D.setSignalingListenStatus(!0),null===(s=this._core)||void 0===s||s.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t,s;i===(null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&D.setSignalingListenStatus(!1),null===(s=this._core)||void 0===s||s.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){const e=D.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();if(!e.has(i))return;const t=e.get(i).inviter===n;this._sendTimeoutNotice({inviteID:i,isInviter:t})}_sendSignaling(e,n){return i(this,void 0,void 0,(function*(){var i,t,s;return null===(s=null===(t=null===(i=this._core)||void 0===i?void 0:i.message)||void 0===t?void 0:t.messageSender)||void 0===s?void 0:s.sendMessage(e,n)}))}_sendTimeoutNotice(e){return i(this,void 0,void 0,(function*(){var i,n,t;this._core.ssoLog.debug("_sendTimeoutNotice",`${d}._sendTimeoutNotice params:${JSON.stringify(e)}`);const{isInviter:s,inviteID:a}=e,{signaling:o,signalingData:r,signalingExtensionOptions:g}=E.createTimeoutSignaling(e),l=yield this._sendSignaling(o,g);if(0===(null==l?void 0:l.code)){const{data:e,groupID:g,inviteeList:l,inviter:c}=r;null===(i=this._core)||void 0===i||i.notificationCenter.emitOuterEvent(null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_TIMEOUT,{name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.INVITATION_TIMEOUT,data:{data:e,groupID:g,inviteID:a,inviteeList:l,inviter:c,isSelfTimeout:!0,message:o}}),s?D.removeSignaling(a):D.updateSignaling(r)}}))}_validateInviteId(i,e){if(!D.hasSignaling(e))throw new this._core.helper.ChatError({functionName:i,code:_})}_validateProcessStatus(i){if(this._isProcessingSignaling)throw new this._core.helper.ChatError({functionName:i,message:"processing other signaling operations"})}_validateBeforeInvite(i){const e=l,{userID:n}=i,t=this._core.common.getCurrentUserID();if(n===t)throw new this._core.helper.ChatError({functionName:e,message:`cannot invite yourself, currentUserId:${t}, inviteeId:${n}`})}_validateBeforeAccept(i){const e=c;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=D.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeCancel(i){const e=v;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviter:t}=D.getSignaling(i);if(t!==n){const i=`unmatched inviter:${t} and my userID:${n}`;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(i){const e=u;this._validateInviteId(e,i),this._validateProcessStatus(e);const n=this._core.common.getCurrentUserID(),{inviteeList:t}=D.getSignaling(i);if(!t.includes(n)){const t=`userID:${n} not in inviteeList. inviteID:${i}`;throw new this._core.helper.ChatError({functionName:e,message:t})}}_validateBeforeModifyInvitation(i){const e=h;this._validateInviteId(e,i)}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}};var b=new class{constructor(){this._actionProcessor=new Map([[e,this._onNewInvitationReceived.bind(this)],[s,this._onInviteeRejected.bind(this)],[t,this._onInviteeAccepted.bind(this)],[n,this._onInvitationCancelled.bind(this)],[a,this._onInvitationTimeout.bind(this)]])}init(i){this._core=i,i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),i.notificationCenter.subscribeOuterEvent(i.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),i.notificationCenter.subscribeInnerEvent(i.InnerEvent.DESTROY,this._dispose,this)}handleActionSignaling(i){i.forEach((i=>{const e=p(i);if(e){const n=this._actionProcessor.get(e.actionType);null==n||n(e,i)}}))}_handleMessageReceived(i){if(!D.getSignalingListenStatus())return;const e=S(i.data);0!==e.length&&this.handleActionSignaling(e)}_handleMessageModified(i){if(!D.getSignalingListenStatus())return;const e=S(i.data);e.length>0&&e.forEach((i=>{const e=p(i);e&&this._onInvitationModified(e,i)}))}_onNewInvitationReceived(i,e){var n,t;const s=`${d}._onNewInvitationReceived`,{inviteID:a,inviteeList:o,groupID:r}=i,g=this._core.common.getCurrentUserID();this._core.ssoLog.debug("_onNewInvitationReceived",`${s} signalingData:${JSON.stringify(i)}}`);if(r&&!o.includes(g))return;let{timeout:l}=i;const c=Date.now()/1e3-e.time;l>0&&c>0&&l>c&&(l-=c);const v=D.getSignaling(a);v!==i&&(v||D.saveSignaling(a,Object.assign(Object.assign({},i),{signaling:e})),l>0&&(null===(n=this._core)||void 0===n||n.helper.taskScheduler.addOnceTask({id:`${I}-${a}`,intervalMs:1e3*l,callback:O.handleInvitationExpiryTimer.bind(O,a)})),this._emitEvent({name:null===(t=this._core)||void 0===t?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:o})}))}_onInviteeRejected(i){var e;const n=`${d}._onInviteeRejected`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=D.hasSignaling(t);this._core.ssoLog.debug("_onInviteeRejected",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInviteeAccepted(i){var e;const n=`${d}._onInviteeAccepted`,{inviteID:t,inviter:s,groupID:a,inviteeList:o}=i,r=D.hasSignaling(t);this._core.ssoLog.debug("_onInviteeAccepted",`${n} inviteID:${t} hasInviteID:${r} inviter:${s} groupID:${a}`),r&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:o[0]})}))}_onInvitationCancelled(i){var e;const n=`${d}._onInvitationCancelled`,{inviteID:t,inviter:s,groupID:a}=i,o=D.hasSignaling(t);this._core.ssoLog.debug("_onInvitationCancelled",`${n} inviteID:${t} hasInviteID:${o} inviter:${s} groupID:${a}`),o&&(D.removeSignaling(t),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){var e;const n=`${d}._onInvitationTimeout`,{inviteID:t,inviteeList:s}=i,a=D.hasSignaling(t);this._core.ssoLog.debug("_onInvitationTimeout",`${n} inviteID:${t} hasInviteID:${a} data:${i.data}`),a&&(D.updateSignaling(i),this._emitEvent({name:null===(e=this._core)||void 0===e?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:s,isSelfTimeout:!1})}))}_onInvitationModified(i,e){var n;const t=`${d}._onInvitationModified`,{inviteID:s,data:a}=i,o=D.hasSignaling(s);this._core.ssoLog.debug("_onInvitationModified",`${t} inviteID:${s} data:${a}`),o&&(D.saveSignaling(s,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null===(n=this._core)||void 0===n?void 0:n.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:s,data:a}}))}_emitEvent(i){var e;null===(e=this._core)||void 0===e||e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){const{inviteID:e,inviter:n,groupID:t,data:s}=i;return{inviteID:e,inviter:n,groupID:t,data:s||""}}_dispose(){var i,e,n;null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null===(n=this._core)||void 0===n||n.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}};var y=new class{constructor(){this._offlineSignalingMap=new Map}init(i){this._core=i;const{notificationCenter:e,helper:n,constants:{InnerEvent:t,WORKFLOW_STEP:s,WORKFLOW_NAME:a}}=i;e.subscribeInnerEvent(t.DESTROY,this._dispose,this),e.subscribeInnerEvent(t.LOGOUT,this._reset,this),n.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,s.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){const{result:{unreadMessageMap:e}={}}=i||{};if(!(0!==(null==e?void 0:e.size)&&D.getSignalingListenStatus()))return;const n=S([...e.values()]);if(0!==n.length&&(n.forEach((i=>{this._handleC2CActionType(i)})),this._offlineSignalingMap.size>0)){const i=this._sortOfflineSignalingByTime();b.handleActionSignaling(i)}}_handleC2CActionType(i){const n=p(i);if(!n)return;const{actionType:t}=n;t===e?this._saveValidOfflineInvite(n,i):this._removeOfflineInvite(n)}_saveValidOfflineInvite(i,e){const{inviteID:n,inviteeList:t=[],timeout:s=0}=i,a=this._core.common.getCurrentUserID();if(!t.includes(a))return;const o=Date.now()/1e3-e.time;s>0&&o>s&&0!==s||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]}))}_removeOfflineInvite(i){const{inviteID:e=""}=i;this._offlineSignalingMap.has(e)&&this._offlineSignalingMap.delete(e)}_sortOfflineSignalingByTime(){let i=[];return this._offlineSignalingMap.forEach((e=>{i=[...i,...e.signalingList]})),i.sort(((i,e)=>i.time-e.time))}_dispose(){var i,e;this._reset(),null===(i=this._core)||void 0===i||i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null===(e=this._core)||void 0===e||e.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};const C={invite:{userID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0},timeout:{required:!1,rules:["number"],allowEmpty:!1},onlineUserOnly:{required:!1,rules:["boolean"],allowEmpty:!1},offlinePushInfo:{required:!1,rules:["object"],allowEmpty:!1}},cancel:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},accept:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},reject:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}},modifyInvitation:{inviteID:{required:!0,rules:["string"],allowEmpty:!1},data:{required:!1,rules:["string"],allowEmpty:!0}}},T={invite:!0,cancel:!0,accept:!0,reject:!0,modifyInvitation:!0};return new class{constructor(){this.name="Signaling"}install(i){O.init(i),b.init(i),E.init(i),D.init(i),y.init(i),i.helper.registerValidateConfig({auth:T,params:C})}}}));
@@ -1 +1 @@
1
- const e=new class{init(e){this.core=e}},t="@TOPIC#_",s=7,o=8,i=17,n=18,r=20,c=300,a=0,u=3600,p=3021,l=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],d=t=>{const{core:{common:{getMessagePreviewText:s}}}=e;return{lastTime:(null==t?void 0:t.time)||0,lastSequence:(null==t?void 0:t.sequence)||0,fromAccount:(null==t?void 0:t.from)||"",payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",messageForShow:s(null==t?void 0:t.type,null==t?void 0:t.payload),nick:(null==t?void 0:t.nick)||"",avatar:(null==t?void 0:t.avatar)||"",version:(null==t?void 0:t.version)||0,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,revoker:(null==t?void 0:t.revoker)||null}};class h{constructor(e){this.topicID="",this.topicName="",this.avatar="",this.introduction="",this.notification="",this.unreadCount=0,this.muteAllMembers=!1,this.customData="",this.groupAtInfoList=[],this.nextMessageSeq=0,this.lastMessage=d(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(const t in e)l.includes(t)&&("lastMessage"===t?this[t]=d(e[t]):"selfInfo"===t?this.updateSelfInfo(e[t]):this[t]="muteAllMembers"===t?1===e[t]:e[t])}updateUnreadCount(e=0){this.unreadCount=e}updateReadSequence(e=0,t=!1){t&&(this.selfInfo.readedSequence=e)}updateNextMessageSeq(e){this.nextMessageSeq=e}updateLastMessage(e){this.lastMessage=d(e)}updateGroupAtInfoList(t){const{core:{common:{updateGroupAtInfo:s}}}=e;s(t,this.groupAtInfoList)}updateTopic(t){const{core:{utils:{isUndefined:s},common:{deepMerge:o}}}=e;s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),o(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){const{core:{common:{deepMerge:s}}}=e;return s(this.selfInfo,t,[],[""])>0}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){var t;return e===(null===(t=this.lastMessage)||void 0===t?void 0:t.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var g=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){this._core=e;const{helper:t,constants:s,notificationCenter:o,InnerEvent:{LOGOUT:i,DESTROY:n}}=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),o.subscribeInnerEvent(i,this._reset,this),o.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_hasTopic(e,t){var s;return!!(null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t))}_setTopic(e,t,s){var o;null===(o=this._topicMap.get(e))||void 0===o||o.set(t,s)}_deleteTopic(e,t){var s;null===(s=this._topicMap.get(e))||void 0===s||s.delete(t)}getTopic(e,t){var s;return null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t)}updateTopic(e){e.forEach(e=>{const{groupID:t,topicID:s}=e;let o=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?(o=this.getTopic(t,s),null==o||o.updateTopic(e)):(this._getLastMessage(e),o=new h(e),this._setTopic(t,s,o));const i=this.computeUnreadCount(o);null==o||o.updateUnreadCount(i)})}_getLastMessage(e){const{utils:{isEmpty:t,isUndefined:s,isArray:o}}=this._core;if(!s(e.lastMsg)){let s=null,i="";o(e.lastMsg.elements)?(s=e.lastMsg.elements[0].content,i=e.lastMsg.elements[0].type):(s=e.lastMsg.elements.content,i=e.lastMsg.elements.type);const n={payload:s,type:i,time:e.lastMsg.time,sequence:e.lastMsg.sequence,from:e.lastMsg.from,nick:e.lastMsg.nick,avatar:e.lastMsg.avatar,version:e.lastMsg.messageVersion,cloudCustomData:e.lastMsg.cloudCustomData,isRevoked:2===e.lastMsg.isPlaceMessage,revoker:t(e.lastMsg.revokerInfo)?null:e.lastMsg.revokerInfo.revoker};e.lastMessage=n}}computeUnreadCount(e){const{excludedUnreadSequenceList:t=[],readedSequence:s}=e.selfInfo;let o=e.nextMessageSeq-e.selfInfo.readedSequence-1;if(t.length>0){let i=0;t.forEach(t=>{t>s&&t<=e.nextMessageSeq-1&&(i+=1)}),i>=1&&(o-=i)}return o>0?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach(e=>{const{utils:{isEmpty:t}}=this._core,{groupAtInfoList:s}=e;t(s)||this.updateGroupAtInfoList(s)})}updateGroupAtInfoList(e,t=!1){if(0===e.length)return;const{common:{getCurrentUserID:s},utils:{isUndefined:o,isEmpty:i}}=this._core,n=s();let r=null;e.forEach(e=>{var s;const{groupID:i,topicID:c,from:a}=e;a!==n&&(r=this.getTopic(i,c),r&&r.updateGroupAtInfoList(e),o(r)&&t&&(this.updateTopic([{groupID:i,topicID:c}]),null===(s=this.getTopic(i,c))||void 0===s||s.updateGroupAtInfoList(e)))}),i(r)||this.emitTopicUpdate(r)}getTopicList(e,t=[]){const s=this._topicMap.get(e),o=[];return s&&o.push(...s.values()),t.length>0?o.filter(e=>t.includes(e.topicID)):o}deleteTopicList(e,t=[]){this._topicMap.has(e)&&(t.forEach(t=>{this._hasTopic(e,t)&&this._deleteTopic(e,t)}),console.log(`${this._name}.deleteTopicList groupID:${e} topicID:${t.join(",")}`))}clearGroupAtInfoList(e,t){const{utils:{isEmpty:s}}=this._core,o=this.getTopic(e,t);s(null==o?void 0:o.groupAtInfoList)||(null==o||o.clearGroupAtInfoList(),console.log(`${this._name}.clearGroupAtInfoList topicID:${t}`))}markMessageAsRead(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,i=(null==s?void 0:s.getLocalMessageList(`${o}${e}`))||[];if(0===i.length)return;const{length:n}=i;for(let e=n-1;e>=0;e--){const s=i[e];if(!(t&&s.sequence>t)){if("in"===s.flow&&s.isRead)break;s.setIsRead(!0)}}}updateUnreadCount(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core;let i=!1;const n=this.getTopic(e,t);if(!n)return;const r=(null==s?void 0:s.getLocalMessageList(`${o}${t}`))||[],c=n.unreadCount,a=null==r?void 0:r.filter(e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted).length;console.log(`${this._name}._updateUnreadCount topicID:${t} currentUnreadCount:${c} newUnreadCount:${a}`),c!==a&&(n.updateUnreadCount(a),i=!0),i&&this.emitTopicUpdate(n)}updateSelfInfo(e){const{groupID:t,topicID:s,selfInfo:o}=e,i=this.getTopic(t,s);if(i){const e=i.updateSelfInfo(o);console.log(`${this._name}.updateSelfInfo topicID:${s} info:${JSON.stringify(o)} isUpdated:${e}`),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){const{OuterEvent:t,notificationCenter:s}=this._core;s.emitOuterEvent(t.TOPIC_UPDATED,{name:t.TOPIC_UPDATED,data:e})}_resetTopicRequestInfo(){this._topicRequestInfo.clear()}_reset(){this._topicMap.clear(),this._topicRequestInfo.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};const _=(t="")=>{const{core:{OuterConstant:s}}=e;return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){this._core=e;const{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:r,TOPIC_MESSAGE_DELETED:c,TOPIC_MESSAGE_REVOKED:a,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=e,{InnerEventSubType:{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}}=s;t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleNewMessage,this),s.subscribeInnerEvent(c,this._handleMessageDeleted,this),s.subscribeInnerEvent(a,this._handleMessageRevoked,this),s.subscribeInnerEvent(u,this._handleMessageModified,this),s.subscribeInnerEvent(n,l,this._handleTopicAtTips,this),s.subscribeInnerEvent(n,d,this._handleLatestMessage,this),s.subscribeInnerEvent(p,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleUnreadSyncFinished(e){var t;const{constants:{WORKFLOW_STEP:s}}=this._core,{groupTipList:o=[]}=e.result[s.UNREAD_MESSAGE_SYNC]||{};for(let e=0;e<o.length;e++){const s=o[e];12===s.Event&&(null===(t=s.GroupTips[0].MsgBody)||void 0===t?void 0:t.TopicId)&&this._handleTopicAtTips(s)}}_handleNewMessage(e){const{conversationUpdateFieldList:t=[]}=e.result||{};if(0===t.length)return;const{common:{isTopic:s}}=this._core;if(!s(t[0].conversationID))return;const{common:{getCurrentUserID:o}}=this._core;t.forEach(e=>{const{conversationID:t="",lastMessage:s}=e,i=_(t),n=m(i),r=g.getTopic(n,i);if(r){const{from:e,sequence:t}=s,i=t+1,n=e===o();if(i>r.nextMessageSeq){r.updateNextMessageSeq(i),r.updateLastMessage(s),r.updateReadSequence(t,n);const e=n?0:g.computeUnreadCount(r);r.updateUnreadCount(e),this._emitTopicUpdate(r)}}})}_handleMessageDeleted(e){const t=_(e),s=m(t),o=g.getTopic(s,t);if(console.log(`${this._name}._handleMessageDeleted, topicID:${t} hasTopic:${!!o}`),!o)return;const{message:{messageDataHandler:i},utils:{isEmpty:n}}=this._core,r=(null==i?void 0:i.getLocalMessageList(e))||[];let c={};for(let e=(r.length||0)-1;e>=0;e--)if(!r[e].isDeleted){c=r[e];break}let a=!1;o.lastMessage.lastSequence!==c.sequence&&(n(c)&&(c={}),o.updateLastMessage(c),a=!0),a&&(this._emitTopicUpdate(o),console.log(`${this._name}._handleMessageDeleted, topicID:${t} with lastMessage:${o.lastMessage}`))}_handleMessageRevoked(e){const{messageList:t=[]}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach(e=>{const{to:t,sequence:i,revoker:n}=e,r=m(t);if(s=g.getTopic(r,t),s){s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked(i)&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(n),o=!0);const{excludedUnreadSequenceList:e=[]}=s.selfInfo;e.push(i),s.updateSelfInfo({excludedUnreadSequenceList:e})}}),o&&this._emitTopicUpdate(s)}_handleMessageModified(e){const{conversationID:t,messageList:s}=e,o=_(t),i=m(o),n=g.getTopic(i,o);if(console.log(`${this._name}._handleMessageModified topicID:${o} hasTopic:${!!n}`),!n)return;const{lastMessage:r}=n;if(r){const e=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(r,e)){const{common:{getMessagePreviewText:t}}=this._core,{time:s,sequence:o,elements:i,cloudCustomData:c,messageVersion:a}=e;r.type=i[0].type,r.payload=i[0].content,r.messageForShow=t(r.type,r.payload),r.cloudCustomData=c,r.version=a,r.lastSequence=o,r.lastTime=s,this._emitTopicUpdate(n)}}}_shouldUpdateLastMessage(e,t){if(null===e.payload)return!0;const{sequence:s,messageVersion:o}=t;return e.lastSequence===s&&e.version!==o}_emitTopicUpdate(e){const{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){const{GroupTips:t=[]}=e,s=[];let o=!1;t.forEach(e=>{const{GroupAtTips:t,MsgBody:i,MsgRandom:n,ClientSeq:r}=e;let c={};t?c=this._convertAtInfo(t):i&&(c=this._convertAtInfo(i),o=!0),c.__random=n,c.__sequence=r,s.push(c)}),g.updateGroupAtInfoList(s,o)}_convertAtInfo(e){const{From_Account:t,GroupId:s,TopicId:o,MsgSeq:i,GroupAtType:n}=e;return{groupID:s,topicID:o,from:t,sequence:i,groupAtType:n}}_handleLatestMessage(e){const{utils:{isEmpty:t},message:{messageHelper:s,messageFactory:o}}=this._core,{NewMsg:i,NoUnreadSeqList:n}=e||{};if(!t(i)){const{GroupInfo:{TopicId:e}}=i,t=s.parseServerGroupMessage(i),r=o.createMessage(t);r.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:r,excludedUnreadSequenceList:n})}}_updateUnreadCountAndLastMessage(e){const{utils:{isUndefined:t}}=this._core,{topicID:s,message:o,excludedUnreadSequenceList:i}=e,n=m(s),r=g.getTopic(n,s);if(r){let e=r.selfInfo.excludedUnreadSequenceList||[];if(t(i)||(e=i),o._isExcludedFromUnreadCount&&e.push(o.sequence),r.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${o.sequence} lastMessage sequence:${r.lastMessage.lastSequence}`),o.sequence>r.lastMessage.lastSequence){const e=o.sequence+1;r.updateLastMessage(o),r.updateNextMessageSeq(e);const t=g.computeUnreadCount(r);r.updateUnreadCount(t),this._emitTopicUpdate(r)}}}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:o,TOPIC_MESSAGE_REVOKED:i,TOPIC_MESSAGE_MODIFIED:n,DESTROY:r}}=this._core,{InnerEventSubType:{TOPIC_AT_TIPS:c,TOPIC_LATEST_MESSAGE:a}}=e;e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(o,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(i,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,c,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,a,this._handleLatestMessage,this),e.unSubscribeInnerEvent(r,this._dispose,this)}};var T=new class{constructor(){this._name="TopicOperationHandler"}init(e){this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:o}}=e,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:r}}=t;t.subscribeInnerEvent(s,i,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,r,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(o,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleTopicTips(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){const{common:{getCurrentUserID:t}}=this._core,{GroupInfo:{GroupId:s,TopicId:o},MsgBody:{MsgMemberInfo:i=[]}}=e;for(let e=0;e<i.length;e++){const{User_Account:n,ShutupTime:r}=i[e];if(n===t()&&r>=0){g.updateSelfInfo({groupID:s,topicID:o,selfInfo:{muteTime:r}});break}}}_handleTopicProfileUpdated(e){const{GroupInfo:{GroupId:t,TopicId:s},MsgBody:{MsgTopicNewInfo:{CustomString:o,ShutupAll:i,TopicFaceUrl:n,TopicIntroduction:r,TopicName:c,TopicNotification:a}}}=e,u=g.getTopic(t,s);u&&(u.updateTopic({groupID:t,topicID:s,customData:o,muteAllMembers:i,avatar:n,introduction:r,topicName:c,notification:a}),g.emitTopicUpdate(u))}_handleTopicNotices(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const s=t[e],{MsgBody:{ReportType:o,TopicIdArray:c,MsgFlag:a},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:c}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:a}):this._emitTopicNotices(s)}}_handleTopicCreated(e){const{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){const{OuterEvent:t,notificationCenter:s}=this._core,{groupID:o,topicIDList:i=[]}=e;g.deleteTopicList(o,i),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){const{groupID:t,topicID:s,messageRemindType:o}=e;g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{messageRemindType:o}})}_handleTopicMessageReadSynced(e){const{GroupTips:t}=e,{utils:{isUndefined:s}}=this._core;t.forEach(e=>{const{GroupReadInfoArray:t}=e.MsgBody;s(t)||t.forEach(e=>{const{GroupId:t,TopicId:s,LastReadMsgSeq:o}=e;console.log(`${this._name}._handleTopicMessageReadSynced topicID:${s} lastReadSequence:${o}`),g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{readedSequence:o}}),g.clearGroupAtInfoList(t,s),g.markMessageAsRead(s,o),g.updateUnreadCount(t,s)})})}_emitTopicTips(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_TIP;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_GROUP;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_emitTopicNotices(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_SYS_NOTICE;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_SYSTEM,r.conversationID=o.CONV_SYSTEM;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:i,TOPIC_MESSAGE_READ_SYNC:n}}=e;e.unSubscribeInnerEvent(t,o,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,i,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function f(e,t,s,o){return new(s||(s=Promise))(function(i,n){function r(e){try{a(o.next(e))}catch(e){n(e)}}function c(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(r,c)}a((o=o.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return f(this,void 0,void 0,function*(){const{apiMap:{getGroupList:e}}=this._core,t=(yield e(!0)).filter(e=>!0===e.isSupportTopic);return console.log(`${this._name}.getJoinedCommunityList ok, count:${t.length}`),{code:0,data:{groupList:t}}})}};var v=new class{constructor(){this._name="CreateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(e){return f(this,void 0,void 0,function*(){const t="createTopicInCommunity",{utils:{isUndefined:s},common:{isTopic:o},helper:{ChatError:i}}=this._core,{topicID:n}=e;if(!s(n)&&!o(n)){const e=new i({functionName:t,code:p});throw console.error(`${this._name}.${t} fail:`,e),e}try{const s=yield function(e,t){return f(this,void 0,void 0,function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a}=e,u={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:u})})}(e,this._core),{TopicId:o=""}=s||{};return console.log(`${this._name}.${t} ok, topicID:${o}`),g.updateTopic([Object.assign(Object.assign({},e),{topicID:o})]),{code:0,data:{topicID:o}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}})}};var E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(e){return f(this,void 0,void 0,function*(){const t="deleteTopicFromCommunity",{groupID:s,topicIDList:o=[]}=e;try{const i=yield function(e,t){return f(this,void 0,void 0,function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:i})})}({groupID:s,topicIDList:o},this._core),{DestroyResultItem:n=[]}=i||{},r=[],c=[],a=[];return n.forEach(e=>{const{TopicId:t,ErrorCode:s,ErrorInfo:o}=e;0===s?(r.push({topicID:t}),a.push(t)):c.push({topicID:t,code:s,message:o})}),a.length>0&&g.deleteTopicList(e.groupID,a),console.log(`${this._name}.${t} ok, groupID:${s} success:${r.length} fail:${c.length}`),{code:0,data:{successTopicList:r,failureTopicList:c}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}})}};var S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{const{store:t}=this._core;return(t.get("cloudConfig")||{})[e]}}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return f(this,void 0,void 0,function*(){const{groupID:t,topicIDList:s=[]}=e,o=0===s.length;if(this._isFetchingFromCache(t,o)){const e=g.getTopicList(t,s);if(o||e.length===s.length)return console.log(`${this._name}.getTopicList groupID:${t} from cache, count:${e.length}`),{code:0,data:{successTopicList:e,failureTopicList:[]}}}return this._getTopicListFromServer({groupID:t,topicIDList:s},o)})}_isFetchingFromCache(e,t){const{hasFetchedAllTopics:s=!1,time:o=0}=g.getTopicRequestInfo(e);return!(!s&&(t||s))&&Date.now()-o<this._getCacheDuration()}_getCacheDuration(){const{utils:{isUndefined:e}}=this._core;let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10),t>=a&&t<=u)?1e3*t:1e3*c}_getTopicListFromServer(e,t){return f(this,void 0,void 0,function*(){const s="_getTopicListFromServer",{utils:{isEmpty:o}}=this._core,{groupID:i}=e;try{const n=yield function(e,t){return f(this,void 0,void 0,function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o,MemberInfoFilter:["NoUnreadSeqList"]};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:i})})}(e,this._core),{TopicAndSelfInfo:r=[]}=n||{},c=[],a=[],u=[];this._convertTopicInfoList(r).forEach(e=>{const{topic:t,selfInfo:s,code:o,message:i}=e,{topicID:n}=t;0===o?(c.push(Object.assign(Object.assign({},t),{selfInfo:s})),a.push(n)):u.push({topicID:n,code:o,message:i})}),g.updateTopic(c),g.updateTopicAtInfo(c),console.log(`${this._name}.${s} groupID:${i} from remote, success count:${a.length}, fail count:${u.length}`);let p=[];return o(a)||(g.setTopicRequestInfo(i,{hasFetchedAllTopics:t,time:Date.now()}),p=g.getTopicList(i,a)),{code:0,data:{successTopicList:p,failureTopicList:u}}}catch(e){throw console.error(`${this._name}.${s} failed, error:`,e),e}})}_convertTopicInfoList(e){const t=[];return e.forEach(e=>{const{TopicInfo:s={},SelfInfo:o={},ErrorCode:i,ErrorInfo:n}=e;t.push({code:i,message:n,topic:this._convertTopicInfo(s),selfInfo:this._convertSelfInfo(o)})}),t}_convertTopicInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{CreateTime:s,CustomString:o,FaceUrl:i,GroupId:n,LastMsgTime:r,NextMsgSeq:c,Introduction:a,Notification:u,ShutUpAllFlag:p,TopicId:l,TopicName:d,LastMsg:h,AtInfoList:g}=e;return{createTime:s,customData:o,avatar:i,groupID:n,lastMessageTime:r,nextMessageSeq:c,introduction:a,notification:u,muteAllMembers:p,topicID:l,topicName:d,lastMsg:this._convertLastMsg(h),groupAtInfoList:this._convertAtInfoList({groupID:n,topicID:l,atInfoList:g})}}_convertLastMsg(e){const{utils:{isUndefined:t},OuterConstant:s}=this._core;if(t(e))return;4===e.Event&&(e.MsgBody.MsgType=s.MSG_GRP_TIP);const{message:{messageHelper:o}}=this._core,i=o.parseServerGroupMessage(e),{From_AccountHeadurl:n,From_AccountNick:r,LastMsgSeq:c}=e;return Object.assign(Object.assign({},i),{avatar:n,nick:r,sequence:c})}_convertAtInfoList(e){const{groupID:t,topicID:s,atInfoList:o=[]}=e;if(0===o.length)return[];const i=[];return o.forEach(e=>{const{AtFlagList:o,AtMsgSeq:n,From_Account:r}=e;i.push({groupID:t,topicID:s,groupAtType:o,sequence:n,from:r})}),i}_convertSelfInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{InheritGroupReceiveOption:s,MsgFlag:o,MsgSeq:i,NoUnreadSeqList:n,ShutUpTime:r}=e;return{inheritGroupReceiveOption:s,messageRemindType:o,readedSequence:i,excludedUnreadSequenceList:n,muteTime:r}}};var D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(e){return f(this,void 0,void 0,function*(){const t="updateTopicProfile",{utils:{isUndefined:s}}=this._core,{groupID:o,topicID:i,muteAllMembers:n}=e;s(e.muteAllMembers)||(e.muteAllMembers=!0===n?"On":"Off");try{return yield function(e,t){return f(this,void 0,void 0,function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a,muteAllMembers:u}=e,p={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a,ShutUpAllMember:u};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:p})})}(e,this._core),console.log(`${this._name}.${t} ok, topicID:${i}`),g.updateTopic([e]),{code:0,data:{topic:g.getTopic(o,i)}}}catch(e){throw console.error(`${this._name}.${t} failed, topicID:${i} error:`,e),e}})}};var C=new class{constructor(){this._name="MessageReadHandler"}init(e){this._core=e;const{helper:t,common:{isTopic:s}}=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(e){return f(this,void 0,void 0,function*(){const{utils:{isEmpty:t},ssoLog:s,helper:{ChatError:o}}=this._core,{conversationID:i}=e,n={code:0,data:{}},r=_(i),c=m(r),a=g.getTopic(c,r),u=`${this._name}.setMessageRead topicID:${r} conversationID:${i} unreadCount:${(null==a?void 0:a.unreadCount)||0}`;if(n.successLog={message:u},!a)return n;if(t(a.groupAtInfoList)||this._deleteTopicAtTips(c,r),0===a.unreadCount)return n;const p=this._getLocalMessageMaxSequence(a);try{yield this._setTopicMessageRead(c,r,p)}catch(e){const{errorCode:t,errorInfo:s}=e;throw new o({functionName:"setMessageRead",code:t,message:s,moreMessage:u})}return Object.assign(Object.assign({},n),{successLog:{message:`${u} lastMessageSequence:${p}`}})})}_deleteTopicAtTips(e,t){return f(this,void 0,void 0,function*(){this._core.ssoLog.debug("_deleteTopicAtTips",`${this._name}._deleteTopicAtTips groupID:${e} topicID:${t}`);const s=g.getTopic(e,t);if(!s)return;const o=(null==s?void 0:s.groupAtInfoList)||[];if(0!==o.length)try{const{common:{getCurrentUserID:s},OuterConstant:{CONV_AT_ALL:i}}=this._core;let n=[...o];if(n=o.filter(e=>!e.atTypeArray.includes(i)),0===n.length)return void g.clearGroupAtInfoList(e,t);const r=n.map(e=>({From_Account:e.from,To_Account:s(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID}));yield function(e,t){return f(this,void 0,void 0,function*(){const{messageListToDelete:s}=e,o={DelMsgList:s};return t.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:o})})}({messageListToDelete:r},this._core),this._core.ssoLog.debug(`${this._name}._deleteTopicAtTips ok. count:${o.length}`),g.clearGroupAtInfoList(e,t)}catch(e){console.error(`${this._name}._deleteTopicAtTips fail:`,e)}})}_getLocalMessageMaxSequence(e){var t;const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,{topicID:i}=e,n=s.getLocalMessageList(`${o}${i}`),r=Math.max(...n.map(e=>e.sequence));let c=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return r>c&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${c} to ${r}`),c=r),c}_setTopicMessageRead(e,t,s){return f(this,void 0,void 0,function*(){try{yield function(e,t){return f(this,void 0,void 0,function*(){const{groupID:s,topicID:o,lastMessageSequence:i}=e,n={GroupId:s,TopicId:o,MsgReadedSeq:i};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:n})})}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(`${this._name}._setTopicMessageRead ok, lastReadSequence:${s}`),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)}catch(e){throw console.error(`${this._name}._setTopicMessageRead fail:`,e),e}})}};var L=new class{init(e){e.ssoLog.debug("TopicAction.init"),M.init(e),v.init(e),E.init(e),S.init(e),D.init(e),C.init(e)}};const A={required:!0,rules:["string"],allowEmpty:!1},y={createTopicInCommunity:{groupID:A,topicName:A},deleteTopicFromCommunity:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:A,topicID:A}},O={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};var R=new class{constructor(){this.name="Topic"}install(t){e.init(t),g.init(t),I.init(t),T.init(t),L.init(t),t.helper.registerValidateConfig({auth:O,params:y})}};export{R as default};
1
+ const e=new class{init(e){this.core=e}},t="@TOPIC#_",s=7,o=8,i=17,n=18,r=20,c=300,a=0,u=3600,p=3021,l=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],d=t=>{const{core:{common:{getMessagePreviewText:s}}}=e;return{lastTime:(null==t?void 0:t.time)||0,lastSequence:(null==t?void 0:t.sequence)||0,fromAccount:(null==t?void 0:t.from)||"",payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",messageForShow:s(null==t?void 0:t.type,null==t?void 0:t.payload),nick:(null==t?void 0:t.nick)||"",avatar:(null==t?void 0:t.avatar)||"",version:(null==t?void 0:t.version)||0,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,revoker:(null==t?void 0:t.revoker)||null}};class h{constructor(e){this.topicID="",this.topicName="",this.avatar="",this.introduction="",this.notification="",this.unreadCount=0,this.muteAllMembers=!1,this.customData="",this.groupAtInfoList=[],this.nextMessageSeq=0,this.lastMessage=d(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(const t in e)l.includes(t)&&("lastMessage"===t?this[t]=d(e[t]):"selfInfo"===t?this.updateSelfInfo(e[t]):this[t]="muteAllMembers"===t?1===e[t]:e[t])}updateUnreadCount(e=0){this.unreadCount=e}updateReadSequence(e=0,t=!1){t&&(this.selfInfo.readedSequence=e)}updateNextMessageSeq(e){this.nextMessageSeq=e}updateLastMessage(e){this.lastMessage=d(e)}updateGroupAtInfoList(t){const{core:{common:{updateGroupAtInfo:s}}}=e;s(t,this.groupAtInfoList)}updateTopic(t){const{core:{utils:{isUndefined:s},common:{deepMerge:o}}}=e;s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),o(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){const{core:{common:{deepMerge:s}}}=e;return s(this.selfInfo,t,[],[""])>0}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){var t;return e===(null===(t=this.lastMessage)||void 0===t?void 0:t.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var g=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){this._core=e;const{helper:t,constants:s,notificationCenter:o,InnerEvent:{LOGOUT:i,DESTROY:n}}=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),o.subscribeInnerEvent(i,this._reset,this),o.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_hasTopic(e,t){var s;return!!(null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t))}_setTopic(e,t,s){var o;null===(o=this._topicMap.get(e))||void 0===o||o.set(t,s)}_deleteTopic(e,t){var s;null===(s=this._topicMap.get(e))||void 0===s||s.delete(t)}getTopic(e,t){var s;return null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t)}updateTopic(e){e.forEach((e=>{const{groupID:t,topicID:s}=e;let o=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?(o=this.getTopic(t,s),null==o||o.updateTopic(e)):(this._getLastMessage(e),o=new h(e),this._setTopic(t,s,o));const i=this.computeUnreadCount(o);null==o||o.updateUnreadCount(i)}))}_getLastMessage(e){const{utils:{isEmpty:t,isUndefined:s,isArray:o}}=this._core;if(!s(e.lastMsg)){let s=null,i="";o(e.lastMsg.elements)?(s=e.lastMsg.elements[0].content,i=e.lastMsg.elements[0].type):(s=e.lastMsg.elements.content,i=e.lastMsg.elements.type);const n={payload:s,type:i,time:e.lastMsg.time,sequence:e.lastMsg.sequence,from:e.lastMsg.from,nick:e.lastMsg.nick,avatar:e.lastMsg.avatar,version:e.lastMsg.messageVersion,cloudCustomData:e.lastMsg.cloudCustomData,isRevoked:2===e.lastMsg.isPlaceMessage,revoker:t(e.lastMsg.revokerInfo)?null:e.lastMsg.revokerInfo.revoker};e.lastMessage=n}}computeUnreadCount(e){const{excludedUnreadSequenceList:t=[],readedSequence:s}=e.selfInfo;let o=e.nextMessageSeq-e.selfInfo.readedSequence-1;if(t.length>0){let i=0;t.forEach((t=>{t>s&&t<=e.nextMessageSeq-1&&(i+=1)})),i>=1&&(o-=i)}return o>0?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach((e=>{const{utils:{isEmpty:t}}=this._core,{groupAtInfoList:s}=e;t(s)||this.updateGroupAtInfoList(s)}))}updateGroupAtInfoList(e,t=!1){if(0===e.length)return;const{common:{getCurrentUserID:s},utils:{isUndefined:o,isEmpty:i}}=this._core,n=s();let r=null;e.forEach((e=>{var s;const{groupID:i,topicID:c,from:a}=e;a!==n&&(r=this.getTopic(i,c),r&&r.updateGroupAtInfoList(e),o(r)&&t&&(this.updateTopic([{groupID:i,topicID:c}]),null===(s=this.getTopic(i,c))||void 0===s||s.updateGroupAtInfoList(e)))})),i(r)||this.emitTopicUpdate(r)}getTopicList(e,t=[]){const s=this._topicMap.get(e),o=[];return s&&o.push(...s.values()),t.length>0?o.filter((e=>t.includes(e.topicID))):o}deleteTopicList(e,t=[]){this._topicMap.has(e)&&(t.forEach((t=>{this._hasTopic(e,t)&&this._deleteTopic(e,t)})),console.log(`${this._name}.deleteTopicList groupID:${e} topicID:${t.join(",")}`))}clearGroupAtInfoList(e,t){const{utils:{isEmpty:s}}=this._core,o=this.getTopic(e,t);s(null==o?void 0:o.groupAtInfoList)||(null==o||o.clearGroupAtInfoList(),console.log(`${this._name}.clearGroupAtInfoList topicID:${t}`))}markMessageAsRead(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,i=(null==s?void 0:s.getLocalMessageList(`${o}${e}`))||[];if(0===i.length)return;const{length:n}=i;for(let e=n-1;e>=0;e--){const s=i[e];if(!(t&&s.sequence>t)){if("in"===s.flow&&s.isRead)break;s.setIsRead(!0)}}}updateUnreadCount(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core;let i=!1;const n=this.getTopic(e,t);if(!n)return;const r=(null==s?void 0:s.getLocalMessageList(`${o}${t}`))||[],c=n.unreadCount,a=null==r?void 0:r.filter((e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted)).length;console.log(`${this._name}._updateUnreadCount topicID:${t} currentUnreadCount:${c} newUnreadCount:${a}`),c!==a&&(n.updateUnreadCount(a),i=!0),i&&this.emitTopicUpdate(n)}updateSelfInfo(e){const{groupID:t,topicID:s,selfInfo:o}=e,i=this.getTopic(t,s);if(i){const e=i.updateSelfInfo(o);console.log(`${this._name}.updateSelfInfo topicID:${s} info:${JSON.stringify(o)} isUpdated:${e}`),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){const{OuterEvent:t,notificationCenter:s}=this._core;s.emitOuterEvent(t.TOPIC_UPDATED,{name:t.TOPIC_UPDATED,data:e})}_resetTopicRequestInfo(){this._topicRequestInfo.clear()}_reset(){this._topicMap.clear(),this._topicRequestInfo.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};const _=(t="")=>{const{core:{OuterConstant:s}}=e;return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){this._core=e;const{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:r,TOPIC_MESSAGE_DELETED:c,TOPIC_MESSAGE_REVOKED:a,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=e,{InnerEventSubType:{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}}=s;t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleNewMessage,this),s.subscribeInnerEvent(c,this._handleMessageDeleted,this),s.subscribeInnerEvent(a,this._handleMessageRevoked,this),s.subscribeInnerEvent(u,this._handleMessageModified,this),s.subscribeInnerEvent(n,l,this._handleTopicAtTips,this),s.subscribeInnerEvent(n,d,this._handleLatestMessage,this),s.subscribeInnerEvent(p,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleUnreadSyncFinished(e){var t;const{constants:{WORKFLOW_STEP:s}}=this._core,{groupTipList:o=[]}=e.result[s.UNREAD_MESSAGE_SYNC]||{};for(let e=0;e<o.length;e++){const s=o[e];12===s.Event&&(null===(t=s.GroupTips[0].MsgBody)||void 0===t?void 0:t.TopicId)&&this._handleTopicAtTips(s)}}_handleNewMessage(e){const{conversationUpdateFieldList:t=[]}=e.result||{};if(0===t.length)return;const{common:{isTopic:s}}=this._core;if(!s(t[0].conversationID))return;const{common:{getCurrentUserID:o}}=this._core;t.forEach((e=>{const{conversationID:t="",lastMessage:s}=e,i=_(t),n=m(i),r=g.getTopic(n,i);if(r){const{from:e,sequence:t}=s,i=t+1,n=e===o();if(i>r.nextMessageSeq){r.updateNextMessageSeq(i),r.updateLastMessage(s),r.updateReadSequence(t,n);const e=n?0:g.computeUnreadCount(r);r.updateUnreadCount(e),this._emitTopicUpdate(r)}}}))}_handleMessageDeleted(e){const t=_(e),s=m(t),o=g.getTopic(s,t);if(console.log(`${this._name}._handleMessageDeleted, topicID:${t} hasTopic:${!!o}`),!o)return;const{message:{messageDataHandler:i},utils:{isEmpty:n}}=this._core,r=(null==i?void 0:i.getLocalMessageList(e))||[];let c={};for(let e=(r.length||0)-1;e>=0;e--)if(!r[e].isDeleted){c=r[e];break}let a=!1;o.lastMessage.lastSequence!==c.sequence&&(n(c)&&(c={}),o.updateLastMessage(c),a=!0),a&&(this._emitTopicUpdate(o),console.log(`${this._name}._handleMessageDeleted, topicID:${t} with lastMessage:${o.lastMessage}`))}_handleMessageRevoked(e){const{messageList:t=[]}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach((e=>{const{to:t,sequence:i,revoker:n}=e,r=m(t);if(s=g.getTopic(r,t),s){s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked(i)&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(n),o=!0);const{excludedUnreadSequenceList:e=[]}=s.selfInfo;e.push(i),s.updateSelfInfo({excludedUnreadSequenceList:e})}})),o&&this._emitTopicUpdate(s)}_handleMessageModified(e){const{conversationID:t,messageList:s}=e,o=_(t),i=m(o),n=g.getTopic(i,o);if(console.log(`${this._name}._handleMessageModified topicID:${o} hasTopic:${!!n}`),!n)return;const{lastMessage:r}=n;if(r){const e=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(r,e)){const{common:{getMessagePreviewText:t}}=this._core,{time:s,sequence:o,elements:i,cloudCustomData:c,messageVersion:a}=e;r.type=i[0].type,r.payload=i[0].content,r.messageForShow=t(r.type,r.payload),r.cloudCustomData=c,r.version=a,r.lastSequence=o,r.lastTime=s,this._emitTopicUpdate(n)}}}_shouldUpdateLastMessage(e,t){if(null===e.payload)return!0;const{sequence:s,messageVersion:o}=t;return e.lastSequence===s&&e.version!==o}_emitTopicUpdate(e){const{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){const{GroupTips:t=[]}=e,s=[];let o=!1;t.forEach((e=>{const{GroupAtTips:t,MsgBody:i,MsgRandom:n,ClientSeq:r}=e;let c={};t?c=this._convertAtInfo(t):i&&(c=this._convertAtInfo(i),o=!0),c.__random=n,c.__sequence=r,s.push(c)})),g.updateGroupAtInfoList(s,o)}_convertAtInfo(e){const{From_Account:t,GroupId:s,TopicId:o,MsgSeq:i,GroupAtType:n}=e;return{groupID:s,topicID:o,from:t,sequence:i,groupAtType:n}}_handleLatestMessage(e){const{utils:{isEmpty:t},message:{messageHelper:s,messageFactory:o}}=this._core,{NewMsg:i,NoUnreadSeqList:n}=e||{};if(!t(i)){const{GroupInfo:{TopicId:e}}=i,t=s.parseServerGroupMessage(i),r=o.createMessage(t);r.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:r,excludedUnreadSequenceList:n})}}_updateUnreadCountAndLastMessage(e){const{utils:{isUndefined:t}}=this._core,{topicID:s,message:o,excludedUnreadSequenceList:i}=e,n=m(s),r=g.getTopic(n,s);if(r){let e=r.selfInfo.excludedUnreadSequenceList||[];if(t(i)||(e=i),o._isExcludedFromUnreadCount&&e.push(o.sequence),r.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${o.sequence} lastMessage sequence:${r.lastMessage.lastSequence}`),o.sequence>r.lastMessage.lastSequence){const e=o.sequence+1;r.updateLastMessage(o),r.updateNextMessageSeq(e);const t=g.computeUnreadCount(r);r.updateUnreadCount(t),this._emitTopicUpdate(r)}}}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:o,TOPIC_MESSAGE_REVOKED:i,TOPIC_MESSAGE_MODIFIED:n,DESTROY:r}}=this._core,{InnerEventSubType:{TOPIC_AT_TIPS:c,TOPIC_LATEST_MESSAGE:a}}=e;e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(o,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(i,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,c,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,a,this._handleLatestMessage,this),e.unSubscribeInnerEvent(r,this._dispose,this)}};var T=new class{constructor(){this._name="TopicOperationHandler"}init(e){this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:o}}=e,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:r}}=t;t.subscribeInnerEvent(s,i,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,r,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(o,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleTopicTips(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){const{common:{getCurrentUserID:t}}=this._core,{GroupInfo:{GroupId:s,TopicId:o},MsgBody:{MsgMemberInfo:i=[]}}=e;for(let e=0;e<i.length;e++){const{User_Account:n,ShutupTime:r}=i[e];if(n===t()&&r>=0){g.updateSelfInfo({groupID:s,topicID:o,selfInfo:{muteTime:r}});break}}}_handleTopicProfileUpdated(e){const{GroupInfo:{GroupId:t,TopicId:s},MsgBody:{MsgTopicNewInfo:{CustomString:o,ShutupAll:i,TopicFaceUrl:n,TopicIntroduction:r,TopicName:c,TopicNotification:a}}}=e,u=g.getTopic(t,s);u&&(u.updateTopic({groupID:t,topicID:s,customData:o,muteAllMembers:i,avatar:n,introduction:r,topicName:c,notification:a}),g.emitTopicUpdate(u))}_handleTopicNotices(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const s=t[e],{MsgBody:{ReportType:o,TopicIdArray:c,MsgFlag:a},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:c}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:a}):this._emitTopicNotices(s)}}_handleTopicCreated(e){const{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){const{OuterEvent:t,notificationCenter:s}=this._core,{groupID:o,topicIDList:i=[]}=e;g.deleteTopicList(o,i),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){const{groupID:t,topicID:s,messageRemindType:o}=e;g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{messageRemindType:o}})}_handleTopicMessageReadSynced(e){const{GroupTips:t}=e,{utils:{isUndefined:s}}=this._core;t.forEach((e=>{const{GroupReadInfoArray:t}=e.MsgBody;s(t)||t.forEach((e=>{const{GroupId:t,TopicId:s,LastReadMsgSeq:o}=e;console.log(`${this._name}._handleTopicMessageReadSynced topicID:${s} lastReadSequence:${o}`),g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{readedSequence:o}}),g.clearGroupAtInfoList(t,s),g.markMessageAsRead(s,o),g.updateUnreadCount(t,s)}))}))}_emitTopicTips(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_TIP;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_GROUP;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_emitTopicNotices(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_SYS_NOTICE;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_SYSTEM,r.conversationID=o.CONV_SYSTEM;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:i,TOPIC_MESSAGE_READ_SYNC:n}}=e;e.unSubscribeInnerEvent(t,o,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,i,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function f(e,t,s,o){return new(s||(s=Promise))((function(i,n){function r(e){try{a(o.next(e))}catch(e){n(e)}}function c(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,c)}a((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return f(this,void 0,void 0,(function*(){const{apiMap:{getGroupList:e}}=this._core,t=(yield e(!0)).filter((e=>!0===e.isSupportTopic));return console.log(`${this._name}.getJoinedCommunityList ok, count:${t.length}`),{code:0,data:{groupList:t}}}))}};var v=new class{constructor(){this._name="CreateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(e){return f(this,void 0,void 0,(function*(){const t="createTopicInCommunity",{utils:{isUndefined:s},common:{isTopic:o},helper:{ChatError:i}}=this._core,{topicID:n}=e;if(!s(n)&&!o(n)){const e=new i({functionName:t,code:p});throw console.error(`${this._name}.${t} fail:`,e),e}try{const s=yield function(e,t){return f(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a}=e,u={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:u})}))}(e,this._core),{TopicId:o=""}=s||{};return console.log(`${this._name}.${t} ok, topicID:${o}`),g.updateTopic([Object.assign(Object.assign({},e),{topicID:o})]),{code:0,data:{topicID:o}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(e){return f(this,void 0,void 0,(function*(){const t="deleteTopicFromCommunity",{groupID:s,topicIDList:o=[]}=e;try{const i=yield function(e,t){return f(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:i})}))}({groupID:s,topicIDList:o},this._core),{DestroyResultItem:n=[]}=i||{},r=[],c=[],a=[];return n.forEach((e=>{const{TopicId:t,ErrorCode:s,ErrorInfo:o}=e;0===s?(r.push({topicID:t}),a.push(t)):c.push({topicID:t,code:s,message:o})})),a.length>0&&g.deleteTopicList(e.groupID,a),console.log(`${this._name}.${t} ok, groupID:${s} success:${r.length} fail:${c.length}`),{code:0,data:{successTopicList:r,failureTopicList:c}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{const{store:t}=this._core;return(t.get("cloudConfig")||{})[e]}}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return f(this,void 0,void 0,(function*(){const{groupID:t,topicIDList:s=[]}=e,o=0===s.length;if(this._isFetchingFromCache(t,o)){const e=g.getTopicList(t,s);if(o||e.length===s.length)return console.log(`${this._name}.getTopicList groupID:${t} from cache, count:${e.length}`),{code:0,data:{successTopicList:e,failureTopicList:[]}}}return this._getTopicListFromServer({groupID:t,topicIDList:s},o)}))}_isFetchingFromCache(e,t){const{hasFetchedAllTopics:s=!1,time:o=0}=g.getTopicRequestInfo(e);return!(!s&&(t||s))&&Date.now()-o<this._getCacheDuration()}_getCacheDuration(){const{utils:{isUndefined:e}}=this._core;let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10),t>=a&&t<=u)?1e3*t:1e3*c}_getTopicListFromServer(e,t){return f(this,void 0,void 0,(function*(){const s="_getTopicListFromServer",{utils:{isEmpty:o}}=this._core,{groupID:i}=e;try{const n=yield function(e,t){return f(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o,MemberInfoFilter:["NoUnreadSeqList"]};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:i})}))}(e,this._core),{TopicAndSelfInfo:r=[]}=n||{},c=[],a=[],u=[];this._convertTopicInfoList(r).forEach((e=>{const{topic:t,selfInfo:s,code:o,message:i}=e,{topicID:n}=t;0===o?(c.push(Object.assign(Object.assign({},t),{selfInfo:s})),a.push(n)):u.push({topicID:n,code:o,message:i})})),g.updateTopic(c),g.updateTopicAtInfo(c),console.log(`${this._name}.${s} groupID:${i} from remote, success count:${a.length}, fail count:${u.length}`);let p=[];return o(a)||(g.setTopicRequestInfo(i,{hasFetchedAllTopics:t,time:Date.now()}),p=g.getTopicList(i,a)),{code:0,data:{successTopicList:p,failureTopicList:u}}}catch(e){throw console.error(`${this._name}.${s} failed, error:`,e),e}}))}_convertTopicInfoList(e){const t=[];return e.forEach((e=>{const{TopicInfo:s={},SelfInfo:o={},ErrorCode:i,ErrorInfo:n}=e;t.push({code:i,message:n,topic:this._convertTopicInfo(s),selfInfo:this._convertSelfInfo(o)})})),t}_convertTopicInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{CreateTime:s,CustomString:o,FaceUrl:i,GroupId:n,LastMsgTime:r,NextMsgSeq:c,Introduction:a,Notification:u,ShutUpAllFlag:p,TopicId:l,TopicName:d,LastMsg:h,AtInfoList:g}=e;return{createTime:s,customData:o,avatar:i,groupID:n,lastMessageTime:r,nextMessageSeq:c,introduction:a,notification:u,muteAllMembers:p,topicID:l,topicName:d,lastMsg:this._convertLastMsg(h),groupAtInfoList:this._convertAtInfoList({groupID:n,topicID:l,atInfoList:g})}}_convertLastMsg(e){const{utils:{isUndefined:t},OuterConstant:s}=this._core;if(t(e))return;4===e.Event&&(e.MsgBody.MsgType=s.MSG_GRP_TIP);const{message:{messageHelper:o}}=this._core,i=o.parseServerGroupMessage(e),{From_AccountHeadurl:n,From_AccountNick:r,LastMsgSeq:c}=e;return Object.assign(Object.assign({},i),{avatar:n,nick:r,sequence:c})}_convertAtInfoList(e){const{groupID:t,topicID:s,atInfoList:o=[]}=e;if(0===o.length)return[];const i=[];return o.forEach((e=>{const{AtFlagList:o,AtMsgSeq:n,From_Account:r}=e;i.push({groupID:t,topicID:s,groupAtType:o,sequence:n,from:r})})),i}_convertSelfInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{InheritGroupReceiveOption:s,MsgFlag:o,MsgSeq:i,NoUnreadSeqList:n,ShutUpTime:r}=e;return{inheritGroupReceiveOption:s,messageRemindType:o,readedSequence:i,excludedUnreadSequenceList:n,muteTime:r}}};var D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(e){return f(this,void 0,void 0,(function*(){const t="updateTopicProfile",{utils:{isUndefined:s}}=this._core,{groupID:o,topicID:i,muteAllMembers:n}=e;s(e.muteAllMembers)||(e.muteAllMembers=!0===n?"On":"Off");try{return yield function(e,t){return f(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a,muteAllMembers:u}=e,p={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a,ShutUpAllMember:u};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:p})}))}(e,this._core),console.log(`${this._name}.${t} ok, topicID:${i}`),g.updateTopic([e]),{code:0,data:{topic:g.getTopic(o,i)}}}catch(e){throw console.error(`${this._name}.${t} failed, topicID:${i} error:`,e),e}}))}};var C=new class{constructor(){this._name="MessageReadHandler"}init(e){this._core=e;const{helper:t,common:{isTopic:s}}=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(e){return f(this,void 0,void 0,(function*(){const{utils:{isEmpty:t},ssoLog:s,helper:{ChatError:o}}=this._core,{conversationID:i}=e,n={code:0,data:{}},r=_(i),c=m(r),a=g.getTopic(c,r),u=`${this._name}.setMessageRead topicID:${r} conversationID:${i} unreadCount:${(null==a?void 0:a.unreadCount)||0}`;if(n.successLog={message:u},!a)return n;if(t(a.groupAtInfoList)||this._deleteTopicAtTips(c,r),0===a.unreadCount)return n;const p=this._getLocalMessageMaxSequence(a);try{yield this._setTopicMessageRead(c,r,p)}catch(e){const{errorCode:t,errorInfo:s}=e;throw new o({functionName:"setMessageRead",code:t,message:s,moreMessage:u})}return Object.assign(Object.assign({},n),{successLog:{message:`${u} lastMessageSequence:${p}`}})}))}_deleteTopicAtTips(e,t){return f(this,void 0,void 0,(function*(){this._core.ssoLog.debug("_deleteTopicAtTips",`${this._name}._deleteTopicAtTips groupID:${e} topicID:${t}`);const s=g.getTopic(e,t);if(!s)return;const o=(null==s?void 0:s.groupAtInfoList)||[];if(0!==o.length)try{const{common:{getCurrentUserID:s},OuterConstant:{CONV_AT_ALL:i}}=this._core;let n=[...o];if(n=o.filter((e=>!e.atTypeArray.includes(i))),0===n.length)return void g.clearGroupAtInfoList(e,t);const r=n.map((e=>({From_Account:e.from,To_Account:s(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID})));yield function(e,t){return f(this,void 0,void 0,(function*(){const{messageListToDelete:s}=e,o={DelMsgList:s};return t.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:o})}))}({messageListToDelete:r},this._core),this._core.ssoLog.debug(`${this._name}._deleteTopicAtTips ok. count:${o.length}`),g.clearGroupAtInfoList(e,t)}catch(e){console.error(`${this._name}._deleteTopicAtTips fail:`,e)}}))}_getLocalMessageMaxSequence(e){var t;const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,{topicID:i}=e,n=s.getLocalMessageList(`${o}${i}`),r=Math.max(...n.map((e=>e.sequence)));let c=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return r>c&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${c} to ${r}`),c=r),c}_setTopicMessageRead(e,t,s){return f(this,void 0,void 0,(function*(){try{yield function(e,t){return f(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,lastMessageSequence:i}=e,n={GroupId:s,TopicId:o,MsgReadedSeq:i};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:n})}))}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(`${this._name}._setTopicMessageRead ok, lastReadSequence:${s}`),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)}catch(e){throw console.error(`${this._name}._setTopicMessageRead fail:`,e),e}}))}};var L=new class{init(e){e.ssoLog.debug("TopicAction.init"),M.init(e),v.init(e),E.init(e),S.init(e),D.init(e),C.init(e)}};const A={required:!0,rules:["string"],allowEmpty:!1},y={createTopicInCommunity:{groupID:A,topicName:A},deleteTopicFromCommunity:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:A,topicID:A}},O={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};var R=new class{constructor(){this.name="Topic"}install(t){e.init(t),g.init(t),I.init(t),T.init(t),L.init(t),t.helper.registerValidateConfig({auth:O,params:y})}};export{R as default};
package/plugins/topic.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).TencentCloudChatTopic=t()}(this,function(){"use strict";const e=new class{init(e){this.core=e}},t="@TOPIC#_",s=7,o=8,i=17,n=18,r=20,c=300,a=0,u=3600,p=3021,l=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],d=t=>{const{core:{common:{getMessagePreviewText:s}}}=e;return{lastTime:(null==t?void 0:t.time)||0,lastSequence:(null==t?void 0:t.sequence)||0,fromAccount:(null==t?void 0:t.from)||"",payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",messageForShow:s(null==t?void 0:t.type,null==t?void 0:t.payload),nick:(null==t?void 0:t.nick)||"",avatar:(null==t?void 0:t.avatar)||"",version:(null==t?void 0:t.version)||0,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,revoker:(null==t?void 0:t.revoker)||null}};class h{constructor(e){this.topicID="",this.topicName="",this.avatar="",this.introduction="",this.notification="",this.unreadCount=0,this.muteAllMembers=!1,this.customData="",this.groupAtInfoList=[],this.nextMessageSeq=0,this.lastMessage=d(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(const t in e)l.includes(t)&&("lastMessage"===t?this[t]=d(e[t]):"selfInfo"===t?this.updateSelfInfo(e[t]):this[t]="muteAllMembers"===t?1===e[t]:e[t])}updateUnreadCount(e=0){this.unreadCount=e}updateReadSequence(e=0,t=!1){t&&(this.selfInfo.readedSequence=e)}updateNextMessageSeq(e){this.nextMessageSeq=e}updateLastMessage(e){this.lastMessage=d(e)}updateGroupAtInfoList(t){const{core:{common:{updateGroupAtInfo:s}}}=e;s(t,this.groupAtInfoList)}updateTopic(t){const{core:{utils:{isUndefined:s},common:{deepMerge:o}}}=e;s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),o(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){const{core:{common:{deepMerge:s}}}=e;return s(this.selfInfo,t,[],[""])>0}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){var t;return e===(null===(t=this.lastMessage)||void 0===t?void 0:t.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var g=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){this._core=e;const{helper:t,constants:s,notificationCenter:o,InnerEvent:{LOGOUT:i,DESTROY:n}}=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),o.subscribeInnerEvent(i,this._reset,this),o.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_hasTopic(e,t){var s;return!!(null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t))}_setTopic(e,t,s){var o;null===(o=this._topicMap.get(e))||void 0===o||o.set(t,s)}_deleteTopic(e,t){var s;null===(s=this._topicMap.get(e))||void 0===s||s.delete(t)}getTopic(e,t){var s;return null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t)}updateTopic(e){e.forEach(e=>{const{groupID:t,topicID:s}=e;let o=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?(o=this.getTopic(t,s),null==o||o.updateTopic(e)):(this._getLastMessage(e),o=new h(e),this._setTopic(t,s,o));const i=this.computeUnreadCount(o);null==o||o.updateUnreadCount(i)})}_getLastMessage(e){const{utils:{isEmpty:t,isUndefined:s,isArray:o}}=this._core;if(!s(e.lastMsg)){let s=null,i="";o(e.lastMsg.elements)?(s=e.lastMsg.elements[0].content,i=e.lastMsg.elements[0].type):(s=e.lastMsg.elements.content,i=e.lastMsg.elements.type);const n={payload:s,type:i,time:e.lastMsg.time,sequence:e.lastMsg.sequence,from:e.lastMsg.from,nick:e.lastMsg.nick,avatar:e.lastMsg.avatar,version:e.lastMsg.messageVersion,cloudCustomData:e.lastMsg.cloudCustomData,isRevoked:2===e.lastMsg.isPlaceMessage,revoker:t(e.lastMsg.revokerInfo)?null:e.lastMsg.revokerInfo.revoker};e.lastMessage=n}}computeUnreadCount(e){const{excludedUnreadSequenceList:t=[],readedSequence:s}=e.selfInfo;let o=e.nextMessageSeq-e.selfInfo.readedSequence-1;if(t.length>0){let i=0;t.forEach(t=>{t>s&&t<=e.nextMessageSeq-1&&(i+=1)}),i>=1&&(o-=i)}return o>0?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach(e=>{const{utils:{isEmpty:t}}=this._core,{groupAtInfoList:s}=e;t(s)||this.updateGroupAtInfoList(s)})}updateGroupAtInfoList(e,t=!1){if(0===e.length)return;const{common:{getCurrentUserID:s},utils:{isUndefined:o,isEmpty:i}}=this._core,n=s();let r=null;e.forEach(e=>{var s;const{groupID:i,topicID:c,from:a}=e;a!==n&&(r=this.getTopic(i,c),r&&r.updateGroupAtInfoList(e),o(r)&&t&&(this.updateTopic([{groupID:i,topicID:c}]),null===(s=this.getTopic(i,c))||void 0===s||s.updateGroupAtInfoList(e)))}),i(r)||this.emitTopicUpdate(r)}getTopicList(e,t=[]){const s=this._topicMap.get(e),o=[];return s&&o.push(...s.values()),t.length>0?o.filter(e=>t.includes(e.topicID)):o}deleteTopicList(e,t=[]){this._topicMap.has(e)&&(t.forEach(t=>{this._hasTopic(e,t)&&this._deleteTopic(e,t)}),console.log(`${this._name}.deleteTopicList groupID:${e} topicID:${t.join(",")}`))}clearGroupAtInfoList(e,t){const{utils:{isEmpty:s}}=this._core,o=this.getTopic(e,t);s(null==o?void 0:o.groupAtInfoList)||(null==o||o.clearGroupAtInfoList(),console.log(`${this._name}.clearGroupAtInfoList topicID:${t}`))}markMessageAsRead(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,i=(null==s?void 0:s.getLocalMessageList(`${o}${e}`))||[];if(0===i.length)return;const{length:n}=i;for(let e=n-1;e>=0;e--){const s=i[e];if(!(t&&s.sequence>t)){if("in"===s.flow&&s.isRead)break;s.setIsRead(!0)}}}updateUnreadCount(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core;let i=!1;const n=this.getTopic(e,t);if(!n)return;const r=(null==s?void 0:s.getLocalMessageList(`${o}${t}`))||[],c=n.unreadCount,a=null==r?void 0:r.filter(e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted).length;console.log(`${this._name}._updateUnreadCount topicID:${t} currentUnreadCount:${c} newUnreadCount:${a}`),c!==a&&(n.updateUnreadCount(a),i=!0),i&&this.emitTopicUpdate(n)}updateSelfInfo(e){const{groupID:t,topicID:s,selfInfo:o}=e,i=this.getTopic(t,s);if(i){const e=i.updateSelfInfo(o);console.log(`${this._name}.updateSelfInfo topicID:${s} info:${JSON.stringify(o)} isUpdated:${e}`),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){const{OuterEvent:t,notificationCenter:s}=this._core;s.emitOuterEvent(t.TOPIC_UPDATED,{name:t.TOPIC_UPDATED,data:e})}_resetTopicRequestInfo(){this._topicRequestInfo.clear()}_reset(){this._topicMap.clear(),this._topicRequestInfo.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};const _=(t="")=>{const{core:{OuterConstant:s}}=e;return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){this._core=e;const{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:r,TOPIC_MESSAGE_DELETED:c,TOPIC_MESSAGE_REVOKED:a,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=e,{InnerEventSubType:{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}}=s;t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleNewMessage,this),s.subscribeInnerEvent(c,this._handleMessageDeleted,this),s.subscribeInnerEvent(a,this._handleMessageRevoked,this),s.subscribeInnerEvent(u,this._handleMessageModified,this),s.subscribeInnerEvent(n,l,this._handleTopicAtTips,this),s.subscribeInnerEvent(n,d,this._handleLatestMessage,this),s.subscribeInnerEvent(p,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleUnreadSyncFinished(e){var t;const{constants:{WORKFLOW_STEP:s}}=this._core,{groupTipList:o=[]}=e.result[s.UNREAD_MESSAGE_SYNC]||{};for(let e=0;e<o.length;e++){const s=o[e];12===s.Event&&(null===(t=s.GroupTips[0].MsgBody)||void 0===t?void 0:t.TopicId)&&this._handleTopicAtTips(s)}}_handleNewMessage(e){const{conversationUpdateFieldList:t=[]}=e.result||{};if(0===t.length)return;const{common:{isTopic:s}}=this._core;if(!s(t[0].conversationID))return;const{common:{getCurrentUserID:o}}=this._core;t.forEach(e=>{const{conversationID:t="",lastMessage:s}=e,i=_(t),n=m(i),r=g.getTopic(n,i);if(r){const{from:e,sequence:t}=s,i=t+1,n=e===o();if(i>r.nextMessageSeq){r.updateNextMessageSeq(i),r.updateLastMessage(s),r.updateReadSequence(t,n);const e=n?0:g.computeUnreadCount(r);r.updateUnreadCount(e),this._emitTopicUpdate(r)}}})}_handleMessageDeleted(e){const t=_(e),s=m(t),o=g.getTopic(s,t);if(console.log(`${this._name}._handleMessageDeleted, topicID:${t} hasTopic:${!!o}`),!o)return;const{message:{messageDataHandler:i},utils:{isEmpty:n}}=this._core,r=(null==i?void 0:i.getLocalMessageList(e))||[];let c={};for(let e=(r.length||0)-1;e>=0;e--)if(!r[e].isDeleted){c=r[e];break}let a=!1;o.lastMessage.lastSequence!==c.sequence&&(n(c)&&(c={}),o.updateLastMessage(c),a=!0),a&&(this._emitTopicUpdate(o),console.log(`${this._name}._handleMessageDeleted, topicID:${t} with lastMessage:${o.lastMessage}`))}_handleMessageRevoked(e){const{messageList:t=[]}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach(e=>{const{to:t,sequence:i,revoker:n}=e,r=m(t);if(s=g.getTopic(r,t),s){s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked(i)&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(n),o=!0);const{excludedUnreadSequenceList:e=[]}=s.selfInfo;e.push(i),s.updateSelfInfo({excludedUnreadSequenceList:e})}}),o&&this._emitTopicUpdate(s)}_handleMessageModified(e){const{conversationID:t,messageList:s}=e,o=_(t),i=m(o),n=g.getTopic(i,o);if(console.log(`${this._name}._handleMessageModified topicID:${o} hasTopic:${!!n}`),!n)return;const{lastMessage:r}=n;if(r){const e=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(r,e)){const{common:{getMessagePreviewText:t}}=this._core,{time:s,sequence:o,elements:i,cloudCustomData:c,messageVersion:a}=e;r.type=i[0].type,r.payload=i[0].content,r.messageForShow=t(r.type,r.payload),r.cloudCustomData=c,r.version=a,r.lastSequence=o,r.lastTime=s,this._emitTopicUpdate(n)}}}_shouldUpdateLastMessage(e,t){if(null===e.payload)return!0;const{sequence:s,messageVersion:o}=t;return e.lastSequence===s&&e.version!==o}_emitTopicUpdate(e){const{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){const{GroupTips:t=[]}=e,s=[];let o=!1;t.forEach(e=>{const{GroupAtTips:t,MsgBody:i,MsgRandom:n,ClientSeq:r}=e;let c={};t?c=this._convertAtInfo(t):i&&(c=this._convertAtInfo(i),o=!0),c.__random=n,c.__sequence=r,s.push(c)}),g.updateGroupAtInfoList(s,o)}_convertAtInfo(e){const{From_Account:t,GroupId:s,TopicId:o,MsgSeq:i,GroupAtType:n}=e;return{groupID:s,topicID:o,from:t,sequence:i,groupAtType:n}}_handleLatestMessage(e){const{utils:{isEmpty:t},message:{messageHelper:s,messageFactory:o}}=this._core,{NewMsg:i,NoUnreadSeqList:n}=e||{};if(!t(i)){const{GroupInfo:{TopicId:e}}=i,t=s.parseServerGroupMessage(i),r=o.createMessage(t);r.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:r,excludedUnreadSequenceList:n})}}_updateUnreadCountAndLastMessage(e){const{utils:{isUndefined:t}}=this._core,{topicID:s,message:o,excludedUnreadSequenceList:i}=e,n=m(s),r=g.getTopic(n,s);if(r){let e=r.selfInfo.excludedUnreadSequenceList||[];if(t(i)||(e=i),o._isExcludedFromUnreadCount&&e.push(o.sequence),r.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${o.sequence} lastMessage sequence:${r.lastMessage.lastSequence}`),o.sequence>r.lastMessage.lastSequence){const e=o.sequence+1;r.updateLastMessage(o),r.updateNextMessageSeq(e);const t=g.computeUnreadCount(r);r.updateUnreadCount(t),this._emitTopicUpdate(r)}}}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:o,TOPIC_MESSAGE_REVOKED:i,TOPIC_MESSAGE_MODIFIED:n,DESTROY:r}}=this._core,{InnerEventSubType:{TOPIC_AT_TIPS:c,TOPIC_LATEST_MESSAGE:a}}=e;e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(o,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(i,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,c,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,a,this._handleLatestMessage,this),e.unSubscribeInnerEvent(r,this._dispose,this)}};var f=new class{constructor(){this._name="TopicOperationHandler"}init(e){this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:o}}=e,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:r}}=t;t.subscribeInnerEvent(s,i,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,r,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(o,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleTopicTips(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){const{common:{getCurrentUserID:t}}=this._core,{GroupInfo:{GroupId:s,TopicId:o},MsgBody:{MsgMemberInfo:i=[]}}=e;for(let e=0;e<i.length;e++){const{User_Account:n,ShutupTime:r}=i[e];if(n===t()&&r>=0){g.updateSelfInfo({groupID:s,topicID:o,selfInfo:{muteTime:r}});break}}}_handleTopicProfileUpdated(e){const{GroupInfo:{GroupId:t,TopicId:s},MsgBody:{MsgTopicNewInfo:{CustomString:o,ShutupAll:i,TopicFaceUrl:n,TopicIntroduction:r,TopicName:c,TopicNotification:a}}}=e,u=g.getTopic(t,s);u&&(u.updateTopic({groupID:t,topicID:s,customData:o,muteAllMembers:i,avatar:n,introduction:r,topicName:c,notification:a}),g.emitTopicUpdate(u))}_handleTopicNotices(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const s=t[e],{MsgBody:{ReportType:o,TopicIdArray:c,MsgFlag:a},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:c}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:a}):this._emitTopicNotices(s)}}_handleTopicCreated(e){const{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){const{OuterEvent:t,notificationCenter:s}=this._core,{groupID:o,topicIDList:i=[]}=e;g.deleteTopicList(o,i),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){const{groupID:t,topicID:s,messageRemindType:o}=e;g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{messageRemindType:o}})}_handleTopicMessageReadSynced(e){const{GroupTips:t}=e,{utils:{isUndefined:s}}=this._core;t.forEach(e=>{const{GroupReadInfoArray:t}=e.MsgBody;s(t)||t.forEach(e=>{const{GroupId:t,TopicId:s,LastReadMsgSeq:o}=e;console.log(`${this._name}._handleTopicMessageReadSynced topicID:${s} lastReadSequence:${o}`),g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{readedSequence:o}}),g.clearGroupAtInfoList(t,s),g.markMessageAsRead(s,o),g.updateUnreadCount(t,s)})})}_emitTopicTips(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_TIP;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_GROUP;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_emitTopicNotices(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_SYS_NOTICE;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_SYSTEM,r.conversationID=o.CONV_SYSTEM;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:i,TOPIC_MESSAGE_READ_SYNC:n}}=e;e.unSubscribeInnerEvent(t,o,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,i,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function T(e,t,s,o){return new(s||(s=Promise))(function(i,n){function r(e){try{a(o.next(e))}catch(e){n(e)}}function c(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s(function(e){e(t)})).then(r,c)}a((o=o.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return T(this,void 0,void 0,function*(){const{apiMap:{getGroupList:e}}=this._core,t=(yield e(!0)).filter(e=>!0===e.isSupportTopic);return console.log(`${this._name}.getJoinedCommunityList ok, count:${t.length}`),{code:0,data:{groupList:t}}})}};var v=new class{constructor(){this._name="CreateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(e){return T(this,void 0,void 0,function*(){const t="createTopicInCommunity",{utils:{isUndefined:s},common:{isTopic:o},helper:{ChatError:i}}=this._core,{topicID:n}=e;if(!s(n)&&!o(n)){const e=new i({functionName:t,code:p});throw console.error(`${this._name}.${t} fail:`,e),e}try{const s=yield function(e,t){return T(this,void 0,void 0,function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a}=e,u={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:u})})}(e,this._core),{TopicId:o=""}=s||{};return console.log(`${this._name}.${t} ok, topicID:${o}`),g.updateTopic([Object.assign(Object.assign({},e),{topicID:o})]),{code:0,data:{topicID:o}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}})}};var E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(e){return T(this,void 0,void 0,function*(){const t="deleteTopicFromCommunity",{groupID:s,topicIDList:o=[]}=e;try{const i=yield function(e,t){return T(this,void 0,void 0,function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:i})})}({groupID:s,topicIDList:o},this._core),{DestroyResultItem:n=[]}=i||{},r=[],c=[],a=[];return n.forEach(e=>{const{TopicId:t,ErrorCode:s,ErrorInfo:o}=e;0===s?(r.push({topicID:t}),a.push(t)):c.push({topicID:t,code:s,message:o})}),a.length>0&&g.deleteTopicList(e.groupID,a),console.log(`${this._name}.${t} ok, groupID:${s} success:${r.length} fail:${c.length}`),{code:0,data:{successTopicList:r,failureTopicList:c}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}})}};var S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{const{store:t}=this._core;return(t.get("cloudConfig")||{})[e]}}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return T(this,void 0,void 0,function*(){const{groupID:t,topicIDList:s=[]}=e,o=0===s.length;if(this._isFetchingFromCache(t,o)){const e=g.getTopicList(t,s);if(o||e.length===s.length)return console.log(`${this._name}.getTopicList groupID:${t} from cache, count:${e.length}`),{code:0,data:{successTopicList:e,failureTopicList:[]}}}return this._getTopicListFromServer({groupID:t,topicIDList:s},o)})}_isFetchingFromCache(e,t){const{hasFetchedAllTopics:s=!1,time:o=0}=g.getTopicRequestInfo(e);return!(!s&&(t||s))&&Date.now()-o<this._getCacheDuration()}_getCacheDuration(){const{utils:{isUndefined:e}}=this._core;let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10),t>=a&&t<=u)?1e3*t:1e3*c}_getTopicListFromServer(e,t){return T(this,void 0,void 0,function*(){const s="_getTopicListFromServer",{utils:{isEmpty:o}}=this._core,{groupID:i}=e;try{const n=yield function(e,t){return T(this,void 0,void 0,function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o,MemberInfoFilter:["NoUnreadSeqList"]};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:i})})}(e,this._core),{TopicAndSelfInfo:r=[]}=n||{},c=[],a=[],u=[];this._convertTopicInfoList(r).forEach(e=>{const{topic:t,selfInfo:s,code:o,message:i}=e,{topicID:n}=t;0===o?(c.push(Object.assign(Object.assign({},t),{selfInfo:s})),a.push(n)):u.push({topicID:n,code:o,message:i})}),g.updateTopic(c),g.updateTopicAtInfo(c),console.log(`${this._name}.${s} groupID:${i} from remote, success count:${a.length}, fail count:${u.length}`);let p=[];return o(a)||(g.setTopicRequestInfo(i,{hasFetchedAllTopics:t,time:Date.now()}),p=g.getTopicList(i,a)),{code:0,data:{successTopicList:p,failureTopicList:u}}}catch(e){throw console.error(`${this._name}.${s} failed, error:`,e),e}})}_convertTopicInfoList(e){const t=[];return e.forEach(e=>{const{TopicInfo:s={},SelfInfo:o={},ErrorCode:i,ErrorInfo:n}=e;t.push({code:i,message:n,topic:this._convertTopicInfo(s),selfInfo:this._convertSelfInfo(o)})}),t}_convertTopicInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{CreateTime:s,CustomString:o,FaceUrl:i,GroupId:n,LastMsgTime:r,NextMsgSeq:c,Introduction:a,Notification:u,ShutUpAllFlag:p,TopicId:l,TopicName:d,LastMsg:h,AtInfoList:g}=e;return{createTime:s,customData:o,avatar:i,groupID:n,lastMessageTime:r,nextMessageSeq:c,introduction:a,notification:u,muteAllMembers:p,topicID:l,topicName:d,lastMsg:this._convertLastMsg(h),groupAtInfoList:this._convertAtInfoList({groupID:n,topicID:l,atInfoList:g})}}_convertLastMsg(e){const{utils:{isUndefined:t},OuterConstant:s}=this._core;if(t(e))return;4===e.Event&&(e.MsgBody.MsgType=s.MSG_GRP_TIP);const{message:{messageHelper:o}}=this._core,i=o.parseServerGroupMessage(e),{From_AccountHeadurl:n,From_AccountNick:r,LastMsgSeq:c}=e;return Object.assign(Object.assign({},i),{avatar:n,nick:r,sequence:c})}_convertAtInfoList(e){const{groupID:t,topicID:s,atInfoList:o=[]}=e;if(0===o.length)return[];const i=[];return o.forEach(e=>{const{AtFlagList:o,AtMsgSeq:n,From_Account:r}=e;i.push({groupID:t,topicID:s,groupAtType:o,sequence:n,from:r})}),i}_convertSelfInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{InheritGroupReceiveOption:s,MsgFlag:o,MsgSeq:i,NoUnreadSeqList:n,ShutUpTime:r}=e;return{inheritGroupReceiveOption:s,messageRemindType:o,readedSequence:i,excludedUnreadSequenceList:n,muteTime:r}}};var D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(e){return T(this,void 0,void 0,function*(){const t="updateTopicProfile",{utils:{isUndefined:s}}=this._core,{groupID:o,topicID:i,muteAllMembers:n}=e;s(e.muteAllMembers)||(e.muteAllMembers=!0===n?"On":"Off");try{return yield function(e,t){return T(this,void 0,void 0,function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a,muteAllMembers:u}=e,p={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a,ShutUpAllMember:u};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:p})})}(e,this._core),console.log(`${this._name}.${t} ok, topicID:${i}`),g.updateTopic([e]),{code:0,data:{topic:g.getTopic(o,i)}}}catch(e){throw console.error(`${this._name}.${t} failed, topicID:${i} error:`,e),e}})}};var C=new class{constructor(){this._name="MessageReadHandler"}init(e){this._core=e;const{helper:t,common:{isTopic:s}}=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(e){return T(this,void 0,void 0,function*(){const{utils:{isEmpty:t},ssoLog:s,helper:{ChatError:o}}=this._core,{conversationID:i}=e,n={code:0,data:{}},r=_(i),c=m(r),a=g.getTopic(c,r),u=`${this._name}.setMessageRead topicID:${r} conversationID:${i} unreadCount:${(null==a?void 0:a.unreadCount)||0}`;if(n.successLog={message:u},!a)return n;if(t(a.groupAtInfoList)||this._deleteTopicAtTips(c,r),0===a.unreadCount)return n;const p=this._getLocalMessageMaxSequence(a);try{yield this._setTopicMessageRead(c,r,p)}catch(e){const{errorCode:t,errorInfo:s}=e;throw new o({functionName:"setMessageRead",code:t,message:s,moreMessage:u})}return Object.assign(Object.assign({},n),{successLog:{message:`${u} lastMessageSequence:${p}`}})})}_deleteTopicAtTips(e,t){return T(this,void 0,void 0,function*(){this._core.ssoLog.debug("_deleteTopicAtTips",`${this._name}._deleteTopicAtTips groupID:${e} topicID:${t}`);const s=g.getTopic(e,t);if(!s)return;const o=(null==s?void 0:s.groupAtInfoList)||[];if(0!==o.length)try{const{common:{getCurrentUserID:s},OuterConstant:{CONV_AT_ALL:i}}=this._core;let n=[...o];if(n=o.filter(e=>!e.atTypeArray.includes(i)),0===n.length)return void g.clearGroupAtInfoList(e,t);const r=n.map(e=>({From_Account:e.from,To_Account:s(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID}));yield function(e,t){return T(this,void 0,void 0,function*(){const{messageListToDelete:s}=e,o={DelMsgList:s};return t.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:o})})}({messageListToDelete:r},this._core),this._core.ssoLog.debug(`${this._name}._deleteTopicAtTips ok. count:${o.length}`),g.clearGroupAtInfoList(e,t)}catch(e){console.error(`${this._name}._deleteTopicAtTips fail:`,e)}})}_getLocalMessageMaxSequence(e){var t;const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,{topicID:i}=e,n=s.getLocalMessageList(`${o}${i}`),r=Math.max(...n.map(e=>e.sequence));let c=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return r>c&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${c} to ${r}`),c=r),c}_setTopicMessageRead(e,t,s){return T(this,void 0,void 0,function*(){try{yield function(e,t){return T(this,void 0,void 0,function*(){const{groupID:s,topicID:o,lastMessageSequence:i}=e,n={GroupId:s,TopicId:o,MsgReadedSeq:i};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:n})})}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(`${this._name}._setTopicMessageRead ok, lastReadSequence:${s}`),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)}catch(e){throw console.error(`${this._name}._setTopicMessageRead fail:`,e),e}})}};var L=new class{init(e){e.ssoLog.debug("TopicAction.init"),M.init(e),v.init(e),E.init(e),S.init(e),D.init(e),C.init(e)}};const A={required:!0,rules:["string"],allowEmpty:!1},y={createTopicInCommunity:{groupID:A,topicName:A},deleteTopicFromCommunity:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:A,topicID:A}},O={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};return new class{constructor(){this.name="Topic"}install(t){e.init(t),g.init(t),I.init(t),f.init(t),L.init(t),t.helper.registerValidateConfig({auth:O,params:y})}}});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).TencentCloudChatTopic=t()}(this,(function(){"use strict";const e=new class{init(e){this.core=e}},t="@TOPIC#_",s=7,o=8,i=17,n=18,r=20,c=300,a=0,u=3600,p=3021,l=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],d=t=>{const{core:{common:{getMessagePreviewText:s}}}=e;return{lastTime:(null==t?void 0:t.time)||0,lastSequence:(null==t?void 0:t.sequence)||0,fromAccount:(null==t?void 0:t.from)||"",payload:(null==t?void 0:t.payload)||null,type:(null==t?void 0:t.type)||"",messageForShow:s(null==t?void 0:t.type,null==t?void 0:t.payload),nick:(null==t?void 0:t.nick)||"",avatar:(null==t?void 0:t.avatar)||"",version:(null==t?void 0:t.version)||0,cloudCustomData:(null==t?void 0:t.cloudCustomData)||"",isRevoked:(null==t?void 0:t.isRevoked)||!1,revoker:(null==t?void 0:t.revoker)||null}};class h{constructor(e){this.topicID="",this.topicName="",this.avatar="",this.introduction="",this.notification="",this.unreadCount=0,this.muteAllMembers=!1,this.customData="",this.groupAtInfoList=[],this.nextMessageSeq=0,this.lastMessage=d(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(const t in e)l.includes(t)&&("lastMessage"===t?this[t]=d(e[t]):"selfInfo"===t?this.updateSelfInfo(e[t]):this[t]="muteAllMembers"===t?1===e[t]:e[t])}updateUnreadCount(e=0){this.unreadCount=e}updateReadSequence(e=0,t=!1){t&&(this.selfInfo.readedSequence=e)}updateNextMessageSeq(e){this.nextMessageSeq=e}updateLastMessage(e){this.lastMessage=d(e)}updateGroupAtInfoList(t){const{core:{common:{updateGroupAtInfo:s}}}=e;s(t,this.groupAtInfoList)}updateTopic(t){const{core:{utils:{isUndefined:s},common:{deepMerge:o}}}=e;s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),o(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){const{core:{common:{deepMerge:s}}}=e;return s(this.selfInfo,t,[],[""])>0}reduceUnreadCount(){return this.unreadCount>=1&&(this.unreadCount-=1,!0)}isLastMessageRevoked(e){var t;return e===(null===(t=this.lastMessage)||void 0===t?void 0:t.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var g=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){this._core=e;const{helper:t,constants:s,notificationCenter:o,InnerEvent:{LOGOUT:i,DESTROY:n}}=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),o.subscribeInnerEvent(i,this._reset,this),o.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_hasTopic(e,t){var s;return!!(null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t))}_setTopic(e,t,s){var o;null===(o=this._topicMap.get(e))||void 0===o||o.set(t,s)}_deleteTopic(e,t){var s;null===(s=this._topicMap.get(e))||void 0===s||s.delete(t)}getTopic(e,t){var s;return null===(s=this._topicMap.get(e))||void 0===s?void 0:s.get(t)}updateTopic(e){e.forEach((e=>{const{groupID:t,topicID:s}=e;let o=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?(o=this.getTopic(t,s),null==o||o.updateTopic(e)):(this._getLastMessage(e),o=new h(e),this._setTopic(t,s,o));const i=this.computeUnreadCount(o);null==o||o.updateUnreadCount(i)}))}_getLastMessage(e){const{utils:{isEmpty:t,isUndefined:s,isArray:o}}=this._core;if(!s(e.lastMsg)){let s=null,i="";o(e.lastMsg.elements)?(s=e.lastMsg.elements[0].content,i=e.lastMsg.elements[0].type):(s=e.lastMsg.elements.content,i=e.lastMsg.elements.type);const n={payload:s,type:i,time:e.lastMsg.time,sequence:e.lastMsg.sequence,from:e.lastMsg.from,nick:e.lastMsg.nick,avatar:e.lastMsg.avatar,version:e.lastMsg.messageVersion,cloudCustomData:e.lastMsg.cloudCustomData,isRevoked:2===e.lastMsg.isPlaceMessage,revoker:t(e.lastMsg.revokerInfo)?null:e.lastMsg.revokerInfo.revoker};e.lastMessage=n}}computeUnreadCount(e){const{excludedUnreadSequenceList:t=[],readedSequence:s}=e.selfInfo;let o=e.nextMessageSeq-e.selfInfo.readedSequence-1;if(t.length>0){let i=0;t.forEach((t=>{t>s&&t<=e.nextMessageSeq-1&&(i+=1)})),i>=1&&(o-=i)}return o>0?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach((e=>{const{utils:{isEmpty:t}}=this._core,{groupAtInfoList:s}=e;t(s)||this.updateGroupAtInfoList(s)}))}updateGroupAtInfoList(e,t=!1){if(0===e.length)return;const{common:{getCurrentUserID:s},utils:{isUndefined:o,isEmpty:i}}=this._core,n=s();let r=null;e.forEach((e=>{var s;const{groupID:i,topicID:c,from:a}=e;a!==n&&(r=this.getTopic(i,c),r&&r.updateGroupAtInfoList(e),o(r)&&t&&(this.updateTopic([{groupID:i,topicID:c}]),null===(s=this.getTopic(i,c))||void 0===s||s.updateGroupAtInfoList(e)))})),i(r)||this.emitTopicUpdate(r)}getTopicList(e,t=[]){const s=this._topicMap.get(e),o=[];return s&&o.push(...s.values()),t.length>0?o.filter((e=>t.includes(e.topicID))):o}deleteTopicList(e,t=[]){this._topicMap.has(e)&&(t.forEach((t=>{this._hasTopic(e,t)&&this._deleteTopic(e,t)})),console.log(`${this._name}.deleteTopicList groupID:${e} topicID:${t.join(",")}`))}clearGroupAtInfoList(e,t){const{utils:{isEmpty:s}}=this._core,o=this.getTopic(e,t);s(null==o?void 0:o.groupAtInfoList)||(null==o||o.clearGroupAtInfoList(),console.log(`${this._name}.clearGroupAtInfoList topicID:${t}`))}markMessageAsRead(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,i=(null==s?void 0:s.getLocalMessageList(`${o}${e}`))||[];if(0===i.length)return;const{length:n}=i;for(let e=n-1;e>=0;e--){const s=i[e];if(!(t&&s.sequence>t)){if("in"===s.flow&&s.isRead)break;s.setIsRead(!0)}}}updateUnreadCount(e,t){const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core;let i=!1;const n=this.getTopic(e,t);if(!n)return;const r=(null==s?void 0:s.getLocalMessageList(`${o}${t}`))||[],c=n.unreadCount,a=null==r?void 0:r.filter((e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted)).length;console.log(`${this._name}._updateUnreadCount topicID:${t} currentUnreadCount:${c} newUnreadCount:${a}`),c!==a&&(n.updateUnreadCount(a),i=!0),i&&this.emitTopicUpdate(n)}updateSelfInfo(e){const{groupID:t,topicID:s,selfInfo:o}=e,i=this.getTopic(t,s);if(i){const e=i.updateSelfInfo(o);console.log(`${this._name}.updateSelfInfo topicID:${s} info:${JSON.stringify(o)} isUpdated:${e}`),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){const{OuterEvent:t,notificationCenter:s}=this._core;s.emitOuterEvent(t.TOPIC_UPDATED,{name:t.TOPIC_UPDATED,data:e})}_resetTopicRequestInfo(){this._topicRequestInfo.clear()}_reset(){this._topicMap.clear(),this._topicRequestInfo.clear()}_dispose(){this._reset();const{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};const _=(t="")=>{const{core:{OuterConstant:s}}=e;return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){this._core=e;const{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:o,WORKFLOW_STEP:i},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:r,TOPIC_MESSAGE_DELETED:c,TOPIC_MESSAGE_REVOKED:a,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=e,{InnerEventSubType:{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}}=s;t.registerWorkflowStep(o.SYNC_SERVER_INFO_AFTER_LOGIN,i.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(o.RECEIVE_GROUP_NEW_MESSAGE,i.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleNewMessage,this),s.subscribeInnerEvent(c,this._handleMessageDeleted,this),s.subscribeInnerEvent(a,this._handleMessageRevoked,this),s.subscribeInnerEvent(u,this._handleMessageModified,this),s.subscribeInnerEvent(n,l,this._handleTopicAtTips,this),s.subscribeInnerEvent(n,d,this._handleLatestMessage,this),s.subscribeInnerEvent(p,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleUnreadSyncFinished(e){var t;const{constants:{WORKFLOW_STEP:s}}=this._core,{groupTipList:o=[]}=e.result[s.UNREAD_MESSAGE_SYNC]||{};for(let e=0;e<o.length;e++){const s=o[e];12===s.Event&&(null===(t=s.GroupTips[0].MsgBody)||void 0===t?void 0:t.TopicId)&&this._handleTopicAtTips(s)}}_handleNewMessage(e){const{conversationUpdateFieldList:t=[]}=e.result||{};if(0===t.length)return;const{common:{isTopic:s}}=this._core;if(!s(t[0].conversationID))return;const{common:{getCurrentUserID:o}}=this._core;t.forEach((e=>{const{conversationID:t="",lastMessage:s}=e,i=_(t),n=m(i),r=g.getTopic(n,i);if(r){const{from:e,sequence:t}=s,i=t+1,n=e===o();if(i>r.nextMessageSeq){r.updateNextMessageSeq(i),r.updateLastMessage(s),r.updateReadSequence(t,n);const e=n?0:g.computeUnreadCount(r);r.updateUnreadCount(e),this._emitTopicUpdate(r)}}}))}_handleMessageDeleted(e){const t=_(e),s=m(t),o=g.getTopic(s,t);if(console.log(`${this._name}._handleMessageDeleted, topicID:${t} hasTopic:${!!o}`),!o)return;const{message:{messageDataHandler:i},utils:{isEmpty:n}}=this._core,r=(null==i?void 0:i.getLocalMessageList(e))||[];let c={};for(let e=(r.length||0)-1;e>=0;e--)if(!r[e].isDeleted){c=r[e];break}let a=!1;o.lastMessage.lastSequence!==c.sequence&&(n(c)&&(c={}),o.updateLastMessage(c),a=!0),a&&(this._emitTopicUpdate(o),console.log(`${this._name}._handleMessageDeleted, topicID:${t} with lastMessage:${o.lastMessage}`))}_handleMessageRevoked(e){const{messageList:t=[]}=e;if(console.log(`${this._name}._handleMessageRevoked messageList:${t.length}`),0===t.length)return;let s=null,o=!1;t.forEach((e=>{const{to:t,sequence:i,revoker:n}=e,r=m(t);if(s=g.getTopic(r,t),s){s.reduceUnreadCount()&&(o=!0),s.isLastMessageRevoked(i)&&(s.setLastMessageRevoked(!0),s.setLastMessageRevoker(n),o=!0);const{excludedUnreadSequenceList:e=[]}=s.selfInfo;e.push(i),s.updateSelfInfo({excludedUnreadSequenceList:e})}})),o&&this._emitTopicUpdate(s)}_handleMessageModified(e){const{conversationID:t,messageList:s}=e,o=_(t),i=m(o),n=g.getTopic(i,o);if(console.log(`${this._name}._handleMessageModified topicID:${o} hasTopic:${!!n}`),!n)return;const{lastMessage:r}=n;if(r){const e=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(r,e)){const{common:{getMessagePreviewText:t}}=this._core,{time:s,sequence:o,elements:i,cloudCustomData:c,messageVersion:a}=e;r.type=i[0].type,r.payload=i[0].content,r.messageForShow=t(r.type,r.payload),r.cloudCustomData=c,r.version=a,r.lastSequence=o,r.lastTime=s,this._emitTopicUpdate(n)}}}_shouldUpdateLastMessage(e,t){if(null===e.payload)return!0;const{sequence:s,messageVersion:o}=t;return e.lastSequence===s&&e.version!==o}_emitTopicUpdate(e){const{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){const{GroupTips:t=[]}=e,s=[];let o=!1;t.forEach((e=>{const{GroupAtTips:t,MsgBody:i,MsgRandom:n,ClientSeq:r}=e;let c={};t?c=this._convertAtInfo(t):i&&(c=this._convertAtInfo(i),o=!0),c.__random=n,c.__sequence=r,s.push(c)})),g.updateGroupAtInfoList(s,o)}_convertAtInfo(e){const{From_Account:t,GroupId:s,TopicId:o,MsgSeq:i,GroupAtType:n}=e;return{groupID:s,topicID:o,from:t,sequence:i,groupAtType:n}}_handleLatestMessage(e){const{utils:{isEmpty:t},message:{messageHelper:s,messageFactory:o}}=this._core,{NewMsg:i,NoUnreadSeqList:n}=e||{};if(!t(i)){const{GroupInfo:{TopicId:e}}=i,t=s.parseServerGroupMessage(i),r=o.createMessage(t);r.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:r,excludedUnreadSequenceList:n})}}_updateUnreadCountAndLastMessage(e){const{utils:{isUndefined:t}}=this._core,{topicID:s,message:o,excludedUnreadSequenceList:i}=e,n=m(s),r=g.getTopic(n,s);if(r){let e=r.selfInfo.excludedUnreadSequenceList||[];if(t(i)||(e=i),o._isExcludedFromUnreadCount&&e.push(o.sequence),r.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${o.sequence} lastMessage sequence:${r.lastMessage.lastSequence}`),o.sequence>r.lastMessage.lastSequence){const e=o.sequence+1;r.updateLastMessage(o),r.updateNextMessageSeq(e);const t=g.computeUnreadCount(r);r.updateUnreadCount(t),this._emitTopicUpdate(r)}}}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:o,TOPIC_MESSAGE_REVOKED:i,TOPIC_MESSAGE_MODIFIED:n,DESTROY:r}}=this._core,{InnerEventSubType:{TOPIC_AT_TIPS:c,TOPIC_LATEST_MESSAGE:a}}=e;e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(o,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(i,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,c,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,a,this._handleLatestMessage,this),e.unSubscribeInnerEvent(r,this._dispose,this)}};var f=new class{constructor(){this._name="TopicOperationHandler"}init(e){this._core=e;const{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:o}}=e,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:r}}=t;t.subscribeInnerEvent(s,i,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,r,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(o,this._dispose,this),e.ssoLog.debug(`${this._name}.init`)}_handleTopicTips(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){const{common:{getCurrentUserID:t}}=this._core,{GroupInfo:{GroupId:s,TopicId:o},MsgBody:{MsgMemberInfo:i=[]}}=e;for(let e=0;e<i.length;e++){const{User_Account:n,ShutupTime:r}=i[e];if(n===t()&&r>=0){g.updateSelfInfo({groupID:s,topicID:o,selfInfo:{muteTime:r}});break}}}_handleTopicProfileUpdated(e){const{GroupInfo:{GroupId:t,TopicId:s},MsgBody:{MsgTopicNewInfo:{CustomString:o,ShutupAll:i,TopicFaceUrl:n,TopicIntroduction:r,TopicName:c,TopicNotification:a}}}=e,u=g.getTopic(t,s);u&&(u.updateTopic({groupID:t,topicID:s,customData:o,muteAllMembers:i,avatar:n,introduction:r,topicName:c,notification:a}),g.emitTopicUpdate(u))}_handleTopicNotices(e){const{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){const s=t[e],{MsgBody:{ReportType:o,TopicIdArray:c,MsgFlag:a},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:c}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:a}):this._emitTopicNotices(s)}}_handleTopicCreated(e){const{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){const{OuterEvent:t,notificationCenter:s}=this._core,{groupID:o,topicIDList:i=[]}=e;g.deleteTopicList(o,i),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){const{groupID:t,topicID:s,messageRemindType:o}=e;g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{messageRemindType:o}})}_handleTopicMessageReadSynced(e){const{GroupTips:t}=e,{utils:{isUndefined:s}}=this._core;t.forEach((e=>{const{GroupReadInfoArray:t}=e.MsgBody;s(t)||t.forEach((e=>{const{GroupId:t,TopicId:s,LastReadMsgSeq:o}=e;console.log(`${this._name}._handleTopicMessageReadSynced topicID:${s} lastReadSequence:${o}`),g.updateSelfInfo({groupID:t,topicID:s,selfInfo:{readedSequence:o}}),g.clearGroupAtInfoList(t,s),g.markMessageAsRead(s,o),g.updateUnreadCount(t,s)}))}))}_emitTopicTips(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_TIP;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_GROUP;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_emitTopicNotices(e){const{message:{messageHelper:t,messageFactory:s},OuterConstant:o,OuterEvent:i,notificationCenter:n}=this._core;e.MsgBody.MsgType=o.MSG_GRP_SYS_NOTICE;const r=t.parseServerGroupMessage(e);r.flow="in",r.conversationType=o.CONV_SYSTEM,r.conversationID=o.CONV_SYSTEM;const c=s.createMessage(r);c.setElement(r.elements),c.isSystemMessage=!0,n.emitOuterEvent(i.MESSAGE_RECEIVED,{name:i.MESSAGE_RECEIVED,data:[c]})}_dispose(){const{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{InnerEventSubType:{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:i,TOPIC_MESSAGE_READ_SYNC:n}}=e;e.unSubscribeInnerEvent(t,o,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,i,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function T(e,t,s,o){return new(s||(s=Promise))((function(i,n){function r(e){try{a(o.next(e))}catch(e){n(e)}}function c(e){try{a(o.throw(e))}catch(e){n(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,c)}a((o=o.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return T(this,void 0,void 0,(function*(){const{apiMap:{getGroupList:e}}=this._core,t=(yield e(!0)).filter((e=>!0===e.isSupportTopic));return console.log(`${this._name}.getJoinedCommunityList ok, count:${t.length}`),{code:0,data:{groupList:t}}}))}};var v=new class{constructor(){this._name="CreateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(e){return T(this,void 0,void 0,(function*(){const t="createTopicInCommunity",{utils:{isUndefined:s},common:{isTopic:o},helper:{ChatError:i}}=this._core,{topicID:n}=e;if(!s(n)&&!o(n)){const e=new i({functionName:t,code:p});throw console.error(`${this._name}.${t} fail:`,e),e}try{const s=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a}=e,u={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:u})}))}(e,this._core),{TopicId:o=""}=s||{};return console.log(`${this._name}.${t} ok, topicID:${o}`),g.updateTopic([Object.assign(Object.assign({},e),{topicID:o})]),{code:0,data:{topicID:o}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(e){return T(this,void 0,void 0,(function*(){const t="deleteTopicFromCommunity",{groupID:s,topicIDList:o=[]}=e;try{const i=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:i})}))}({groupID:s,topicIDList:o},this._core),{DestroyResultItem:n=[]}=i||{},r=[],c=[],a=[];return n.forEach((e=>{const{TopicId:t,ErrorCode:s,ErrorInfo:o}=e;0===s?(r.push({topicID:t}),a.push(t)):c.push({topicID:t,code:s,message:o})})),a.length>0&&g.deleteTopicList(e.groupID,a),console.log(`${this._name}.${t} ok, groupID:${s} success:${r.length} fail:${c.length}`),{code:0,data:{successTopicList:r,failureTopicList:c}}}catch(e){throw console.error(`${this._name}.${t} failed, error:`,e),e}}))}};var S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{const{store:t}=this._core;return(t.get("cloudConfig")||{})[e]}}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return T(this,void 0,void 0,(function*(){const{groupID:t,topicIDList:s=[]}=e,o=0===s.length;if(this._isFetchingFromCache(t,o)){const e=g.getTopicList(t,s);if(o||e.length===s.length)return console.log(`${this._name}.getTopicList groupID:${t} from cache, count:${e.length}`),{code:0,data:{successTopicList:e,failureTopicList:[]}}}return this._getTopicListFromServer({groupID:t,topicIDList:s},o)}))}_isFetchingFromCache(e,t){const{hasFetchedAllTopics:s=!1,time:o=0}=g.getTopicRequestInfo(e);return!(!s&&(t||s))&&Date.now()-o<this._getCacheDuration()}_getCacheDuration(){const{utils:{isUndefined:e}}=this._core;let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10),t>=a&&t<=u)?1e3*t:1e3*c}_getTopicListFromServer(e,t){return T(this,void 0,void 0,(function*(){const s="_getTopicListFromServer",{utils:{isEmpty:o}}=this._core,{groupID:i}=e;try{const n=yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicIDList:o}=e,i={GroupId:s,TopicIdList:o,MemberInfoFilter:["NoUnreadSeqList"]};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:i})}))}(e,this._core),{TopicAndSelfInfo:r=[]}=n||{},c=[],a=[],u=[];this._convertTopicInfoList(r).forEach((e=>{const{topic:t,selfInfo:s,code:o,message:i}=e,{topicID:n}=t;0===o?(c.push(Object.assign(Object.assign({},t),{selfInfo:s})),a.push(n)):u.push({topicID:n,code:o,message:i})})),g.updateTopic(c),g.updateTopicAtInfo(c),console.log(`${this._name}.${s} groupID:${i} from remote, success count:${a.length}, fail count:${u.length}`);let p=[];return o(a)||(g.setTopicRequestInfo(i,{hasFetchedAllTopics:t,time:Date.now()}),p=g.getTopicList(i,a)),{code:0,data:{successTopicList:p,failureTopicList:u}}}catch(e){throw console.error(`${this._name}.${s} failed, error:`,e),e}}))}_convertTopicInfoList(e){const t=[];return e.forEach((e=>{const{TopicInfo:s={},SelfInfo:o={},ErrorCode:i,ErrorInfo:n}=e;t.push({code:i,message:n,topic:this._convertTopicInfo(s),selfInfo:this._convertSelfInfo(o)})})),t}_convertTopicInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{CreateTime:s,CustomString:o,FaceUrl:i,GroupId:n,LastMsgTime:r,NextMsgSeq:c,Introduction:a,Notification:u,ShutUpAllFlag:p,TopicId:l,TopicName:d,LastMsg:h,AtInfoList:g}=e;return{createTime:s,customData:o,avatar:i,groupID:n,lastMessageTime:r,nextMessageSeq:c,introduction:a,notification:u,muteAllMembers:p,topicID:l,topicName:d,lastMsg:this._convertLastMsg(h),groupAtInfoList:this._convertAtInfoList({groupID:n,topicID:l,atInfoList:g})}}_convertLastMsg(e){const{utils:{isUndefined:t},OuterConstant:s}=this._core;if(t(e))return;4===e.Event&&(e.MsgBody.MsgType=s.MSG_GRP_TIP);const{message:{messageHelper:o}}=this._core,i=o.parseServerGroupMessage(e),{From_AccountHeadurl:n,From_AccountNick:r,LastMsgSeq:c}=e;return Object.assign(Object.assign({},i),{avatar:n,nick:r,sequence:c})}_convertAtInfoList(e){const{groupID:t,topicID:s,atInfoList:o=[]}=e;if(0===o.length)return[];const i=[];return o.forEach((e=>{const{AtFlagList:o,AtMsgSeq:n,From_Account:r}=e;i.push({groupID:t,topicID:s,groupAtType:o,sequence:n,from:r})})),i}_convertSelfInfo(e){const{utils:{isEmpty:t}}=this._core;if(t(e))return{};const{InheritGroupReceiveOption:s,MsgFlag:o,MsgSeq:i,NoUnreadSeqList:n,ShutUpTime:r}=e;return{inheritGroupReceiveOption:s,messageRemindType:o,readedSequence:i,excludedUnreadSequenceList:n,muteTime:r}}};var D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){this._core=e;const{helper:t}=e;t.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(e){return T(this,void 0,void 0,(function*(){const t="updateTopicProfile",{utils:{isUndefined:s}}=this._core,{groupID:o,topicID:i,muteAllMembers:n}=e;s(e.muteAllMembers)||(e.muteAllMembers=!0===n?"On":"Off");try{return yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,topicName:i,avatar:n,notification:r,introduction:c,customData:a,muteAllMembers:u}=e,p={GroupId:s,TopicId:o,TopicName:i,FaceUrl:n,Notification:r,Introduction:c,CustomString:a,ShutUpAllMember:u};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:p})}))}(e,this._core),console.log(`${this._name}.${t} ok, topicID:${i}`),g.updateTopic([e]),{code:0,data:{topic:g.getTopic(o,i)}}}catch(e){throw console.error(`${this._name}.${t} failed, topicID:${i} error:`,e),e}}))}};var C=new class{constructor(){this._name="MessageReadHandler"}init(e){this._core=e;const{helper:t,common:{isTopic:s}}=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(e){return T(this,void 0,void 0,(function*(){const{utils:{isEmpty:t},ssoLog:s,helper:{ChatError:o}}=this._core,{conversationID:i}=e,n={code:0,data:{}},r=_(i),c=m(r),a=g.getTopic(c,r),u=`${this._name}.setMessageRead topicID:${r} conversationID:${i} unreadCount:${(null==a?void 0:a.unreadCount)||0}`;if(n.successLog={message:u},!a)return n;if(t(a.groupAtInfoList)||this._deleteTopicAtTips(c,r),0===a.unreadCount)return n;const p=this._getLocalMessageMaxSequence(a);try{yield this._setTopicMessageRead(c,r,p)}catch(e){const{errorCode:t,errorInfo:s}=e;throw new o({functionName:"setMessageRead",code:t,message:s,moreMessage:u})}return Object.assign(Object.assign({},n),{successLog:{message:`${u} lastMessageSequence:${p}`}})}))}_deleteTopicAtTips(e,t){return T(this,void 0,void 0,(function*(){this._core.ssoLog.debug("_deleteTopicAtTips",`${this._name}._deleteTopicAtTips groupID:${e} topicID:${t}`);const s=g.getTopic(e,t);if(!s)return;const o=(null==s?void 0:s.groupAtInfoList)||[];if(0!==o.length)try{const{common:{getCurrentUserID:s},OuterConstant:{CONV_AT_ALL:i}}=this._core;let n=[...o];if(n=o.filter((e=>!e.atTypeArray.includes(i))),0===n.length)return void g.clearGroupAtInfoList(e,t);const r=n.map((e=>({From_Account:e.from,To_Account:s(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID})));yield function(e,t){return T(this,void 0,void 0,(function*(){const{messageListToDelete:s}=e,o={DelMsgList:s};return t.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:o})}))}({messageListToDelete:r},this._core),this._core.ssoLog.debug(`${this._name}._deleteTopicAtTips ok. count:${o.length}`),g.clearGroupAtInfoList(e,t)}catch(e){console.error(`${this._name}._deleteTopicAtTips fail:`,e)}}))}_getLocalMessageMaxSequence(e){var t;const{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:o}}=this._core,{topicID:i}=e,n=s.getLocalMessageList(`${o}${i}`),r=Math.max(...n.map((e=>e.sequence)));let c=(null===(t=null==e?void 0:e.lastMessage)||void 0===t?void 0:t.lastSequence)||0;return r>c&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${c} to ${r}`),c=r),c}_setTopicMessageRead(e,t,s){return T(this,void 0,void 0,(function*(){try{yield function(e,t){return T(this,void 0,void 0,(function*(){const{groupID:s,topicID:o,lastMessageSequence:i}=e,n={GroupId:s,TopicId:o,MsgReadedSeq:i};return t.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:n})}))}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(`${this._name}._setTopicMessageRead ok, lastReadSequence:${s}`),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)}catch(e){throw console.error(`${this._name}._setTopicMessageRead fail:`,e),e}}))}};var L=new class{init(e){e.ssoLog.debug("TopicAction.init"),M.init(e),v.init(e),E.init(e),S.init(e),D.init(e),C.init(e)}};const A={required:!0,rules:["string"],allowEmpty:!1},y={createTopicInCommunity:{groupID:A,topicName:A},deleteTopicFromCommunity:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:A,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:A,topicID:A}},O={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};return new class{constructor(){this.name="Topic"}install(t){e.init(t),g.init(t),I.init(t),f.init(t),L.init(t),t.helper.registerValidateConfig({auth:O,params:y})}}}));