personchat.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const assert = require('assert');
  3. // const _ = require('lodash');
  4. // const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. // const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. // const moment = require('moment');
  8. class PersonchatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'chat');
  11. this.model = this.ctx.model.Personchat;
  12. }
  13. async create(query, { sender_id, receiver_id, content, personroom_id }) {
  14. assert(sender_id, '缺少发送人信息');
  15. assert(content, '缺少发言内容');
  16. assert(receiver_id, '缺少接收人信息');
  17. assert(personroom_id, '缺少聊天房间信息');
  18. const res = await this.model.create({
  19. sender_id,
  20. receiver_id,
  21. content,
  22. personroom_id,
  23. });
  24. // TODO MQ
  25. const { mq } = this.ctx;
  26. if (mq) {
  27. const exchange = 'person_chat';
  28. const parm = {
  29. durable: true,
  30. headers: {
  31. sender_id,
  32. receiver_id,
  33. },
  34. };
  35. await mq.topic(
  36. exchange,
  37. personroom_id + '_' + receiver_id,
  38. JSON.stringify(res),
  39. parm
  40. );
  41. }
  42. return res;
  43. }
  44. }
  45. module.exports = PersonchatService;