🔔 Implementing notifications (#231)

This commit is contained in:
Montassar Ghanmy
2023-11-02 12:25:17 +01:00
committed by GitHub
parent 826e568b5c
commit fea76974a5
15 changed files with 366 additions and 30 deletions
@@ -1,10 +1,47 @@
import globalResolver from "../../../global-resolver";
import { logger } from "../../../../core/platform/framework";
import { localEventBus } from "../../../../core/platform/framework/event-bus";
import { Initializable } from "../../../../core/platform/framework";
import { DocumentEvents, NotificationPayloadType } from "../../types";
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";
export class DocumentsEngine implements Initializable {
private documentRepository: Repository<DriveFile>;
async DispatchDocumentEvent(e: NotificationPayloadType, event: string) {
const sender = await globalResolver.services.users.get({ id: e.notificationEmitter });
const receiver = await globalResolver.services.users.get({ id: e.notificationReceiver });
const company = await globalResolver.services.companies.getCompany({
id: e.context.company.id,
});
try {
const { html, text, subject } = await globalResolver.platformServices.emailPusher.build(
"notification-document",
receiver.language || "en",
{
sender,
receiver,
company,
notifications: [
{
type: event,
item: e.item,
},
],
},
);
await globalResolver.platformServices.emailPusher.send(receiver.email_canonical, {
subject,
html,
text,
});
} catch (error) {
logger.error(error);
}
}
async init(): Promise<this> {
const repository = await globalResolver.database.getRepository<DriveFile>(TYPE, DriveFile);
@@ -13,6 +50,25 @@ export class DocumentsEngine implements Initializable {
new DocumentsFinishedProcess(repository),
);
localEventBus.subscribe(DocumentEvents.DOCUMENT_SAHRED, async (e: NotificationPayloadType) => {
await this.DispatchDocumentEvent(e, DocumentEvents.DOCUMENT_SAHRED);
});
localEventBus.subscribe(
DocumentEvents.DOCUMENT_VERSION_UPDATED,
async (e: NotificationPayloadType) => {
await this.DispatchDocumentEvent(e, DocumentEvents.DOCUMENT_VERSION_UPDATED);
},
);
return this;
}
notifyDocumentShared(notificationPayload: NotificationPayloadType) {
localEventBus.publish(DocumentEvents.DOCUMENT_SAHRED, notificationPayload);
}
notifyDocumentVersionUpdated(notificationPayload: NotificationPayloadType) {
localEventBus.publish(DocumentEvents.DOCUMENT_VERSION_UPDATED, notificationPayload);
}
}
@@ -50,7 +50,6 @@ import {
getItemScope,
} from "./access-check";
import { websocketEventBus } from "../../../core/platform/services/realtime/bus";
import archiver from "archiver";
import internal from "stream";
import {
@@ -416,10 +415,26 @@ export class DocumentsService {
} else {
oldParent = item.parent_id;
}
if (key === "access_info") {
const sharedWith = content.access_info.entities.filter(
info =>
!item.access_info.entities.find(entity => entity.id === info.id) &&
info.type === "user",
);
item.access_info = content.access_info;
item.access_info.entities.forEach(info => {
if (sharedWith.length > 0) {
// Notify the user that the document has been shared with them
gr.services.documents.engine.notifyDocumentShared({
context,
item,
notificationEmitter: context.user.id,
notificationReceiver: sharedWith[0].id,
});
}
item.access_info.entities.forEach(async info => {
if (!info.grantor) {
info.grantor = context.user.id;
}
@@ -717,6 +732,14 @@ export class DocumentsService {
await this.repository.save(item);
// Notify the user that the document versions have been updated
gr.services.documents.engine.notifyDocumentVersionUpdated({
context,
item,
notificationEmitter: context.user.id,
notificationReceiver: item.creator,
});
this.notifyWebsocket(item.parent_id, context);
await updateItemSize(item.parent_id, this.repository, context);
@@ -91,3 +91,15 @@ export type DriveTdriveTab = {
item_id: string;
level: "read" | "write";
};
export enum DocumentEvents {
DOCUMENT_SAHRED = "document_shared",
DOCUMENT_VERSION_UPDATED = "document_version_updated",
}
export type NotificationPayloadType = {
context: CompanyExecutionContext;
item: DriveFile;
notificationEmitter: string;
notificationReceiver: string;
};