1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- '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, receiver_id, content, personroom_id }) {
- assert(sender_id, '缺少发送人信息');
- assert(content, '缺少发言内容');
- assert(receiver_id, '缺少接收人信息');
- assert(personroom_id, '缺少聊天房间信息');
- const res = await this.model.create({
- sender_id,
- receiver_id,
- content,
- personroom_id,
- });
- // 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
- );
- }
- return res;
- }
- }
- module.exports = PersonchatService;
|