Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit 710d6e7

Browse files
author
BatAmar Battulga
committed
fix(conversation): cleaning content
close #641
1 parent 9313ddd commit 710d6e7

File tree

6 files changed

+54
-44
lines changed

6 files changed

+54
-44
lines changed

src/data/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fs from 'fs';
66
import * as Handlebars from 'handlebars';
77
import * as nodemailer from 'nodemailer';
88
import * as requestify from 'requestify';
9+
import * as strip from 'strip';
910
import * as xlsxPopulate from 'xlsx-populate';
1011
import { Customers, Notifications, Users } from '../db/models';
1112
import { IUser, IUserDocument } from '../db/models/definitions/users';
@@ -814,6 +815,8 @@ export default {
814815
createTransporter,
815816
};
816817

818+
export const cleanHtml = (content?: string) => strip(content || '').substring(0, 100);
819+
817820
export const validSearchText = (values: string[]) => {
818821
const value = values.join(' ');
819822

src/db/models/ConversationMessages.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,11 @@ export const loadClass = () => {
2727
conversationId: message.conversationId,
2828
}).countDocuments();
2929

30-
await Conversations.updateOne(
31-
{ _id: message.conversationId },
32-
{
33-
$set: {
34-
messageCount,
35-
36-
// updating updatedAt
37-
updatedAt: new Date(),
38-
},
39-
},
40-
);
30+
await Conversations.updateConversation(message.conversationId, {
31+
messageCount,
32+
// updating updatedAt
33+
updatedAt: new Date(),
34+
});
4135

4236
if (message.userId) {
4337
// add created user to participators
@@ -92,7 +86,7 @@ export const loadClass = () => {
9286
modifier.firstRespondedDate = new Date();
9387
}
9488

95-
await Conversations.updateOne({ _id: doc.conversationId }, { $set: modifier });
89+
await Conversations.updateConversation(doc.conversationId, modifier);
9690

9791
return this.createMessage({ ...doc, userId });
9892
}

