tjc-sdk
Version:
TIANJI IM SDK
2 lines (1 loc) • 3.37 kB
JavaScript
import{MessageContentType as e}from"./messageContentType.js";import s from"../client/tjc.js";import t from"./mediaMessageContent.js";import{compare as i,stringValue as r}from"../util/longUtil.js";import o from"./message.js";import m from"../model/conversation.js";import n from"long";import l from"./messagePayload.js";import{isElectron as a}from"../platform.js";import c from"./articlesMessageContent.js";import h from"./messageContentMediaType.js";class d extends t{title="";messages=[];loaded=!1;constructor(){super(e.Composite_Message,h.General,"")}setMessages(e){this.messages=[],e.forEach((e=>{e.messageContent instanceof c?e.messageContent.toLinkMessageContent().forEach((s=>{let t=Object.assign(new o,e);t.messageContent=s,this.messages.push(t)})):this.messages.push(e)})),this.messages=this.messages.sort(((e,s)=>i(e.messageUid,s.messageUid)))}digest(e){return"[聊天记录]"+this.title}encode(){let i=super.encode();i.content=this.title;let o,m,n=[],l=0;if(this.messages.forEach((e=>{let s=e.messageContent.encode(),m={uid:r(e.messageUid),type:e.conversation.type,target:e.conversation.target,line:e.conversation.line,from:e.from,tos:e.to,direction:e.direction,status:e.status,serverTime:r(e.timestamp),ctype:s.type,csc:s.searchableContent,cpc:s.pushContent,cpd:s.pushData,cc:s.content,cmt:s.mentionedType,cmts:s.mentionedTargets,ce:s.extra};s.searchableContent&&(i.searchableContent=i.searchableContent+s.searchableContent+" "),s.binaryContent&&(m.cbc=s.binaryContent),e.messageContent instanceof t&&(m.mt=e.messageContent.mediaType,m.mru=e.messageContent.remotePath),o||(l+=JSON.stringify(m).length,l>20480&&n.length>0&&(o=n.map((e=>e)))),n.push(m)})),o&&!this.file){m={ms:n};let s=JSON.stringify(m);s=s.replace(/"uid":"([0-9]+)"/,'"uid":$1'),s=s.replace(/"serverTime":"([0-9]+)"/,'"serverTime":$1');let t=new Blob([s]),r="wcf-"+(new Date).getTime()+".data";if(this.file=new File([t],r),a()){const t=require("os"),o=require("path");this.localPath=o.join(t.tmpdir(),r),require("fs").writeFileSync(this.localPath,s),i.localMediaPath=this.localPath,i.mediaType=e.File}m={ms:o}}else m=o?{ms:o}:{ms:n};let c=JSON.stringify(m);return c=c.replace(/"uid":"([0-9]+)"/,'"uid":$1'),c=c.replace(/"serverTime":"([0-9]+)"/,'"serverTime":$1'),i.binaryContent=s.utf8_to_b64(c),i}decode(e){let t;if(super.decode(e),this.title=e.content,this.file){let e=new FileReader;e.onload((e=>{this.t(e.target.result)})),e.readAsBinaryString(this.file)}else if(this.localPath&&a()){const e=require("fs");e.existsSync(this.localPath)&&(t=e.readFileSync(this.localPath).toString(),this.t(t))}t||(t=s.b64_to_utf8(e.binaryContent),this.t(t))}t(e){this.loaded||(this.messages=[],e=(e=(e=e.substring(0,e.lastIndexOf("}")+1)).replace(/"uid":([0-9]+)/g,'"uid":"$1"')).replace(/"serverTime":([0-9]+)/g,'"serverTime":"$1"'),JSON.parse(e).ms.forEach((e=>{let s=new m(e.type,e.target,e.line),t=new o;t.messageUid=n.fromValue(e.uid),t.conversation=s,t.from=e.from,t.to=e.tos,t.direction=e.direction,t.status=e.status,t.timestamp=n.fromValue(e.serverTime);let i=new l;i.type=e.ctype,i.searchableContent=e.csc,i.pushContent=e.cpc,i.pushData=e.cpd,i.content=e.cc,i.mentionedType=e.cmt,i.mentionedTargets=e.cmts,i.extra=e.ce,i.binaryContent=e.cbc,i.mediaType=e.mt,i.remoteMediaUrl=e.mru,t.messageContent=o.messageContentFromMessagePayload(i,t.from),this.messages.push(t)})))}}export{d as default};