-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
290 lines (260 loc) · 8.13 KB
/
plugin.js
File metadata and controls
290 lines (260 loc) · 8.13 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* We.js notification plugin config
*/
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
plugin.setConfigs({
notification: {
/**
* time to wait after an notification is created to send in notification email
* @type {Object}
*/
waitMinutesForSendEmail: 30
},
emailTypes: {
userNotifications: {
label: 'Email com a lista de notificações não lidas do usuário',
templateVariables: {
displayName: {
example: 'Alberto Souza',
description: 'Nome da pessoa convidada'
},
email: {
example: 'contact@linkysysytems.com',
description: 'Email da pessoa convidada'
},
notifications: {
example: 'Lista de notificações',
description: 'Nome deste site'
},
siteName: {
example: 'Site Name',
description: 'Nome deste site'
},
siteUrl: {
example: '/#example',
description: 'Endereço deste site'
}
}
}
}
});
// set plugin routes
plugin.setRoutes({
'get /link-permanent/notification/:id([0-9]+)': {
controller : 'notification',
model : 'notification',
action : 'linkPermanent',
permission : true
},
// get current user notification count
'get /api/v1/current-user/notification-count': {
controller : 'notification',
action : 'getNotificationCount',
permission : true
},
'post /api/v1/notify/mark-all-as-read': {
controller : 'notification',
model : 'notification',
action : 'markAllNotificationAsRead',
permission : true
},
'post /api/v1/notify/:model/:modelId/mark-all-as-read': {
controller : 'notification',
model : 'notification',
action : 'markAllModelNotificationAsRead',
permission : true
},
// get notifications
'get /notification': {
controller : 'notification',
model : 'notification',
action : 'find',
layoutName : 'fullwidth',
permission : true,
search: {
// since search is avaible in findAll by default
since: {
parser: 'since',
target: {
type: 'field',
field: 'createdAt'
}
}
}
},
'post /api/v1/notify/:notificationId([0-9]+)': {
controller : 'notification',
model : 'notification',
action : 'setNotificationRead',
permission : true
},
'get /notification-settings': {
controller : 'notification',
model : 'notificationSettings',
action : 'userNotificationSettings',
layoutName : 'fullwidth',
template : 'notification/settings',
titleHandler : 'i18n',
titleI18n : 'notification.user.settings',
permission : true
},
'post /notification-settings': {
controller : 'notification',
model : 'notificationSettings',
action : 'userNotificationSettings',
layoutName : 'fullwidth',
titleHandler : 'i18n',
titleI18n : 'notification.user.settings',
template : 'notification/settings',
permission : true
}
});
plugin.events.on('we:after:load:plugins', function (we) {
we.notification = true;
});
plugin.hooks.on('we-plugin-menu:after:set:core:menus', function(data, done){
const req = data.req;
const res = data.res;
// set user menu
if (req.isAuthenticated()) {
res.locals.userMenu.addLink({
id: 'notifications',
href: '/notification',
text: '<span class="glyphicon glyphicon-bell"></span>'+
'<span class="main-menu-link-notification-count badge"></span> '+
'<span class="main-menu-notification-text">'+
res.locals.__('notification.find') +
'</span>',
class: 'main-menu-notification-link',
weight: 0
});
}
done();
});
/**
* Send email notifications to all users
*/
plugin.sendUsersEmailNotifications = function sendUsersEmailNotifications(we, done) {
const afterCreatedAt = we.utils.moment();
const qLiteral = we.db.defaultConnection.literal;
afterCreatedAt.subtract(we.config.notification.waitMinutesForSendEmail, 'm');
let usersToReceive = {};
we.db.models.notification
.findAll({
where: {
emailSend: false,
read: false,
createdAt: {
[we.Op.lt]: afterCreatedAt.format('YYYY-MM-DD HH:mm:ss')
},
[we.Op.and]: [
qLiteral('settings.followingEmailNotifications != false OR settings.followingEmailNotifications is NULL')
]
},
orderBy: [
[ 'userId', 'DESC'],
[ 'createdAt', 'DESC']
],
include: [
{ as: 'settings', model: we.db.models.notificationSettings }
]
})
.then( (notifications)=> {
// group by user id
for (let i = notifications.length - 1; i >= 0; i--) {
if (!usersToReceive[notifications[i].userId]) {
usersToReceive[notifications[i].userId] = {
id: notifications[i].userId,
notifications: [],
notificationsIds: []
};
}
usersToReceive[notifications[i].userId].notifications.push(notifications[i]);
usersToReceive[notifications[i].userId].notificationsIds.push(notifications[i].id);
}
// for each user
we.utils.async.eachSeries(usersToReceive, ( userToReceive, done)=> {
we.db.models.user
.findById(userToReceive.id)
.then( (user)=> {
if (!user) {
done();
return null;
}
let email = user.email;
if (userToReceive.notifications[0] &&
userToReceive.notifications[0].settings &&
userToReceive.notifications[0].settings.email
) {
email = userToReceive.notifications[0].settings.email;
}
let nt = '<table class="notitication-table">';
userToReceive.notifications.forEach( (n)=> {
nt += `<tr>
<td>${n.title}</td>
<td>
<a class="notification-link" href="${we.config.hostname}${n.redirectUrl}">Ver</a>
</td>
</tr>`;
});
nt += '</table>';
let appName = we.config.appName;
if (we.systemSettings && we.systemSettings.siteName) {
appName = we.systemSettings.siteName;
}
we.email.sendEmail('userNotifications', {
to: email
},
// template data
{
user: user,
displayName: user.displayName,
email: user.email,
notifications: nt,
siteName: appName,
siteURL: we.config.hostname
}, (err)=> {
if (err) {
we.log.error(err);
done();
} else {
we.log.info('Send '+userToReceive.notifications.length+' notifications in one email to:', email);
// update all notifications
we.db.models.notification
.update({
emailSend: true
}, {
where: {
id: userToReceive.notificationsIds
}
})
.then( ()=> {
done();
return null;
})
.catch( (err)=> {
we.log.error(err);
done();
return null;
});
}
});
return null;
})
.catch(done);
}, done);
return null;
})
.catch(done);
}
plugin.addJs('we.notification', {
type: 'plugin', weight: 15, pluginName: 'we-plugin-notification',
path: 'files/public/we.notification.js'
});
plugin.addCss('we.notification', {
type: 'plugin', weight: 15, pluginName: 'we-plugin-notification',
path: 'files/public/we.notification.css'
});
return plugin;
}