forked from d3k4y/simple-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods_server.js
96 lines (87 loc) · 3.28 KB
/
methods_server.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
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
/**
* Created by cesar on 23/2/16.
*/
import {Meteor} from 'meteor/meteor'
import {check} from 'meteor/check'
import {Chats} from './collections'
import {Rooms} from './collections'
import {SimpleChat} from './config'
//todo improve security, for now any body con access to this methods and potentially change the data (off messages recieved or room joins no a big deal but have to be fixed)
Meteor.methods({
"SimpleChat.messageReceived": function (id, username) {
check(id, String)
check(username, String)
check(username, username)
this.unblock()
if (!SimpleChat.options.showReceived) return false
Meteor._sleepForMs(800 * Meteor.isDevelopment)
const message = Chats.findOne(id, {fields: {roomId: 1, receivedBy: 1}})
if (!message)
throw Meteor.Error(403, "Message does not exist")
const room = Rooms.findOne(message.roomId)
if (!_.contains(message.receivedBy, username)) {
return Chats.update(id, {
$addToSet: {receivedBy: username},
$set: {receivedAll: room.usernames.length - 2 <= message.receivedBy.length}
})
}
SimpleChat.options.onReceiveMessage(id, message, room)
return false
},
"SimpleChat.join": function (roomId, username, avatar, name) {
check(roomId, String);
check(username, String);
check(avatar, Match.Maybe(String));
check(name, Match.Maybe(String));
this.unblock()
if (!SimpleChat.options.showViewed) return false
//todo remove
Meteor._sleepForMs(800 * Meteor.isDevelopment)
const date = new Date()
if (SimpleChat.options.showJoined) {
Chats.insert({
roomId,
username,
name,
avatar,
date,
join: true
})
}
Rooms.upsert(roomId, {$addToSet: {usernames: username}})
this.connection.onClose(function () {
if(SimpleChat && SimpleChat.Chats && SimpleChat.Chats.insert) {
SimpleChat.Chats.insert({
roomId,
username,
name,
avatar,
date: new Date(),
join: false
})
Rooms.update(roomId, {$pull: {usernames: username}})
SimpleChat.options.onLeft(roomId, username, name, date)
}
});
SimpleChat.options.onJoin(roomId, username, name, date)
},
"SimpleChat.messageViewed": function (id, username) {
check(id, String);
check(username, String);
this.unblock()
if (!SimpleChat.options.showViewed) return false
//todo remove
Meteor._sleepForMs(800 * Meteor.isDevelopment)
const message = Chats.findOne(id, {fields: {roomId: 1, viewedBy: 1}})
if (!message)
throw Meteor.Error(403, "Message does not exist")
const room = Rooms.findOne(message.roomId)
if (!_.contains(message.viewedBy, username)) {
return Chats.update(id, {
$addToSet: {viewedBy: username},
$set: {viewedAll: room.usernames.length - 2 <= message.viewedBy.length}
})
}
return false
},
})