mirror of
https://github.com/outline/outline.git
synced 2026-12-05 10:45:07 +03:00
rework channel selection
This commit is contained in:
1 parent
aaa94d63c4
commit
e059d82383
10 files changed
+121
-71
No files matched your search
+24
-23
@@ -244,39 +244,40 @@ class User extends ParanoidModel implements Searchable {
|
||||
@action
|
||||
setNotificationEventType = async (
|
||||
eventType: NotificationEventType,
|
||||
value: boolean | Record<NotificationChannelType, boolean>,
|
||||
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,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
+22
-21
@@ -357,29 +357,30 @@ class User extends ParanoidModel<
|
||||
*/
|
||||
public setNotificationEventType = (
|
||||
type: NotificationEventType,
|
||||
value: boolean | Record<NotificationChannelType, boolean> = 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,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -68,7 +68,7 @@ export type UsersListReq = z.infer<typeof UsersListSchema>;
|
||||
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(),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -681,14 +681,14 @@ router.post(
|
||||
validate(T.UsersNotificationsSubscribeSchema),
|
||||
transaction(),
|
||||
async (ctx: APIContext<T.UsersNotificationsSubscribeReq>) => {
|
||||
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<T.UsersNotificationsUnsubscribeReq>) => {
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user