UNPKG

@rongcloud/electron-solution

Version:

@rongcloud/electron-solution

17 lines (14 loc) 33 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@rongcloud/engine"),t=require("electron"),o=require("path"),s=require("fs"),r=require("os"),n=require("crypto"); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function i(e,t,o,s){return new(o||(o=Promise))((function(r,n){function i(e){try{c(s.next(e))}catch(e){n(e)}}function a(e){try{c(s.throw(e))}catch(e){n(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(i,a)}c((s=s.apply(e,t||[])).next())}))}const a=new e.Logger("RCMain");let c=null;const d=()=>{const e=Date.now(),t=[r.arch(),r.platform(),process.pid,e,Math.floor(Math.random()*e)].join("__");return o=t,n.createHash("md5").update(o).digest("hex");var o},C=e=>{if(c)return c;const t=o.resolve(e,"deviceId");return a.debug(`deviceIdPath -> ${t}`),s.existsSync(t)?(c=s.readFileSync(t).toString(),c):(c=d(),a.debug(`createDeviceId -> ${c}`),s.writeFileSync(t,c),c)};class p{constructor(e,t){this._timerId=0,this._heartbeatTimeoutId=0,this._isFirstPing=!0,this._hasPingRes=e,this._connectionListener=t}start(e){const t=this,o=()=>{const s=this._isFirstPing?0:15e3;t._timerId=setTimeout((()=>{t._isFirstPing=!1,t._hasPingRes?(e(),t._hasPingRes=!1,o()):t._heartbeatTimeoutId=setTimeout((()=>{}),9e4)}),s)};o()}stop(){clearTimeout(this._timerId)}setHeartbeatRes(e){this._hasPingRes=e,this._hasPingRes&&clearTimeout(this._heartbeatTimeoutId)}}class l{constructor(e){this._setTime=15962112e5,this._cppProto=e}_setToProtocol(e,t,o,s,r){const n=JSON.stringify(s);a.info("MsgExpansion._setToProtocol ===>",e,t,o,n,r),this._cppProto.setMessageExtension(e,t,o,n,r)}setLocalFromReceiveMsg(e){a.info("MsgExpansion.setLocalFromReceiveMsg ===>",e);const{channelId:t,conversationType:o,targetId:s,messageUId:r,expansion:n,sentTime:i}=e,c={};for(const e in n)c[e]={v:"",ts:0},c[e].v=n[e],c[e].ts=i;this._setToProtocol(o,s,r,c,t||"")}setLocalFromExtMsg(e){a.info("MsgExpansion.setLocalFromExtMsg ===>",e);const{channelId:t,conversationType:o,targetId:s,messageUId:r,content:n,sentTime:i}=e,c=n,d=c.put,C=this._cppProto.getMessage(c.mid),p=JSON.parse(C).extMsg,l=JSON.parse(p||"{}");if(a.info("MsgExpansion.setLocalFromExtMsg, serverExtObj",d,"localExt",l),d)for(const e in d){const t=l[e];t&&t.ts<i-this._setTime?(t.v=d[e],t.ts=i-this._setTime):(l[e]={v:"",ts:0},l[e].v=d[e],l[e].ts=i-this._setTime)}c.del&&c.del.forEach((e=>{delete l[e]})),this._setToProtocol(o,s,c.mid,l,t||"")}parseReceiveExpansion(e){const t=JSON.parse(e.extMsg||"{}"),o={};for(const e in t)o[e]=t[e].v;return o}parseConverOrHisExpansion(e){const t=JSON.parse(e.extMsg),o=JSON.parse(t||"{}"),s={};for(const e in o)s[e]=o[e].v;return s}}class u{constructor(o,s=""){this._cppProtocol=o,this._dbpath=s,this._currentToken="",this._connectionStatus=e.ConnectionStatus.DISCONNECTED,this._promiseHandler={},this._customMessageType={},this._heartbeat={},this._cppConnectionStatus=e.ConnectionStatus.DISCONNECTED,this._connectedTime=0,this._currentUserId="",this._inited=!1,this._version="1.0.11",this._senders={},t.ipcMain.on("__RC_CHANNEL_CALL_MAIN_SYNC__",((t,o,...s)=>i(this,void 0,void 0,(function*(){a.debug("__RC_CHANNEL_CALL_MAIN_SYNC__",o,...s);const r=this[o];if("function"!=typeof r)return void(t.returnValue=e.ErrorCode.NOT_SUPPORT);let n=e.ErrorCode.EXTRA_METHOD_UNDEFINED;try{n=yield r.call(this,...s)}catch(e){a.error(`call main process method \`${o}\` failed.`),a.error(e)}t.returnValue=n})))),t.ipcMain.on("__RC_CHANNEL_CALL_MAIN__",((t,o,s,...r)=>i(this,void 0,void 0,(function*(){a.debug("__RC_CHANNEL_CALL_MAIN__",o,s,...r);const n=this[s];if("function"!=typeof n)return void this._send2Renderer(t.sender,"__RC_CHANNEL_RESULT_FROM_MAIN__",o,e.ErrorCode.NOT_SUPPORT);if("sendMessage"===s){const e=r.pop();r.push((o=>{this._send2Renderer(t.sender,"__RC_CHANNEL_RESULT_FROM_MAIN__",e,o)}))}let i=e.ErrorCode.EXTRA_METHOD_UNDEFINED;try{i=yield n.call(this,...r)}catch(e){a.error(`call main process method \`${s}\` failed.`),a.error(e)}this._send2Renderer(t.sender,"__RC_CHANNEL_RESULT_FROM_MAIN__",o,i)})))),t.ipcMain.on("__RC_CHANNEL_SEND_2_MAIN__",((e,t,...o)=>{a.debug("__RC_CHANNEL_CALL_MAIN__",t,...o);const s=this[t];if("function"==typeof s)try{s.call(this,...o)}catch(e){a.error(`call main process method \`${t}\` failed.`),a.error(e)}else a.error(`cannot not found method: ${t}`)})),t.ipcMain.on("__RC_INNER_COMMAND_INIT__",this._init.bind(this)),this._msgExpansionHandler=new l(this._cppProtocol)}getCppProto(){return this._cppProtocol}callExtra(t,...o){a.info(`callExtra -> ${t}`,...o);const s=this[t];if(!s)return a.warn(`ExtraHandler.${t} is undefined`),Promise.resolve({code:e.ErrorCode.EXTRA_METHOD_UNDEFINED});try{return s.call(this,...o)}catch(e){a.error(e)}return Promise.resolve({code:e.ErrorCode.MAIN_PROCESS_ERROR})}getConversationsByPage(t,o,s,r){const n=this._cppProtocol.getConversationListByPage(t,o,s,r||"");let i;try{i=JSON.parse(n).list.map((e=>this._buildConversation(e.obj)))}catch(t){return a.error(t),Promise.resolve({code:e.ErrorCode.MAIN_PROCESS_ERROR})}return Promise.resolve({code:e.ErrorCode.SUCCESS,data:i})}getHistoryMessagesByObjectNames(t,o,s,r,n,i,c){const d=0===i,C=r+1,p=this._cppProtocol.getHistoryMessagesByObjectNames(t,o,s,C,n,d,c||""),l=[];try{const e=JSON.parse(p).list;e.reverse();for(let t=0;t<e.length;t++){const o=this._buildMessage(e[t].obj);l[t]=o}}catch(t){return a.error(t),Promise.resolve({code:e.ErrorCode.MAIN_PROCESS_ERROR})}return Promise.resolve({code:e.ErrorCode.SUCCESS,data:{list:C===l.length?l.slice(1,C):l,hasMore:r<l.length}})}updateMessageReceiptStatus(t,o,s,r=""){a.info("updateMessageReceiptStatus ->",t,o,s,r);const n=this._cppProtocol.updateMessageReceiptStatus(t,o,s,r);return Promise.resolve({code:e.ErrorCode.SUCCESS,data:n})}getConnectedTime(){return this._connectedTime}_send2Renderer(e,t,...o){e.isDestroyed()?delete this._senders[e.id]:e.send(t,...o)}_send2AllRenderer(e,t,o=[]){Object.keys(this._senders).forEach((s=>{const r=this._senders[s];o.includes(r)||this._send2Renderer(r,"__RC_CHANNEL_NOTIFICATION_FROM_MAIN__",e,t)}))}_init(e,s){const r=e.sender,n=r.id;if(this._senders[n]||(this._senders[n]=r,r.once("destroyed",(()=>{delete this._senders[n]}))),this._inited)return;this._inited=!0,this._initOptions=s,a.info("init ->",s);const i=this._dbpath||o.resolve(t.app.getPath("userData"));this._cppProtocol.initWithAppkey(s.appkey,i),a.warn("cppProto version ->",this._cppProtocol.getNodeVersion()),this._registerMsgTypes();const c=C(i);a.info(`deviceId -> ${c}`),this._cppProtocol.setDeviceId(c),this._setConnectionStatusListener(),this._setOnReceiveMessageListener(),this._setConversationStatusListener(),this._setUserProfileListener()}_registerMsgTypes(){e.SEND_MESSAGE_TYPE_OPTION["RC:RcCmd"]={isCounted:!1,isPersited:!1};for(const t in e.SEND_MESSAGE_TYPE_OPTION){const{isCounted:o,isPersited:s}=e.SEND_MESSAGE_TYPE_OPTION[t];let r=0;s&&(r|=1),o&&(r|=2),a.info("_registerMsgTypes ====>",t,r),this._cppProtocol.registerMessageType(t,r)}}_buildMessage(t,o={}){const{isOffLineMessage:s,buildFrom:r}=o,n=JSON.parse(t);a.info("_buildMessage:",n);const{channelId:i,conversationType:c,targetId:d,senderUserId:C,content:p,objectName:l,messageUid:u,direction:S,status:h,source:_,messageId:g,sentTime:E,readReceiptRequest:m,supportExt:T,extMsg:P}=n;let R;R=e.isObject(p)?p:p?JSON.parse(p):p;let f={isCounted:!1,isPersited:!1};l in e.SEND_MESSAGE_TYPE_OPTION?f=e.SEND_MESSAGE_TYPE_OPTION[l]:l in this._customMessageType&&(f=this._customMessageType[l]);let M=null;T&&(M=this._msgExpansionHandler.parseReceiveExpansion(n),"conversation"!==r&&"history"!==r||(M=this._msgExpansionHandler.parseConverOrHisExpansion(n)));const N={channelId:i,conversationType:c,targetId:d,senderUserId:C,content:R||{},messageType:l,messageUId:u,messageDirection:S,isOffLineMessage:e.isUndefined(s)?E<this._connectedTime:s,sentTime:E,receivedTime:0,isPersited:f.isPersited,isCounted:f.isCounted,isMentioned:!1,disableNotification:!1,isStatusMessage:!1,canIncludeExpansion:T,expansion:T?M:null,receivedStatus:h,messageId:g};if(S===e.MessageDirection.RECEIVE?N.receivedStatus=h:S===e.MessageDirection.SEND&&(N.sentStatus=h),m){const{hasRespond:e,readerInfo:t,readCount:o,totalCount:s}=JSON.parse(m),r={hasRespond:e,readCount:o,totalCount:s},n=[];if(t)for(const e in t)n.push({userId:e,readTime:t[e]});r.readerList=n,N.readReceiptInfo=r}return N}_updateReadReceiptInfo(e,t){const o=this._cppProtocol.getMessage(e),s=JSON.parse(o),r=s.readReceiptRequest?JSON.parse(s.readReceiptRequest):{},n=Object.assign(Object.assign({},r),t);this._cppProtocol.updateReadReceiptRequestInfo(e,JSON.stringify(n))}_buildConversation(t){const o=JSON.parse(t);a.info("_buildConversation",o);const{channelId:s,conversationType:r,targetId:n,unreadCount:i,lastestMsg:c,isTop:d,isBlocked:C,matchCount:p}=o,l=C?e.NotificationStatus.OPEN:e.NotificationStatus.CLOSE;return{channelId:s,conversationType:r,targetId:n,unreadMessageCount:i,latestMessage:-1===JSON.parse(c).messageId?null:this._buildMessage(c),hasMentioned:!1,mentionedInfo:null,notificationStatus:l,isTop:d,lastUnreadTime:0,matchCount:p}}_sendConnectionStatus2Renderer(e){this._send2AllRenderer("status",e)}_convertResultList(e,t){let o=JSON.parse(e).list;return o=o.map((e=>{const o=JSON.parse(e.obj);return t?t(o):o})),o}_setConnectionStatusListener(){this._cppProtocol.setConnectionStatusListener((t=>{let o;switch(a.warn("protocol connection status changed:",t),this._cppConnectionStatus=t,t){case 10:o=e.ConnectionStatus.CONNECTING;break;case 31004:return void setTimeout((()=>{this._promiseHandler.connect&&this._promiseHandler.connect.resolve(e.ErrorCode.RC_CONN_USER_OR_PASSWD_ERROR)}));case 12:o=e.ConnectionStatus.DISCONNECTED;break;case 13:o=e.ConnectionStatus.BLOCKED;break;case 1:case 9:case 11:case 31011:case 3e4:case 30002:o=e.ConnectionStatus.NETWORK_UNAVAILABLE;break;case 30010:o=e.ConnectionStatus.NETWORK_UNAVAILABLE,setTimeout((()=>this._doConnect()),5e3);break;case 0:case 33005:o=e.ConnectionStatus.CONNECTED,this._connectedTime=Date.now()-this._cppProtocol.getDeltaTime(),setTimeout((()=>{this._promiseHandler.connect&&this._promiseHandler.connect.resolve(e.ErrorCode.SUCCESS),this._heartbeat=new p(!0,this._sendConnectionStatus2Renderer.bind(this)),this._heartbeat.start(this._sendHeartbeat.bind(this))}));break;case e.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:o=e.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT;break;case 30004:return;case 8:case 31006:case 32061:o=e.ConnectionStatus.REDIRECT;break;default:o=t}this._connectionStatus=o,this._sendConnectionStatus2Renderer(o)}),this._dbOpenCallback,(()=>{this._heartbeat.setHeartbeatRes(!0)}))}_dbOpenCallback(e){a.info(`Sqlite DB Open -> ${e}`)}_sendHeartbeat(){this._cppProtocol.sendHeartbeat()}_setOnReceiveMessageListener(){this._cppProtocol.setOnReceiveMessageListener(((t,o,s,r)=>{const n=this._buildMessage(t,{isOffLineMessage:s});if(n.messageType===e.MessageType.GROUP_READ_RECEIPT_REQUEST){const{msgId:e,rrn:t,tmn:o}=n.content;this._updateReadReceiptInfo(e,{readCount:t,totalCount:o})}if("RC:MsgExMsg"===n.messageType)return this._msgExpansionHandler.setLocalFromExtMsg(n),void this._sendMessageExpansion(n);n.canIncludeExpansion&&this._msgExpansionHandler.setLocalFromReceiveMsg(n),this._sendMessageNotification(n)}))}_sendMessageExpansion(e,t=[]){const{channelId:o,content:s}=e,{put:r,del:n,mid:i}=s,a={};r&&(a.updatedExpansion={channelId:o,messageUId:i,expansion:r}),n&&(a.deletedExpansion={channelId:o,messageUId:i,deletedKeys:n}),this._send2AllRenderer("expansion",a,t)}_sendMessageNotification(e,t=[]){this._send2AllRenderer("message",e,t)}_setConversationStatusListener(){this._cppProtocol.setConversationStatusListener((t=>{const o=JSON.parse(t).list,s=[];o.forEach((t=>{const o=JSON.parse(t.obj),{conversationType:r,targetId:n,status:i,channelId:a}=o,c={notificationStatus:2,isTop:!1};i.forEach((t=>{const o=JSON.parse(t.item);1===o.type?c.notificationStatus=1===Number(o.value)?e.NotificationStatus.OPEN:e.NotificationStatus.CLOSE:c.isTop=1===Number(o.value)})),s.push({channelId:a,conversationType:r,targetId:n,updatedItems:{notificationStatus:{val:c.notificationStatus,time:0},isTop:{val:c.isTop,time:0}}})})),this._send2AllRenderer("conversation",s)}),(e=>{a.info("会话标签状态变更",e),this._send2AllRenderer("conversationTag",e)}))}_setUserProfileListener(){this._cppProtocol.setUserProfileListener((()=>{}),(e=>{a.info("标签变更",e),this._send2AllRenderer("tag",e)}))}_clearListener(){this._cppProtocol.setOnReceiveMessageListener(),this._cppProtocol.setConnectionStatusListener(),this._cppProtocol.setOnReceiveStatusListener(),this._cppProtocol.setUserProfileListener()}registerMessageType(e,t,o,s){let r=0;t&&(r|=1),o&&(r|=2),this._customMessageType[e]={isCounted:o,isPersited:t},this._cppProtocol.registerMessageType(e,r,s)}connect(e,t){return a.info("connect ->",e,t),this._currentToken=e,this._naviInfo=t,this._currentUserId=t.userId,this._doConnect(),new Promise(((e,t)=>{this._promiseHandler.connect={resolve:e,reject:t}}))}_doConnect(){this._cppProtocol.connectWithToken(this._currentToken,this._initOptions.apiVersion,JSON.stringify(this._naviInfo),!0)}reportSDKInfo(e){e["electron-solution"]=this._version,this._cppProtocol.uploadSDKVersion(JSON.stringify(e)),a.info(e)}disconnect(){this._cppProtocol.disconnect(!0),this._heartbeat.stop(),this._sendConnectionStatus2Renderer(e.ConnectionStatus.DISCONNECTED)}logout(){this.disconnect()}setUserStatusListener(e,t){this._cppProtocol.setOnReceiveStatusListener(((e,o)=>{t({userId:e,status:o})}));const o=e.userIds||[];o.length&&this.subscribeUserStatus(o)}subscribeUserStatus(t){return new Promise(((o,s)=>{this._cppProtocol.subscribeUserStatus(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}setUserStatus(t){return new Promise(((o,s)=>{this._cppProtocol.setUserStatus(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}getUserStatus(t){return new Promise(((o,s)=>{this._cppProtocol.getUserStatus(t,(t=>{o({code:e.ErrorCode.SUCCESS,data:{status:t}})}),(e=>{o({code:e})}))}))}sendMessage(t,o,s,r){let{messageType:n,content:i,pushContent:a,pushData:c,directionalUserIdList:d,disableNotification:C,canIncludeExpansion:p,expansion:l,isVoipPush:u,pushConfig:S,channelId:h}=s,{iOSConfig:_,androidConfig:g,pushTitle:E,pushContent:m,pushData:T,disablePushTitle:P,forceShowDetailContent:R,templateId:f}=S||{};const M=e.pushConfigsToJSON(_,g);return new Promise((s=>{a=m||a||"",c=T||c||"",C=C||!1,u=u||!1,E=E||"",m=m||"",T=T||"",P=P||!1,R=R||!1,p=p||!1;const S=(null==g?void 0:g.notificationId)||"";d=d||[];if(t===e.ConversationType.GROUP&&n===e.MessageType.READ_RECEIPT_RESPONSE&&i.receiptMessageDic)for(const e in i.receiptMessageDic)d.push(e);const _={};for(const e in l)_[e]={v:""},_[e].v=l[e];const N=f||"",v=this._cppProtocol.sendMessage(((t,o)=>{const r=this._buildMessage(t,{isOffLineMessage:!1});return o===e.ErrorCode.SENSITIVE_REPLACE?s({code:o}):("RC:MsgExMsg"===r.messageType&&this._msgExpansionHandler.setLocalFromExtMsg(r),r.canIncludeExpansion&&this._msgExpansionHandler.setLocalFromReceiveMsg(r),s({code:e.ErrorCode.SUCCESS,data:r}))}),((e,t)=>{const o=this._buildMessage(e,{isOffLineMessage:!1});return o.sentTime=Date.now()-this._cppProtocol.getDeltaTime(),s({code:t,data:o})}),t,o,n,JSON.stringify(i),d,C,P,R,a,c,S,E,M,N,p,JSON.stringify(_),u,h),O=JSON.parse(v);r&&r(O.messageId)}))}recallMsg(t,o,s,r,n){return new Promise(((i,a)=>{let{user:c,pushContent:d,channelId:C,oriContent:p}=n;d=d||"";const l={conversationType:t,targetId:o,senderUserId:this._currentUserId,content:p,objectName:e.MessageType.RECALL,messageUid:s,direction:e.MessageDirection.SEND,status:e.ReceivedStatus.UNREAD,sentTime:r,channelId:C},u=this._buildMessage(JSON.stringify(l),{isOffLineMessage:!1});this._cppProtocol.recallMessage((()=>{i({code:e.ErrorCode.SUCCESS,data:u})}),(e=>{i({code:e})}),e.MessageType.RECALL,JSON.stringify(u),!1,!1,!1,d,"","","","","",C)}))}getHistoryMessage(t,o,s,r,n,i,a){return new Promise(((c,d)=>{s=s||0;const C=0===n,p=r+1,l=this._cppProtocol.getHistoryMessages(t,o,s,p,a,C,i),u=JSON.parse(l).list,S=[];u.reverse();for(let e=0;e<u.length;e++){const t=this._buildMessage(u[e].obj);S[e]=t}c({code:e.ErrorCode.SUCCESS,data:{list:p===S.length?S.slice(1,p):S,hasMore:r<S.length}})}))}getRemoteHistoryMessages(t,o,s,r,n,i){return new Promise(((a,c)=>{s=s||0;this._cppProtocol.getRemoteHistoryMessages(t,o,s,r,((t,o)=>{const s=JSON.parse(t).list,r=[];s.reverse();for(let e=0;e<s.length;e++){const t=this._buildMessage(s[e].obj);r[e]=t}a({code:e.ErrorCode.SUCCESS,data:{list:r,hasMore:!!o}})}),(e=>{a({code:e})}),n,!0,i)}))}deleteRemoteMessage(t,o,s,r){return new Promise(((n,i)=>{const a=JSON.stringify(s);this._cppProtocol.deleteRemoteHistoryMessages(t,o,a,!1,r,(()=>{n(e.ErrorCode.SUCCESS)}),n)}))}deleteRemoteMessageByTimestamp(t,o,s,r){return new Promise(((n,i)=>{this._cppProtocol.clearRemoteHistoryMessages(t,o,s,(()=>{n(e.ErrorCode.SUCCESS)}),(e=>{n(e)}),r)}))}clearMessages(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.clearMessages(t,o,s),r(e.ErrorCode.SUCCESS)}))}getConversationList(t,o,s,r,n=""){return new Promise(((t,o)=>{const s=this._cppProtocol.getConversationList([1,3,6,7],n),r=JSON.parse(s).list,i=[];for(let e=0;e<r.length;e++)i.push(this._buildConversation(r[e].obj));t({code:e.ErrorCode.SUCCESS,data:i})}))}getConversation(t,o,s){return new Promise(((r,n)=>{const i=this._cppProtocol.getConversation(t,o,s);if(i)r({code:e.ErrorCode.SUCCESS,data:this._buildConversation(i)});else{const n=this._cppProtocol.getHistoryMessages(t,o,0,1,"",!0,""),i=JSON.parse(n).list;let a=null;i.length>0&&(a=this._buildMessage(i[0].obj)),r({code:e.ErrorCode.SUCCESS,data:{conversationType:t,targetId:o,channelId:s,unreadMessageCount:0,latestMessage:a,notificationStatus:e.NotificationStatus.CLOSE,isTop:!1,lastUnreadTime:0}})}}))}removeConversation(t,o,s){const r=this._cppProtocol.removeConversation(t,o,s);return a.info(`removeConversation result -> ${r}`),Promise.resolve(r?e.ErrorCode.SUCCESS:e.ErrorCode.UNKNOWN)}clearConversations(t,o){return new Promise(((s,r)=>{let n=[];n=e.isArray(t)?t:[e.ConversationType.PRIVATE,e.ConversationType.GROUP,e.ConversationType.SYSTEM,e.ConversationType.PUBLIC_SERVICE],this._cppProtocol.clearConversations(n,o),s(e.ErrorCode.SUCCESS)}))}getAllConversationUnreadCount(t,o,s){const r=this._cppProtocol.getTotalUnreadCount(o,s,t);return Promise.resolve({code:e.ErrorCode.SUCCESS,data:r})}getConversationUnreadCount(t,o,s=""){return new Promise((r=>{const n=this._cppProtocol.getUnreadCount(t,o,s);r({code:e.ErrorCode.SUCCESS,data:n})}))}clearConversationUnreadCount(t,o,s=""){return new Promise(((r,n)=>{this._cppProtocol.clearUnreadCount(t,o,s),r(e.ErrorCode.SUCCESS)}))}clearUnreadCountByTimestamp(t,o,s,r){return new Promise(((n,i)=>{this._cppProtocol.clearUnreadCountByTimestamp(t,o,s,r),n(e.ErrorCode.SUCCESS)}))}setConversationToTop(t,o,s,r=""){return new Promise(((n,i)=>{a.info("setConversationToTop",t,o,s,r,true),this._cppProtocol.setConversationToTop(t,o,s,r,true),n(e.ErrorCode.SUCCESS)}))}setConversationHidden(t,o,s,r){return new Promise(((n,i)=>{this._cppProtocol.setConversationHidden(t,o,s,r),n(e.ErrorCode.SUCCESS)}))}setConversationNotificationStatus(t,o,s,r){return new Promise(((n,i)=>{a.info("setConversationNotificationStatus",t,o,s,r),this._cppProtocol.setConversationNotificationStatus(t,o,s,(()=>{n(e.ErrorCode.SUCCESS)}),n,r)}))}setConversationStatus(t,o,s,r,n){return new Promise(((i,c)=>{a.info("setConversationStatus",t,o,s,r,n,true),this._cppProtocol.setConversationStatus(t,o,s,r,(()=>{i(e.ErrorCode.SUCCESS)}),i,n,true)}))}getConversationNotificationStatus(t,o,s){return new Promise(((r,n)=>{const i=this._cppProtocol.getConversationNotificationStatus(t,o,s);r({code:e.ErrorCode.SUCCESS,data:i?e.NotificationStatus.OPEN:e.NotificationStatus.CLOSE})}))}searchConversationByContent(t,o,s,r){return new Promise(((n,i)=>{r=r||[1,3,6,7];const a=this._cppProtocol.searchConversationByContent(r,t,o,s),c=JSON.parse(a).list,d=[];for(let e=0;e<c.length;e++)d[e]=this._buildConversation(c[e].obj);n({code:e.ErrorCode.SUCCESS,data:d})}))}searchMessageByContent(t,o,s,r,n,i,a){return new Promise(((i,c)=>{this._cppProtocol.searchMessageByContent(t,o,s,r,n,1,((t,o)=>{const s=t?JSON.parse(t).list:[],r=[];for(let e=0;e<s.length;e++)r[e]=this._buildMessage(s[e].obj);i({code:e.ErrorCode.SUCCESS,data:{messages:r,count:o}})}),a)}))}getUnreadMentionedMessages(e,t,o){const s=JSON.parse(this._cppProtocol.getUnreadMentionedMessages(e,t,o)).list;for(let e=0;e<s.length;e++)s[e]=this._buildMessage(s[e].obj);return s}addToBlacklist(t){return new Promise(((o,s)=>{this._cppProtocol.addToBlacklist(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}removeFromBlacklist(t){return new Promise(((o,s)=>{this._cppProtocol.removeFromBlacklist(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}getBlacklist(){return new Promise(((t,o)=>{this._cppProtocol.getBlacklist((o=>{t({code:e.ErrorCode.SUCCESS,data:o})}),(e=>{t({code:e})}))}))}getBlacklistStatus(t){return new Promise(((o,s)=>{this._cppProtocol.getBlacklistStatus(t,(t=>{o({code:e.ErrorCode.SUCCESS,data:t})}),(e=>{o({code:e})}))}))}insertMessage(t,o,s){let{content:r,senderUserId:n,messageType:i,messageDirection:c,readStatus:d,sentStatus:C,sentTime:p,searchContent:l,isUnread:u,messageUId:S,disableNotification:h,canIncludeExpansion:_,expansionMsg:g,channelId:E}=s;return r=JSON.stringify(r),new Promise(((s,m)=>{const T=u?0:1,P=this._cppProtocol.insertMessage(t,o,n,i,r,(()=>{}),(e=>{s({code:e})}),c,d,C,p,l,T,S,h,_,g,E);a.info("after insert msg",P);const R=this._buildMessage(P,{isOffLineMessage:!1});R.messageUId=S,R.sentTime=p,s({code:-1===R.messageId?e.ErrorCode.MSG_INSERT_ERROR:e.ErrorCode.SUCCESS,data:R})}))}deleteMessages(t){return new Promise(((o,s)=>{this._cppProtocol.deleteMessages(t),o(e.ErrorCode.SUCCESS)}))}deleteMessagesByTimestamp(t,o,s,r,n){return new Promise(((i,a)=>{this._cppProtocol.deleteMessagesByTimestamp(t,o,s,r,n),i(e.ErrorCode.SUCCESS)}))}getMessage(t){return new Promise(((o,s)=>{const r=this._cppProtocol.getMessage(t);a.info("getMessage ===>",r);const n=this._buildMessage(r,{isOffLineMessage:!1});o({code:e.ErrorCode.SUCCESS,data:n})}))}setMessageContent(t,o,s){return new Promise(((r,n)=>{o=JSON.stringify(o),this._cppProtocol.setMessageContent(t,o,s),r(e.ErrorCode.SUCCESS)}))}setMessageSearchField(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.setMessageSearchField(t,o,s),r(e.ErrorCode.SUCCESS)}))}setMessageSentStatus(t,o){return new Promise(((s,r)=>{this._cppProtocol.setMessageSentStatus(t,o),s(e.ErrorCode.SUCCESS)}))}setMessageReceivedStatus(t,o){return new Promise(((s,r)=>{this._cppProtocol.setMessageReceivedStatus(t,o),s(e.ErrorCode.SUCCESS)}))}saveConversationMessageDraft(e,t,o){throw new Error("Method not implemented.")}getConversationMessageDraft(e,t){throw new Error("Method not implemented.")}clearConversationMessageDraft(e,t){throw new Error("Method not implemented.")}pullConversationStatus(e){throw new Error("Method not implemented.")}batchSetConversationStatus(t){const{conversationType:o,targetId:s,isTop:r,notificationStatus:n,channelId:i}=t[0];if(void 0!==r&&void 0===n)return this.setConversationToTop(o,s,r,i);if(void 0!==n&&void 0===r){const t=n===e.NotificationStatus.OPEN;return this.setConversationNotificationStatus(o,s,t,i)}{const t=n===e.NotificationStatus.OPEN;return this.setConversationStatus(o,s,t,r,i)}}pullUserSettings(e){throw new Error("Method not implemented.")}sendReadReceiptMessage(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.sendReadReceipt(t,s||"",o,(()=>{o.forEach((e=>{this._updateReadReceiptInfo(e,{hasRespond:!0})})),r({code:e.ErrorCode.SUCCESS})}),(e=>{r({code:e})}))}))}getMessageReader(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.getMessageReader(t,s,o,((t,s)=>{a.info("getMessageReader -> ",t,s);const n=[],i={};(t=JSON.parse(t)).list.forEach((e=>{const{id:t,time:o}=JSON.parse(e.obj);n.push({userId:t,readTime:o}),i[t]=o}));const c={readerInfo:i,readCount:n.length,totalCount:s};this._updateReadReceiptInfo(o,c),r({code:e.ErrorCode.SUCCESS,data:{list:n,totalMemberCount:s}})}),(e=>{r({code:e})}))}))}joinChatroom(t,o){return new Promise(((s,r)=>{this._cppProtocol.joinChatRoom(t,o,(()=>{s(e.ErrorCode.SUCCESS)}),s)}))}joinExistChatroom(e,t){throw new Error("Method not implemented.")}quitChatroom(t){return new Promise(((o,s)=>{this._cppProtocol.quitChatRoom(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}getChatroomInfo(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.getChatroomInfo(t,o,s,((t,o)=>{const s=t?JSON.parse(t).list:[],n=[];if(s.length>0)for(let e=0;e<s.length;e++)n.push(JSON.parse(s[e].obj));r({code:e.ErrorCode.SUCCESS,data:{userInfos:n,userCount:o}})}),(e=>{r({code:e})}))}))}getChatroomHistoryMessages(e,t,o,s){throw new Error("Method not implemented.")}setChatroomEntry(e,t){throw new Error("Method not implemented.")}forceSetChatroomEntry(e,t){throw new Error("Method not implemented.")}removeChatroomEntry(e,t){throw new Error("Method not implemented.")}forceRemoveChatroomEntry(e,t){throw new Error("Method not implemented.")}getChatroomEntry(e,t){throw new Error("Method not implemented.")}getAllChatroomEntry(e){throw new Error("Method not implemented.")}getFileToken(t,o,s,r){return s=s||"",r=r||"",new Promise((n=>{var i;i=r&&"uploads"!==r?o||"":e.getUploadFileName(t,o),this._cppProtocol.getUploadToken(t,i,s,r,((t,o,s,r,a,c,d,C,p,l,u,S,h,_,g,E,m,T)=>{n({code:e.ErrorCode.SUCCESS,data:{token:t,deadline:0,bosToken:o,bosDate:s,path:r,osskeyId:a,ossPolicy:c,ossSign:d,ossBucketName:C,fileName:i,s3Credential:p,s3Algorithm:l,s3Date:u,s3Policy:S,s3Signature:h,s3BucketName:_,stcAuthorization:g,stcContentSha256:E,stcDate:m,stcBucketName:T}})}),(e=>{n({code:e})}))}))}getFileUrl(t,o,s,r){return new Promise((n=>{this._cppProtocol.getDownloadUrl(t,s,r,o-1,(t=>{n({code:e.ErrorCode.SUCCESS,data:{downloadUrl:t}})}),(e=>{n({code:e})}))}))}clearData(){return new Promise(((t,o)=>{const s=this._cppProtocol.clearData();t({code:e.ErrorCode.SUCCESS,data:s})}))}getVoIPKey(t,o){return new Promise(((s,r)=>{this._cppProtocol.getVoIPKey(t,o,"",(t=>{s({code:e.ErrorCode.SUCCESS,data:t})}),(e=>{s({code:e})}))}))}joinRTCRoom(t,o,s){return new Promise((r=>{this._cppProtocol.joinRTCRoom(t,o,s||0,((t,o,s)=>{const n=JSON.parse(t).list,i={token:o,sessionId:s,users:{}};n.forEach((e=>{const{id:t,data:o}=e;i.users[t]=o[0]||{}})),r({code:e.ErrorCode.SUCCESS,data:i})}),(e=>{a.error(`joinRTCRoom error -> ${e}`),r({code:e})}))}))}quitRTCRoom(t){return new Promise((o=>{this._cppProtocol.exitRTCRoom(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}rtcPing(t,o,s){return new Promise((o=>{this._cppProtocol.sendRTCPing(t,(()=>{o(e.ErrorCode.SUCCESS)}),o)}))}getRTCRoomInfo(t){return new Promise((o=>{this._cppProtocol.getRTCResouce(t,2,(t=>{o({code:e.ErrorCode.SUCCESS,data:JSON.parse(t)})}),(e=>{o({code:e})}))}))}getRTCUserInfoList(t){return new Promise(((o,s)=>{this._cppProtocol.getRTCResouce(t,1,(t=>{const s=JSON.parse(t).list,r={users:{}};s.forEach((e=>{const{id:t,data:o}=e;r.users[t]=o[0]||{}})),o({code:e.ErrorCode.SUCCESS,data:r})}),(e=>{o({code:e})}))}))}getRTCUserInfo(e){throw new Error("Method not implemented.")}setRTCUserInfo(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.setRTCInnerData(t,e.RTCApiType.PERSON,o,s,"","",(()=>{r(e.ErrorCode.SUCCESS)}),r)}))}removeRTCUserInfo(t,o){return new Promise(((s,r)=>{this._cppProtocol.deleteRTCInnerData(t,e.RTCApiType.PERSON,o,"","",(()=>{s(e.ErrorCode.SUCCESS)}),s)}))}setRTCData(t,o,s,r,n,i){return new Promise((a=>{const c=this._cppProtocol,d={room_innder:(t,o,s,r,n,i,a)=>{c.setRTCInnerData(t,e.RTCApiType.ROOM,o,s,r,n,i,a)},room_outer:(t,o,s,r,n,i,a)=>{c.setRTCOuterData(t,e.RTCApiType.ROOM,o,s,r,n,i,a)},user_innder:(t,o,s,r,n,i,a)=>{c.setRTCInnerData(t,e.RTCApiType.PERSON,o,s,r,n,i,a)},user_outer:(t,o,s,r,n,i,a)=>{c.setRTCOuterData(t,e.RTCApiType.PERSON,o,s,r,n,i,a)}},C=d[`${e.RTCApiType.PERSON===n?"user":"room"}_${r?"inner":"outer"}`];if(C){const r=(null==i?void 0:i.content)?JSON.stringify(i.content):"",n=(null==i?void 0:i.name)||"";C(t,o,s,n,r,(()=>{a(e.ErrorCode.SUCCESS)}),(e=>{a(e)}))}}))}setRTCTotalRes(t,o,s,r){return new Promise((n=>{const{name:i,content:a}=o;this._cppProtocol.setUserResource(t,i,a,r,s,(()=>n(e.ErrorCode.SUCCESS)),n)}))}getRTCData(t,o,s,r){return new Promise((n=>{const i=this._cppProtocol,a={room_inner:(t,o,s,r)=>{i.getRTCInnerData(t,e.RTCApiType.ROOM,o,s,r)},room_outer:(t,o,s,r)=>{i.getRTCOuterData(t,e.RTCApiType.ROOM,o,s,r)}},c=a[`${e.RTCApiType.PERSON===r?"user":"room"}_${s?"inner":"outer"}`];c&&c(t,o,(function(t){const o=JSON.parse(t),s={};o.list.forEach((function(e){s[e.key]=e.value})),n({code:e.ErrorCode.SUCCESS,data:s})}),(function(e){n({code:e})}))}))}removeRTCData(t,o,s,r,n){return new Promise((i=>{const a=this._cppProtocol,c={room_inner:function(t,o,s,r,n,i){a.deleteRTCInnerData(t,e.RTCApiType.ROOM,o,s,r,n,i)},room_outer:function(t,o,s,r,n,i){a.deleteRTCOuterData(t,e.RTCApiType.ROOM,o,s,r,n,i)},user_inner:function(t,o,s,r,n,i){a.deleteRTCInnerData(t,e.RTCApiType.PERSON,o,s,r,n,i)},user_outer:function(t,o,s,r,n,i){a.deleteRTCOuterData(t,e.RTCApiType.PERSON,o,s,r,n,i)}},d=c[`${e.RTCApiType.PERSON===r?"user":"room"}_${s?"inner":"outer"}`];if(d){const s=(null==n?void 0:n.content)?JSON.stringify(n.content):"",r=(null==n?void 0:n.name)||"";d(t,o,r,s,(()=>{i(e.ErrorCode.SUCCESS)}),(e=>{i(e)}))}}))}setRTCOutData(e,t,o,s){throw new Error("Method not implemented.")}getRTCOutData(e,t){throw new Error("Method not implemented.")}getRTCToken(t,o,s){return new Promise((o=>{this._cppProtocol.getRTCToken(t,(t=>{o({code:e.ErrorCode.SUCCESS,data:{rtcToken:t}})}),(e=>{o({code:e})}))}))}setRTCState(t,o){return new Promise((s=>{this._cppProtocol.setRTCUserState(t,o,(()=>{s(e.ErrorCode.SUCCESS)}),s)}))}getRTCUserList(t){return new Promise((o=>{this._cppProtocol.getRTCUsers(t,1,(t=>{o({code:e.ErrorCode.SUCCESS,data:t})}),(e=>{o({code:e})}))}))}createTag(t){return new Promise(((o,s)=>{this._cppProtocol.addTag(t.tagId,t.tagName,(()=>{o({code:e.ErrorCode.SUCCESS})}),(e=>{o({code:e})}))}))}removeTag(t){return new Promise(((o,s)=>{this._cppProtocol.deleteTag(t,(()=>{o({code:e.ErrorCode.SUCCESS})}),(e=>{o({code:e})}))}))}updateTag(t){return new Promise(((o,s)=>{this._cppProtocol.renameTag(t.tagId,t.tagName,(()=>{o({code:e.ErrorCode.SUCCESS})}),(e=>{o({code:e})}))}))}getTagList(){return new Promise(((t,o)=>{const s=this._cppProtocol.getTags();a.info("getTagList -> ",s);const r=this._convertResultList(s,(e=>({tagId:e.tagId,tagName:e.tagName,conversationCount:e.conversationCount,createdTime:e.tagTime})));t({code:e.ErrorCode.SUCCESS,data:r})}))}addTagForConversations(t,o){return new Promise(((s,r)=>{this._cppProtocol.addConversationsForTag(t,JSON.stringify(o),(()=>{s({code:e.ErrorCode.SUCCESS})}),(e=>{s({code:e})}))}))}removeTagForConversations(t,o){return new Promise(((s,r)=>{this._cppProtocol.removeConversationsForTag(t,JSON.stringify(o),(()=>{s({code:e.ErrorCode.SUCCESS})}),(e=>{s({code:e})}))}))}removeTagsForConversation(t,o){return new Promise(((s,r)=>{this._cppProtocol.removeTagsForConversation(t.type,t.targetId,t.channelId||"",o,(()=>{s({code:e.ErrorCode.SUCCESS})}),(e=>{s({code:e})}))}))}getConversationListByTag(t,o,s,r){return new Promise(((n,i)=>{const c=this._cppProtocol.getConversationsForTagByPage(t,o,s||20,r||"");a.info("getConversationListByTag -> ",c);const d=this._convertResultList(c,(e=>Object.assign(Object.assign({},this._buildConversation(JSON.stringify(e))),{isTopInTag:e.topForTag})));n({code:e.ErrorCode.SUCCESS,data:d})}))}getUnreadCountByTag(t,o){return new Promise(((s,r)=>{const n=this._cppProtocol.getConversationUnreadCountForTag(t,o);a.info("getUnreadCountByTag -> ",n),s({code:e.ErrorCode.SUCCESS,data:n})}))}setConversationStatusInTag(t,o,s){return new Promise(((r,n)=>{this._cppProtocol.setConversationToTopForTag(t,o.type,o.targetId,o.channelId||"",s.isTop||!1,(()=>{r({code:e.ErrorCode.SUCCESS})}),(e=>{r({code:e})}))}))}getTagsForConversation(t){return new Promise(((o,s)=>{const r=this._cppProtocol.getTagsForConversation(t.type,t.targetId,t.channelId||"");a.info("getTagsForConversation -> ",r);const n=this._convertResultList(r,(e=>({tagId:e.tagId,tagName:e.tagName,isTop:e.isTop,createdTime:e.tagTime})));o({code:e.ErrorCode.SUCCESS,data:n})}))}}exports.init=(e,t)=>(a.setLogLevel(null==t?void 0:t.logLevel),a.setLogStdout(null==t?void 0:t.logStdout),new u(e,null==t?void 0:t.dbpath));