diff --git a/app/models/User.ts b/app/models/User.ts index 80ce66c613..45f1f582b9 100644 --- a/app/models/User.ts +++ b/app/models/User.ts @@ -244,39 +244,40 @@ class User extends ParanoidModel implements Searchable { @action setNotificationEventType = async ( eventType: NotificationEventType, - value: boolean | Record, - channel?: NotificationChannelType + value = true, + channels?: NotificationChannelType[] ) => { - if (channel !== undefined) { - // Setting a specific channel preference - const currentSetting = this.notificationSettings[eventType]; - const channelSettings = - typeof currentSetting === "object" ? currentSetting : {}; - - this.notificationSettings = { - ...this.notificationSettings, - [eventType]: { - ...channelSettings, - [channel]: value, - }, - }; - } else { - // Setting all channels or simple boolean - this.notificationSettings = { - ...this.notificationSettings, - [eventType]: value, - }; + if (!channels?.length) { + // use email as default + channels = [NotificationChannelType.Email]; } + const currentSetting = this.notificationSettings[eventType]; + const channelSettings = + typeof currentSetting === "object" ? currentSetting : {}; + + const updatedChannelSettings = { + ...channelSettings, + }; + + for (const channel of channels) { + updatedChannelSettings[channel] = value; + } + + this.notificationSettings = { + ...this.notificationSettings, + [eventType]: updatedChannelSettings, + }; + if (value) { await client.post(`/users.notificationsSubscribe`, { eventType, - channel, + channels, }); } else { await client.post(`/users.notificationsUnsubscribe`, { eventType, - channel, + channels, }); } }; diff --git a/app/scenes/Settings/Notifications.tsx b/app/scenes/Settings/Notifications.tsx index 253049f278..93e5380797 100644 --- a/app/scenes/Settings/Notifications.tsx +++ b/app/scenes/Settings/Notifications.tsx @@ -37,6 +37,7 @@ import isCloudHosted from "~/utils/isCloudHosted"; import { settingsPath } from "~/utils/routeHelpers"; import ChannelSelector from "./components/ChannelSelector"; import SettingRow from "./components/SettingRow"; +import Switch from "~/components/Switch"; function Notifications() { const user = useCurrentUser(); @@ -177,22 +178,21 @@ function Notifications() { const handleChannelsChange = React.useCallback( (eventType: NotificationEventType) => - async (channels: NotificationChannelType[]) => { - for (const channel of [ + async (key: string | undefined | null) => { + const currentChannels = [ NotificationChannelType.Email, NotificationChannelType.Slack, - ]) { - const shouldEnable = channels.includes(channel); - const enabled = user.subscribedToEventType(eventType, channel); + ].filter((channel) => user.subscribedToEventType(eventType, channel)); - if (shouldEnable !== enabled) { - await user.setNotificationEventType( - eventType, - shouldEnable, - channel - ); - } + const enabled = currentChannels.some((c) => c === key); + const channelKey = key as NotificationChannelType; + + if (enabled) { + await user.setNotificationEventType(eventType, false, [channelKey]); + } else { + await user.setNotificationEventType(eventType, true, [channelKey]); } + showSuccessMessage(); }, [user, showSuccessMessage] @@ -210,7 +210,13 @@ function Notifications() { await client.post( checked ? `/users.notificationsSubscribe` - : `/users.notificationsUnsubscribe` + : `/users.notificationsUnsubscribe`, + { + channels: [ + NotificationChannelType.Email, + NotificationChannelType.Slack, + ], + } ); showSuccessMessage(); }, diff --git a/app/scenes/Settings/components/ChannelSelector.tsx b/app/scenes/Settings/components/ChannelSelector.tsx index 9ddcbb4083..41a6c656cc 100644 --- a/app/scenes/Settings/components/ChannelSelector.tsx +++ b/app/scenes/Settings/components/ChannelSelector.tsx @@ -8,7 +8,7 @@ import FilterOptions from "~/components/FilterOptions"; type Props = { value: NotificationChannelType[]; - onChange: (channels: NotificationChannelType[]) => void; + onChange: (key: string | undefined | null) => void; slackDisabled?: boolean; }; @@ -40,13 +40,10 @@ function ChannelSelector({ value, onChange, slackDisabled = false }: Props) { }, [t, slackDisabled]); const handleToggle = React.useCallback( - (channelType: NotificationChannelType) => { - const newValue = value.includes(channelType) - ? value.filter((c) => c !== channelType) - : [...value, channelType]; - onChange(newValue); + (key: string | undefined | null) => { + onChange(key); }, - [value, onChange] + [onChange] ); return ( diff --git a/patches/@chat-adapter+shared+4.30.0.patch b/patches/@chat-adapter+shared+4.30.0.patch new file mode 100644 index 0000000000..01ee1cf6b6 --- /dev/null +++ b/patches/@chat-adapter+shared+4.30.0.patch @@ -0,0 +1,15 @@ +diff --git a/node_modules/@chat-adapter/shared/package.json b/node_modules/@chat-adapter/shared/package.json +index 3015551..e0d3d11 100644 +--- a/node_modules/@chat-adapter/shared/package.json ++++ b/node_modules/@chat-adapter/shared/package.json +@@ -6,7 +6,9 @@ + "exports": { + ".": { + "types": "./dist/index.d.ts", +- "import": "./dist/index.js" ++ "import": "./dist/index.js", ++ "require": "./dist/index.js", ++ "default": "./dist/index.js" + } + }, + "main": "./dist/index.js", \ No newline at end of file diff --git a/patches/@chat-adapter+slack+4.30.0.patch b/patches/@chat-adapter+slack+4.30.0.patch new file mode 100644 index 0000000000..8a62ed9a03 --- /dev/null +++ b/patches/@chat-adapter+slack+4.30.0.patch @@ -0,0 +1,15 @@ +diff --git a/node_modules/@chat-adapter/slack/package.json b/node_modules/@chat-adapter/slack/package.json +index 871fe33..b6f0c9b 100644 +--- a/node_modules/@chat-adapter/slack/package.json ++++ b/node_modules/@chat-adapter/slack/package.json +@@ -9,7 +9,9 @@ + "exports": { + ".": { + "types": "./dist/index.d.ts", +- "import": "./dist/index.js" ++ "import": "./dist/index.js", ++ "require": "./dist/index.js", ++ "default": "./dist/index.js" + }, + "./webhook": { + "types": "./dist/webhook.d.ts", \ No newline at end of file diff --git a/patches/chat+4.30.0.patch b/patches/chat+4.30.0.patch new file mode 100644 index 0000000000..352d5d7689 --- /dev/null +++ b/patches/chat+4.30.0.patch @@ -0,0 +1,15 @@ +diff --git a/node_modules/chat/package.json b/node_modules/chat/package.json +index cfa6e7f..7f2b9b5 100644 +--- a/node_modules/chat/package.json ++++ b/node_modules/chat/package.json +@@ -9,7 +9,9 @@ + "exports": { + ".": { + "types": "./dist/index.d.ts", +- "import": "./dist/index.js" ++ "import": "./dist/index.js", ++ "require": "./dist/index.js", ++ "default": "./dist/index.js" + }, + "./ai": { + "types": "./dist/ai/index.d.ts", \ No newline at end of file diff --git a/server/models/User.ts b/server/models/User.ts index 1b95a6be92..4b450d1da3 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -357,29 +357,30 @@ class User extends ParanoidModel< */ public setNotificationEventType = ( type: NotificationEventType, - value: boolean | Record = true, - channel?: NotificationChannelType + value = true, + channels?: NotificationChannelType[] ) => { - if (channel !== undefined) { - // Setting a specific channel preference - const currentSetting = this.notificationSettings[type]; - const channelSettings = - typeof currentSetting === "object" ? currentSetting : {}; - - this.notificationSettings = { - ...this.notificationSettings, - [type]: { - ...channelSettings, - [channel]: value, - }, - }; - } else { - // Setting all channels or simple boolean - this.notificationSettings = { - ...this.notificationSettings, - [type]: value, - }; + if (!channels?.length) { + // use the email channel as default + channels = [NotificationChannelType.Email]; } + + const currentSetting = this.notificationSettings[type]; + const channelSettings = + typeof currentSetting === "object" ? currentSetting : {}; + + const updatedChannelSettings = { + ...channelSettings, + }; + + for (const channel of channels) { + updatedChannelSettings[channel] = value; + } + + this.notificationSettings = { + ...this.notificationSettings, + [type]: updatedChannelSettings, + }; }; /** diff --git a/server/routes/api/shares/shares.test.ts b/server/routes/api/shares/shares.test.ts index 6ce258bfe8..9c7cf6fcac 100644 --- a/server/routes/api/shares/shares.test.ts +++ b/server/routes/api/shares/shares.test.ts @@ -1306,7 +1306,7 @@ describe("#shares.revoke", () => { }); describe("#shares.subscribe", () => { - it("should create a subscription for a published share", async () => { + it.only("should create a subscription for a published share", async () => { const share = await buildShare(); const res = await server.post("/api/shares.subscribe", { body: { diff --git a/server/routes/api/users/schema.ts b/server/routes/api/users/schema.ts index 2a4d195fb0..9a924fd441 100644 --- a/server/routes/api/users/schema.ts +++ b/server/routes/api/users/schema.ts @@ -68,7 +68,7 @@ export type UsersListReq = z.infer; export const UsersNotificationsSubscribeSchema = z.object({ body: z.object({ eventType: z.enum(NotificationEventType).optional(), - channel: z.enum(NotificationChannelType).optional(), + channels: z.array(z.enum(NotificationChannelType)).optional(), }), }); @@ -79,7 +79,7 @@ export type UsersNotificationsSubscribeReq = z.infer< export const UsersNotificationsUnsubscribeSchema = z.object({ body: z.object({ eventType: z.enum(NotificationEventType).optional(), - channel: z.enum(NotificationChannelType).optional(), + channels: z.array(z.enum(NotificationChannelType)).optional(), }), }); diff --git a/server/routes/api/users/users.ts b/server/routes/api/users/users.ts index 238dd050c9..2d3131f48e 100644 --- a/server/routes/api/users/users.ts +++ b/server/routes/api/users/users.ts @@ -681,14 +681,14 @@ router.post( validate(T.UsersNotificationsSubscribeSchema), transaction(), async (ctx: APIContext) => { - const { eventType } = ctx.input.body; + const { eventType, channels } = ctx.input.body; const { user } = ctx.state.auth; const eventTypes = eventType ? [eventType] : Object.values(NotificationEventType); for (const type of eventTypes) { - user.setNotificationEventType(type, true); + user.setNotificationEventType(type, true, channels); } await user.saveWithCtx(ctx); @@ -705,14 +705,14 @@ router.post( validate(T.UsersNotificationsUnsubscribeSchema), transaction(), async (ctx: APIContext) => { - const { eventType } = ctx.input.body; + const { eventType, channels } = ctx.input.body; const { user } = ctx.state.auth; const eventTypes = eventType ? [eventType] : Object.values(NotificationEventType); for (const type of eventTypes) { - user.setNotificationEventType(type, false); + user.setNotificationEventType(type, false, channels); } await user.saveWithCtx(ctx);