forked from strophe/strophejs-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrophe.chatstates.js
80 lines (67 loc) · 1.57 KB
/
strophe.chatstates.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
/**
* Chat state notifications (XEP 0085) plugin
* @see http://xmpp.org/extensions/xep-0085.html
*/
Strophe.addConnectionPlugin('chatstates',
{
init: function (connection)
{
this._connection = connection;
Strophe.addNamespace('CHATSTATES', 'http://jabber.org/protocol/chatstates');
},
statusChanged: function (status)
{
if (status === Strophe.Status.CONNECTED
|| status === Strophe.Status.ATTACHED)
{
this._connection.addHandler(this._notificationReceived.bind(this),
Strophe.NS.CHATSTATES, "message");
}
},
addActive: function(message)
{
return message.c('active', {xmlns: Strophe.NS.CHATSTATES}).up();
},
_notificationReceived: function(message)
{
var composing = $(message).find('composing'),
paused = $(message).find('paused'),
active = $(message).find('active'),
jid = $(message).attr('from');
if (composing.length > 0)
{
$(document).trigger('composing.chatstates', jid);
}
if (paused.length > 0)
{
$(document).trigger('paused.chatstates', jid);
}
if (active.length > 0)
{
$(document).trigger('active.chatstates', jid);
}
return true;
},
sendActive: function(jid, type)
{
this._sendNotification(jid, type, 'active');
},
sendComposing: function(jid, type)
{
this._sendNotification(jid, type, 'composing');
},
sendPaused: function(jid, type)
{
this._sendNotification(jid, type, 'paused');
},
_sendNotification: function(jid, type, notification)
{
if (!type) type = 'chat';
this._connection.send($msg(
{
to: jid,
type: type
})
.c(notification, {xmlns: Strophe.NS.CHATSTATES}));
}
});