forked from ether/ep_email_notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleMessage.js
215 lines (189 loc) · 8.76 KB
/
handleMessage.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
212
213
214
215
var db = require('../../src/node/db/DB').db,
API = require('../../src/node/db/API.js'),
async = require('../../src/node_modules/async'),
check = require('validator').check,
settings = require('../../src/node/utils/Settings');
var pluginSettings = settings.ep_email_notifications;
// When a new message comes in from the client - FML this is ugly
exports.handleMessage = function(hook_name, context, callback){
if (context.message && context.message.data){
if (context.message.data.type == 'USERINFO_UPDATE' ) { // if it's a request to update an authors email
if (context.message.data.userInfo){
if(context.message.data.userInfo.email){ // it contains email
console.debug(context.message);
// does email Subscription already exist for this email address?
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){
var alreadyExists = false;
if(userIds){
async.forEach(Object.keys(userIds), function(user, cb){
console.debug("UserIds subscribed by email to this pad:", userIds);
if(user == context.message.data.userInfo.email){ // If we already have this email registered for this pad
// This user ID is already assigned to this padId so don't do anything except tell the user they are already subscribed somehow..
alreadyExists = true;
}
cb();
},
function(err){
// There should be something in here!
}); // end async for each
}
if(context.message.data.userInfo.email_option == 'subscribe' && alreadyExists == true){
// SUbscription
console.debug("email ", context.message.data.userInfo.email, "already subscribed to ", context.message.data.padId, " so sending message to client");
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailSubscriptionSuccess",
payload: false
}
});
} else if(context.message.data.userInfo.email_option == 'subscribe' && alreadyExists == false){
// SUbscription
var validatesAsEmail = check(context.message.data.userInfo.email).isEmail();
if(!validatesAsEmail){ // send validation failed if it's malformed.. y'know in general fuck em!
console.warn("Dropped email subscription due to malformed email address");
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailSubscriptionSuccess",
payload: false
}
});
} else {
console.debug ("Subscription: Wrote to the database and sent client a positive response ",context.message.data.userInfo.email);
exports.setAuthorEmail(
context.message.data.userInfo.userId,
context.message.data.userInfo,
callback
);
exports.setAuthorEmailRegistered(
context.message.data.userInfo,
context.message.data.userInfo.userId,
context.message.data.padId
);
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailSubscriptionSuccess",
payload: true
}
});
}
} else if(context.message.data.userInfo.email_option == 'unsubscribe' && alreadyExists == true) {
// Unsubscription
console.debug ("Unsubscription: Remove from the database and sent client a positive response ",context.message.data.userInfo.email);
exports.unsetAuthorEmail(
context.message.data.userInfo.userId,
context.message.data.userInfo,
callback
);
exports.unsetAuthorEmailRegistered(
context.message.data.userInfo,
context.message.data.userInfo.userId,
context.message.data.padId
);
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailUnsubscriptionSuccess",
payload: true
}
});
} else if(context.message.data.userInfo.email_option == 'unsubscribe' && alreadyExists == false) {
// Unsubscription
console.debug ("Unsubscription: Send client a negative response ",context.message.data.userInfo.email);
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailUnsubscriptionSuccess",
payload: false
}
});
}
}); // close db get
callback([null]); // don't run onto passing colorId or anything else to the message handler
}
}
} else if (context.message.data.type == 'USERINFO_GET' ) { // A request to find datas for a username
if (context.message.data.userInfo){
if(context.message.data.userInfo.userId){ // it contains the userId
console.debug(context.message);
var userIdFound = false;
// does email Subscription already exist for this name and padID?
db.get("emailSubscription:"+context.message.data.padId, function(err, userIds){
if(userIds){
async.forEach(Object.keys(userIds), function(user, cb){
if(userIds[user].authorId == context.message.data.userInfo.userId){ // if we find the same Id in the Db as the one used by the user
console.debug("Options for this pad ", userIds[user].authorId, " found in the Db");
userIdFound = true;
// We send back the options set for this user
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailNotificationGetUserInfo",
payload: {
email: user,
onStart: userIds[user].onStart && typeof userIds[user].onStart === 'boolean'?userIds[user].onStart:true,
onEnd: userIds[user].onEnd && typeof userIds[user].onEnd === 'boolean'?userIds[user].onEnd:false,
success:true
}
}
});
}
cb();
},
function(err){
// There should be something in here!
}); // end async for each
}
});
if (!userIdFound) {
// We send back the options set for this user
context.client.json.send({ type: "COLLABROOM",
data:{
type: "emailNotificationGetUserInfo",
payload: {
success:false
}
}
});
}
}
}
}
}
callback();
}
// Updates the database with the email record
exports.setAuthorEmail = function (author, datas, callback){
db.setSub("globalAuthor:" + author, ["email"], datas.email, callback);
}
// Write email and padId to the database
exports.setAuthorEmailRegistered = function(datas, authorId, padId){
var timestamp = new Date().getTime();
var registered = {
authorId: authorId,
onStart: datas.email_onStart,
onEnd: datas.email_onEnd,
timestamp: timestamp
};
console.debug("registered", registered, " to ", padId);
// Here we have to basically hack a new value into the database, this isn't clean or polite.
db.get("emailSubscription:" + padId, function(err, value){ // get the current value
if(!value){value = {};} // if an emailSubscription doesnt exist yet for this padId don't panic
value[datas.email] = registered; // add the registered values to the object
console.warn("written to database");
db.set("emailSubscription:" + padId, value); // stick it in the database
});
}
// Updates the database by removing the email record for that AuthorId
exports.unsetAuthorEmail = function (author, datas, callback){
db.get("globalAuthor:" + author, function(err, value){ // get the current value
delete value['email'];
db.set("globalAuthor:" + author, value);
});
}
// Remove email and padId from the database
exports.unsetAuthorEmailRegistered = function(datas, authorId, padId){
console.debug("unregistered", datas.email, " to ", padId);
// Here we have to basically hack a new value into the database, this isn't clean or polite.
db.get("emailSubscription:" + padId, function(err, value){ // get the current value
delete value[datas.email]; // remove the registered values to the object
console.warn("written to database");
db.set("emailSubscription:" + padId, value); // stick it in the database
});
}