Configure remote database connection and update documentation
This commit is contained in:
98
backend/dist/modules/messages/messages.service.js
vendored
Normal file
98
backend/dist/modules/messages/messages.service.js
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (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;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MessagesService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const typeorm_2 = require("typeorm");
|
||||
const message_entity_1 = require("./entities/message.entity");
|
||||
const conversations_service_1 = require("../conversations/conversations.service");
|
||||
let MessagesService = class MessagesService {
|
||||
messagesRepository;
|
||||
conversationsService;
|
||||
constructor(messagesRepository, conversationsService) {
|
||||
this.messagesRepository = messagesRepository;
|
||||
this.conversationsService = conversationsService;
|
||||
}
|
||||
async create(conversationId, senderId, content) {
|
||||
const conversation = await this.conversationsService.findOne(conversationId, senderId);
|
||||
const message = this.messagesRepository.create({
|
||||
content,
|
||||
sender: { id: senderId },
|
||||
conversation: { id: conversationId },
|
||||
});
|
||||
return this.messagesRepository.save(message);
|
||||
}
|
||||
async findAllInConversation(conversationId, userId) {
|
||||
await this.conversationsService.findOne(conversationId, userId);
|
||||
return this.messagesRepository.find({
|
||||
where: { conversation: { id: conversationId } },
|
||||
relations: ['sender'],
|
||||
order: { createdAt: 'ASC' },
|
||||
});
|
||||
}
|
||||
async update(messageId, userId, content) {
|
||||
const message = await this.messagesRepository.findOne({
|
||||
where: { id: messageId },
|
||||
relations: ['sender'],
|
||||
});
|
||||
if (!message) {
|
||||
throw new common_1.NotFoundException('Сообщение не найдено');
|
||||
}
|
||||
if (message.sender.id !== userId) {
|
||||
throw new common_1.ForbiddenException('Вы можете редактировать только свои сообщения');
|
||||
}
|
||||
message.content = content;
|
||||
message.isEdited = true;
|
||||
message.editedAt = new Date();
|
||||
return this.messagesRepository.save(message);
|
||||
}
|
||||
async markAsRead(messageId, userId) {
|
||||
const message = await this.messagesRepository.findOne({
|
||||
where: { id: messageId },
|
||||
relations: ['conversation', 'conversation.participants'],
|
||||
});
|
||||
if (!message) {
|
||||
throw new common_1.NotFoundException('Сообщение не найдено');
|
||||
}
|
||||
const isParticipant = message.conversation.participants.some(p => p.id === userId);
|
||||
if (!isParticipant) {
|
||||
throw new common_1.ForbiddenException('Вы не являетесь участником этой беседы');
|
||||
}
|
||||
message.isRead = true;
|
||||
return this.messagesRepository.save(message);
|
||||
}
|
||||
async delete(messageId, userId) {
|
||||
const message = await this.messagesRepository.findOne({
|
||||
where: { id: messageId },
|
||||
relations: ['sender'],
|
||||
});
|
||||
if (!message) {
|
||||
throw new common_1.NotFoundException('Сообщение не найдено');
|
||||
}
|
||||
if (message.sender.id !== userId) {
|
||||
throw new common_1.ForbiddenException('Вы можете удалять только свои сообщения');
|
||||
}
|
||||
await this.messagesRepository.remove(message);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
exports.MessagesService = MessagesService;
|
||||
exports.MessagesService = MessagesService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__param(0, (0, typeorm_1.InjectRepository)(message_entity_1.Message)),
|
||||
__metadata("design:paramtypes", [typeorm_2.Repository,
|
||||
conversations_service_1.ConversationsService])
|
||||
], MessagesService);
|
||||
//# sourceMappingURL=messages.service.js.map
|
Reference in New Issue
Block a user