From be093e1b758b858190caafd01913c55e656b2a4b Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 16 Apr 2025 16:16:44 +0800 Subject: [PATCH] feat(desktop): updateNotificationsToken --- apps/desktop/src/main/src/index.ts | 4 +- apps/desktop/src/main/src/init.ts | 2 - apps/desktop/src/main/src/lib/store.ts | 3 - apps/desktop/src/main/src/lib/user.ts | 18 +++++- apps/desktop/src/main/src/tipc/auth.ts | 13 ++++ apps/desktop/src/main/src/tipc/index.ts | 2 + apps/desktop/src/renderer/src/queries/auth.ts | 8 ++- packages/shared/src/hono.ts | 61 +++++++++++++++++++ 8 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 apps/desktop/src/main/src/tipc/auth.ts diff --git a/apps/desktop/src/main/src/index.ts b/apps/desktop/src/main/src/index.ts index 2f863c1dd..adcdcd8b9 100644 --- a/apps/desktop/src/main/src/index.ts +++ b/apps/desktop/src/main/src/index.ts @@ -183,9 +183,9 @@ function bootstrap() { if (ck && apiURL) { const cookie = parse(atob(ck), { decode: (value) => value }) - Object.keys(cookie).forEach((name) => { + Object.keys(cookie).forEach(async (name) => { const value = cookie[name] - mainWindow.webContents.session.cookies.set({ + await mainWindow.webContents.session.cookies.set({ url: apiURL, name, value, diff --git a/apps/desktop/src/main/src/init.ts b/apps/desktop/src/main/src/init.ts index e4ab0de9f..cba43e38f 100644 --- a/apps/desktop/src/main/src/init.ts +++ b/apps/desktop/src/main/src/init.ts @@ -153,8 +153,6 @@ const registerPushNotifications = async () => { const credentials = store.get(credentialsKey) const persistentIds = store.get(persistentIdsKey) - updateNotificationsToken() - const instance = new PushReceiver({ debug: true, firebase: JSON.parse(env.VITE_FIREBASE_CONFIG), diff --git a/apps/desktop/src/main/src/lib/store.ts b/apps/desktop/src/main/src/lib/store.ts index edb1abf0c..b8423ad64 100644 --- a/apps/desktop/src/main/src/lib/store.ts +++ b/apps/desktop/src/main/src/lib/store.ts @@ -1,5 +1,4 @@ import type { Credentials } from "@eneris/push-receiver/dist/types" -import type { Cookie } from "electron" import Store from "electron-store" // @keep-sorted @@ -7,9 +6,7 @@ type StoreData = { "notifications-credentials"?: Credentials | null "notifications-persistent-ids"?: string[] | null appearance?: "light" | "dark" | "system" | null - betterAuthSessionCookie?: string | null cacheSizeLimit?: number | null - cookies?: Cookie[] | null minimizeToTray?: boolean | null proxy?: string | null user?: string | null diff --git a/apps/desktop/src/main/src/lib/user.ts b/apps/desktop/src/main/src/lib/user.ts index 4857ae23e..1432f2b91 100644 --- a/apps/desktop/src/main/src/lib/user.ts +++ b/apps/desktop/src/main/src/lib/user.ts @@ -6,6 +6,14 @@ import { logger } from "~/logger" import { apiClient } from "./api-client" import { store } from "./store" +const notificationChannel = isMacOS + ? "macos" + : isWindows + ? "windows" + : isLinux + ? "linux" + : "desktop" + export const updateNotificationsToken = async (newCredentials?: Credentials) => { if (newCredentials) { store.set("notifications-credentials", newCredentials) @@ -16,7 +24,7 @@ export const updateNotificationsToken = async (newCredentials?: Credentials) => await apiClient.messaging.$post({ json: { token: credentials.fcm.token, - channel: isMacOS ? "macos" : isWindows ? "windows" : isLinux ? "linux" : "desktop", + channel: notificationChannel, }, }) } catch (error) { @@ -24,3 +32,11 @@ export const updateNotificationsToken = async (newCredentials?: Credentials) => } } } + +export const deleteNotificationsToken = async () => { + await apiClient.messaging.$delete({ + json: { + channel: notificationChannel, + }, + }) +} diff --git a/apps/desktop/src/main/src/tipc/auth.ts b/apps/desktop/src/main/src/tipc/auth.ts new file mode 100644 index 000000000..d06a38e9d --- /dev/null +++ b/apps/desktop/src/main/src/tipc/auth.ts @@ -0,0 +1,13 @@ +import { deleteNotificationsToken, updateNotificationsToken } from "~/lib/user" + +import { t } from "./_instance" + +export const authRoute = { + sessionChanged: t.procedure.action(async () => { + await updateNotificationsToken() + }), + + signOut: t.procedure.action(async () => { + await deleteNotificationsToken() + }), +} diff --git a/apps/desktop/src/main/src/tipc/index.ts b/apps/desktop/src/main/src/tipc/index.ts index 8de402fa3..66a472fef 100644 --- a/apps/desktop/src/main/src/tipc/index.ts +++ b/apps/desktop/src/main/src/tipc/index.ts @@ -1,4 +1,5 @@ import { appRoute } from "./app" +import { authRoute } from "./auth" import { debugRoute } from "./debug" import { dockRoute } from "./dock" import { menuRoute } from "./menu" @@ -12,6 +13,7 @@ export const router = { ...appRoute, ...dockRoute, ...readerRoute, + ...authRoute, } export type Router = typeof router diff --git a/apps/desktop/src/renderer/src/queries/auth.ts b/apps/desktop/src/renderer/src/queries/auth.ts index c8733efaa..a8e7ed076 100644 --- a/apps/desktop/src/renderer/src/queries/auth.ts +++ b/apps/desktop/src/renderer/src/queries/auth.ts @@ -6,6 +6,7 @@ import { setWhoami } from "~/atoms/user" import { QUERY_PERSIST_KEY } from "~/constants" import { useAuthQuery } from "~/hooks/common" import { getAccountInfo, getSession, signOut as signOutFn } from "~/lib/auth" +import { tipcClient } from "~/lib/client" import { defineQuery } from "~/lib/defineQuery" import { clearLocalPersistStoreData } from "~/store/utils/clear" @@ -71,6 +72,7 @@ export const useSession = (options?: { enabled?: boolean }) => { } export const handleSessionChanges = () => { + tipcClient?.sessionChanged() window.location.reload() } @@ -92,7 +94,7 @@ export const signOut = async () => { // clear local store data await clearLocalPersistStoreData() // Sign out - await signOutFn().then(() => { - handleSessionChanges() - }) + await tipcClient?.signOut() + await signOutFn() + window.location.reload() } diff --git a/packages/shared/src/hono.ts b/packages/shared/src/hono.ts index 6634690e4..8c0f69c96 100644 --- a/packages/shared/src/hono.ts +++ b/packages/shared/src/hono.ts @@ -2193,6 +2193,23 @@ declare const entries: drizzle_orm_pg_core.PgTableWithColumns<{ identity: undefined; generated: undefined; }, {}, {}>; + readabilityContent: drizzle_orm_pg_core.PgColumn<{ + name: "readability_content"; + tableName: "entries"; + dataType: "string"; + columnType: "PgText"; + data: string; + driverParam: string; + notNull: false; + hasDefault: false; + isPrimaryKey: false; + isAutoincrement: false; + hasRuntimeDefault: false; + enumValues: [string, ...string[]]; + baseColumn: never; + identity: undefined; + generated: undefined; + }, {}, {}>; }; dialect: "pg"; }>; @@ -2426,6 +2443,7 @@ declare const entriesOpenAPISchema: z.ZodObject>; language: z.ZodNullable; feedId: z.ZodString; + readabilityContent: z.ZodNullable; }, "media" | "attachments" | "extra">, { attachments: z.ZodNullable | hono_types.MergeSchemaPath<{ "/configs": { $get: {