@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
137 lines (136 loc) • 7.31 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, "CommentsManager", {
enumerable: true,
get: function() {
return CommentsManager;
}
});
const _common = require("@nestjs/common");
const _contextmanagerservice = require("../../../infrastructure/context/services/context-manager.service");
const _filesqueriesservice = require("../../files/services/files-queries.service");
const _files = require("../../files/utils/files");
const _notifications = require("../../notifications/constants/notifications");
const _notificationsmanagerservice = require("../../notifications/services/notifications-manager.service");
const _commentsqueriesservice = require("./comments-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 CommentsManager = class CommentsManager {
async getComments(user, space) {
const fileId = await this.getFileId(space);
if (!fileId) {
return [];
}
return this.commentQueries.getComments(user.id, space.dbFile?.ownerId === user.id, fileId);
}
async createComment(user, space, createCommentDto) {
if ((space.dbFile.spaceExternalRootId || space.dbFile.shareExternalId) && space.dbFile.path === '.') {
// If path is empty a file with path = '.' and name = '.' will be created
// The space browser does not support this kind of file and will remove it
// Maybe to implement later
throw new _common.HttpException(`Not supported on this kind of ${space.dbFile.spaceExternalRootId ? 'space root' : 'share'}`, _common.HttpStatus.BAD_REQUEST);
}
let fileId;
if (createCommentDto.fileId > 0) {
fileId = await this.getFileId(space);
if (createCommentDto.fileId !== fileId) {
throw new _common.HttpException('File id mismatch', _common.HttpStatus.BAD_REQUEST);
}
} else {
fileId = await this.getFileId(space, createCommentDto.fileId);
}
const commentId = await this.commentQueries.createComment(user.id, fileId, createCommentDto.content);
this.notify(user, fileId, space, createCommentDto.content).catch((e)=>this.logger.error(`${this.createComment.name} - ${e}`));
return (await this.commentQueries.getComments(user.id, space.dbFile?.ownerId === user.id, null, commentId))[0];
}
async updateComment(user, space, updateCommentDto) {
const [comment] = await this.commentQueries.getComments(user.id, space.dbFile?.ownerId === user.id, null, updateCommentDto.commentId);
if (!comment) {
throw new _common.HttpException('Location not found', _common.HttpStatus.NOT_FOUND);
}
const fileId = await this.getFileId(space);
if (updateCommentDto.fileId !== fileId || comment.fileId !== fileId) {
throw new _common.HttpException('File id mismatch', _common.HttpStatus.BAD_REQUEST);
}
if (!await this.commentQueries.updateComment(user.id, updateCommentDto.commentId, updateCommentDto.fileId, updateCommentDto.content)) {
throw new _common.HttpException('Unable to update', _common.HttpStatus.INTERNAL_SERVER_ERROR);
}
return (await this.commentQueries.getComments(user.id, space.dbFile?.ownerId === user.id, null, updateCommentDto.commentId))[0];
}
async deleteComment(user, space, deleteCommentDto) {
const fileId = await this.getFileId(space);
if (deleteCommentDto.fileId !== fileId) {
throw new _common.HttpException('File id mismatch', _common.HttpStatus.BAD_REQUEST);
}
if (!await this.commentQueries.deleteComment(user.id, deleteCommentDto.commentId, fileId, space.dbFile?.ownerId === user.id)) {
throw new _common.HttpException('You are not allowed to do this action', _common.HttpStatus.FORBIDDEN);
}
}
async getFileId(space, fileId) {
if (!await (0, _files.isPathExists)(space.realPath)) {
throw new _common.HttpException('Location not found', _common.HttpStatus.NOT_FOUND);
}
const fileProps = {
...await (0, _files.getProps)(space.realPath, space.dbFile.path),
id: undefined
};
if (fileId) {
// get or create
return this.filesQueries.getOrCreateSpaceFile(fileId, fileProps, space.dbFile);
} else {
// get only
return this.filesQueries.getSpaceFileId(fileProps, space.dbFile);
}
}
async getRecents(user, limit) {
return this.commentQueries.getRecentsFromUser(user, limit);
}
async notify(fromUser, fileId, space, comment) {
const members = await this.commentQueries.membersToNotify(fromUser.id, fileId);
if (!members.length) {
return;
}
const notification = {
app: _notifications.NOTIFICATION_APP.COMMENTS,
event: _notifications.NOTIFICATION_APP_EVENT.COMMENTS,
element: (0, _files.fileName)(space.url),
url: (0, _files.dirName)(space.url)
};
this.notificationsManager.create(members, notification, {
author: fromUser,
currentUrl: this.contextManager.headerOriginUrl(),
content: comment
}).catch((e)=>this.logger.error(`${this.notify.name} - ${e}`));
}
constructor(contextManager, commentQueries, filesQueries, notificationsManager){
this.contextManager = contextManager;
this.commentQueries = commentQueries;
this.filesQueries = filesQueries;
this.notificationsManager = notificationsManager;
this.logger = new _common.Logger(CommentsManager.name);
}
};
CommentsManager = _ts_decorate([
(0, _common.Injectable)(),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof _contextmanagerservice.ContextManager === "undefined" ? Object : _contextmanagerservice.ContextManager,
typeof _commentsqueriesservice.CommentsQueries === "undefined" ? Object : _commentsqueriesservice.CommentsQueries,
typeof _filesqueriesservice.FilesQueries === "undefined" ? Object : _filesqueriesservice.FilesQueries,
typeof _notificationsmanagerservice.NotificationsManager === "undefined" ? Object : _notificationsmanagerservice.NotificationsManager
])
], CommentsManager);
//# sourceMappingURL=comments-manager.service.js.map