🐛 Fix email notification link (#738)

🐛 Fix email notification link
This commit is contained in:
Montassar Ghanmy
2024-11-25 13:27:00 +01:00
committed by GitHub
parent 1481a0c596
commit 36881ce0a1
10 changed files with 205 additions and 18 deletions
@@ -7,6 +7,8 @@ import { DocumentsProcessor } from "./extract-keywords";
import Repository from "../../../../core/platform/services/database/services/orm/repository/repository";
import { DriveFile, TYPE } from "../../entities/drive-file";
import { DocumentsFinishedProcess } from "./save-keywords";
import { generateEncodedUrlComponents } from "../../utils";
export class DocumentsEngine implements Initializable {
private documentRepository: Repository<DriveFile>;
@@ -24,6 +26,7 @@ export class DocumentsEngine implements Initializable {
logger.error(`Error dispatching document event. Unknown event type: ${event}`);
return; // Early return on unknown event type
}
const urlComponents = generateEncodedUrlComponents(e, receiver.id);
try {
const { html, text, subject } = await globalResolver.platformServices.emailPusher.build(
@@ -37,10 +40,12 @@ export class DocumentsEngine implements Initializable {
{
type: event,
item: e.item,
urlComponents,
},
],
},
);
logger.info(`Sending email notification to ${receiver.email_canonical}`);
await globalResolver.platformServices.emailPusher.send(receiver.email_canonical, {
subject,
@@ -30,6 +30,7 @@ import {
DriveFileAccessLevel,
DriveItemDetails,
DriveTdriveTab,
NotificationActionType,
RootType,
SearchDocumentsOptions,
TrashType,
@@ -457,6 +458,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentShared({
context,
item: driveItem,
type: NotificationActionType.UPDATE,
notificationEmitter: context.user.id,
notificationReceiver: parentItem.creator,
});
@@ -628,6 +630,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentShared({
context,
item,
type: NotificationActionType.DIRECT,
notificationEmitter: context.user.id,
notificationReceiver: sharedWith[0].id,
});
@@ -1004,6 +1007,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentVersionUpdated({
context,
item,
type: NotificationActionType.UPDATE,
notificationEmitter: context.user.id,
notificationReceiver: item.creator,
});
@@ -1761,6 +1765,7 @@ export class DocumentsService {
await gr.services.documents.engine.notifyDocumentAVScanAlert({
context,
item,
type: NotificationActionType.DIRECT,
notificationEmitter: context.user.id,
notificationReceiver: context.user.id,
});
@@ -125,9 +125,15 @@ export const eventToTemplateMap: Record<string, any> = {
[DocumentEvents.DOCUMENT_SAHRED]: "notification-document-shared",
};
export enum NotificationActionType {
DIRECT = "direct",
UPDATE = "update",
}
export type NotificationPayloadType = {
context: CompanyExecutionContext;
item: DriveFile;
type: NotificationActionType;
notificationEmitter: string;
notificationReceiver: string;
};
@@ -21,10 +21,13 @@ import { checkAccess, generateAccessToken } from "./services/access-check";
import {
CompanyExecutionContext,
DriveExecutionContext,
NotificationActionType,
NotificationPayloadType,
RootType,
SharedWithMeType,
TrashType,
} from "./types";
import short, { Translator } from "short-uuid";
const ROOT: RootType = "root";
const TRASH: TrashType = "trash";
@@ -673,3 +676,43 @@ export const getKeywordsOfFile = async (
}
return extractKeywords(content_strings);
};
/**
* Generate encodedUrl for email notification
*/
export const generateEncodedUrlComponents = (e: NotificationPayloadType, receiver: string) => {
const translator: Translator = short();
const encodedCompanyId = translator.fromUUID(e.item.company_id);
const clientPath = ["client", encodedCompanyId, "v"];
const isPersonalScope = e.item.scope === "personal";
const isDirectory = e.item.is_directory;
const itemId = isDirectory ? e.item.id : e.item.parent_id;
// Determine the scope and base view
let view: string;
switch (e.type) {
case NotificationActionType.UPDATE:
view = isPersonalScope ? `user_${receiver}` : "root";
break;
case NotificationActionType.DIRECT:
view = "shared_with_me";
break;
default:
throw new Error(`Unexpected NotificationActionType value: ${e.type}`);
}
// Build URL components
const urlComponents = [...clientPath, view];
// Add directory and itemId if applicable
if (e.type === NotificationActionType.UPDATE || isDirectory) {
urlComponents.push("d", itemId);
}
// To highlight the file in the document browser when the user clicks on the notification
if (!isDirectory) {
urlComponents.push("preview", e.item.id);
}
return urlComponents;
};