-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatter.js
211 lines (173 loc) · 5.2 KB
/
chatter.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
Chatter = {
options: {
messageLimit: 100,
nickProperty: "username",
initialRoomLoad: 5
}
};
Chatter.configure = function (opts) {
_.extend(this.options, opts);
};
/**
* @summary Retrieves nickname of user.
* @locus Server
* @param {object} meteor user.
* @returns {string} the nickname of the user.
*/
function getNickname(user) {
const nickPath = Chatter.options.nickProperty;
const nick = nickPath.split('.').reduce((prevVal, el) => prevVal[el] , user);
return nick;
}
/**
* @summary Adds user to Chatter and defines its privileges
* @locus Server
* @param params Information about the new user.
* @param {string} params.userId Meteor user id.
* @param {string} params.userType Defines the restricitons on the new user. Can either be set to "admin" or "standard".
* @returns {string} chatterId
*/
Chatter.addUser = function(params) {
check(params, {
userId: String,
admin: Match.Optional(Match.OneOf(Boolean, String, undefined))
});
const {userId, userType} = params;
const user = Meteor.users.findOne(userId);
if (!user) {
throw new Meteor.Error("user-does-not-exists", "user id provided is not correct");
}
if (user.profile.isChatterUser) {
throw new Meteor.Error("user-already-exists", "user has already been added to chatter");
}
const isAdmin = isChatterAdmin ? true : false;
const avatarURL = `http://api.adorable.io/avatars/${user.username}`;
Meteor.users.update(
{_id: userId},
{ $set: {
"profile.isChatterUser": true,
"profile.isChatterAdmin": isAdmin,
"profile.chatterNickname": user.username,
"profile.chatterAvatar": avatarURL,
}
});
return userId;
};
/**
* @summary Removes user from Chatter
* @locus Server
* @param params Information about the new user.
* @param {string} params.userId Meteor user id.
* @param {string} params.userType Defines the restricitons on the new user. Can either be set to "admin" or "standard".
* @returns {string} chatterId
*/
Chatter.removeUser = function(params) {
check(params, {
userId: String
});
const {userId} = params;
const user = Meteor.users.findOne(userId);
if (!user) {
throw new Meteor.Error("user-does-not-exists", "user id provided is not correct");
}
if (!user.profile.isChatterUser) {
throw new Meteor.Error("user-is-not-chatter-user", "user is not a chatter user");
}
Meteor.users.update({_id: userId}, { $set: {"profile.isChatterUser": false} });
return userId;
};
/**
* @summary Adds empty room to Chatter.
* @locus Server
* @param {string} params Information about the new room.
* @param {string} params.name The name of the room.
* @param {string} params.description The description of the room.
* @returns {string} roomId
*/
Chatter.addRoom = function(params) {
check(params, {
name: String,
description: String
});
const {name, description} = params;
const room = new Chatter.Room({
name,
description
})
if (room.validate()) {
return room.save();
}
room.throwValidationException();
};
/**
* @summary Removes room from Chatter.
* @locus Server
* @param {string} roomId.
*/
Chatter.removeRoom = function(params) {
check(params, {
roomId: String
});
const room = Chatter.Room.findOne(params.roomId);
if (!room) {
throw new Meteor.Error("room-does-not-exist", "the value provided for the roomId is incorrect");
}
Chatter.UserRoom.remove({'roomId':{'$in':[room._id]}})
return room.remove();
};
/**
* @summary Adds user to room.
* @locus Server
* @param params Information about the new room.
* @param {string} params.userId Unique string identifying user.
* @param {string} params.roomId Unique string identifying the room.
* @returns {string} the string of the userRoom
*/
Chatter.addUserToRoom = function(params) {
check(params, {
userId: String,
roomId: String
});
const {userId, roomId} = params;
const room = Chatter.Room.findOne(roomId);
const user = Meteor.users.findOne(userId);
if (!room) {
throw new Meteor.Error("room-does-not-exist", "the value provided for the roomId is incorrect");
}
if (!user) {
throw new Meteor.Error("user-does-not-exists", "user id provided is not correct");
}
const userRoom = new Chatter.UserRoom({
userId,
roomId
})
if (userRoom.validate()) {
return userRoom.save();
}
userRoom.throwValidationException();
};
/**
* @summary Removes user from room.
* @locus Server
* @param {string} params.userId Unique string identifying user.
* @param {string} params.roomId Unique string identifying the room.
*/
Chatter.removeUserFromRoom = function(params) {
check(params, {
userId: String,
roomId: String
});
const {userId, roomId} = params;
const user = Meteor.users.findOne(userId);
const room = Chatter.Room.findOne(roomId);
if (!room) {
throw new Meteor.Error("room-does-not-exist", "the value provided for the roomId is incorrect");
} else if (!user) {
throw new Meteor.Error("user-does-not-exists", "user id provided is not correct");
}
const userRoom = Chatter.UserRoom.findOne({roomId, userId: user._id});
if (!userRoom) {
throw new Meteor.Error("user-has-not-been-added-to-room", "the user had not been added to room ");
}
return userRoom.remove();
};