123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 'use strict';
- const assert = require('assert');
- // const _ = require('lodash');
- // const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- // const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- class PersonchatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'chat');
- this.model = this.ctx.model.Personchat;
- }
- async create(query, { sender_id, sender_name, receiver_id, receiver_name, content, personroom_id }) {
- assert(sender_id, '缺少发送人信息');
- assert(content, '缺少发言内容');
- assert(receiver_id, '缺少接收人信息');
- assert(personroom_id, '缺少聊天房间信息');
- const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
- const res = await this.model.create({
- sender_id,
- sender_name,
- receiver_id,
- receiver_name,
- content,
- personroom_id,
- send_time,
- });
- // TODO MQ
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'person_chat';
- const parm = {
- durable: true,
- headers: {
- sender_id,
- receiver_id,
- },
- };
- await mq.topic(
- exchange,
- personroom_id + '_' + receiver_id,
- JSON.stringify(res),
- parm
- );
- await mq.topic(
- 'chat_message',
- receiver_id,
- JSON.stringify(res),
- parm
- );
- }
- return res;
- }
- async received(data) {
- const { ids, sender_id, receiver_id, personroom_id } = data;
- for (const id of ids) {
- const chat = await this.model.findById(id);
- chat.status = '1';
- await chat.save();
- }
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'person_chat';
- const parm = {
- durable: true,
- headers: {
- sender_id,
- receiver_id,
- },
- };
- await mq.topic(
- exchange,
- personroom_id + '_' + receiver_id,
- '消息已查收',
- parm
- );
- }
- }
- }
- module.exports = PersonchatService;
|