Remove NotificationSettings table (#5036
* helper * Add script to move notification settings * wip, removal of NotificationSettings * event name * iteration * test * test * Remove last of NotificationSettings model * refactor * More fixes * snapshots * Change emails to class instances for type safety * test * docs * Update migration for self-hosted * tsc
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import { subMinutes } from "date-fns";
|
||||
import { computed, observable } from "mobx";
|
||||
import { computed, action, observable } from "mobx";
|
||||
import { now } from "mobx-utils";
|
||||
import type { Role, UserPreference, UserPreferences } from "@shared/types";
|
||||
import {
|
||||
NotificationEventDefaults,
|
||||
NotificationEventType,
|
||||
UserPreference,
|
||||
UserPreferences,
|
||||
} from "@shared/types";
|
||||
import type { Role, NotificationSettings } from "@shared/types";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import ParanoidModel from "./ParanoidModel";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
@@ -30,6 +37,10 @@ class User extends ParanoidModel {
|
||||
@observable
|
||||
preferences: UserPreferences | null;
|
||||
|
||||
@Field
|
||||
@observable
|
||||
notificationSettings: NotificationSettings;
|
||||
|
||||
email: string;
|
||||
|
||||
isAdmin: boolean;
|
||||
@@ -72,6 +83,49 @@ class User extends ParanoidModel {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current preference for the given notification event type taking
|
||||
* into account the default system value.
|
||||
*
|
||||
* @param type The type of notification event
|
||||
* @returns The current preference
|
||||
*/
|
||||
public subscribedToEventType = (type: NotificationEventType) => {
|
||||
return (
|
||||
this.notificationSettings[type] ??
|
||||
NotificationEventDefaults[type] ??
|
||||
false
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a preference for the users notification settings on the model and
|
||||
* saves the change to the server.
|
||||
*
|
||||
* @param type The type of notification event
|
||||
* @param value Set the preference to true/false
|
||||
*/
|
||||
@action
|
||||
setNotificationEventType = async (
|
||||
eventType: NotificationEventType,
|
||||
value: boolean
|
||||
) => {
|
||||
this.notificationSettings = {
|
||||
...this.notificationSettings,
|
||||
[eventType]: value,
|
||||
};
|
||||
|
||||
if (value) {
|
||||
await client.post(`/users.notificationsSubscribe`, {
|
||||
eventType,
|
||||
});
|
||||
} else {
|
||||
await client.post(`/users.notificationsUnsubscribe`, {
|
||||
eventType,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the value for a specific preference key, or return the fallback if
|
||||
* none is set.
|
||||
|
||||
Reference in New Issue
Block a user