diff --git a/server/account/src/index.ts b/server/account/src/index.ts index ca29a311f1..2d30c68a27 100644 --- a/server/account/src/index.ts +++ b/server/account/src/index.ts @@ -89,6 +89,8 @@ export interface Account { admin?: boolean confirmed?: boolean lastWorkspace?: number + createdOn: number + lastVisit: number } /** @@ -104,6 +106,8 @@ export interface Workspace { workspaceUrl?: string | null // An optional url to the workspace, if not set workspace will be used workspaceName?: string // An displayed workspace name + createdOn: number + lastVisit: number } /** @@ -552,6 +556,8 @@ export async function createAcc ( last, confirmed, workspaces: [], + createdOn: Date.now(), + lastVisit: Date.now(), ...(extra ?? {}) }) @@ -674,7 +680,9 @@ async function generateWorkspaceRecord ( version, workspaceName, accounts: [], - disabled: false + disabled: false, + createdOn: Date.now(), + lastVisit: Date.now() } // Add fixed workspace const id = await coll.insertOne(data) @@ -701,7 +709,9 @@ async function generateWorkspaceRecord ( version, workspaceName, accounts: [], - disabled: false + disabled: false, + createdOn: Date.now(), + lastVisit: Date.now() } // Nice we do not have a workspace or workspaceUrl duplicated. const id = await coll.insertOne(data) @@ -953,10 +963,15 @@ export async function getUserWorkspaces (db: Db, productId: string, token: strin /** * @public */ -export async function getWorkspaceInfo (db: Db, productId: string, token: string): Promise<ClientWorkspaceInfo> { +export async function getWorkspaceInfo ( + db: Db, + productId: string, + token: string, + updateLatsVisit: boolean = false +): Promise<ClientWorkspaceInfo> { const { email, workspace, extra } = decodeToken(token) const guest = extra?.guest === 'true' - let account: Pick<Account, 'admin' | 'workspaces'> | null = null + let account: Pick<Account, 'admin' | 'workspaces'> | Account | null = null const query: Filter<Workspace> = { workspace: workspace.name } @@ -987,9 +1002,24 @@ export async function getWorkspaceInfo (db: Db, productId: string, token: string if (ws == null) { throw new PlatformError(new Status(Severity.ERROR, platform.status.Forbidden, {})) } + if (updateLatsVisit && isAccount(account)) { + await updateLastVisit(db, ws, account) + } return mapToClientWorkspace(ws) } +function isAccount (data: Pick<Account, 'admin' | 'workspaces'> | Account | null): data is Account { + return (data as Account)._id !== undefined +} + +async function updateLastVisit (db: Db, ws: Workspace, account: Account): Promise<void> { + const now = Date.now() + await db.collection(WORKSPACE_COLLECTION).updateOne({ _id: ws._id }, { $set: { lastVisit: now } }) + + // Add workspace to account + await db.collection(ACCOUNT_COLLECTION).updateOne({ _id: account._id }, { $set: { lastVisit: now } }) +} + async function getWorkspaceAndAccount ( db: Db, productId: string, diff --git a/server/ws/src/server.ts b/server/ws/src/server.ts index b1f16ac9f1..fa4054478d 100644 --- a/server/ws/src/server.ts +++ b/server/ws/src/server.ts @@ -175,7 +175,7 @@ class TSessionManager implements SessionManager { }, body: JSON.stringify({ method: 'getWorkspaceInfo', - params: [] + params: [true] }) }) ).json()