-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagefetchercreator.js
101 lines (96 loc) · 3.09 KB
/
messagefetchercreator.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
function createMessageFetcher (lib, utilslib) {
var q = lib.q,
qlib = lib.qlib,
JobBase = qlib.JobBase,
zeroString = String.fromCharCode(0);
function MessageFetcherJob (conversationevent, msgfunc, userid, conversationid, oldestmessageid, howmany, defer) {
JobBase.call(this, defer);
this.conversationevent = conversationevent;
this.msgfunc = msgfunc;
this.userid = userid;
this.conversationid = conversationid;
this.oldestmessageid = oldestmessageid;
this.howmany = howmany;
this.listener = null;
this.notifications = [];
}
lib.inherit(MessageFetcherJob, JobBase);
MessageFetcherJob.prototype.destroy = function () {
this.notifications = null;
if (this.listener) {
this.listener.destroy();
}
this.listener = null;
this.howmany = null;
this.oldestmessageid = null;
this.conversationid = null;
this.userid = null;
this.msgfunc = null;
this.conversationevent = null;
JobBase.prototype.destroy.call(this);
};
MessageFetcherJob.prototype.go = function () {
if (!this.oldestmessageid) {
this.listener = this.conversationevent.attach(this.onConversationNotification.bind(this));
}
this.msgfunc(this.userid, this.conversationid, this.oldestmessageid, this.howmany).then(
this.onMessages.bind(this),
this.reject.bind(this)
);
return this.defer.promise;
};
MessageFetcherJob.prototype.onConversationNotification = function (ntf) {
if (!(ntf && ntf.id===this.conversationid && ntf.mids.length>0)) {
//console.error(ntf);
//console.error('ne valja');
return;
}
if (ntf.rcvdby) {
return;
}
if (ntf.seenby) {
return;
}
this.notifications.push(lib.extend({}, ntf.lastm, {id:ntf.mids[1]}));
};
MessageFetcherJob.prototype.onMessages = function (msgs) {
var lastmsgid, allmsgs, myid;
lastmsgid = msgs.length>0 ? msgs[msgs.length-1].id : undefined;
allmsgs = msgs.concat(this.notifications.filter(gt.bind(null, lastmsgid)).slice(-this.howmany));
//console.log('onMessages', msgs);
//console.log('onMessages my notifications', this.notifications);
//console.log('onMessages', msgs[msgs.length-1]);
//console.log('lastmsgid', lastmsgid);
msgs.forEach(this.checkForNotRcvdByUserid.bind(this));
myid = this.userid;
allmsgs.forEach(personalizer.bind(null, myid));
myid = null;
this.resolve(allmsgs);
};
MessageFetcherJob.prototype.checkForNotRcvdByUserid = function (msg) {
//console.log('checkForNotRcvdByUserid', msg);
if (!msg) {
return;
}
if (msg.from === null) {
return;
}
if (msg.rcvd) {
return;
}
this.notify({
conversationid: this.conversationid,
userid: this.userid,
messageid: msg.id
});
};
function personalizer (myid, msg) {
utilslib.rcvdseen2personal(msg, lib.isArray(msg.rcvdby), myid, 'rcvd', false);
utilslib.rcvdseen2personal(msg, lib.isArray(msg.rcvdby), myid, 'seen', false);
}
function gt (lastmsgid, msg) {
return msg.id>lastmsgid;
}
return MessageFetcherJob;
}
module.exports = createMessageFetcher;