forked from elmarburke/hoodie-plugin-direct-messages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
40 lines (32 loc) · 1.26 KB
/
worker.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
module.exports = function (hoodie, done) {
hoodie.task.on('directmessage:add', handleNewMessage);
function handleNewMessage(originDb, message) {
var recipient = message.to;
// workaround to ensure the task will be found when calling success
var id = message.id;
var type = message.type;
hoodie.account.find('user', recipient,
function (error, user) {
if (error) {
return hoodie.task.error(originDb, message, error);
}
;
var targetDb = 'user/' + user.hoodieId;
// workaround to prevent document update conflicts
delete message._id;
delete message._rev;
hoodie.database(targetDb).add('directmessage',
message,
function (error, message) {
if (error) {
return hoodie.task.error(originDb, message, error);
}
// resetting id and type to identify the original task
message.id = id;
message.type = type;
return hoodie.task.success(originDb, message);
});
});
};
done();
};