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:
45
server/models/helpers/NotificationSettingsHelper.ts
Normal file
45
server/models/helpers/NotificationSettingsHelper.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import crypto from "crypto";
|
||||
import {
|
||||
NotificationEventDefaults,
|
||||
NotificationEventType,
|
||||
} from "@shared/types";
|
||||
import env from "@server/env";
|
||||
import User from "../User";
|
||||
|
||||
/**
|
||||
* Helper class for working with notification settings
|
||||
*/
|
||||
export default class NotificationSettingsHelper {
|
||||
/**
|
||||
* Get the default notification settings for a user
|
||||
*
|
||||
* @returns The default notification settings
|
||||
*/
|
||||
public static getDefaults() {
|
||||
return NotificationEventDefaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unsubscribe URL for a user and event type. This url allows the user
|
||||
* to unsubscribe from a specific event without being signed in, for one-click
|
||||
* links in emails.
|
||||
*
|
||||
* @param user The user to unsubscribe
|
||||
* @param eventType The event type to unsubscribe from
|
||||
* @returns The unsubscribe URL
|
||||
*/
|
||||
public static unsubscribeUrl(user: User, eventType: NotificationEventType) {
|
||||
return `${
|
||||
env.URL
|
||||
}/api/notifications.unsubscribe?token=${this.unsubscribeToken(
|
||||
user,
|
||||
eventType
|
||||
)}&userId=${user.id}&eventType=${eventType}`;
|
||||
}
|
||||
|
||||
public static unsubscribeToken(user: User, eventType: NotificationEventType) {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(`${user.id}-${env.SECRET_KEY}-${eventType}`);
|
||||
return hash.digest("hex");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user