|
@@ -0,0 +1,47 @@
|
|
|
|
+'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.Chat;
|
|
|
|
+ }
|
|
|
|
+ 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: sender_id,
|
|
|
|
+ receiver_id: receiver_id,
|
|
|
|
+ },
|
|
|
|
+ };
|
|
|
|
+ await mq.topic(
|
|
|
|
+ exchange,
|
|
|
|
+ personroom_id + "_" + receiver_id,
|
|
|
|
+ JSON.stringify(res),
|
|
|
|
+ parm
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = PersonchatService;
|