src/db/models/Conversations.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Model, model } from 'mongoose';
22
import { ConversationMessages, Users } from '.';
3+
import { cleanHtml } from '../../data/utils';
34
import { CONVERSATION_STATUSES } from './definitions/constants';
45
import { IMessageDocument } from './definitions/conversationMessages';
56
import { conversationSchema, IConversation, IConversationDocument } from './definitions/conversations';
@@ -14,6 +15,7 @@ interface ISTATUSES {
1415
export interface IConversationModel extends Model<IConversationDocument> {
1516
getConversationStatuses(): ISTATUSES;
1617
createConversation(doc: IConversation): Promise<IConversationDocument>;
18+
updateConversation(_id: string, doc): Promise<IConversationDocument>;
1719
checkExistanceConversations(ids: string[]): any;
1820
reopen(_id: string): Promise<IConversationDocument>;
1921

@@ -66,32 +68,39 @@ export const loadClass = () => {
6668
return Conversations.create({
6769
status: this.getConversationStatuses().NEW,
6870
...doc,
71+
content: cleanHtml(doc.content),
6972
createdAt: doc.createdAt || now,
7073
updatedAt: doc.createdAt || now,
7174
number: (await Conversations.find().countDocuments()) + 1,
7275
messageCount: 0,
7376
});
7477
}
7578

79+
/**
80+
* Update a conversation
81+
*/
82+
public static async updateConversation(_id, doc) {
83+
if (doc.content) {
84+
doc.content = cleanHtml(doc.content);
85+
}
86+
87+
return Conversations.updateOne({ _id }, { $set: doc });
88+
}
89+
7690
/*
7791
* Reopens conversation
7892
*/
7993
public static async reopen(_id: string) {
80-
await Conversations.updateOne(
81-
{ _id },
82-
{
83-
$set: {
84-
// reset read state
85-
readUserIds: [],
86-
87-
// if closed, reopen
88-
status: this.getConversationStatuses().OPEN,
89-
90-
closedAt: null,
91-
closedUserId: null,
92-
},
93-
},
94-
);
94+
await Conversations.updateConversation(_id, {
95+
// reset read state
96+
readUserIds: [],
97+
98+
// if closed, reopen
99+
status: this.getConversationStatuses().OPEN,
100+
101+
closedAt: null,
102+
closedUserId: null,
103+
});
95104

96105
return Conversations.findOne({ _id });
97106
}
@@ -185,12 +194,12 @@ export const loadClass = () => {
185194

186195
// if current user is first one
187196
if (!readUserIds || readUserIds.length === 0) {
188-
await Conversations.updateOne({ _id }, { $set: { readUserIds: [userId] } });
197+
await Conversations.updateConversation(_id, { readUserIds: [userId] });
189198
}
190199

191200
// if current user is not in read users list then add it
192201
if (!readUserIds.includes(userId)) {
193-
await Conversations.updateOne({ _id }, { $push: { readUserIds: userId } });
202+
await Conversations.updateConversation(_id, { readUserIds: userId });
194203
}
195204

196205
return Conversations.findOne({ _id });

src/db/models/definitions/conversations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface IConversation {
1212
readUserIds?: string[];
1313

1414
createdAt?: Date;
15+
updatedAt?: Date;
1516
closedAt?: Date;
1617
closedUserId?: string;
1718

src/middlewares/integrationsApiMiddleware.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ import { ActivityLogs, ConversationMessages, Conversations, Customers, Integrati
22
import { CONVERSATION_STATUSES } from '../db/models/definitions/constants';
33
import { graphqlPubsub } from '../pubsub';
44

5-
interface IMessage {
6-
status: string;
7-
attachments: string[];
8-
readUserIds: string[];
9-
content?: string;
10-
updatedAt?: Date;
11-
}
12-
135
/*
146
* Handle requests from integrations api
157
*/
@@ -59,7 +51,7 @@ const integrationsApiMiddleware = async (req, res) => {
5951
if (doc.conversationId) {
6052
const { conversationId, content } = doc;
6153

62-
await Conversations.updateOne({ _id: conversationId }, { $set: { content } });
54+
await Conversations.updateConversation(conversationId, { content });
6355

6456
return res.json({ _id: conversationId });
6557
}
@@ -74,25 +66,23 @@ const integrationsApiMiddleware = async (req, res) => {
7466
if (action === 'create-conversation-message') {
7567
const message = await ConversationMessages.createMessage(doc);
7668

77-
const messageDoc: IMessage = {
69+
const conversationDoc: { status: string; readUserIds: string[]; content?: string; updatedAt?: Date } = {
7870
// Reopen its conversation if it's closed
7971
status: doc.unread || doc.unread === undefined ? CONVERSATION_STATUSES.OPEN : CONVERSATION_STATUSES.CLOSED,
8072

81-
attachments: message.attachments,
82-
8373
// Mark as unread
8474
readUserIds: [],
8575
};
8676

8777
if (message.content && metaInfo === 'replaceContent') {
88-
messageDoc.content = message.content;
78+
conversationDoc.content = message.content;
8979
}
9080

9181
if (doc.createdAt) {
92-
messageDoc.updatedAt = doc.createdAt;
82+
conversationDoc.updatedAt = doc.createdAt;
9383
}
9484

95-
await Conversations.updateOne({ _id: message.conversationId }, { $set: messageDoc });
85+
await Conversations.updateConversation(message.conversationId, conversationDoc);
9686

9787
graphqlPubsub.publish('conversationClientMessageInserted', {
9888
conversationClientMessageInserted: message,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { cleanHtml } from '../data/utils';
2+
import { connect } from '../db/connection';
3+
import { Conversations } from '../db/models';
4+
5+
module.exports.up = async () => {
6+
await connect();
7+
8+
const conversations = await Conversations.find({}, { _id: 1, content: 1 });
9+
10+
for (const conversation of conversations) {
11+
await Conversations.updateOne({ _id: conversation._id }, { $set: { content: cleanHtml(conversation.content) } });
12+
}
13+
};

0 commit comments

Comments
 (0)