From 883aca22c6609ee5bc3b8e03b7d2b8b99174f9f8 Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 26 Oct 2023 11:43:54 +0700 Subject: [PATCH] fix --- app/srv/ws/sync/actions-def.ts | 8 ++++++-- app/srv/ws/sync/actions.ts | 4 ++++ app/srv/ws/sync/actions/activity.ts | 6 +++--- app/srv/ws/sync/actions/client_info.ts | 18 ++++++++++++++++++ app/srv/ws/sync/actions/index.ts | 1 + app/srv/ws/sync/actions/page_load.ts | 21 +++++++++++++++++++++ app/srv/ws/sync/entity/actstore.ts | 11 +++++++---- app/web/src/render/ed/logic/ed-global.ts | 4 ++++ app/web/src/render/ed/panel/script/site.tsx | 7 ++++++- app/web/src/render/ed/panel/top/left/js.tsx | 3 ++- 10 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 app/srv/ws/sync/actions/client_info.ts diff --git a/app/srv/ws/sync/actions-def.ts b/app/srv/ws/sync/actions-def.ts index 06e1e3b9..1fc9dc35 100644 --- a/app/srv/ws/sync/actions-def.ts +++ b/app/srv/ws/sync/actions-def.ts @@ -21,7 +21,10 @@ export const SyncActionDefinition = { "diff_local": "12", "sv_remote": "13" }, - "activity": "14" + "activity": "14", + "client": { + "info": "15" + } }; export const SyncActionPaths = { "0": "site.list", @@ -38,5 +41,6 @@ export const SyncActionPaths = { "11": "yjs.sv_local", "12": "yjs.diff_local", "13": "yjs.sv_remote", - "14": "activity" + "14": "activity", + "15": "client.info" }; diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index 05fc6619..73f7c74a 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -75,4 +75,8 @@ export const SyncActions = { kind: "js" | "css" | "html" | "text", activity: Activity ) => {}, + client: { + info: async (client_ids: string[]) => + ({}) as Record, + }, }; diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index 2d77c9e9..2a9a4fdf 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -1,6 +1,6 @@ import { validate } from "uuid"; import { SAction } from "../actions"; -import { actstore, broadcastActivity } from "../entity/actstore"; +import { Activity, actstore, broadcastActivity } from "../entity/actstore"; import { SyncConnection } from "../type"; export const activity: SAction["activity"] = async function ( @@ -24,7 +24,7 @@ export const activity: SAction["activity"] = async function ( const obj = actstore.page[target.page_id][target.item_id][kind]; if (obj) { - if (act === "-") delete obj[this.client_id]; + if (act === Activity.Null) delete obj[this.client_id]; else obj[this.client_id] = act; } @@ -46,7 +46,7 @@ export const activity: SAction["activity"] = async function ( const obj = actstore.comp[target.comp_id][target.item_id][kind]; if (obj) { - if (act === "-") delete obj[this.client_id]; + if (act === Activity.Null) delete obj[this.client_id]; else obj[this.client_id] = act; } diff --git a/app/srv/ws/sync/actions/client_info.ts b/app/srv/ws/sync/actions/client_info.ts new file mode 100644 index 00000000..a91e80fa --- /dev/null +++ b/app/srv/ws/sync/actions/client_info.ts @@ -0,0 +1,18 @@ +import { SAction } from "../actions"; +import { conns } from "../entity/conn"; +import { SyncConnection } from "../type"; + +export const client_info: SAction["client"]["info"] = async function ( + this: SyncConnection, + ids +) { + const result = {} as any; + for (const client_id of ids) { + const user = conns.get(client_id)?.user; + if (user) { + result[client_id] = { id: user.id, username: user.username }; + } + } + + return result; +}; diff --git a/app/srv/ws/sync/actions/index.ts b/app/srv/ws/sync/actions/index.ts index a06fae42..d0fa6da7 100644 --- a/app/srv/ws/sync/actions/index.ts +++ b/app/srv/ws/sync/actions/index.ts @@ -1,4 +1,5 @@ export * from "./activity"; +export * from "./client_info"; export * from "./site_js"; export * from "./site_load"; export * from "./site_group"; diff --git a/app/srv/ws/sync/actions/page_load.ts b/app/srv/ws/sync/actions/page_load.ts index 53491cb1..e141761c 100644 --- a/app/srv/ws/sync/actions/page_load.ts +++ b/app/srv/ws/sync/actions/page_load.ts @@ -7,6 +7,7 @@ import { user } from "../entity/user"; import { gzipAsync } from "../entity/zlib"; import { sendWS } from "../sync-handler"; import { SyncConnection, SyncType } from "../type"; +import { Activity, actstore, broadcastActivity } from "../entity/actstore"; export const page_load: SAction["page"]["load"] = async function ( this: SyncConnection, @@ -19,6 +20,26 @@ export const page_load: SAction["page"]["load"] = async function ( if (!conf) return undefined; conf.page_id = id; + if (!actstore.page[id]) { + actstore.page[id] = { + load: { + root: { + [this.client_id]: Activity.Open, + }, + }, + }; + } else { + const load = actstore.page[id]["load"]; + if (load && load.root) { + load.root[this.client_id] = Activity.Open; + } + } + broadcastActivity( + { + page_id: id, + }, + [this.client_id] + ); const createUndoManager = async (root: Y.Map) => { const um = new Y.UndoManager(root, { diff --git a/app/srv/ws/sync/entity/actstore.ts b/app/srv/ws/sync/entity/actstore.ts index 0624974b..73122a2a 100644 --- a/app/srv/ws/sync/entity/actstore.ts +++ b/app/srv/ws/sync/entity/actstore.ts @@ -10,8 +10,11 @@ type COMP_ID = string; type ITEM_ID = string; type CLIENT_ID = string; -export type Activity = "open" | "-"; -export type ActivityKind = "js" | "css" | "html" | "text"; +export enum Activity { + Open, + Null, +} +export type ActivityKind = "root" | "js" | "css" | "html" | "text"; export type ActivityList = Record< ITEM_ID, Partial>> @@ -28,7 +31,7 @@ export const broadcastActivity = ( const wss = new Set>(); const data = {} as any; if (arg.page_id) { - data.page = actstore.page[arg.page_id]; + data.page = actstore.page[arg.page_id] || {}; user.active.findAll({ page_id: arg.page_id }).forEach((u) => { if (u.client_id && (!exclude || !exclude.includes(u.client_id))) { const ws = conns.get(u.client_id)?.ws; @@ -37,7 +40,7 @@ export const broadcastActivity = ( }); } if (arg.comp_id) { - data.comp = actstore.page[arg.comp_id]; + data.comp = actstore.page[arg.comp_id] || {}; user.active.findAll({ comp_id: arg.comp_id }).forEach((u) => { if (u.client_id && (!exclude || !exclude.includes(u.client_id))) { const ws = conns.get(u.client_id)?.ws; diff --git a/app/web/src/render/ed/logic/ed-global.ts b/app/web/src/render/ed/logic/ed-global.ts index b4ceeb95..e952f383 100644 --- a/app/web/src/render/ed/logic/ed-global.ts +++ b/app/web/src/render/ed/logic/ed-global.ts @@ -69,6 +69,10 @@ export const EDGlobal = { | "ready", sync: null as unknown as Awaited>, site: EmptySite, + activity: { + page: {}, + comp: {}, + }, script: { siteTypes: {} as Record }, page: { cur: EmptyPage, diff --git a/app/web/src/render/ed/panel/script/site.tsx b/app/web/src/render/ed/panel/script/site.tsx index 9dd0a7ef..a877fe85 100644 --- a/app/web/src/render/ed/panel/script/site.tsx +++ b/app/web/src/render/ed/panel/script/site.tsx @@ -3,6 +3,7 @@ import { EdMonaco } from "./monaco/monaco"; import { EDGlobal, active } from "../../logic/ed-global"; import { compress } from "wasm-gzip"; import { jscript } from "../../../../utils/script/jscript"; +import { Activity } from "../../../../../../srv/ws/sync/entity/actstore"; export const EdScriptSite = () => { const p = useGlobal(EDGlobal, "EDITOR"); @@ -47,7 +48,11 @@ export const EdScriptSite = () => { }} onClose={() => { p.ui.script.site = false; - p.sync.activity({ page_id: p.page.cur.id, item_id: "site" }, "js", "-"); + p.sync.activity( + { page_id: p.page.cur.id, item_id: "site" }, + "js", + Activity.Null + ); p.render(); }} /> diff --git a/app/web/src/render/ed/panel/top/left/js.tsx b/app/web/src/render/ed/panel/top/left/js.tsx index 0774188e..9eae9c4a 100644 --- a/app/web/src/render/ed/panel/top/left/js.tsx +++ b/app/web/src/render/ed/panel/top/left/js.tsx @@ -1,6 +1,7 @@ import { useGlobal } from "web-utils"; import { TopBtn } from "../top-btn"; import { EDGlobal, active } from "../../../logic/ed-global"; +import { Activity } from "../../../../../../../srv/ws/sync/entity/actstore"; export const EdSiteJS = () => { const p = useGlobal(EDGlobal, "EDITOR"); @@ -12,7 +13,7 @@ export const EdSiteJS = () => { p.sync.activity( { page_id: p.page.cur.id, item_id: "site" }, "js", - "open" + Activity.Open ); p.ui.script.site = true; p.render();