@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
143 lines (142 loc) • 7.03 kB
JavaScript
/*
* Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com>
* This file is part of Sync-in | The open source file sync and share solution
* See the LICENSE file for licensing details
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "NotificationsManager", {
enumerable: true,
get: function() {
return NotificationsManager;
}
});
const _common = require("@nestjs/common");
const _mailerservice = require("../../../infrastructure/mailer/mailer.service");
const _user = require("../../users/constants/user");
const _usersmanagerservice = require("../../users/services/users-manager.service");
const _notifications = require("../constants/notifications");
const _websocket = require("../constants/websocket");
const _models = require("../mails/models");
const _notificationsgateway = require("../notifications.gateway");
const _notificationsqueriesservice = require("./notifications-queries.service");
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
let NotificationsManager = class NotificationsManager {
list(user, onlyUnread = false) {
return this.notificationsQueries.list(user.id, onlyUnread);
}
async create(toUsers, content, options) {
// store it in db
const isArrayOfUsers = typeof toUsers[0] === 'object';
const toUserIds = isArrayOfUsers ? toUsers.map((m)=>m.id) : toUsers;
this.storeNotification(toUserIds, content, options?.author?.id).catch((e)=>this.logger.error(`${this.create.name} - ${e}`));
// send websocket notification
this.webSocketNotifications.sendMessageToUsers(toUserIds, _websocket.NOTIFICATIONS_WS.EVENTS.NOTIFICATION, 'check');
// send emails
if (this.mailer.available) {
const usersNotifiedByEmail = isArrayOfUsers ? toUsers.filter((u)=>u.notification === _user.USER_NOTIFICATION.APPLICATION_EMAIL) : await this.notificationsQueries.usersNotifiedByEmail(toUsers);
if (!usersNotifiedByEmail.length) {
return;
}
this.sendEmailNotification(usersNotifiedByEmail, content, options).catch((e)=>this.logger.error(`${this.create.name} - ${e}`));
}
}
wasRead(user, notificationId) {
this.notificationsQueries.wasRead(user.id, notificationId).catch((e)=>this.logger.error(`${this.wasRead.name} - ${e}`));
}
async delete(user, notificationId) {
return this.notificationsQueries.delete(user.id, notificationId);
}
async storeNotification(toUserIds, content, authorId) {
// store it in db
try {
await this.notificationsQueries.create(authorId || null, toUserIds, content);
} catch (e) {
this.logger.error(`${this.create.name} - ${e}`);
}
}
async sendEmailNotification(toUsers, content, options) {
if (!this.mailer.available) {
return;
}
if (options?.author) {
options.author.avatarBase64 = await this.usersManager.getAvatarBase64(options.author.login);
}
this.mailer.sendMails(await Promise.all(toUsers.map(async (m)=>{
const [title, html] = this.genMail(m.language, content, options);
return {
to: m.email,
subject: title,
html: html
};
}))).catch((e)=>this.logger.error(`${this.sendEmailNotification.name} - ${e}`));
}
genMail(language, content, options) {
switch(content.app){
case _notifications.NOTIFICATION_APP.COMMENTS:
return (0, _models.commentMail)(language, content, {
content: options.content,
currentUrl: options.currentUrl,
author: options.author
});
case _notifications.NOTIFICATION_APP.SPACES:
return (0, _models.spaceMail)(language, content, {
currentUrl: options.currentUrl,
action: options.action
});
case _notifications.NOTIFICATION_APP.SPACE_ROOTS:
return (0, _models.spaceRootMail)(language, content, {
currentUrl: options.currentUrl,
author: options.author,
action: options.action
});
case _notifications.NOTIFICATION_APP.SHARES:
return (0, _models.shareMail)(language, content, {
currentUrl: options.currentUrl,
author: options.author,
action: options.action
});
case _notifications.NOTIFICATION_APP.LINKS:
return (0, _models.linkMail)(language, content, {
currentUrl: options.currentUrl,
author: options.author,
linkUUID: options.linkUUID,
action: options.action
});
case _notifications.NOTIFICATION_APP.SYNC:
return (0, _models.syncMail)(language, content, {
currentUrl: options.currentUrl,
action: options.action
});
default:
this.logger.error(`${this.genMail.name} - case not handled : ${content.app}`);
}
}
constructor(usersManager, mailer, notificationsQueries, webSocketNotifications){
this.usersManager = usersManager;
this.mailer = mailer;
this.notificationsQueries = notificationsQueries;
this.webSocketNotifications = webSocketNotifications;
this.logger = new _common.Logger(NotificationsManager.name);
}
};
NotificationsManager = _ts_decorate([
(0, _common.Injectable)(),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof _usersmanagerservice.UsersManager === "undefined" ? Object : _usersmanagerservice.UsersManager,
typeof _mailerservice.Mailer === "undefined" ? Object : _mailerservice.Mailer,
typeof _notificationsqueriesservice.NotificationsQueries === "undefined" ? Object : _notificationsqueriesservice.NotificationsQueries,
typeof _notificationsgateway.WebSocketNotifications === "undefined" ? Object : _notificationsgateway.WebSocketNotifications
])
], NotificationsManager);
//# sourceMappingURL=notifications-manager.service.js.map