File: /volume1/@appstore/Spreadsheet/server/common/base/client.js
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"default",{enumerable:true,get:function(){return o}});const e=require("../helper");const t=s(require("../webapi"));const i=require("@synology-office/logger");function s(e){return e&&e.__esModule?e:{default:e}}const n=4294967295;class r{constructor(e,t,i){this.instance=e;this.socket=t;this.uid=i.uid;this.remoteIP=i.remote_ip;this.linkId=i.link_id;const s=t.handshake.query;this.sharingLink=s&&s.sharing_link;this.sharingToken=s&&s.sharing_token;this.fileCache=i.file_info;this.isAdmin=i.is_admin;this.lang=i.lang;this.perm={previewable:i.previewable,readable:i.readable,writable:i.writable,commentable:i.commentable,organizable:i.organizable};this.guestId=null;this.selection=null;this.userInfoReady=false;e.updateSnapshotVersion(i.snapshot_version);if(this.isAnonymous()){this.guestId=e.addGuest(this.id);i.username=String(this.guestId)}this._initUserRecord(i);e.registerClient(this);this.registerEvents();e.checkFileInfo(i.file_info,i.uid)}registerEvents(){const e=this.socket;e.on("disconnect",(()=>this._onDestroy()));e.on("say_hello",(e=>this.onSayHello(e)));e.on("fetch_usergroup",((e,t,i)=>this._onFetchUserGroup(e,t,i)));e.on("ask_peers",(e=>this._onAskPeers(e)))}get id(){return this.socket.id}getUid(){return this.uid}getPermission(){return this.perm}getRemoteIP(){return this.remoteIP}getLinkId(){return this.linkId}getSharingToken(){return this.sharingToken}getSharingLink(){return this.sharingLink}isAnonymous(){return this.uid===n}isCommentable(){const e=this.getPermission();return e&&e.commentable===true}isWritable(){const e=this.getPermission();return e&&e.writable===true}isConnected(){return this.socket&&this.socket.connected}getObjectId(){return this.instance.objectId}getFileCacheInfo(){return this.fileCache}getGuestId(){return this.guestId}emit(e,...t){this.socket.emit(e,...t)}_onDestroy(){this.sayBye();this.instance.unregisterClient(this);if(this.isAnonymous()){this.instance.removeGuest(this.id)}}_onFetchUserGroup(e,t,i){this.instance.getApp().fetchUserGroup(this.isAnonymous(),e,t,i)}kick(e){if(e){this.emit("kick",e)}this.socket.disconnect(true)}updateMtime(){(0,e.touch)(this.getObjectId(),this.getRemoteIP(),this.getUid(),this.getSharingToken(),this.isAdmin,true,(()=>this.instance.isSnapshotting()))}setFileName(e){this.getFileCacheInfo().name=e}setFileRemoved(e){this.getFileCacheInfo().removed=e}setStar(e){this.getFileCacheInfo().star=e}broadcast(e,...t){this.socket.to(this.getObjectId()).emit(e,...t)}broadcastPeerInfo(e,t){if(!this.isConnected()){return}for(const i of this.instance.listClients()){if(i.id===this.id){return}const s={...t};if(i.isAnonymous()){delete s.name}i.emit(e,s)}}_initUserRecord(e){this.instance.getApp().initUserRecord(e);this._initPeerInfo()}_initPeerInfo(){if(this.isAnonymous()){return}this.getPeerInfo()}async getPeerInfo(){if(!this.userInfoReady&&!this.isAnonymous()){try{await this.fetchUserInfo();this.userInfoReady=true}catch{}}const e=this.instance.getApp().getUserRecord(this.uid);return{id:this.id,uid:this.uid,selection:this.selection,nickname:e?.nick,color:e?.color,guest_id:this.guestId,name:e?.name}}_askPeers(){return Promise.all(this.instance.listClients().map((async e=>{try{const t=await e.getPeerInfo();if(this.isAnonymous()){delete t.name}return t}catch(t){i.logger.error("getPeerInfo failed",e.id,t)}}))).then((e=>e.filter((e=>{if(!e){return false}const t=this.instance.getClient(e.id);if(!t){return false}return t.isConnected()}))))}_onAskPeers(e){this._askPeers().then((t=>{e(t)}))}fetchUserInfo(){return this.instance.getApp().fetchUserRecord(this.uid)}notifyComment(i){if(!(0,e.isFile)(this.getObjectId())){return}t.default.root.queue.push({...i,api:"SYNO.Office.Notification",version:1,method:"send",object_id:this.getObjectId(),remote_ip:this.getRemoteIP(),sharing_token:this.getSharingToken(),is_admin:this.isAdmin,async:false})}}const o=r;