@tencentcloud/lite-chat 1.6.4 → 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,r,o,g){return new(o=o||Promise)(function(n,e){function t(i){try{s(g.next(i))}catch(i){e(i)}}function a(i){try{s(g.throw(i))}catch(i){e(i)}}function s(i){var e;i.done?n(i.value):((e=i.value)instanceof o?e:new o(function(i){i(e)})).then(t,a)}s((g=g.apply(i,r||[])).next())})}let 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=>{var e,n;return i.type===o&&({cloudCustomData:i="",payload:{data:n=""}={}}=i,i=i.match(/"type":"tsignaling"/),e=n.match(/inviteID/),n=n.match(/actionType/),i||e&&n)})}function p(i){var e=i.payload["data"];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||53<i)throw new Error("Number of digits must be between 0 and 53");return i<=30?Math.floor(Math.random()*(1<<i)):(i=i-30,Math.floor(Math.random()*(1<<i))*(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).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){var e=d+".isInvitationValid",{inviteID:i,groupID:n,inviter:t}=i,t=t===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(i))return!1;var a=this._onlineSignalingMap.get(i)["inviteeList"];if(console.log(e+` inviteID:${i} inviteeList:${a} groupID:`+n),n){if(t)return 0<a.length;let i=this._core.common.getCurrentUserID();return a.includes(i)}return!0}updateSignaling(i){let n=d+".updateSignaling",{inviteID:t,inviter:e,inviteeList:a,groupID:s}=i;if(console.log(n+` inviteID:${t} inviter:${e} groupID:`+s),s&&this.hasSignaling(t)){let i=a[0],e=this._onlineSignalingMap.get(t)["inviteeList"];e.includes(i)&&(e.splice(e.indexOf(i),1),console.log(n+` remove ${i}. localInviteeList.length:`+e.length)),0===e.length&&this.removeSignaling(t)}else this.removeSignaling(t)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}},E=new class{init(i){this._core=i}createInviteSignaling(i){var e=this._generateInviteID(),e=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:n,inviteeList:t}=e,n=n||t[0];return{signaling:this._createSignaling(e,n),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){var e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e;return{signaling:this._createSignaling(e,n||t),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){var e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,n=n||t[0];return{signaling:this._createSignaling(e,n),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){var e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e;return{signaling:this._createSignaling(e,n||t),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){var{isInviter:e=!1}=i,i=this._createTimeoutSignalingData(i),{groupID:n,inviteeList:t,inviter:a}=i,n=n||(e?t[0]:a);return{signaling:this._createSignaling(i,n),signalingData:i,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}_createSignalingExtensionOptions(i){var{data:i="",onlineUserOnly:e,inviteID:n="",offlinePushInfo:t,actionType:a}=i,n=(null==(n=null==(n=m.getSignaling(n))?void 0:n.signaling)?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:e||n,offlinePushInfo:t,messageControlInfo:this._createMessageControlInfo(i,a)}}_createMessageControlInfo(i,e){e=e===a&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),i=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:e||i,excludedFromLastMessage:e||i}}_createInviteSignalingData(i){var n=d+"._createInviteSignalingData",{userID:t,timeout:a=0,groupID:s="",inviteeList:r=[]}=i,o=this._core.common.getCurrentUserID(),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:e,inviter:o,inviteeList:s?r:[t],timeout:a});return console.log(n+" signalingData:",i),i}_createAcceptSignalingData(i){var e=d+"._createAcceptSignalingData",n=i["inviteID"],a=this._core.common.getCurrentUserID(),{inviter:n,groupID:s}=m.getSignaling(n),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:t,groupID:s,inviter:n,inviteeList:[a]});return console.log(e+" signalingData:",i),i}_createCancelSignalingData(i){var e=d+"._createCancelSignalingData",t=i["inviteID"],a=this._core.common.getCurrentUserID(),{inviteeList:t,groupID:s}=m.getSignaling(t),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:n,groupID:s,inviter:a,inviteeList:t});return console.log(e+" signalingData:",i),i}_createRejectSignalingData(i){var e=d+"._createRejectSignalingData",n=i["inviteID"],t=this._core.common.getCurrentUserID(),{inviter:n,groupID:a}=m.getSignaling(n),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:s,groupID:a,inviter:n,inviteeList:[t]});return console.log(e+" signalingData:",i),i}_createTimeoutSignalingData(i){var e=d+"._createTimeoutSignalingData",{isInviter:n=!1,inviteID:t}=i,{inviteeList:t,inviter:s}=m.getSignaling(t),r=this._core.common.getCurrentUserID(),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:a,inviter:s,inviteeList:n?t:[r]});return console.log(e+" signalingData:",i),i}_createSignaling(i,e){var{groupID:n=""}=i,e={to:e,conversationType:n?g:r,priority:"High",payload:{data:JSON.stringify(i)}};return null==(n=null==(i=null==(n=this._core)?void 0:n.message)?void 0:i.messageFactory)?void 0:n.createCustomMessage(e)}_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){var{data:i="",inviteID:e="",groupID:n=""}=i;return{businessID:1,timeout:0,data:i,inviteID:e,groupID:n}}},O=new class{constructor(){this._isProcessingSignaling=!1}init(i){(this._core=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 n;try{this._validateBeforeInvite(e);var{signaling:t,signalingData:a,signalingExtensionOptions:i}=E.createInviteSignaling(e),s=yield this._sendSignaling(t,i);if(0!==(null==s?void 0:s.code))return s;{let{inviteID:i,timeout:e}=a;return m.saveSignaling(i,Object.assign(Object.assign({},a),{signaling:t})),0<e&&(null!=(n=this._core)&&n.helper.taskScheduler.addOnceTask({id:I+"-"+i,intervalMs:1e3*(e+5),callback:this.handleInvitationExpiryTimer.bind(this,i)})),Object.assign(Object.assign({},s),{inviteID:i})}}catch(n){throw n}})}accept(s){return i(this,void 0,void 0,function*(){try{var i=s["inviteID"],{signaling:e,signalingData:n,signalingExtensionOptions:t}=(this._validateBeforeAccept(i),this._isProcessingSignaling=!0,E.createAcceptSignaling(s)),a=yield this._sendSignaling(e,t);return 0===(null==a?void 0:a.code)?(m.updateSignaling(n),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}cancel(a){return i(this,void 0,void 0,function*(){try{var i=a["inviteID"],{signaling:e,signalingExtensionOptions:n}=(this._validateBeforeCancel(i),this._isProcessingSignaling=!0,E.createCancelSignaling(a)),t=yield this._sendSignaling(e,n);return 0===(null==t?void 0:t.code)?(m.removeSignaling(i),Object.assign(Object.assign({},t),{inviteID:i})):t}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}reject(a){return i(this,void 0,void 0,function*(){try{var i=a["inviteID"],{signaling:e,signalingExtensionOptions:n}=(this._validateBeforeReject(i),this._isProcessingSignaling=!0,E.createRejectSignaling(a)),t=yield this._sendSignaling(e,n);return 0===(null==t?void 0:t.code)?(m.removeSignaling(i),Object.assign(Object.assign({},t),{inviteID:i})):t}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}modifyInvitation(e){return i(this,void 0,void 0,function*(){var t,a,{inviteID:s,data:r}=e;let o="";try{this._validateBeforeModifyInvitation(s);let i=m.getSignaling(s),e=i["signaling"],n=function(i,e){var n={};for(a in i)Object.prototype.hasOwnProperty.call(i,a)&&e.indexOf(a)<0&&(n[a]=i[a]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols)for(var t=0,a=Object.getOwnPropertySymbols(i);t<a.length;t++)e.indexOf(a[t])<0&&Object.prototype.propertyIsEnumerable.call(i,a[t])&&(n[a[t]]=i[a[t]]);return n}(i,["signaling"]);o=e.payload.data,n.data=r,e.payload.data=JSON.stringify(n);var g=yield null==(a=null==(t=this._core)?void 0:t.message.messageAction)?void 0:a.modifyMessage(e);return m.hasSignaling(s)&&m.saveSignaling(s,Object.assign(Object.assign({},n),{signaling:e})),g}catch(t){if(o){let i=m.getSignaling(s)["signaling"];i.payload.data=o}throw t}})}getSignalingInfo(i){var{ssoLog:n,utils:{safeStringify:t}}=this._core;if(0!==S([i]).length)return i={businessID:(i=p(i)).businessID||1,inviteID:i.inviteID,groupID:i.groupID||"",inviter:i.inviter||"",inviteeList:i.inviteeList||[],data:i.data||"",actionType:i.actionType||e,timeout:i.timeout||0},n.debug(d+" getSignalingInfo "+t(i)),i}addSignalingListener(i,e,n){var t;i===(null==(t=this._core)?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!0),null!=(t=this._core)&&t.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t;i===(null==(t=this._core)?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&m.setSignalingListenStatus(!1),null!=(t=this._core)&&t.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){var e=m.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();e.has(i)&&(e=e.get(i).inviter===n,this._sendTimeoutNotice({inviteID:i,isInviter:e}))}_sendSignaling(e,n){return i(this,void 0,void 0,function*(){var i;return null==(i=null==(i=null==(i=this._core)?void 0:i.message)?void 0:i.messageSender)?void 0:i.sendMessage(e,n)})}_sendTimeoutNotice(n){return i(this,void 0,void 0,function*(){var a;this._core.ssoLog.debug("_sendTimeoutNotice",d+"._sendTimeoutNotice params:"+JSON.stringify(n));let{isInviter:s,inviteID:r}=n,{signaling:o,signalingData:g,signalingExtensionOptions:i}=E.createTimeoutSignaling(n),e=yield this._sendSignaling(o,i);if(0===(null==e?void 0:e.code)){let{data:i,groupID:e,inviteeList:n,inviter:t}=g;null!=(a=this._core)&&a.notificationCenter.emitOuterEvent(null==(a=this._core)?void 0:a.SignalingEvent.INVITATION_TIMEOUT,{name:null==(a=this._core)?void 0:a.SignalingEvent.INVITATION_TIMEOUT,data:{data:i,groupID:e,inviteID:r,inviteeList:n,inviter:t,isSelfTimeout:!0,message:o}}),s?m.removeSignaling(r):m.updateSignaling(g)}})}_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){var e=l,i=i["userID"],n=this._core.common.getCurrentUserID();if(i===n)throw new this._core.helper.ChatError({functionName:e,message:`cannot invite yourself, currentUserId:${n}, inviteeId:`+i})}_validateBeforeAccept(e){var n=c;this._validateInviteId(n,e),this._validateProcessStatus(n);let t=this._core.common.getCurrentUserID(),i=m.getSignaling(e)["inviteeList"];if(!i.includes(t)){let i=`userID:${t} not in inviteeList. inviteID:`+e;throw new this._core.helper.ChatError({functionName:n,message:i})}}_validateBeforeCancel(i){var e=v,n=(this._validateInviteId(e,i),this._validateProcessStatus(e),this._core.common.getCurrentUserID()),t=m.getSignaling(i)["inviter"];if(t!==n){let i=`unmatched inviter:${t} and my userID:`+n;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(e){var n=u;this._validateInviteId(n,e),this._validateProcessStatus(n);let t=this._core.common.getCurrentUserID(),i=m.getSignaling(e)["inviteeList"];if(!i.includes(t)){let i=`userID:${t} not in inviteeList. inviteID:`+e;throw new this._core.helper.ChatError({functionName:n,message:i})}}_validateBeforeModifyInvitation(i){var e=h;this._validateInviteId(e,i)}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}},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).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=>{var e,n=p(i);n&&null!=(e=this._actionProcessor.get(n.actionType))&&e(n,i)})}_handleMessageReceived(i){m.getSignalingListenStatus()&&0!==(i=S(i.data)).length&&this.handleActionSignaling(i)}_handleMessageModified(i){m.getSignalingListenStatus()&&0<(i=S(i.data)).length&&i.forEach(i=>{var e=p(i);e&&this._onInvitationModified(e,i)})}_onNewInvitationReceived(e,n){var t=d+"._onNewInvitationReceived",{inviteID:a,inviteeList:s,groupID:r}=e,o=this._core.common.getCurrentUserID();if(this._core.ssoLog.debug("_onNewInvitationReceived",t+` signalingData:${JSON.stringify(e)}}`),!r||s.includes(o)){let i=e["timeout"];var t=Date.now()/1e3-n.time,r=(0<i&&0<t&&i>t&&(i-=t),m.getSignaling(a));r!==e&&(r||m.saveSignaling(a,Object.assign(Object.assign({},e),{signaling:n})),0<i&&(null!=(o=this._core)&&o.helper.taskScheduler.addOnceTask({id:I+"-"+a,intervalMs:1e3*i,callback:O.handleInvitationExpiryTimer.bind(O,a)})),this._emitEvent({name:null==(t=this._core)?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(e)),{inviteeList:s})}))}}_onInviteeRejected(i){var e=d+"._onInviteeRejected",{inviteID:n,inviter:t,groupID:a,inviteeList:s}=i,r=m.hasSignaling(n);this._core.ssoLog.debug("_onInviteeRejected",e+` inviteID:${n} hasInviteID:${r} inviter:${t} groupID:`+a),r&&(m.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:s[0]})}))}_onInviteeAccepted(i){var e=d+"._onInviteeAccepted",{inviteID:n,inviter:t,groupID:a,inviteeList:s}=i,r=m.hasSignaling(n);this._core.ssoLog.debug("_onInviteeAccepted",e+` inviteID:${n} hasInviteID:${r} inviter:${t} groupID:`+a),r&&(m.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:s[0]})}))}_onInvitationCancelled(i){var e=d+"._onInvitationCancelled",{inviteID:n,inviter:t,groupID:a}=i,s=m.hasSignaling(n);this._core.ssoLog.debug("_onInvitationCancelled",e+` inviteID:${n} hasInviteID:${s} inviter:${t} groupID:`+a),s&&(m.removeSignaling(n),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){var e=d+"._onInvitationTimeout",{inviteID:n,inviteeList:t}=i,a=m.hasSignaling(n);this._core.ssoLog.debug("_onInvitationTimeout",e+` inviteID:${n} hasInviteID:${a} data:`+i.data),a&&(m.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:t,isSelfTimeout:!1})}))}_onInvitationModified(i,e){var n=d+"._onInvitationModified",{inviteID:t,data:a}=i,s=m.hasSignaling(t);this._core.ssoLog.debug("_onInvitationModified",n+` inviteID:${t} data:`+a),s&&(m.saveSignaling(t,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null==(n=this._core)?void 0:n.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:t,data:a}}))}_emitEvent(i){var e;null!=(e=this._core)&&e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){var{inviteID:i,inviter:e,groupID:n,data:t}=i;return{inviteID:i,inviter:e,groupID:n,data:t||""}}_dispose(){var i;null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}},y=new class{constructor(){this._offlineSignalingMap=new Map}init(i){var{notificationCenter:i,helper:e,constants:{InnerEvent:n,WORKFLOW_STEP:t,WORKFLOW_NAME:a}}=this._core=i;i.subscribeInnerEvent(n.DESTROY,this._dispose,this),i.subscribeInnerEvent(n.LOGOUT,this._reset,this),e.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,t.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){var{result:{unreadMessageMap:i}={}}=i||{};if(0!==(null==i?void 0:i.size)&&m.getSignalingListenStatus()){i=S([...i.values()]);if(0!==i.length&&(i.forEach(i=>{this._handleC2CActionType(i)}),0<this._offlineSignalingMap.size)){let i=this._sortOfflineSignalingByTime();b.handleActionSignaling(i)}}}_handleC2CActionType(i){var n,t=p(i);t&&(n=t["actionType"],n===e?this._saveValidOfflineInvite(t,i):this._removeOfflineInvite(t))}_saveValidOfflineInvite(i,e){var{inviteID:n,inviteeList:t=[],timeout:a=0}=i,s=this._core.common.getCurrentUserID();t.includes(s)&&(t=Date.now()/1e3-e.time,0<a&&a<t&&0!==a||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]})))}_removeOfflineInvite(i){var{inviteID:i=""}=i;this._offlineSignalingMap.has(i)&&this._offlineSignalingMap.delete(i)}_sortOfflineSignalingByTime(){let e=[];return this._offlineSignalingMap.forEach(i=>{e=[...e,...i.signalingList]}),e.sort((i,e)=>i.time-e.time)}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};let 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(){function t(i,r,o,g){return new(o=o||Promise)(function(n,e){function t(i){try{s(g.next(i))}catch(i){e(i)}}function a(i){try{s(g.throw(i))}catch(i){e(i)}}function s(i){var e;i.done?n(i.value):((e=i.value)instanceof o?e:new o(function(i){i(e)})).then(t,a)}s((g=g.apply(i,r||[])).next())})}let l="signaling",o="signaling-timeout";function a(i){return i.filter(i=>{var e,n;return"TIMCustomElem"===i.type&&({cloudCustomData:i="",payload:{data:n=""}={}}=i,i=i.match(/"type":"tsignaling"/),e=n.match(/inviteID/),n=n.match(/actionType/),i||e&&n)})}function s(i){var e=i.payload["data"];try{return JSON.parse(e)}catch(i){return console.error(i),null}}function i(i,e){return i.toString(16).padStart(e,"0")}function e(i){if(i<0||53<i)throw new Error("Number of digits must be between 0 and 53");return i<=30?Math.floor(Math.random()*(1<<i)):(i=i-30,Math.floor(Math.random()*(1<<i))*(1<<30)+Math.floor(Math.random()*(1<<30)))}var c=new class{constructor(){this._onlineSignalingMap=new Map,this._isSignalingListening=!1}init(i){(this._core=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){l;var{inviteID:i,groupID:e,inviter:n}=i,n=n===this._core.common.getCurrentUserID();if(!this._onlineSignalingMap.has(i))return!1;var t=this._onlineSignalingMap.get(i)["inviteeList"];if(console.log(`signaling.isInvitationValid inviteID:${i} inviteeList:${t} groupID:`+e),e){if(n)return 0<t.length;let i=this._core.common.getCurrentUserID();return t.includes(i)}return!0}updateSignaling(i){let n=l+".updateSignaling",{inviteID:t,inviter:e,inviteeList:a,groupID:s}=i;if(console.log(n+` inviteID:${t} inviter:${e} groupID:`+s),s&&this.hasSignaling(t)){let i=a[0],e=this._onlineSignalingMap.get(t)["inviteeList"];e.includes(i)&&(e.splice(e.indexOf(i),1),console.log(n+` remove ${i}. localInviteeList.length:`+e.length)),0===e.length&&this.removeSignaling(t)}else this.removeSignaling(t)}setSignalingListenStatus(i){this._isSignalingListening=i}getSignalingListenStatus(){return this._isSignalingListening}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),this._isSignalingListening=!1}_reset(){this._onlineSignalingMap.clear()}},v=new class{init(i){this._core=i}createInviteSignaling(i){var e=this._generateInviteID(),e=this._createInviteSignalingData(Object.assign(Object.assign({},i),{inviteID:e})),{groupID:n,inviteeList:t}=e,n=n||t[0];return{signaling:this._createSignaling(e,n),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createAcceptSignaling(i){var e=this._createAcceptSignalingData(i),{groupID:n,inviter:t}=e;return{signaling:this._createSignaling(e,n||t),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createCancelSignaling(i){var e=this._createCancelSignalingData(i),{groupID:n,inviteeList:t}=e,n=n||t[0];return{signaling:this._createSignaling(e,n),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createRejectSignaling(i){var e=this._createRejectSignalingData(i),{groupID:n,inviter:t}=e;return{signaling:this._createSignaling(e,n||t),signalingData:e,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}createTimeoutSignaling(i){var{isInviter:e=!1}=i,i=this._createTimeoutSignalingData(i),{groupID:n,inviteeList:t,inviter:a}=i,n=n||(e?t[0]:a);return{signaling:this._createSignaling(i,n),signalingData:i,signalingExtensionOptions:this._createSignalingExtensionOptions(i)}}_createSignalingExtensionOptions(i){var{data:i="",onlineUserOnly:e,inviteID:n="",offlinePushInfo:t,actionType:a}=i,n=(null==(n=null==(n=c.getSignaling(n))?void 0:n.signaling)?void 0:n._onlineOnlyFlag)||!1;return{onlineUserOnly:e||n,offlinePushInfo:t,messageControlInfo:this._createMessageControlInfo(i,a)}}_createMessageControlInfo(i,e){e=5===e&&!!i.match(/excludeTimeoutSignalingFromHistoryMessage/),i=!!i.match(/excludeFromHistoryMessage/)||!!i.match(/excludeOriginalSignalingFromHistoryMessage/);return{excludedFromContentModeration:!0,excludedFromUnreadCount:e||i,excludedFromLastMessage:e||i}}_createInviteSignalingData(i){l;var{userID:e,timeout:n=0,groupID:t="",inviteeList:a=[]}=i,s=this._core.common.getCurrentUserID(),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:1,inviter:s,inviteeList:t?a:[e],timeout:n});return console.log("signaling._createInviteSignalingData signalingData:",i),i}_createAcceptSignalingData(i){l;var e=i["inviteID"],n=this._core.common.getCurrentUserID(),{inviter:e,groupID:t}=c.getSignaling(e),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:3,groupID:t,inviter:e,inviteeList:[n]});return console.log("signaling._createAcceptSignalingData signalingData:",i),i}_createCancelSignalingData(i){l;var e=i["inviteID"],n=this._core.common.getCurrentUserID(),{inviteeList:e,groupID:t}=c.getSignaling(e),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:2,groupID:t,inviter:n,inviteeList:e});return console.log("signaling._createCancelSignalingData signalingData:",i),i}_createRejectSignalingData(i){l;var e=i["inviteID"],n=this._core.common.getCurrentUserID(),{inviter:e,groupID:t}=c.getSignaling(e),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:4,groupID:t,inviter:e,inviteeList:[n]});return console.log("signaling._createRejectSignalingData signalingData:",i),i}_createTimeoutSignalingData(i){l;var{isInviter:e=!1,inviteID:n}=i,{inviteeList:n,inviter:t}=c.getSignaling(n),a=this._core.common.getCurrentUserID(),i=Object.assign(Object.assign({},this._generateBaseSignalData(i)),{actionType:5,inviter:t,inviteeList:e?n:[a]});return console.log("signaling._createTimeoutSignalingData signalingData:",i),i}_createSignaling(i,e){var{groupID:n=""}=i,e={to:e,conversationType:n?"GROUP":"C2C",priority:"High",payload:{data:JSON.stringify(i)}};return null==(n=null==(i=null==(n=this._core)?void 0:n.message)?void 0:i.messageFactory)?void 0:n.createCustomMessage(e)}_generateInviteID(){return[i(e(32),8),i(e(16),4),i(16384|e(12),4),i(32768|e(14),4),i(e(48),12)].join("-")}_generateBaseSignalData(i){var{data:i="",inviteID:e="",groupID:n=""}=i;return{businessID:1,timeout:0,data:i,inviteID:e,groupID:n}}},g=new class{constructor(){this._isProcessingSignaling=!1}init(i){(this._core=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 t(this,void 0,void 0,function*(){var n;try{this._validateBeforeInvite(e);var{signaling:t,signalingData:a,signalingExtensionOptions:i}=v.createInviteSignaling(e),s=yield this._sendSignaling(t,i);if(0!==(null==s?void 0:s.code))return s;{let{inviteID:i,timeout:e}=a;return c.saveSignaling(i,Object.assign(Object.assign({},a),{signaling:t})),0<e&&(null!=(n=this._core)&&n.helper.taskScheduler.addOnceTask({id:o+"-"+i,intervalMs:1e3*(e+5),callback:this.handleInvitationExpiryTimer.bind(this,i)})),Object.assign(Object.assign({},s),{inviteID:i})}}catch(n){throw n}})}accept(s){return t(this,void 0,void 0,function*(){try{var i=s["inviteID"],{signaling:e,signalingData:n,signalingExtensionOptions:t}=(this._validateBeforeAccept(i),this._isProcessingSignaling=!0,v.createAcceptSignaling(s)),a=yield this._sendSignaling(e,t);return 0===(null==a?void 0:a.code)?(c.updateSignaling(n),Object.assign(Object.assign({},a),{inviteID:i})):a}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}cancel(a){return t(this,void 0,void 0,function*(){try{var i=a["inviteID"],{signaling:e,signalingExtensionOptions:n}=(this._validateBeforeCancel(i),this._isProcessingSignaling=!0,v.createCancelSignaling(a)),t=yield this._sendSignaling(e,n);return 0===(null==t?void 0:t.code)?(c.removeSignaling(i),Object.assign(Object.assign({},t),{inviteID:i})):t}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}reject(a){return t(this,void 0,void 0,function*(){try{var i=a["inviteID"],{signaling:e,signalingExtensionOptions:n}=(this._validateBeforeReject(i),this._isProcessingSignaling=!0,v.createRejectSignaling(a)),t=yield this._sendSignaling(e,n);return 0===(null==t?void 0:t.code)?(c.removeSignaling(i),Object.assign(Object.assign({},t),{inviteID:i})):t}catch(i){throw i}finally{this._isProcessingSignaling=!1}})}modifyInvitation(i){return t(this,void 0,void 0,function*(){var t,a,{inviteID:s,data:r}=i;let o="";try{this._validateBeforeModifyInvitation(s);let i=c.getSignaling(s),e=i["signaling"],n=function(i,e){var n={};for(a in i)Object.prototype.hasOwnProperty.call(i,a)&&e.indexOf(a)<0&&(n[a]=i[a]);if(null!=i&&"function"==typeof Object.getOwnPropertySymbols)for(var t=0,a=Object.getOwnPropertySymbols(i);t<a.length;t++)e.indexOf(a[t])<0&&Object.prototype.propertyIsEnumerable.call(i,a[t])&&(n[a[t]]=i[a[t]]);return n}(i,["signaling"]);o=e.payload.data,n.data=r,e.payload.data=JSON.stringify(n);var g=yield null==(a=null==(t=this._core)?void 0:t.message.messageAction)?void 0:a.modifyMessage(e);return c.hasSignaling(s)&&c.saveSignaling(s,Object.assign(Object.assign({},n),{signaling:e})),g}catch(t){if(o){let i=c.getSignaling(s)["signaling"];i.payload.data=o}throw t}})}getSignalingInfo(i){var{ssoLog:e,utils:{safeStringify:n}}=this._core;if(0!==a([i]).length)return i={businessID:(i=s(i)).businessID||1,inviteID:i.inviteID,groupID:i.groupID||"",inviter:i.inviter||"",inviteeList:i.inviteeList||[],data:i.data||"",actionType:i.actionType||1,timeout:i.timeout||0},e.debug(l+" getSignalingInfo "+n(i)),i}addSignalingListener(i,e,n){var t;i===(null==(t=this._core)?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&c.setSignalingListenStatus(!0),null!=(t=this._core)&&t.notificationCenter.subscribeOuterEvent(i,e,n)}removeSignalingListener(i,e,n){var t;i===(null==(t=this._core)?void 0:t.SignalingEvent.NEW_INVITATION_RECEIVED)&&c.setSignalingListenStatus(!1),null!=(t=this._core)&&t.notificationCenter.unSubscribeOuterEvent(i,e,n)}handleInvitationExpiryTimer(i){var e=c.getOnlineSignalingMap(),n=this._core.common.getCurrentUserID();e.has(i)&&(e=e.get(i).inviter===n,this._sendTimeoutNotice({inviteID:i,isInviter:e}))}_sendSignaling(e,n){return t(this,void 0,void 0,function*(){var i;return null==(i=null==(i=null==(i=this._core)?void 0:i.message)?void 0:i.messageSender)?void 0:i.sendMessage(e,n)})}_sendTimeoutNotice(n){return t(this,void 0,void 0,function*(){var a;this._core.ssoLog.debug("_sendTimeoutNotice",l+"._sendTimeoutNotice params:"+JSON.stringify(n));let{isInviter:s,inviteID:r}=n,{signaling:o,signalingData:g,signalingExtensionOptions:i}=v.createTimeoutSignaling(n),e=yield this._sendSignaling(o,i);if(0===(null==e?void 0:e.code)){let{data:i,groupID:e,inviteeList:n,inviter:t}=g;null!=(a=this._core)&&a.notificationCenter.emitOuterEvent(null==(a=this._core)?void 0:a.SignalingEvent.INVITATION_TIMEOUT,{name:null==(a=this._core)?void 0:a.SignalingEvent.INVITATION_TIMEOUT,data:{data:i,groupID:e,inviteID:r,inviteeList:n,inviter:t,isSelfTimeout:!0,message:o}}),s?c.removeSignaling(r):c.updateSignaling(g)}})}_validateInviteId(i,e){if(!c.hasSignaling(e))throw new this._core.helper.ChatError({functionName:i,code:8010})}_validateProcessStatus(i){if(this._isProcessingSignaling)throw new this._core.helper.ChatError({functionName:i,message:"processing other signaling operations"})}_validateBeforeInvite(i){var i=i["userID"],e=this._core.common.getCurrentUserID();if(i===e)throw new this._core.helper.ChatError({functionName:"invite",message:`cannot invite yourself, currentUserId:${e}, inviteeId:`+i})}_validateBeforeAccept(e){var n="accept";this._validateInviteId(n,e),this._validateProcessStatus(n);let t=this._core.common.getCurrentUserID(),i=c.getSignaling(e)["inviteeList"];if(!i.includes(t)){let i=`userID:${t} not in inviteeList. inviteID:`+e;throw new this._core.helper.ChatError({functionName:n,message:i})}}_validateBeforeCancel(i){var e="cancel",n=(this._validateInviteId(e,i),this._validateProcessStatus(e),this._core.common.getCurrentUserID()),t=c.getSignaling(i)["inviter"];if(t!==n){let i=`unmatched inviter:${t} and my userID:`+n;throw new this._core.helper.ChatError({functionName:e,message:i})}}_validateBeforeReject(e){var n="reject";this._validateInviteId(n,e),this._validateProcessStatus(n);let t=this._core.common.getCurrentUserID(),i=c.getSignaling(e)["inviteeList"];if(!i.includes(t)){let i=`userID:${t} not in inviteeList. inviteID:`+e;throw new this._core.helper.ChatError({functionName:n,message:i})}}_validateBeforeModifyInvitation(i){this._validateInviteId("modifyInvitation",i)}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this)}_reset(){this._isProcessingSignaling=!1}},n=new class{constructor(){this._actionProcessor=new Map([[1,this._onNewInvitationReceived.bind(this)],[4,this._onInviteeRejected.bind(this)],[3,this._onInviteeAccepted.bind(this)],[2,this._onInvitationCancelled.bind(this)],[5,this._onInvitationTimeout.bind(this)]])}init(i){(this._core=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=>{var e,n=s(i);n&&null!=(e=this._actionProcessor.get(n.actionType))&&e(n,i)})}_handleMessageReceived(i){c.getSignalingListenStatus()&&0!==(i=a(i.data)).length&&this.handleActionSignaling(i)}_handleMessageModified(i){c.getSignalingListenStatus()&&0<(i=a(i.data)).length&&i.forEach(i=>{var e=s(i);e&&this._onInvitationModified(e,i)})}_onNewInvitationReceived(e,n){l;var{inviteID:t,inviteeList:a,groupID:s}=e,r=this._core.common.getCurrentUserID();if(this._core.ssoLog.debug("_onNewInvitationReceived",`signaling._onNewInvitationReceived signalingData:${JSON.stringify(e)}}`),!s||a.includes(r)){let i=e["timeout"];var s=Date.now()/1e3-n.time,r=(0<i&&0<s&&i>s&&(i-=s),c.getSignaling(t));r!==e&&(r||c.saveSignaling(t,Object.assign(Object.assign({},e),{signaling:n})),0<i&&(null!=(s=this._core)&&s.helper.taskScheduler.addOnceTask({id:o+"-"+t,intervalMs:1e3*i,callback:g.handleInvitationExpiryTimer.bind(g,t)})),this._emitEvent({name:null==(r=this._core)?void 0:r.SignalingEvent.NEW_INVITATION_RECEIVED,data:Object.assign(Object.assign({},this._generateBaseEmitData(e)),{inviteeList:a})}))}}_onInviteeRejected(i){l;var{inviteID:e,inviter:n,groupID:t,inviteeList:a}=i,s=c.hasSignaling(e);this._core.ssoLog.debug("_onInviteeRejected",`signaling._onInviteeRejected inviteID:${e} hasInviteID:${s} inviter:${n} groupID:`+t),s&&(c.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITEE_REJECTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:a[0]})}))}_onInviteeAccepted(i){l;var{inviteID:e,inviter:n,groupID:t,inviteeList:a}=i,s=c.hasSignaling(e);this._core.ssoLog.debug("_onInviteeAccepted",`signaling._onInviteeAccepted inviteID:${e} hasInviteID:${s} inviter:${n} groupID:`+t),s&&(c.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITEE_ACCEPTED,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{invitee:a[0]})}))}_onInvitationCancelled(i){l;var{inviteID:e,inviter:n,groupID:t}=i,a=c.hasSignaling(e);this._core.ssoLog.debug("_onInvitationCancelled",`signaling._onInvitationCancelled inviteID:${e} hasInviteID:${a} inviter:${n} groupID:`+t),a&&(c.removeSignaling(e),this._emitEvent({name:null==(n=this._core)?void 0:n.SignalingEvent.INVITATION_CANCELLED,data:this._generateBaseEmitData(i)}))}_onInvitationTimeout(i){l;var{inviteID:e,inviteeList:n}=i,t=c.hasSignaling(e);this._core.ssoLog.debug("_onInvitationTimeout",`signaling._onInvitationTimeout inviteID:${e} hasInviteID:${t} data:`+i.data),t&&(c.updateSignaling(i),this._emitEvent({name:null==(e=this._core)?void 0:e.SignalingEvent.INVITATION_TIMEOUT,data:Object.assign(Object.assign({},this._generateBaseEmitData(i)),{inviteeList:n,isSelfTimeout:!1})}))}_onInvitationModified(i,e){l;var{inviteID:n,data:t}=i,a=c.hasSignaling(n);this._core.ssoLog.debug("_onInvitationModified",`signaling._onInvitationModified inviteID:${n} data:`+t),a&&(c.saveSignaling(n,Object.assign(Object.assign({},i),{signaling:e})),this._emitEvent({name:null==(a=this._core)?void 0:a.SignalingEvent.INVITATION_MODIFIED,data:{inviteID:n,data:t}}))}_emitEvent(i){var e;null!=(e=this._core)&&e.notificationCenter.emitOuterEvent(i.name,i)}_generateBaseEmitData(i){var{inviteID:i,inviter:e,groupID:n,data:t}=i;return{inviteID:i,inviter:e,groupID:n,data:t||""}}_dispose(){var i;null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_RECEIVED,this._handleMessageReceived,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.OuterEvent.MESSAGE_MODIFIED,this._handleMessageModified,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeOuterEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}},r=new class{constructor(){this._offlineSignalingMap=new Map}init(i){var{notificationCenter:i,helper:e,constants:{InnerEvent:n,WORKFLOW_STEP:t,WORKFLOW_NAME:a}}=this._core=i;i.subscribeInnerEvent(n.DESTROY,this._dispose,this),i.subscribeInnerEvent(n.LOGOUT,this._reset,this),e.registerWorkflowStep(a.SYNC_SERVER_INFO_AFTER_LOGIN,t.SIGNALING_MESSAGE_RECOVER,this._handleC2COfflineMessage,this)}_handleC2COfflineMessage(i){var{result:{unreadMessageMap:i}={}}=i||{};if(0!==(null==i?void 0:i.size)&&c.getSignalingListenStatus()){i=a([...i.values()]);if(0!==i.length&&(i.forEach(i=>{this._handleC2CActionType(i)}),0<this._offlineSignalingMap.size)){let i=this._sortOfflineSignalingByTime();n.handleActionSignaling(i)}}}_handleC2CActionType(i){var e,n=s(i);n&&(e=n["actionType"],1===e?this._saveValidOfflineInvite(n,i):this._removeOfflineInvite(n))}_saveValidOfflineInvite(i,e){var{inviteID:n,inviteeList:t=[],timeout:a=0}=i,s=this._core.common.getCurrentUserID();t.includes(s)&&(t=Date.now()/1e3-e.time,0<a&&a<t&&0!==a||this._offlineSignalingMap.set(n,Object.assign(Object.assign({},i),{signalingList:[e]})))}_removeOfflineInvite(i){var{inviteID:i=""}=i;this._offlineSignalingMap.has(i)&&this._offlineSignalingMap.delete(i)}_sortOfflineSignalingByTime(){let e=[];return this._offlineSignalingMap.forEach(i=>{e=[...e,...i.signalingList]}),e.sort((i,e)=>i.time-e.time)}_dispose(){var i;this._reset(),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.LOGOUT,this._reset,this),null!=(i=this._core)&&i.notificationCenter.unSubscribeInnerEvent(this._core.InnerEvent.DESTROY,this._dispose,this)}_reset(){this._offlineSignalingMap.clear()}};let h={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}}},u={invite:!0,cancel:!0,accept:!0,reject:!0,modifyInvitation:!0};return new class{constructor(){this.name="Signaling"}install(i){g.init(i),n.init(i),v.init(i),c.init(i),r.init(i),i.helper.registerValidateConfig({auth:u,params:h})}}});
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
- let 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=>{var{common:{getMessagePreviewText:s}}=e["core"];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(var 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){var{common:{updateGroupAtInfo:s}}=e["core"];s(t,this.groupAtInfoList)}updateTopic(t){var{utils:{isUndefined:s},common:{deepMerge:i}}=e["core"];s(t.selfInfo)||this.updateSelfInfo(t.selfInfo),s(t.muteAllMembers)||(this.muteAllMembers=1===t.muteAllMembers),i(this,t,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(t){var{common:{deepMerge:s}}=e["core"];return 0<s(this.selfInfo,t,[],[""])}reduceUnreadCount(){return 1<=this.unreadCount&&(--this.unreadCount,!0)}isLastMessageRevoked(e){return e===(null==(e=this.lastMessage)?void 0:e.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){var{helper:t,constants:s,notificationCenter:i,InnerEvent:{LOGOUT:o,DESTROY:n}}=this._core=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),i.subscribeInnerEvent(o,this._reset,this),i.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(this._name+".init")}_hasTopic(e,t){return!(null==(e=this._topicMap.get(e))||!e.get(t))}_setTopic(e,t,s){null!=(e=this._topicMap.get(e))&&e.set(t,s)}_deleteTopic(e,t){null!=(e=this._topicMap.get(e))&&e.delete(t)}getTopic(e,t){return null==(e=this._topicMap.get(e))?void 0:e.get(t)}updateTopic(e){e.forEach(e=>{var{groupID:t,topicID:s}=e;let i=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?null!=(i=this.getTopic(t,s))&&i.updateTopic(e):(this._getLastMessage(e),i=new h(e),this._setTopic(t,s,i));e=this.computeUnreadCount(i);null!=i&&i.updateUnreadCount(e)})}_getLastMessage(s){var{isEmpty:i,isUndefined:o,isArray:n}=this._core["utils"];if(!o(s.lastMsg)){let e=null,t="";t=(n(s.lastMsg.elements)?(e=s.lastMsg.elements[0].content,s.lastMsg.elements[0]):(e=s.lastMsg.elements.content,s.lastMsg.elements)).type;o={payload:e,type:t,time:s.lastMsg.time,sequence:s.lastMsg.sequence,from:s.lastMsg.from,nick:s.lastMsg.nick,avatar:s.lastMsg.avatar,version:s.lastMsg.messageVersion,cloudCustomData:s.lastMsg.cloudCustomData,isRevoked:2===s.lastMsg.isPlaceMessage,revoker:i(s.lastMsg.revokerInfo)?null:s.lastMsg.revokerInfo.revoker};s.lastMessage=o}}computeUnreadCount(s){let{excludedUnreadSequenceList:e=[],readedSequence:i}=s.selfInfo,o=s.nextMessageSeq-s.selfInfo.readedSequence-1;if(0<e.length){let t=0;e.forEach(e=>{e>i&&e<=s.nextMessageSeq-1&&(t+=1)}),1<=t&&(o-=t)}return 0<o?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach(e=>{var t=this._core["utils"]["isEmpty"],e=e["groupAtInfoList"];t(e)||this.updateGroupAtInfoList(e)})}updateGroupAtInfoList(s,r=!1){if(0!==s.length){let{common:{getCurrentUserID:e},utils:{isUndefined:o,isEmpty:t}}=this._core,n=e(),a=null;s.forEach(e=>{var{groupID:t,topicID:s,from:i}=e;i!==n&&((a=this.getTopic(t,s))&&a.updateGroupAtInfoList(e),o(a))&&r&&(this.updateTopic([{groupID:t,topicID:s}]),null!=(i=this.getTopic(t,s)))&&i.updateGroupAtInfoList(e)}),t(a)||this.emitTopicUpdate(a)}}getTopicList(e,t=[]){var e=this._topicMap.get(e),s=[];return e&&s.push(...e.values()),0<t.length?s.filter(e=>t.includes(e.topicID)):s}deleteTopicList(t,e=[]){this._topicMap.has(t)&&(e.forEach(e=>{this._hasTopic(t,e)&&this._deleteTopic(t,e)}),console.log(this._name+`.deleteTopicList groupID:${t} topicID:`+e.join(",")))}clearGroupAtInfoList(e,t){var s=this._core["utils"]["isEmpty"],e=this.getTopic(e,t);s(null==e?void 0:e.groupAtInfoList)||(null!=e&&e.clearGroupAtInfoList(),console.log(this._name+".clearGroupAtInfoList topicID:"+t))}markMessageAsRead(e,s){let{message:{messageDataHandler:t},OuterConstant:{CONV_GROUP:i}}=this._core,o=(null==t?void 0:t.getLocalMessageList(""+i+e))||[];if(0!==o.length){e=o["length"];for(let t=e-1;0<=t;t--){let e=o[t];if(!(s&&e.sequence>s)){if("in"===e.flow&&e.isRead)break;e.setIsRead(!0)}}}}updateUnreadCount(e,t){var{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:i}}=this._core;let o=!1;e=this.getTopic(e,t);e&&(s=(null==s?void 0:s.getLocalMessageList(""+i+t))||[],i=e.unreadCount,s=null==s?void 0:s.filter(e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted).length,console.log(this._name+`._updateUnreadCount topicID:${t} currentUnreadCount:${i} newUnreadCount:`+s),i!==s&&(e.updateUnreadCount(s),o=!0),o)&&this.emitTopicUpdate(e)}updateSelfInfo(e){var{groupID:e,topicID:t,selfInfo:s}=e,i=this.getTopic(e,t);if(i){let e=i.updateSelfInfo(s);console.log(`${this._name}.updateSelfInfo topicID:${t} info:${JSON.stringify(s)} isUpdated:`+e),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){var{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();var{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};let _=(t="")=>{var{OuterConstant:s}=e["core"];return t.replace(s.CONV_GROUP,"")},m=(e="")=>e.split(t)[0];var I=new class{constructor(){this._name="TopicMessageHandler"}init(e){var{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:i,WORKFLOW_STEP:o},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:a,TOPIC_MESSAGE_DELETED:r,TOPIC_MESSAGE_REVOKED:c,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=this._core=e,{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}=s["InnerEventSubType"];t.registerWorkflowStep(i.SYNC_SERVER_INFO_AFTER_LOGIN,o.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(i.RECEIVE_GROUP_NEW_MESSAGE,o.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(a,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleMessageDeleted,this),s.subscribeInnerEvent(c,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){let s,{WORKFLOW_STEP:t}=this._core.constants,{groupTipList:i=[]}=e.result[t.UNREAD_MESSAGE_SYNC]||{};for(let t=0;t<i.length;t++){let e=i[t];12===e.Event&&null!=(s=e.GroupTips[0].MsgBody)&&s.TopicId&&this._handleTopicAtTips(e)}}_handleNewMessage(e){var{conversationUpdateFieldList:e=[]}=e.result||{};if(0!==e.length){var t=this._core["common"]["isTopic"];if(t(e[0].conversationID)){let a=this._core["common"]["getCurrentUserID"];e.forEach(e=>{let{conversationID:t="",lastMessage:o}=e,s=_(t),i=m(s),n=g.getTopic(i,s);if(n){let{from:e,sequence:t}=o,s=t+1,i=e===a();if(s>n.nextMessageSeq){n.updateNextMessageSeq(s),n.updateLastMessage(o),n.updateReadSequence(t,i);let e=i?0:g.computeUnreadCount(n);n.updateUnreadCount(e),this._emitTopicUpdate(n)}}})}}}_handleMessageDeleted(s){var i=_(s),o=m(i),o=g.getTopic(o,i);if(console.log(this._name+`._handleMessageDeleted, topicID:${i} hasTopic:`+!!o),o){var{message:{messageDataHandler:n},utils:{isEmpty:a}}=this._core,r=(null==n?void 0:n.getLocalMessageList(s))||[];let t={};for(let e=(r.length||0)-1;0<=e;e--)if(!r[e].isDeleted){t=r[e];break}let e=!1;o.lastMessage.lastSequence!==t.sequence&&(a(t)&&(t={}),o.updateLastMessage(t),e=!0),e&&(this._emitTopicUpdate(o),console.log(this._name+`._handleMessageDeleted, topicID:${i} with lastMessage:`+o.lastMessage))}}_handleMessageRevoked(e){var{messageList:e=[]}=e;if(console.log(this._name+"._handleMessageRevoked messageList:"+e.length),0!==e.length){let o=null,n=!1;e.forEach(e=>{var{to:e,sequence:t,revoker:s}=e,i=m(e);if(o=g.getTopic(i,e)){o.reduceUnreadCount()&&(n=!0),o.isLastMessageRevoked(t)&&(o.setLastMessageRevoked(!0),o.setLastMessageRevoker(s),n=!0);let{excludedUnreadSequenceList:e=[]}=o.selfInfo;e.push(t),o.updateSelfInfo({excludedUnreadSequenceList:e})}}),n&&this._emitTopicUpdate(o)}}_handleMessageModified(e){let{conversationID:t,messageList:s}=e,i=_(t),o=m(i),r=g.getTopic(o,i);if(console.log(`${this._name}._handleMessageModified topicID:${i} hasTopic:`+!!r),r){var c=r["lastMessage"];if(c){let a=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(c,a)){let{getMessagePreviewText:e}=this._core["common"],{time:t,sequence:s,elements:i,cloudCustomData:o,messageVersion:n}=a;c.type=i[0].type,c.payload=i[0].content,c.messageForShow=e(c.type,c.payload),c.cloudCustomData=o,c.version=n,c.lastSequence=s,c.lastTime=t,this._emitTopicUpdate(r)}}}}_shouldUpdateLastMessage(e,t){var s;return null===e.payload||({sequence:t,messageVersion:s}=t,e.lastSequence===t&&e.version!==s)}_emitTopicUpdate(e){var{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){let{GroupTips:t=[]}=e,n=[],a=!1;t.forEach(e=>{var{GroupAtTips:e,MsgBody:t,MsgRandom:s,ClientSeq:i}=e;let o={};e?o=this._convertAtInfo(e):t&&(o=this._convertAtInfo(t),a=!0),o.__random=s,o.__sequence=i,n.push(o)}),g.updateGroupAtInfoList(n,a)}_convertAtInfo(e){var{From_Account:e,GroupId:t,TopicId:s,MsgSeq:i,GroupAtType:o}=e;return{groupID:t,topicID:s,from:e,sequence:i,groupAtType:o}}_handleLatestMessage(e){let{utils:{isEmpty:t},message:{messageHelper:i,messageFactory:o}}=this._core,{NewMsg:n,NoUnreadSeqList:a}=e||{};if(!t(n)){let{TopicId:e}=n["GroupInfo"],t=i.parseServerGroupMessage(n),s=o.createMessage(t);s.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:s,excludedUnreadSequenceList:a})}}_updateUnreadCountAndLastMessage(e){let{isUndefined:t}=this._core["utils"],{topicID:s,message:i,excludedUnreadSequenceList:o}=e,n=m(s),a=g.getTopic(n,s);if(a){let e=a.selfInfo.excludedUnreadSequenceList||[];if(t(o)||(e=o),i._isExcludedFromUnreadCount&&e.push(i.sequence),a.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${i.sequence} lastMessage sequence:`+a.lastMessage.lastSequence),i.sequence>a.lastMessage.lastSequence){let e=i.sequence+1,t=(a.updateLastMessage(i),a.updateNextMessageSeq(e),g.computeUnreadCount(a));a.updateUnreadCount(t),this._emitTopicUpdate(a)}}}_dispose(){var{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:i,TOPIC_MESSAGE_REVOKED:o,TOPIC_MESSAGE_MODIFIED:n,DESTROY:a}}=this._core,{TOPIC_AT_TIPS:r,TOPIC_LATEST_MESSAGE:c}=e["InnerEventSubType"];e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(i,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(o,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,r,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,c,this._handleLatestMessage,this),e.unSubscribeInnerEvent(a,this._dispose,this)}},T=new class{constructor(){this._name="TopicOperationHandler"}init(e){var{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:i}}=this._core=e,{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:a}=t["InnerEventSubType"];t.subscribeInnerEvent(s,o,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,a,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(i,this._dispose,this),e.ssoLog.debug(this._name+".init")}_handleTopicTips(e){var{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){var i=t[e];i.MsgBody.OpType!==s?i.MsgBody.OpType!==o?this._emitTopicTips(i):this._handleTopicProfileUpdated(i):this._handleMuteTimeUpdated(i)}}_handleMuteTimeUpdated(e){var{getCurrentUserID:t}=this._core["common"],{GroupInfo:{GroupId:s,TopicId:i},MsgBody:{MsgMemberInfo:o=[]}}=e;for(let e=0;e<o.length;e++){var{User_Account:n,ShutupTime:a}=o[e];if(n===t()&&0<=a){g.updateSelfInfo({groupID:s,topicID:i,selfInfo:{muteTime:a}});break}}}_handleTopicProfileUpdated(e){var{GroupInfo:{GroupId:e,TopicId:t},MsgBody:{MsgTopicNewInfo:{CustomString:s,ShutupAll:i,TopicFaceUrl:o,TopicIntroduction:n,TopicName:a,TopicNotification:r}}}=e,c=g.getTopic(e,t);c&&(c.updateTopic({groupID:e,topicID:t,customData:s,muteAllMembers:i,avatar:o,introduction:n,topicName:a,notification:r}),g.emitTopicUpdate(c))}_handleTopicNotices(e){var{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){var s=t[e],{MsgBody:{ReportType:o,TopicIdArray:a,MsgFlag:c},GroupInfo:{GroupId:u,TopicId:p}}=s;o===i?this._handleTopicCreated({groupID:u,topicID:p}):o===n?this._handleTopicDeleted({groupID:u,topicIDList:a}):o===r?this._handleMessageRemindTypeUpdated({groupID:u,topicID:p,messageRemindType:c}):this._emitTopicNotices(s)}}_handleTopicCreated(e){var{OuterEvent:t,notificationCenter:s}=this._core;g.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){var{OuterEvent:t,notificationCenter:s}=this._core,{groupID:i,topicIDList:o=[]}=e;g.deleteTopicList(i,o),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){var{groupID:e,topicID:t,messageRemindType:s}=e;g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{messageRemindType:s}})}_handleTopicMessageReadSynced(e){let t=e["GroupTips"],{isUndefined:s}=this._core["utils"];t.forEach(e=>{e=e.MsgBody.GroupReadInfoArray;s(e)||e.forEach(e=>{var{GroupId:e,TopicId:t,LastReadMsgSeq:s}=e;console.log(this._name+`._handleTopicMessageReadSynced topicID:${t} lastReadSequence:`+s),g.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),g.clearGroupAtInfoList(e,t),g.markMessageAsRead(t,s),g.updateUnreadCount(e,t)})})}_emitTopicTips(e){var{message:{messageHelper:t,messageFactory:s},OuterConstant:i,OuterEvent:o,notificationCenter:n}=this._core,t=(e.MsgBody.MsgType=i.MSG_GRP_TIP,t.parseServerGroupMessage(e)),e=(t.flow="in",t.conversationType=i.CONV_GROUP,s.createMessage(t));e.setElement(t.elements),e.isSystemMessage=!0,n.emitOuterEvent(o.MESSAGE_RECEIVED,{name:o.MESSAGE_RECEIVED,data:[e]})}_emitTopicNotices(e){var{message:{messageHelper:t,messageFactory:s},OuterConstant:i,OuterEvent:o,notificationCenter:n}=this._core,t=(e.MsgBody.MsgType=i.MSG_GRP_SYS_NOTICE,t.parseServerGroupMessage(e)),e=(t.flow="in",t.conversationType=i.CONV_SYSTEM,t.conversationID=i.CONV_SYSTEM,s.createMessage(t));e.setElement(t.elements),e.isSystemMessage=!0,n.emitOuterEvent(o.MESSAGE_RECEIVED,{name:o.MESSAGE_RECEIVED,data:[e]})}_dispose(){var{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:o,TOPIC_MESSAGE_READ_SYNC:n}=e["InnerEventSubType"];e.unSubscribeInnerEvent(t,i,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,o,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function f(e,a,r,c){return new(r=r||Promise)(function(s,t){function i(e){try{n(c.next(e))}catch(e){t(e)}}function o(e){try{n(c.throw(e))}catch(e){t(e)}}function n(e){var t;e.done?s(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(i,o)}n((c=c.apply(e,a||[])).next())})}var M=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return f(this,void 0,void 0,function*(){var e=this._core["apiMap"]["getGroupList"],e=(yield e(!0)).filter(e=>!0===e.isSupportTopic);return console.log(this._name+".getJoinedCommunityList ok, count:"+e.length),{code:0,data:{groupList:e}}})}},v=new class{constructor(){this._name="CreateTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(n){return f(this,void 0,void 0,function*(){let s="createTopicInCommunity",{utils:{isUndefined:e},common:{isTopic:t},helper:{ChatError:i}}=this._core,o=n["topicID"];if(!e(o)&&!t(o)){let e=new i({functionName:s,code:p});throw console.error(this._name+`.${s} fail:`,e),e}try{let e=yield function(r,c){return f(this,void 0,void 0,function*(){var{groupID:e,topicID:t,topicName:s,avatar:i,notification:o,introduction:n,customData:a}=r;return c.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:{GroupId:e,TopicId:t,TopicName:s,FaceUrl:i,Notification:o,Introduction:n,CustomString:a}})})}(n,this._core),{TopicId:t=""}=e||{};return console.log(this._name+`.${s} ok, topicID:`+t),g.updateTopic([Object.assign(Object.assign({},n),{topicID:t})]),{code:0,data:{topicID:t}}}catch(e){throw console.error(this._name+`.${s} failed, error:`,e),e}})}},E=new class{constructor(){this._name="DeleteTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(c){return f(this,void 0,void 0,function*(){var s="deleteTopicFromCommunity",{groupID:a,topicIDList:r=[]}=c;try{let e=yield function(s,i){return f(this,void 0,void 0,function*(){var{groupID:e,topicIDList:t}=s;return i.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:{GroupId:e,TopicIdList:t}})})}({groupID:a,topicIDList:r},this._core),{DestroyResultItem:t=[]}=e||{},i=[],o=[],n=[];return t.forEach(e=>{var{TopicId:e,ErrorCode:t,ErrorInfo:s}=e;0===t?(i.push({topicID:e}),n.push(e)):o.push({topicID:e,code:t,message:s})}),0<n.length&&g.deleteTopicList(c.groupID,n),console.log(`${this._name}.${s} ok, groupID:${a} success:${i.length} fail:`+o.length),{code:0,data:{successTopicList:i,failureTopicList:o}}}catch(e){throw console.error(this._name+`.${s} failed, error:`,e),e}})}},S=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{var t=this._core["store"];return(t.get("cloudConfig")||{})[e]}}init(e){e=(this._core=e).helper;e.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return f(this,void 0,void 0,function*(){var{groupID:t,topicIDList:s=[]}=e,i=0===s.length;if(this._isFetchingFromCache(t,i)){let e=g.getTopicList(t,s);if(i||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},i)})}_isFetchingFromCache(e,t){var{hasFetchedAllTopics:e=!1,time:s=0}=g.getTopicRequestInfo(e);return!(!e&&(t||e))&&Date.now()-s<this._getCacheDuration()}_getCacheDuration(){var{isUndefined:e}=this._core["utils"];let t=this._getCloudConfig("topic_cache_time");return!e(t)&&(t=parseInt(t,10))>=a&&t<=u?1e3*t:1e3*c}_getTopicListFromServer(u,p){return f(this,void 0,void 0,function*(){var i="_getTopicListFromServer",{isEmpty:o}=this._core["utils"],c=u["groupID"];try{let e=yield function(s,i){return f(this,void 0,void 0,function*(){var{groupID:e,topicIDList:t}=s;return i.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:{GroupId:e,TopicIdList:t,MemberInfoFilter:["NoUnreadSeqList"]}})})}(u,this._core),{TopicAndSelfInfo:t=[]}=e||{},n=[],a=[],r=[],s=(this._convertTopicInfoList(t).forEach(e=>{var{topic:e,selfInfo:t,code:s,message:i}=e,o=e["topicID"];0===s?(n.push(Object.assign(Object.assign({},e),{selfInfo:t})),a.push(o)):r.push({topicID:o,code:s,message:i})}),g.updateTopic(n),g.updateTopicAtInfo(n),console.log(`${this._name}.${i} groupID:${c} from remote, success count:${a.length}, fail count:`+r.length),[]);return o(a)||(g.setTopicRequestInfo(c,{hasFetchedAllTopics:p,time:Date.now()}),s=g.getTopicList(c,a)),{code:0,data:{successTopicList:s,failureTopicList:r}}}catch(e){throw console.error(this._name+`.${i} failed, error:`,e),e}})}_convertTopicInfoList(e){let o=[];return e.forEach(e=>{var{TopicInfo:e={},SelfInfo:t={},ErrorCode:s,ErrorInfo:i}=e;o.push({code:s,message:i,topic:this._convertTopicInfo(e),selfInfo:this._convertSelfInfo(t)})}),o}_convertTopicInfo(e){var t,s,i,o,n,a,r,c,u,p,l,d=this._core["utils"]["isEmpty"];return d(e)?{}:({CreateTime:d,CustomString:e,FaceUrl:t,GroupId:s,LastMsgTime:i,NextMsgSeq:o,Introduction:n,Notification:a,ShutUpAllFlag:r,TopicId:c,TopicName:u,LastMsg:p,AtInfoList:l}=e,{createTime:d,customData:e,avatar:t,groupID:s,lastMessageTime:i,nextMessageSeq:o,introduction:n,notification:a,muteAllMembers:r,topicID:c,topicName:u,lastMsg:this._convertLastMsg(p),groupAtInfoList:this._convertAtInfoList({groupID:s,topicID:c,atInfoList:l})})}_convertLastMsg(e){var t,{utils:{isUndefined:s},OuterConstant:i}=this._core;if(!s(e))return 4===e.Event&&(e.MsgBody.MsgType=i.MSG_GRP_TIP),{messageHelper:s}=this._core["message"],i=s.parseServerGroupMessage(e),{From_AccountHeadurl:s,From_AccountNick:e,LastMsgSeq:t}=e,Object.assign(Object.assign({},i),{avatar:s,nick:e,sequence:t})}_convertAtInfoList(e){let{groupID:i,topicID:o,atInfoList:t=[]}=e;if(0===t.length)return[];let n=[];return t.forEach(e=>{var{AtFlagList:e,AtMsgSeq:t,From_Account:s}=e;n.push({groupID:i,topicID:o,groupAtType:e,sequence:t,from:s})}),n}_convertSelfInfo(e){var t,s,i,o=this._core["utils"]["isEmpty"];return o(e)?{}:({InheritGroupReceiveOption:o,MsgFlag:e,MsgSeq:t,NoUnreadSeqList:s,ShutUpTime:i}=e,{inheritGroupReceiveOption:o,messageRemindType:e,readedSequence:t,excludedUnreadSequenceList:s,muteTime:i})}},D=new class{constructor(){this._name="UpdateTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(n){return f(this,void 0,void 0,function*(){var t="updateTopicProfile",{isUndefined:e}=this._core["utils"],{groupID:s,topicID:i,muteAllMembers:o}=n;e(n.muteAllMembers)||(n.muteAllMembers=!0===o?"On":"Off");try{return yield function(c,u){return f(this,void 0,void 0,function*(){var{groupID:e,topicID:t,topicName:s,avatar:i,notification:o,introduction:n,customData:a,muteAllMembers:r}=c;return u.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:{GroupId:e,TopicId:t,TopicName:s,FaceUrl:i,Notification:o,Introduction:n,CustomString:a,ShutUpAllMember:r}})})}(n,this._core),console.log(this._name+`.${t} ok, topicID:`+i),g.updateTopic([n]),{code:0,data:{topic:g.getTopic(s,i)}}}catch(e){throw console.error(this._name+`.${t} failed, topicID:${i} error:`,e),e}})}},C=new class{constructor(){this._name="MessageReadHandler"}init(e){let{helper:t,common:{isTopic:s}}=this._core=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(p){return f(this,void 0,void 0,function*(){let{utils:{isEmpty:e},ssoLog:t,helper:{ChatError:i}}=this._core,s=p["conversationID"],o={code:0,data:{}},n=_(s),a=m(n),r=g.getTopic(a,n),c=this._name+`.setMessageRead topicID:${n} conversationID:${s} unreadCount:`+((null==r?void 0:r.unreadCount)||0);if(o.successLog={message:c},!r)return o;if(e(r.groupAtInfoList)||this._deleteTopicAtTips(a,n),0===r.unreadCount)return o;var u=this._getLocalMessageMaxSequence(r);try{yield this._setTopicMessageRead(a,n,u)}catch(e){let{errorCode:t,errorInfo:s}=e;throw new i({functionName:"setMessageRead",code:t,message:s,moreMessage:c})}return Object.assign(Object.assign({},o),{successLog:{message:c+" lastMessageSequence:"+u}})})}_deleteTopicAtTips(n,a){return f(this,void 0,void 0,function*(){this._core.ssoLog.debug("_deleteTopicAtTips",this._name+`._deleteTopicAtTips groupID:${n} topicID:`+a);let e=g.getTopic(n,a);if(e){var i=(null==e?void 0:e.groupAtInfoList)||[];if(0!==i.length)try{let{common:{getCurrentUserID:t},OuterConstant:{CONV_AT_ALL:s}}=this._core;var o=[...i];0===(o=i.filter(e=>!e.atTypeArray.includes(s))).length?g.clearGroupAtInfoList(n,a):(yield function(t,s){return f(this,void 0,void 0,function*(){var e=t["messageListToDelete"];return s.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:{DelMsgList:e}})})}({messageListToDelete:o.map(e=>({From_Account:e.from,To_Account:t(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID}))},this._core),this._core.ssoLog.debug(this._name+"._deleteTopicAtTips ok. count:"+i.length),g.clearGroupAtInfoList(n,a))}catch(e){console.error(this._name+"._deleteTopicAtTips fail:",e)}}})}_getLocalMessageMaxSequence(e){var{message:{messageDataHandler:t},OuterConstant:{CONV_GROUP:s}}=this._core,i=e["topicID"],t=t.getLocalMessageList(""+s+i),s=Math.max(...t.map(e=>e.sequence));let o=(null==(i=null==e?void 0:e.lastMessage)?void 0:i.lastSequence)||0;return s>o&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${o} to `+s),o=s),o}_setTopicMessageRead(e,t,s){return f(this,void 0,void 0,function*(){try{yield function(i,o){return f(this,void 0,void 0,function*(){var{groupID:e,topicID:t,lastMessageSequence:s}=i;return o.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:{GroupId:e,TopicId:t,MsgReadedSeq:s}})})}({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}})}},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)}};let 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(){let i=new class{init(e){this.core=e}},s=["topicID","topicName","avatar","introduction","notification","unreadCount","muteAllMembers","customData","nextMessageSeq","selfInfo","lastMessage"],o=e=>{var{common:{getMessagePreviewText:t}}=i["core"];return{lastTime:(null==e?void 0:e.time)||0,lastSequence:(null==e?void 0:e.sequence)||0,fromAccount:(null==e?void 0:e.from)||"",payload:(null==e?void 0:e.payload)||null,type:(null==e?void 0:e.type)||"",messageForShow:t(null==e?void 0:e.type,null==e?void 0:e.payload),nick:(null==e?void 0:e.nick)||"",avatar:(null==e?void 0:e.avatar)||"",version:(null==e?void 0:e.version)||0,cloudCustomData:(null==e?void 0:e.cloudCustomData)||"",isRevoked:(null==e?void 0:e.isRevoked)||!1,revoker:(null==e?void 0:e.revoker)||null}};class n{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=o(),this.selfInfo={muteTime:0,readedSequence:0,messageRemindType:"",excludedUnreadSequenceList:void 0},this._init(e)}_init(e){for(var t in e)s.includes(t)&&("lastMessage"===t?this[t]=o(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=o(e)}updateGroupAtInfoList(e){var{common:{updateGroupAtInfo:t}}=i["core"];t(e,this.groupAtInfoList)}updateTopic(e){var{utils:{isUndefined:t},common:{deepMerge:s}}=i["core"];t(e.selfInfo)||this.updateSelfInfo(e.selfInfo),t(e.muteAllMembers)||(this.muteAllMembers=1===e.muteAllMembers),s(this,e,["groupID","lastMessageTime","selfInfo","muteAllMembers","lastMsg","groupAtInfoList"])}updateSelfInfo(e){var{common:{deepMerge:t}}=i["core"];return 0<t(this.selfInfo,e,[],[""])}reduceUnreadCount(){return 1<=this.unreadCount&&(--this.unreadCount,!0)}isLastMessageRevoked(e){return e===(null==(e=this.lastMessage)?void 0:e.lastSequence)}setLastMessageRevoked(e){this.lastMessage.isRevoked=e}setLastMessageRevoker(e){this.lastMessage.revoker=e}clearGroupAtInfoList(){this.groupAtInfoList.length=0}}var l=new class{constructor(){this._name="TopicDataHandler",this._topicMap=new Map,this._topicRequestInfo=new Map}init(e){var{helper:t,constants:s,notificationCenter:i,InnerEvent:{LOGOUT:o,DESTROY:n}}=this._core=e;t.registerWorkflowStep(s.WORKFLOW_NAME.SYNC_SERVER_INFO_AFTER_RE_ONLINE,s.WORKFLOW_STEP.TOPIC_REQUEST_INFO_RESET,this._resetTopicRequestInfo,this),i.subscribeInnerEvent(o,this._reset,this),i.subscribeInnerEvent(n,this._dispose,this),e.ssoLog.debug(this._name+".init")}_hasTopic(e,t){return!(null==(e=this._topicMap.get(e))||!e.get(t))}_setTopic(e,t,s){null!=(e=this._topicMap.get(e))&&e.set(t,s)}_deleteTopic(e,t){null!=(e=this._topicMap.get(e))&&e.delete(t)}getTopic(e,t){return null==(e=this._topicMap.get(e))?void 0:e.get(t)}updateTopic(e){e.forEach(e=>{var{groupID:t,topicID:s}=e;let i=null;this._topicMap.has(t)||this._topicMap.set(t,new Map),this._hasTopic(t,s)?null!=(i=this.getTopic(t,s))&&i.updateTopic(e):(this._getLastMessage(e),i=new n(e),this._setTopic(t,s,i));e=this.computeUnreadCount(i);null!=i&&i.updateUnreadCount(e)})}_getLastMessage(s){var{isEmpty:i,isUndefined:o,isArray:n}=this._core["utils"];if(!o(s.lastMsg)){let e=null,t="";t=(n(s.lastMsg.elements)?(e=s.lastMsg.elements[0].content,s.lastMsg.elements[0]):(e=s.lastMsg.elements.content,s.lastMsg.elements)).type;o={payload:e,type:t,time:s.lastMsg.time,sequence:s.lastMsg.sequence,from:s.lastMsg.from,nick:s.lastMsg.nick,avatar:s.lastMsg.avatar,version:s.lastMsg.messageVersion,cloudCustomData:s.lastMsg.cloudCustomData,isRevoked:2===s.lastMsg.isPlaceMessage,revoker:i(s.lastMsg.revokerInfo)?null:s.lastMsg.revokerInfo.revoker};s.lastMessage=o}}computeUnreadCount(s){let{excludedUnreadSequenceList:e=[],readedSequence:i}=s.selfInfo,o=s.nextMessageSeq-s.selfInfo.readedSequence-1;if(0<e.length){let t=0;e.forEach(e=>{e>i&&e<=s.nextMessageSeq-1&&(t+=1)}),1<=t&&(o-=t)}return 0<o?o:0}updateTopicAtInfo(e){0!==e.length&&e.forEach(e=>{var t=this._core["utils"]["isEmpty"],e=e["groupAtInfoList"];t(e)||this.updateGroupAtInfoList(e)})}updateGroupAtInfoList(s,r=!1){if(0!==s.length){let{common:{getCurrentUserID:e},utils:{isUndefined:o,isEmpty:t}}=this._core,n=e(),a=null;s.forEach(e=>{var{groupID:t,topicID:s,from:i}=e;i!==n&&((a=this.getTopic(t,s))&&a.updateGroupAtInfoList(e),o(a))&&r&&(this.updateTopic([{groupID:t,topicID:s}]),null!=(i=this.getTopic(t,s)))&&i.updateGroupAtInfoList(e)}),t(a)||this.emitTopicUpdate(a)}}getTopicList(e,t=[]){var e=this._topicMap.get(e),s=[];return e&&s.push(...e.values()),0<t.length?s.filter(e=>t.includes(e.topicID)):s}deleteTopicList(t,e=[]){this._topicMap.has(t)&&(e.forEach(e=>{this._hasTopic(t,e)&&this._deleteTopic(t,e)}),console.log(this._name+`.deleteTopicList groupID:${t} topicID:`+e.join(",")))}clearGroupAtInfoList(e,t){var s=this._core["utils"]["isEmpty"],e=this.getTopic(e,t);s(null==e?void 0:e.groupAtInfoList)||(null!=e&&e.clearGroupAtInfoList(),console.log(this._name+".clearGroupAtInfoList topicID:"+t))}markMessageAsRead(e,s){let{message:{messageDataHandler:t},OuterConstant:{CONV_GROUP:i}}=this._core,o=(null==t?void 0:t.getLocalMessageList(""+i+e))||[];if(0!==o.length){e=o["length"];for(let t=e-1;0<=t;t--){let e=o[t];if(!(s&&e.sequence>s)){if("in"===e.flow&&e.isRead)break;e.setIsRead(!0)}}}}updateUnreadCount(e,t){var{message:{messageDataHandler:s},OuterConstant:{CONV_GROUP:i}}=this._core;let o=!1;e=this.getTopic(e,t);e&&(s=(null==s?void 0:s.getLocalMessageList(""+i+t))||[],i=e.unreadCount,s=null==s?void 0:s.filter(e=>!e.isRead&&!e._onlineOnlyFlag&&!e.isDeleted).length,console.log(this._name+`._updateUnreadCount topicID:${t} currentUnreadCount:${i} newUnreadCount:`+s),i!==s&&(e.updateUnreadCount(s),o=!0),o)&&this.emitTopicUpdate(e)}updateSelfInfo(e){var{groupID:e,topicID:t,selfInfo:s}=e,i=this.getTopic(e,t);if(i){let e=i.updateSelfInfo(s);console.log(`${this._name}.updateSelfInfo topicID:${t} info:${JSON.stringify(s)} isUpdated:`+e),e&&this.emitTopicUpdate(i)}}getTopicRequestInfo(e){return this._topicRequestInfo.get(e)||{}}setTopicRequestInfo(e,t){return this._topicRequestInfo.set(e,t)}emitTopicUpdate(e){var{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();var{notificationCenter:e,InnerEvent:t}=this._core;e.unSubscribeInnerEvent(t.LOGOUT,this._reset,this),e.unSubscribeInnerEvent(t.DESTROY,this._dispose,this)}};let d=(e="")=>{var{OuterConstant:t}=i["core"];return e.replace(t.CONV_GROUP,"")},h=(e="")=>e.split("@TOPIC#_")[0];var t=new class{constructor(){this._name="TopicMessageHandler"}init(e){var{helper:t,notificationCenter:s,constants:{WORKFLOW_NAME:i,WORKFLOW_STEP:o},InnerEvent:{MESSAGE_PUSH:n,TOPIC_NEW_MESSAGE:a,TOPIC_MESSAGE_DELETED:r,TOPIC_MESSAGE_REVOKED:c,TOPIC_MESSAGE_MODIFIED:u,DESTROY:p}}=this._core=e,{TOPIC_AT_TIPS:l,TOPIC_LATEST_MESSAGE:d}=s["InnerEventSubType"];t.registerWorkflowStep(i.SYNC_SERVER_INFO_AFTER_LOGIN,o.UPDATE_TOPIC_AFTER_UNREAD_SYNC_FINISHED,this._handleUnreadSyncFinished,this),t.registerWorkflowStep(i.RECEIVE_GROUP_NEW_MESSAGE,o.UPDATE_TOPIC_BY_RECEIVE_NEW_MESSAGE,this._handleNewMessage,this),s.subscribeInnerEvent(a,this._handleNewMessage,this),s.subscribeInnerEvent(r,this._handleMessageDeleted,this),s.subscribeInnerEvent(c,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){let s,{WORKFLOW_STEP:t}=this._core.constants,{groupTipList:i=[]}=e.result[t.UNREAD_MESSAGE_SYNC]||{};for(let t=0;t<i.length;t++){let e=i[t];12===e.Event&&null!=(s=e.GroupTips[0].MsgBody)&&s.TopicId&&this._handleTopicAtTips(e)}}_handleNewMessage(e){var{conversationUpdateFieldList:e=[]}=e.result||{};if(0!==e.length){var t=this._core["common"]["isTopic"];if(t(e[0].conversationID)){let a=this._core["common"]["getCurrentUserID"];e.forEach(e=>{let{conversationID:t="",lastMessage:o}=e,s=d(t),i=h(s),n=l.getTopic(i,s);if(n){let{from:e,sequence:t}=o,s=t+1,i=e===a();if(s>n.nextMessageSeq){n.updateNextMessageSeq(s),n.updateLastMessage(o),n.updateReadSequence(t,i);let e=i?0:l.computeUnreadCount(n);n.updateUnreadCount(e),this._emitTopicUpdate(n)}}})}}}_handleMessageDeleted(s){var i=d(s),o=h(i),o=l.getTopic(o,i);if(console.log(this._name+`._handleMessageDeleted, topicID:${i} hasTopic:`+!!o),o){var{message:{messageDataHandler:n},utils:{isEmpty:a}}=this._core,r=(null==n?void 0:n.getLocalMessageList(s))||[];let t={};for(let e=(r.length||0)-1;0<=e;e--)if(!r[e].isDeleted){t=r[e];break}let e=!1;o.lastMessage.lastSequence!==t.sequence&&(a(t)&&(t={}),o.updateLastMessage(t),e=!0),e&&(this._emitTopicUpdate(o),console.log(this._name+`._handleMessageDeleted, topicID:${i} with lastMessage:`+o.lastMessage))}}_handleMessageRevoked(e){var{messageList:e=[]}=e;if(console.log(this._name+"._handleMessageRevoked messageList:"+e.length),0!==e.length){let o=null,n=!1;e.forEach(e=>{var{to:e,sequence:t,revoker:s}=e,i=h(e);if(o=l.getTopic(i,e)){o.reduceUnreadCount()&&(n=!0),o.isLastMessageRevoked(t)&&(o.setLastMessageRevoked(!0),o.setLastMessageRevoker(s),n=!0);let{excludedUnreadSequenceList:e=[]}=o.selfInfo;e.push(t),o.updateSelfInfo({excludedUnreadSequenceList:e})}}),n&&this._emitTopicUpdate(o)}}_handleMessageModified(e){let{conversationID:t,messageList:s}=e,i=d(t),o=h(i),r=l.getTopic(o,i);if(console.log(`${this._name}._handleMessageModified topicID:${i} hasTopic:`+!!r),r){var c=r["lastMessage"];if(c){let a=(null==s?void 0:s[0])||{};if(this._shouldUpdateLastMessage(c,a)){let{getMessagePreviewText:e}=this._core["common"],{time:t,sequence:s,elements:i,cloudCustomData:o,messageVersion:n}=a;c.type=i[0].type,c.payload=i[0].content,c.messageForShow=e(c.type,c.payload),c.cloudCustomData=o,c.version=n,c.lastSequence=s,c.lastTime=t,this._emitTopicUpdate(r)}}}}_shouldUpdateLastMessage(e,t){var s;return null===e.payload||({sequence:t,messageVersion:s}=t,e.lastSequence===t&&e.version!==s)}_emitTopicUpdate(e){var{notificationCenter:t,OuterEvent:s}=this._core;t.emitOuterEvent(s.TOPIC_UPDATED,{name:s.TOPIC_UPDATED,data:e})}_handleTopicAtTips(e){let{GroupTips:t=[]}=e,n=[],a=!1;t.forEach(e=>{var{GroupAtTips:e,MsgBody:t,MsgRandom:s,ClientSeq:i}=e;let o={};e?o=this._convertAtInfo(e):t&&(o=this._convertAtInfo(t),a=!0),o.__random=s,o.__sequence=i,n.push(o)}),l.updateGroupAtInfoList(n,a)}_convertAtInfo(e){var{From_Account:e,GroupId:t,TopicId:s,MsgSeq:i,GroupAtType:o}=e;return{groupID:t,topicID:s,from:e,sequence:i,groupAtType:o}}_handleLatestMessage(e){let{utils:{isEmpty:t},message:{messageHelper:i,messageFactory:o}}=this._core,{NewMsg:n,NoUnreadSeqList:a}=e||{};if(!t(n)){let{TopicId:e}=n["GroupInfo"],t=i.parseServerGroupMessage(n),s=o.createMessage(t);s.setElement(t.elements),this._updateUnreadCountAndLastMessage({topicID:e,message:s,excludedUnreadSequenceList:a})}}_updateUnreadCountAndLastMessage(e){let{isUndefined:t}=this._core["utils"],{topicID:s,message:i,excludedUnreadSequenceList:o}=e,n=h(s),a=l.getTopic(n,s);if(a){let e=a.selfInfo.excludedUnreadSequenceList||[];if(t(o)||(e=o),i._isExcludedFromUnreadCount&&e.push(i.sequence),a.updateSelfInfo({excludedUnreadSequenceList:e}),console.log(`${this._name}._updateUnreadCountAndLastMessage latest message sequence:${i.sequence} lastMessage sequence:`+a.lastMessage.lastSequence),i.sequence>a.lastMessage.lastSequence){let e=i.sequence+1,t=(a.updateLastMessage(i),a.updateNextMessageSeq(e),l.computeUnreadCount(a));a.updateUnreadCount(t),this._emitTopicUpdate(a)}}}_dispose(){var{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,TOPIC_NEW_MESSAGE:s,TOPIC_MESSAGE_DELETED:i,TOPIC_MESSAGE_REVOKED:o,TOPIC_MESSAGE_MODIFIED:n,DESTROY:a}}=this._core,{TOPIC_AT_TIPS:r,TOPIC_LATEST_MESSAGE:c}=e["InnerEventSubType"];e.unSubscribeInnerEvent(s,this._handleNewMessage,this),e.unSubscribeInnerEvent(i,this._handleMessageDeleted,this),e.unSubscribeInnerEvent(o,this._handleMessageRevoked,this),e.unSubscribeInnerEvent(n,this._handleMessageModified,this),e.unSubscribeInnerEvent(t,r,this._handleTopicAtTips,this),e.unSubscribeInnerEvent(t,c,this._handleLatestMessage,this),e.unSubscribeInnerEvent(a,this._dispose,this)}},a=new class{constructor(){this._name="TopicOperationHandler"}init(e){var{notificationCenter:t,InnerEvent:{MESSAGE_PUSH:s,DESTROY:i}}=this._core=e,{TOPIC_TIPS_NOTIFICATION:o,TOPIC_SYSTEM_NOTIFICATION:n,TOPIC_MESSAGE_READ_SYNC:a}=t["InnerEventSubType"];t.subscribeInnerEvent(s,o,this._handleTopicTips,this),t.subscribeInnerEvent(s,n,this._handleTopicNotices,this),t.subscribeInnerEvent(s,a,this._handleTopicMessageReadSynced,this),t.subscribeInnerEvent(i,this._dispose,this),e.ssoLog.debug(this._name+".init")}_handleTopicTips(e){var{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){var s=t[e];7!==s.MsgBody.OpType?8!==s.MsgBody.OpType?this._emitTopicTips(s):this._handleTopicProfileUpdated(s):this._handleMuteTimeUpdated(s)}}_handleMuteTimeUpdated(e){var{getCurrentUserID:t}=this._core["common"],{GroupInfo:{GroupId:s,TopicId:i},MsgBody:{MsgMemberInfo:o=[]}}=e;for(let e=0;e<o.length;e++){var{User_Account:n,ShutupTime:a}=o[e];if(n===t()&&0<=a){l.updateSelfInfo({groupID:s,topicID:i,selfInfo:{muteTime:a}});break}}}_handleTopicProfileUpdated(e){var{GroupInfo:{GroupId:e,TopicId:t},MsgBody:{MsgTopicNewInfo:{CustomString:s,ShutupAll:i,TopicFaceUrl:o,TopicIntroduction:n,TopicName:a,TopicNotification:r}}}=e,c=l.getTopic(e,t);c&&(c.updateTopic({groupID:e,topicID:t,customData:s,muteAllMembers:i,avatar:o,introduction:n,topicName:a,notification:r}),l.emitTopicUpdate(c))}_handleTopicNotices(e){var{GroupTips:t=[]}=e;for(let e=0;e<t.length;e++){var s=t[e],{MsgBody:{ReportType:i,TopicIdArray:o,MsgFlag:n},GroupInfo:{GroupId:a,TopicId:r}}=s;17===i?this._handleTopicCreated({groupID:a,topicID:r}):18===i?this._handleTopicDeleted({groupID:a,topicIDList:o}):20===i?this._handleMessageRemindTypeUpdated({groupID:a,topicID:r,messageRemindType:n}):this._emitTopicNotices(s)}}_handleTopicCreated(e){var{OuterEvent:t,notificationCenter:s}=this._core;l.setTopicRequestInfo(e.groupID,{}),s.emitOuterEvent(t.TOPIC_CREATED,{name:t.TOPIC_CREATED,data:e})}_handleTopicDeleted(e){var{OuterEvent:t,notificationCenter:s}=this._core,{groupID:i,topicIDList:o=[]}=e;l.deleteTopicList(i,o),s.emitOuterEvent(t.TOPIC_DELETED,{name:t.TOPIC_DELETED,data:e})}_handleMessageRemindTypeUpdated(e){var{groupID:e,topicID:t,messageRemindType:s}=e;l.updateSelfInfo({groupID:e,topicID:t,selfInfo:{messageRemindType:s}})}_handleTopicMessageReadSynced(e){let t=e["GroupTips"],{isUndefined:s}=this._core["utils"];t.forEach(e=>{e=e.MsgBody.GroupReadInfoArray;s(e)||e.forEach(e=>{var{GroupId:e,TopicId:t,LastReadMsgSeq:s}=e;console.log(this._name+`._handleTopicMessageReadSynced topicID:${t} lastReadSequence:`+s),l.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),l.clearGroupAtInfoList(e,t),l.markMessageAsRead(t,s),l.updateUnreadCount(e,t)})})}_emitTopicTips(e){var{message:{messageHelper:t,messageFactory:s},OuterConstant:i,OuterEvent:o,notificationCenter:n}=this._core,t=(e.MsgBody.MsgType=i.MSG_GRP_TIP,t.parseServerGroupMessage(e)),e=(t.flow="in",t.conversationType=i.CONV_GROUP,s.createMessage(t));e.setElement(t.elements),e.isSystemMessage=!0,n.emitOuterEvent(o.MESSAGE_RECEIVED,{name:o.MESSAGE_RECEIVED,data:[e]})}_emitTopicNotices(e){var{message:{messageHelper:t,messageFactory:s},OuterConstant:i,OuterEvent:o,notificationCenter:n}=this._core,t=(e.MsgBody.MsgType=i.MSG_GRP_SYS_NOTICE,t.parseServerGroupMessage(e)),e=(t.flow="in",t.conversationType=i.CONV_SYSTEM,t.conversationID=i.CONV_SYSTEM,s.createMessage(t));e.setElement(t.elements),e.isSystemMessage=!0,n.emitOuterEvent(o.MESSAGE_RECEIVED,{name:o.MESSAGE_RECEIVED,data:[e]})}_dispose(){var{notificationCenter:e,InnerEvent:{MESSAGE_PUSH:t,DESTROY:s}}=this._core,{TOPIC_TIPS_NOTIFICATION:i,TOPIC_SYSTEM_NOTIFICATION:o,TOPIC_MESSAGE_READ_SYNC:n}=e["InnerEventSubType"];e.unSubscribeInnerEvent(t,i,this._handleTopicTips,this),e.unSubscribeInnerEvent(t,o,this._handleTopicNotices,this),e.unSubscribeInnerEvent(t,n,this._handleTopicMessageReadSynced,this),e.unSubscribeInnerEvent(s,this._dispose,this)}};function g(e,a,r,c){return new(r=r||Promise)(function(s,t){function i(e){try{n(c.next(e))}catch(e){t(e)}}function o(e){try{n(c.throw(e))}catch(e){t(e)}}function n(e){var t;e.done?s(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(i,o)}n((c=c.apply(e,a||[])).next())})}var r=new class{constructor(){this._name="GetJoinedCommunityHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"getJoinedCommunityList",context:this})}getJoinedCommunityList(){return g(this,void 0,void 0,function*(){var e=this._core["apiMap"]["getGroupList"],e=(yield e(!0)).filter(e=>!0===e.isSupportTopic);return console.log(this._name+".getJoinedCommunityList ok, count:"+e.length),{code:0,data:{groupList:e}}})}},c=new class{constructor(){this._name="CreateTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"createTopicInCommunity",context:this})}createTopicInCommunity(n){return g(this,void 0,void 0,function*(){let s="createTopicInCommunity",{utils:{isUndefined:e},common:{isTopic:t},helper:{ChatError:i}}=this._core,o=n["topicID"];if(!e(o)&&!t(o)){let e=new i({functionName:s,code:3021});throw console.error(this._name+`.${s} fail:`,e),e}try{let e=yield function(r,c){return g(this,void 0,void 0,function*(){var{groupID:e,topicID:t,topicName:s,avatar:i,notification:o,introduction:n,customData:a}=r;return c.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.create_topic",data:{GroupId:e,TopicId:t,TopicName:s,FaceUrl:i,Notification:o,Introduction:n,CustomString:a}})})}(n,this._core),{TopicId:t=""}=e||{};return console.log(this._name+`.${s} ok, topicID:`+t),l.updateTopic([Object.assign(Object.assign({},n),{topicID:t})]),{code:0,data:{topicID:t}}}catch(e){throw console.error(this._name+`.${s} failed, error:`,e),e}})}},u=new class{constructor(){this._name="DeleteTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"deleteTopicFromCommunity",context:this})}deleteTopicFromCommunity(c){return g(this,void 0,void 0,function*(){var s="deleteTopicFromCommunity",{groupID:a,topicIDList:r=[]}=c;try{let e=yield function(s,i){return g(this,void 0,void 0,function*(){var{groupID:e,topicIDList:t}=s;return i.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.destroy_topic",data:{GroupId:e,TopicIdList:t}})})}({groupID:a,topicIDList:r},this._core),{DestroyResultItem:t=[]}=e||{},i=[],o=[],n=[];return t.forEach(e=>{var{TopicId:e,ErrorCode:t,ErrorInfo:s}=e;0===t?(i.push({topicID:e}),n.push(e)):o.push({topicID:e,code:t,message:s})}),0<n.length&&l.deleteTopicList(c.groupID,n),console.log(`${this._name}.${s} ok, groupID:${a} success:${i.length} fail:`+o.length),{code:0,data:{successTopicList:i,failureTopicList:o}}}catch(e){throw console.error(this._name+`.${s} failed, error:`,e),e}})}},p=new class{constructor(){this._name="GetTopicHandler",this._getCloudConfig=e=>{var t=this._core["store"];return(t.get("cloudConfig")||{})[e]}}init(e){e=(this._core=e).helper;e.registerApi({apiName:"getTopicList",context:this})}getTopicList(e){return g(this,void 0,void 0,function*(){var{groupID:t,topicIDList:s=[]}=e,i=0===s.length;if(this._isFetchingFromCache(t,i)){let e=l.getTopicList(t,s);if(i||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},i)})}_isFetchingFromCache(e,t){var{hasFetchedAllTopics:e=!1,time:s=0}=l.getTopicRequestInfo(e);return!(!e&&(t||e))&&Date.now()-s<this._getCacheDuration()}_getCacheDuration(){var{isUndefined:e}=this._core["utils"];let t=this._getCloudConfig("topic_cache_time");return!e(t)&&0<=(t=parseInt(t,10))&&t<=3600?1e3*t:3e5}_getTopicListFromServer(u,p){return g(this,void 0,void 0,function*(){var i="_getTopicListFromServer",{isEmpty:o}=this._core["utils"],c=u["groupID"];try{let e=yield function(s,i){return g(this,void 0,void 0,function*(){var{groupID:e,topicIDList:t}=s;return i.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.get_topic",data:{GroupId:e,TopicIdList:t,MemberInfoFilter:["NoUnreadSeqList"]}})})}(u,this._core),{TopicAndSelfInfo:t=[]}=e||{},n=[],a=[],r=[],s=(this._convertTopicInfoList(t).forEach(e=>{var{topic:e,selfInfo:t,code:s,message:i}=e,o=e["topicID"];0===s?(n.push(Object.assign(Object.assign({},e),{selfInfo:t})),a.push(o)):r.push({topicID:o,code:s,message:i})}),l.updateTopic(n),l.updateTopicAtInfo(n),console.log(`${this._name}.${i} groupID:${c} from remote, success count:${a.length}, fail count:`+r.length),[]);return o(a)||(l.setTopicRequestInfo(c,{hasFetchedAllTopics:p,time:Date.now()}),s=l.getTopicList(c,a)),{code:0,data:{successTopicList:s,failureTopicList:r}}}catch(e){throw console.error(this._name+`.${i} failed, error:`,e),e}})}_convertTopicInfoList(e){let o=[];return e.forEach(e=>{var{TopicInfo:e={},SelfInfo:t={},ErrorCode:s,ErrorInfo:i}=e;o.push({code:s,message:i,topic:this._convertTopicInfo(e),selfInfo:this._convertSelfInfo(t)})}),o}_convertTopicInfo(e){var t,s,i,o,n,a,r,c,u,p,l,d=this._core["utils"]["isEmpty"];return d(e)?{}:({CreateTime:d,CustomString:e,FaceUrl:t,GroupId:s,LastMsgTime:i,NextMsgSeq:o,Introduction:n,Notification:a,ShutUpAllFlag:r,TopicId:c,TopicName:u,LastMsg:p,AtInfoList:l}=e,{createTime:d,customData:e,avatar:t,groupID:s,lastMessageTime:i,nextMessageSeq:o,introduction:n,notification:a,muteAllMembers:r,topicID:c,topicName:u,lastMsg:this._convertLastMsg(p),groupAtInfoList:this._convertAtInfoList({groupID:s,topicID:c,atInfoList:l})})}_convertLastMsg(e){var t,{utils:{isUndefined:s},OuterConstant:i}=this._core;if(!s(e))return 4===e.Event&&(e.MsgBody.MsgType=i.MSG_GRP_TIP),{messageHelper:s}=this._core["message"],i=s.parseServerGroupMessage(e),{From_AccountHeadurl:s,From_AccountNick:e,LastMsgSeq:t}=e,Object.assign(Object.assign({},i),{avatar:s,nick:e,sequence:t})}_convertAtInfoList(e){let{groupID:i,topicID:o,atInfoList:t=[]}=e;if(0===t.length)return[];let n=[];return t.forEach(e=>{var{AtFlagList:e,AtMsgSeq:t,From_Account:s}=e;n.push({groupID:i,topicID:o,groupAtType:e,sequence:t,from:s})}),n}_convertSelfInfo(e){var t,s,i,o=this._core["utils"]["isEmpty"];return o(e)?{}:({InheritGroupReceiveOption:o,MsgFlag:e,MsgSeq:t,NoUnreadSeqList:s,ShutUpTime:i}=e,{inheritGroupReceiveOption:o,messageRemindType:e,readedSequence:t,excludedUnreadSequenceList:s,muteTime:i})}},_=new class{constructor(){this._name="UpdateTopicHandler"}init(e){e=(this._core=e).helper;e.registerApi({apiName:"updateTopicProfile",context:this})}updateTopicProfile(n){return g(this,void 0,void 0,function*(){var t="updateTopicProfile",{isUndefined:e}=this._core["utils"],{groupID:s,topicID:i,muteAllMembers:o}=n;e(n.muteAllMembers)||(n.muteAllMembers=!0===o?"On":"Off");try{return yield function(c,u){return g(this,void 0,void 0,function*(){var{groupID:e,topicID:t,topicName:s,avatar:i,notification:o,introduction:n,customData:a,muteAllMembers:r}=c;return u.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.modify_topic",data:{GroupId:e,TopicId:t,TopicName:s,FaceUrl:i,Notification:o,Introduction:n,CustomString:a,ShutUpAllMember:r}})})}(n,this._core),console.log(this._name+`.${t} ok, topicID:`+i),l.updateTopic([n]),{code:0,data:{topic:l.getTopic(s,i)}}}catch(e){throw console.error(this._name+`.${t} failed, topicID:${i} error:`,e),e}})}},m=new class{constructor(){this._name="MessageReadHandler"}init(e){let{helper:t,common:{isTopic:s}}=this._core=e;t.registerApi({apiName:"setMessageRead",context:this,matcher:e=>s(e[0].conversationID)})}setMessageRead(p){return g(this,void 0,void 0,function*(){let{utils:{isEmpty:e},ssoLog:t,helper:{ChatError:i}}=this._core,s=p["conversationID"],o={code:0,data:{}},n=d(s),a=h(n),r=l.getTopic(a,n),c=this._name+`.setMessageRead topicID:${n} conversationID:${s} unreadCount:`+((null==r?void 0:r.unreadCount)||0);if(o.successLog={message:c},!r)return o;if(e(r.groupAtInfoList)||this._deleteTopicAtTips(a,n),0===r.unreadCount)return o;var u=this._getLocalMessageMaxSequence(r);try{yield this._setTopicMessageRead(a,n,u)}catch(e){let{errorCode:t,errorInfo:s}=e;throw new i({functionName:"setMessageRead",code:t,message:s,moreMessage:c})}return Object.assign(Object.assign({},o),{successLog:{message:c+" lastMessageSequence:"+u}})})}_deleteTopicAtTips(n,a){return g(this,void 0,void 0,function*(){this._core.ssoLog.debug("_deleteTopicAtTips",this._name+`._deleteTopicAtTips groupID:${n} topicID:`+a);let e=l.getTopic(n,a);if(e){var i=(null==e?void 0:e.groupAtInfoList)||[];if(0!==i.length)try{let{common:{getCurrentUserID:t},OuterConstant:{CONV_AT_ALL:s}}=this._core;var o=[...i];0===(o=i.filter(e=>!e.atTypeArray.includes(s))).length?l.clearGroupAtInfoList(n,a):(yield function(t,s){return g(this,void 0,void 0,function*(){var e=t["messageListToDelete"];return s.common.buildAndSendPacket({servcmd:"openim.deletemsg",data:{DelMsgList:e}})})}({messageListToDelete:o.map(e=>({From_Account:e.from,To_Account:t(),MsgSeq:e.__sequence,MsgRandom:e.__random,GroupId:e.groupID}))},this._core),this._core.ssoLog.debug(this._name+"._deleteTopicAtTips ok. count:"+i.length),l.clearGroupAtInfoList(n,a))}catch(e){console.error(this._name+"._deleteTopicAtTips fail:",e)}}})}_getLocalMessageMaxSequence(e){var{message:{messageDataHandler:t},OuterConstant:{CONV_GROUP:s}}=this._core,i=e["topicID"],t=t.getLocalMessageList(""+s+i),s=Math.max(...t.map(e=>e.sequence));let o=(null==(i=null==e?void 0:e.lastMessage)?void 0:i.lastSequence)||0;return s>o&&(console.log(`${this._name}._getLocalMessageMaxSequence update lastMessageSequence from ${o} to `+s),o=s),o}_setTopicMessageRead(e,t,s){return g(this,void 0,void 0,function*(){try{yield function(i,o){return g(this,void 0,void 0,function*(){var{groupID:e,topicID:t,lastMessageSequence:s}=i;return o.common.buildAndSendPacket({servcmd:"million_group_open_http_svc.msg_read_report",data:{GroupId:e,TopicId:t,MsgReadedSeq:s}})})}({groupID:e,topicID:t,lastMessageSequence:s},this._core),console.log(this._name+"._setTopicMessageRead ok, lastReadSequence:"+s),l.updateSelfInfo({groupID:e,topicID:t,selfInfo:{readedSequence:s}}),l.markMessageAsRead(t,s),l.updateUnreadCount(e,t)}catch(e){throw console.error(this._name+"._setTopicMessageRead fail:",e),e}})}},I=new class{init(e){e.ssoLog.debug("TopicAction.init"),r.init(e),c.init(e),u.init(e),p.init(e),_.init(e),m.init(e)}};let e={required:!0,rules:["string"],allowEmpty:!1},T={createTopicInCommunity:{groupID:e,topicName:e},deleteTopicFromCommunity:{groupID:e,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},getTopicList:{groupID:e,topicIDList:{required:!1,rules:["array"],allowEmpty:!0}},updateTopicProfile:{groupID:e,topicID:e}},f={getJoinedCommunityList:!0,createTopicInCommunity:!0,deleteTopicFromCommunity:!0,getTopicList:!0,updateTopicProfile:!0};return new class{constructor(){this.name="Topic"}install(e){i.init(e),l.init(e),t.init(e),a.init(e),I.init(e),e.helper.registerValidateConfig({auth:f,params:T})}}});
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})}}}));
package/professional.d.ts CHANGED
@@ -610,6 +610,12 @@ declare interface LOGIN_OPTIONS {
610
610
  */
611
611
  type?: TencentCloudChat.TYPES;
612
612
  }
613
+
614
+ declare interface PIN_GROUP_MESSAGE_OPTIONS {
615
+ groupID: string;
616
+ message: Message;
617
+ isPinned: boolean;
618
+ }
613
619
 
614
620
  declare interface CHANGE_GROUP_OWNER_OPTIONS {
615
621
  groupID: string;
@@ -2134,6 +2140,18 @@ declare interface LOGIN_OPTIONS {
2134
2140
  * - A group owner can only leave work groups. After the group owner leaves a work group, the work group has no group owner.
2135
2141
  */
2136
2142
  quitGroup(groupID: string): Promise<any>
2143
+
2144
+ /**
2145
+ * Pin group message.
2146
+ * - Group members can pin group messages.
2147
+ */
2148
+ pinGroupMessage(options: PIN_GROUP_MESSAGE_OPTIONS): Promise<any>
2149
+
2150
+ /**
2151
+ * Get Pinned group messages.
2152
+ * - Users can get pinned group messages by groupID.
2153
+ */
2154
+ getPinnedGroupMessageList(groupID: string): Promise<any>
2137
2155
 
2138
2156
  /**
2139
2157
  * Search for a group by groupID.
@@ -2560,6 +2578,11 @@ declare interface LOGIN_OPTIONS {
2560
2578
  * This event is triggered when the all receiving message option changes.
2561
2579
  */
2562
2580
  ALL_RECEIVE_MESSAGE_OPT_UPDATED = 'onAllReceiveMessageOptUpdated',
2581
+
2582
+ /**
2583
+ * This event is triggered when group members pin or unpin the group messages.
2584
+ */
2585
+ PINNED_GROUP_MESSAGE_UPDATED = 'onPinnedGroupMessageUpdated',
2563
2586
  }
2564
2587
 
2565
2588
  enum TYPES {
@@ -2668,9 +2691,9 @@ declare interface LOGIN_OPTIONS {
2668
2691
 
2669
2692
  CONV_MARK_TYPE_UNREAD = 0x2,
2670
2693
 
2671
- CONV_MARK_TYPE_FOLD = 0x3,
2694
+ CONV_MARK_TYPE_FOLD = 0x4,
2672
2695
 
2673
- CONV_MARK_TYPE_HIDE = 0x4,
2696
+ CONV_MARK_TYPE_HIDE = 0x8,
2674
2697
 
2675
2698
  /**
2676
2699
  * Group type: work group
@@ -2906,6 +2929,11 @@ declare interface LOGIN_OPTIONS {
2906
2929
  * Group profile: group joining option
2907
2930
  */
2908
2931
  GRP_PROFILE_JOIN_OPTION = 'joinOption',
2932
+ GRP_PROFILE_INVITE_OPTION = 'inviteOption',
2933
+ INVITE_OPTIONS_DISABLE_INVITE = 'DisableInvite',
2934
+ INVITE_OPTIONS_NEED_PERMISSION = 'NeedPermission',
2935
+ INVITE_OPTIONS_FREE_ACCESS = 'FreeAccess',
2936
+
2909
2937
 
2910
2938
  /**
2911
2939
  * Group profile: group introduction