1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
'use strict';
var _ = require('lodash'),
mongoose = require('mongoose'),
helpers = require('./helpers');
function MessageManager(options) {
this.core = options.core;
}
MessageManager.prototype.create = function(options, cb) {
var Message = mongoose.model('Message'),
Room = mongoose.model('Room'),
User = mongoose.model('User');
if (typeof cb !== 'function') {
cb = function() {};
}
Room.findById(options.room, function(err, room) {
if (err) {
console.error(err);
return cb(err);
}
if (!room) {
return cb('Room does not exist.');
}
if (room.archived) {
return cb('Room is archived.');
}
if (!room.isAuthorized(options.owner)) {
return cb('Not authorized.');
}
Message.create(options, function(err, message) {
if (err) {
console.error(err);
return cb(err);
}
// Touch Room's lastActive
room.lastActive = message.posted;
room.save();
// Temporary workaround for _id until populate can do aliasing
User.findOne(message.owner, function(err, user) {
if (err) {
console.error(err);
return cb(err);
}
cb(null, message, room, user);
this.core.emit('messages:new', message, room, user, options.data);
}.bind(this));
}.bind(this));
}.bind(this));
};
MessageManager.prototype.list = function(options, cb) {
var Room = mongoose.model('Room');
options = options || {};
if (!options.room) {
return cb(null, []);
}
options = helpers.sanitizeQuery(options, {
defaults: {
reverse: true,
take: 500
},
maxTake: 5000
});
var Message = mongoose.model('Message');
var find = Message.find({
room: options.room
});
if (options.since_id) {
find.where('_id').gt(options.since_id);
}
if (options.from) {
find.where('posted').gt(options.from);
}
if (options.to) {
find.where('posted').lte(options.to);
}
if (options.query) {
find = find.find({$text: {$search: options.query}});
}
if (options.expand) {
var includes = options.expand.replace(/\s/, '').split(',');
if (_.includes(includes, 'owner')) {
find.populate('owner', 'id username displayName email avatar');
}
if (_.includes(includes, 'room')) {
find.populate('room', 'id name');
}
}
if (options.skip) {
find.skip(options.skip);
}
if (options.reverse) {
find.sort({ 'posted': -1 });
} else {
find.sort({ 'posted': 1 });
}
Room.findById(options.room, function(err, room) {
if (err) {
console.error(err);
return cb(err);
}
var opts = {
userId: options.userId,
password: options.password
};
room.canJoin(opts, function(err, canJoin) {
if (err) {
console.error(err);
return cb(err);
}
if (!canJoin) {
return cb(null, []);
}
find.limit(options.take)
.exec(function(err, messages) {
if (err) {
console.error(err);
return cb(err);
}
cb(null, messages);
});
});
});
};
module.exports = MessageManager;
|