Add document unsubscribe link in email footer (#5762)
This commit is contained in:
@@ -4,7 +4,6 @@ import {
|
||||
NotificationEventType,
|
||||
} from "@shared/types";
|
||||
import env from "@server/env";
|
||||
import User from "../User";
|
||||
|
||||
/**
|
||||
* Helper class for working with notification settings
|
||||
@@ -24,22 +23,28 @@ export default class NotificationSettingsHelper {
|
||||
* to unsubscribe from a specific event without being signed in, for one-click
|
||||
* links in emails.
|
||||
*
|
||||
* @param user The user to unsubscribe
|
||||
* @param userId The user ID to unsubscribe
|
||||
* @param eventType The event type to unsubscribe from
|
||||
* @returns The unsubscribe URL
|
||||
*/
|
||||
public static unsubscribeUrl(user: User, eventType: NotificationEventType) {
|
||||
public static unsubscribeUrl(
|
||||
userId: string,
|
||||
eventType: NotificationEventType
|
||||
) {
|
||||
return `${
|
||||
env.URL
|
||||
}/api/notifications.unsubscribe?token=${this.unsubscribeToken(
|
||||
user,
|
||||
userId,
|
||||
eventType
|
||||
)}&userId=${user.id}&eventType=${eventType}`;
|
||||
)}&userId=${userId}&eventType=${eventType}`;
|
||||
}
|
||||
|
||||
public static unsubscribeToken(user: User, eventType: NotificationEventType) {
|
||||
public static unsubscribeToken(
|
||||
userId: string,
|
||||
eventType: NotificationEventType
|
||||
) {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(`${user.id}-${env.SECRET_KEY}-${eventType}`);
|
||||
hash.update(`${userId}-${env.SECRET_KEY}-${eventType}`);
|
||||
return hash.digest("hex");
|
||||
}
|
||||
}
|
||||
|
||||
29
server/models/helpers/SubscriptionHelper.ts
Normal file
29
server/models/helpers/SubscriptionHelper.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import crypto from "crypto";
|
||||
import env from "@server/env";
|
||||
|
||||
/**
|
||||
* Helper class for working with subscription settings
|
||||
*/
|
||||
export default class SubscriptionHelper {
|
||||
/**
|
||||
* Get the unsubscribe URL for a user and document. This url allows the user
|
||||
* to unsubscribe from a specific document without being signed in, for one-click
|
||||
* links in emails.
|
||||
*
|
||||
* @param userId The user ID to unsubscribe
|
||||
* @param documentId The document ID to unsubscribe from
|
||||
* @returns The unsubscribe URL
|
||||
*/
|
||||
public static unsubscribeUrl(userId: string, documentId: string) {
|
||||
return `${env.URL}/api/subscriptions.delete?token=${this.unsubscribeToken(
|
||||
userId,
|
||||
documentId
|
||||
)}&userId=${userId}&documentId=${documentId}`;
|
||||
}
|
||||
|
||||
public static unsubscribeToken(userId: string, documentId: string) {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(`${userId}-${env.SECRET_KEY}-${documentId}`);
|
||||
return hash.digest("hex");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user