1212 * Copyright (c) 2014-2019. All rights reserved.
1313 */
1414
15- var mongoose = require ( 'mongoose' )
16- var _ = require ( 'lodash' )
15+ const mongoose = require ( 'mongoose' )
16+ const _ = require ( 'lodash' )
17+ const utils = require ( '../../helpers/utils' )
1718
18- var COLLECTION = 'messages'
19+ const COLLECTION = 'messages'
1920
20- var messageSchema = mongoose . Schema (
21+ const messageSchema = mongoose . Schema (
2122 {
2223 conversation : {
2324 type : mongoose . Schema . Types . ObjectId ,
@@ -35,6 +36,12 @@ var messageSchema = mongoose.Schema(
3536 { timestamps : true }
3637)
3738
39+ messageSchema . pre ( 'save' , function ( next ) {
40+ this . body = utils . sanitizeFieldPlainText ( utils . applyExtremeTextLength ( this . body ) )
41+
42+ next ( )
43+ } )
44+
3845messageSchema . statics . getFullConversation = function ( convoId , callback ) {
3946 return this . model ( COLLECTION )
4047 . find ( { conversation : convoId } )
@@ -65,22 +72,22 @@ messageSchema.statics.getConversationWithObject = function (object, callback) {
6572 return callback ( 'Invalid Object (Must by of type Object) - MessageSchema.GetUserWithObject()' , null )
6673 }
6774
68- var self = this
69- var deletedAt = null
75+ const self = this
76+ let deletedAt = null
7077
71- var limit = object . limit === null ? 25 : object . limit
72- var page = object . page === null ? 0 : object . page
78+ const limit = object . limit === null ? 25 : object . limit
79+ const page = object . page === null ? 0 : object . page
7380
7481 if ( object . requestingUser ) {
75- var userMetaIdx = _ . findIndex ( object . userMeta , function ( item ) {
82+ const userMetaIdx = _ . findIndex ( object . userMeta , function ( item ) {
7683 return item . userId . toString ( ) === object . requestingUser . _id . toString ( )
7784 } )
7885 if ( userMetaIdx !== - 1 && object . userMeta [ userMetaIdx ] . deletedAt ) {
7986 deletedAt = new Date ( object . userMeta [ userMetaIdx ] . deletedAt )
8087 }
8188 }
8289
83- var q = self
90+ const q = self
8491 . model ( COLLECTION )
8592 . find ( { } )
8693 . sort ( '-createdAt' )
0 commit comments