File: /volume1/@appstore/Spreadsheet/server/common/collab/Connection.js
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"default",{enumerable:true,get:function(){return a}});const t=r(require("./Connection2"));const e=r(require("./Constants"));const i=require("./error");const n=require("../prosemirror");const o=require("@synology-office/logger");function r(t){return t&&t.__esModule?t:{default:t}}function s(t){const e=new Set;const i=t.author;if(typeof i==="number"&&i>0){e.add(i)}t.replies?.forEach((t=>{const i=t.author;if(typeof i==="number"&&i>0){e.add(i)}}));return Array.from(e)}class a extends t.default{constructor(t,e,i){super(t,e,i);this.waitingVersion=undefined;this.selectionVersion=-1}registerEvents(){super.registerEvents();const t=this.socket;t.on("fetch",this.onFetch.bind(this));t.on("event",this.onEvent.bind(this));t.on("caret",this.onCaret.bind(this));t.on("broadcast",this.onBroadcast.bind(this));t.on("poll",this.onPoll.bind(this));t.on("peek",this.onPeek.bind(this));t.on("save",this.onSave.bind(this))}async onFetch(t){try{t(null,await this.instance.fetchData())}catch(n){o.logger.error("onFetch failed",this.id,n,n instanceof Error?n.stack:undefined);t({status:n instanceof i.OfficeError?n.status:e.default.ERR_GENERAL},n)}}async onCaret({selections:t}){this.broadcast("caret",{id:this.id,selections:t});this.updateSelection(t[0],this.instance.getDataVersionCache())}async onPoll({version:t},n){let r;try{r=await this.instance.fetchReplyData(t)??{version:t,wait:true}}catch(r){o.logger.error(this.id,t,r);n({status:r instanceof i.OfficeError?r.status:e.default.ERR_GENERAL},r);return}if(r.wait){this.setWaitingVersion(r.version)}n(null,r)}createNotifyCommentConfig(t,e){if(t==="create"){const{operator:i,operation:n}=e;return{comment_id:n.id,type:t,author:i,original_author:[n.author],message:n.text}}else if(t==="reply"){const{operator:i,operation:n}=e;return{comment_id:n.target,type:t,author:i,original_author:[],message:n.text}}else{const{operator:i,operation:n}=e;return{comment_id:n.target,type:t,author:i,original_author:[],message:n.text}}}async notifyCommentSteps(t){const e=[];const i=[];for(const o of t){let t;switch(o.stepType){case"createCommentV2":if(o.operation.type===n.schemaComment.CommentEnum.TYPE_DISCUSSION){t=this.createNotifyCommentConfig("create",o)}break;case"createReplyV2":t=this.createNotifyCommentConfig("reply",o);break;case"editCommentV2":case"editReplyV2":t=this.createNotifyCommentConfig("edit",o);break;default:break}if(t){if(t.original_author.length>0){e.push(t)}else{i.push(t)}}}if(i.length){const t=await this.instance.fetchComments(i.map((t=>t.comment_id)));i.forEach(((i,n)=>{const o=t[n];if(o){i.original_author=s(o);e.push(i)}}))}for(const t of e){this.notifyComment(t)}}getWaitingVersion(){return this.waitingVersion}setWaitingVersion(t){this.waitingVersion=t}isWaiting(){return this.waitingVersion!==undefined}stopWaiting(){this.waitingVersion=undefined}getSelectionAfterVersion(t){if(t<this.selectionVersion){return this.selection}else{return null}}updateSelection(t,e){this.selection=t;this.selectionVersion=e}mapSelection(t){const e=this.selection;if(!e){return}if("from"in e){for(const i of t){e.from=i.mapNode(e.from)}}else{for(const i of t){e.anchor=i.map(e.anchor);e.head=i.map(e.head)}}}async stat(){return{uid:this.getUid(),waitingVersion:this.getWaitingVersion()}}}