You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
// chat.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const mongoose = require('mongoose');
|
|
const ChatMessage = mongoose.model('ChatMessage');
|
|
|
|
const ioEmitter = require('socket.io-emitter');
|
|
const striptags = require('striptags');
|
|
const unzalgo = require('unzalgo');
|
|
|
|
const { SiteService } = require('../../lib/site-lib');
|
|
|
|
class ChatService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
this.populateContentReport = [
|
|
{
|
|
path: 'user',
|
|
select: '_id username username_lc displayName picture',
|
|
},
|
|
{
|
|
path: 'resource',
|
|
populate: [
|
|
{
|
|
path: 'author',
|
|
select: '_id username username_lc displayName picture',
|
|
},
|
|
],
|
|
},
|
|
];
|
|
}
|
|
|
|
async start ( ) {
|
|
this.emitter = ioEmitter(this.dtp.redis);
|
|
}
|
|
|
|
async removeMessage (message) {
|
|
await ChatMessage.deleteOne({ _id: message._id });
|
|
this.emitter(`site:${this.dtp.config.site.domainKey}:chat`, {
|
|
command: 'removeMessage',
|
|
params: { messageId: message._id },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Filters an input string to remove "zalgo" text and to strip all HTML tags.
|
|
* This prevents cross-site scripting and the malicious destruction of text
|
|
* layouts.
|
|
* @param {String} content The text content to be filtered.
|
|
* @returns the filtered text
|
|
*/
|
|
filterText (content) {
|
|
return striptags(unzalgo.clean(content.trim()));
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'chat',
|
|
name: 'chat',
|
|
create: (dtp) => { return new ChatService(dtp); },
|
|
};
|