forked from strophe/strophejs-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strophe.receipts.js
155 lines (131 loc) · 4.06 KB
/
strophe.receipts.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
Strophe.addConnectionPlugin('receipts', {
_conn: null,
_msgQueue: {},
_retries: {},
_resendCount: 10,
_resendTime: 9000,
init: function(conn) {
this._conn = conn;
Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
},
statusChanged: function (status) {
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
// set up handlers for receipts
//this._conn.addHandler(this._onRequestReceived.bind(this), Strophe.NS.RECEIPTS, "message");
var that = this;
setTimeout(function(){that.resendQueue();},5000);
}
},
/*
_onRequestReceived: function(msg){
this._processReceipt(msg);
return true;
},
* */
/* sendMessage
** sends a message with a receipt and stores the message in the queue
** in case a receipt is never received
**
** msg should be a builder
*/
sendMessage: function(msg) {
var id = this._conn.getUniqueId();
msg.tree().setAttribute('id', id);
var request = Strophe.xmlElement('request', {'xmlns': Strophe.NS.RECEIPTS});
msg.tree().appendChild(request);
this._msgQueue[id] = msg;
this._retries[id] = 0;
this._conn.send(msg);
this.resendMessage(id);
return id;
},
/*
** resend queued message
*/
resendMessage: function(id){
var that = this;
setTimeout(function(){
if (that._msgQueue[id]){
// if we are disconnected, dont increment retries count and retry later
if (!that._conn.connected) {
that.resendMessage(id);
return;
}
that._retries[id]++;
if (that._retries[id] > that._resendCount) {
//TODO: use mod_rest to force injection of the message
//console.debug('message could not be delivered after ' + that._resendCount + ' attempts');
return;
}
// FIX: use our actual jid in case we disconnected and changed jid
that._msgQueue[id].tree().setAttribute('from', that._conn.jid);
that._conn.send(that._msgQueue[id]);
that.resendMessage(id);
}
},this._resendTime);
},
/* addMessageHandler
** add a message handler that handles XEP-0184 message receipts
*/
addReceiptHandler: function(handler, type, from, options) {
var that = this;
var proxyHandler = function(msg) {
that._processReceipt(msg);
// call original handler
return handler(msg);
};
this._conn.addHandler(proxyHandler, Strophe.NS.RECEIPTS, 'message',
type, null, from, options);
},
/*
* process a XEP-0184 message receipts
* send recept on request
* remove msg from queue on received
*/
_processReceipt: function(msg){
var id = msg.getAttribute('id'),
from = msg.getAttribute('from'),
req = msg.getElementsByTagName('request'),
rec = msg.getElementsByTagName('received');
// check for request in message
if (req.length > 0) {
// send receipt
var out = $msg({to: from, from: this._conn.jid, id: this._conn.getUniqueId()}),
request = Strophe.xmlElement('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id});
out.tree().appendChild(request);
this._conn.send(out);
}
// check for received
if (rec.length > 0) {
var recv_id = rec[0].getAttribute('id');
if (recv_id) { // delete msg from queue
delete this._msgQueue[recv_id];
delete this._retries[recv_id];
}
}
},
resendQueue: function(){
if (!this._conn.connected) {
var that = this;
setTimeout(function(){that.resendQueue();},5000);
return;
}
for (var id in this._msgQueue) {
if (this._msgQueue.hasOwnProperty(id)) {
this._conn.send(this._msgQueue[id]);
}
}
},
getUnreceivedMsgs: function() {
var msgs = [];
for (var id in this._msgQueue) {
if (this._msgQueue.hasOwnProperty(id)) {
msgs.push(this._msgQueue[id]);
}
}
return msgs;
},
clearMessages: function() {
this._msgQueue = {};
}
});