-
Notifications
You must be signed in to change notification settings - Fork 1
/
room.js
64 lines (52 loc) · 1.48 KB
/
room.js
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
var lodash = require("lodash");
var Emoji = require("emojione");
Emoji.ascii = true;
Emoji.imageType = "svg";
Emoji.imagePathSVGSprites = "./emojis.svg";
Emoji.sprites = true;
Emoji.spriteSize = 64;
module.exports = function (Room) {
var methods = {}
function defaults(room) {
return lodash.defaults(room, {
creator: 'system',
created: Date.now(),
messages: []
})
}
methods.get = function (fields) {
return Room.get(fields)
}
methods.disconnect = function () {
if (Room) Room.disconnect()
}
methods.create = function (roomid, userid) {
return defaults({
id: roomid,
creator: userid
})
}
methods.addMessage = function (message) {
message.message = Emoji.shortnameToImage(message.message);
Room.unshift('messages', message)
while (Room('messages').length > 25) {
Room.pop('messages')
}
return Room
}
function filterMessages(prop, comparison) {
var messages = Room('messages')
messages = lodash.filter(messages, message => {
return message[prop] != comparison
})
Room.set('messages', messages)
return Room
}
methods.removeMessage = function (messageid) {
return filterMessages('id', messageid)
}
methods.removeUserMessages = function (userid) {
return filterMessages('userid', userid)
}
return methods
}