person_chat.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. // 个人聊天表
  8. class Person_chatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'person_chat');
  11. this.model = this.ctx.model.PersonChat;
  12. }
  13. async create(data) {
  14. const res = await this.model.create(data);
  15. if (res) {
  16. const exchange = 'personChat';
  17. const routeKey = `${res.room_id}.${res.receiver_id}`;
  18. const content = JSON.stringify(_.pick(res, [ 'sender_id', 'sender_name', 'receiver_id', 'receiver_name', 'room_id', 'content', 'is_read', 'send_time' ]));
  19. const param = { durable: true };
  20. const { mq } = this.ctx;
  21. if (mq) {
  22. try {
  23. await mq.topic(exchange, routeKey, content, param);
  24. } catch (error) {
  25. this.ctx.logger.error(error);
  26. }
  27. } else {
  28. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  29. }
  30. }
  31. return res;
  32. }
  33. async allRead({ user_id, room_id }) {
  34. assert(user_id && room_id, '缺少信息');
  35. const res = await this.model.updateMany({ receiver_id: user_id, room_id }, { is_read: true });
  36. return res;
  37. }
  38. }
  39. module.exports = Person_chatService;