123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const _ = require('lodash');
- const assert = require('assert');
- // 个人聊天表
- class Person_chatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'person_chat');
- this.model = this.ctx.model.PersonChat;
- }
- async create(data) {
- const res = await this.model.create(data);
- if (res) {
- const exchange = 'personChat';
- const routeKey = `${res.room_id}.${res.receiver_id}`;
- const content = JSON.stringify(_.pick(res, [ 'sender_id', 'sender_name', 'receiver_id', 'receiver_name', 'room_id', 'content', 'is_read', 'send_time' ]));
- const param = { durable: true };
- const { mq } = this.ctx;
- if (mq) {
- try {
- await mq.topic(exchange, routeKey, content, param);
- } catch (error) {
- this.ctx.logger.error(error);
- }
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- return res;
- }
- async allRead({ user_id, room_id }) {
- assert(user_id && room_id, '缺少信息');
- const res = await this.model.updateMany({ receiver_id: user_id, room_id }, { is_read: true });
- return res;
- }
- }
- module.exports = Person_chatService;
|