-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
muc.js
148 lines (117 loc) · 7.21 KB
/
muc.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
/*global mock, converse */
const { Strophe, sizzle, stx, u } = converse.env;
describe("Groupchats", function () {
it("keeps track of unread messages and mentions",
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
const nick = 'romeo';
const muc_jid = 'lounge@montague.lit';
// Open a hidden room
await mock.openAndEnterChatRoom(_converse, muc_jid, nick, [], [], false, {'hidden': true});
const model = _converse.chatboxes.get(muc_jid);
_converse.api.connection.get()._dataRecv(mock.createRequest(stx`
<message xmlns="jabber:client" type="groupchat" id="1" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
<body>Romeo oh romeo</body>
</message>`));
await u.waitUntil(() => model.messages.length);
expect(model.get('num_unread_general')).toBe(1);
expect(model.get('num_unread')).toBe(1);
_converse.api.connection.get()._dataRecv(mock.createRequest(stx`
<message xmlns="jabber:client" type="groupchat" id="2" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
<body>Wherefore art though?</body>
</message>`));
await u.waitUntil(() => model.messages.length === 2);
expect(model.get('num_unread_general')).toBe(2);
expect(model.get('num_unread')).toBe(1);
// Check that unread counters are cleared when chat becomes visible
model.set('hidden', false);
expect(model.get('num_unread_general')).toBe(0);
expect(model.get('num_unread')).toBe(0);
}));
describe("A groupchat", function () {
it("sends the user status when joining and when it changes",
mock.initConverse(['statusInitialized'], {}, async function (_converse) {
const muc_jid = 'coven@chat.shakespeare.lit';
_converse.xmppstatus.set('status', 'away');
const sent_stanzas = _converse.api.connection.get().sent_stanzas;
while (sent_stanzas.length) sent_stanzas.pop();
const muc = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
let pres = await u.waitUntil(() => sent_stanzas.filter(s => s.nodeName === 'presence').pop());
expect(Strophe.serialize(pres)).toBe(
`<presence from="${_converse.jid}" id="${pres.getAttribute('id')}" to="${muc_jid}/romeo" xmlns="jabber:client">`+
`<x xmlns="http://jabber.org/protocol/muc"><history maxstanzas="0"/></x>`+
`<show>away</show>`+
`<c hash="sha-1" node="https://conversejs.org" ver="TfHz9vOOfqIG0Z9lW5CuPaWGnrQ=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`);
expect(muc.getOwnOccupant().get('show')).toBe('away');
while (sent_stanzas.length) sent_stanzas.pop();
_converse.xmppstatus.set('status', 'xa');
pres = await u.waitUntil(() => sent_stanzas.filter(s => s.nodeName === 'presence' && s.getAttribute('to') === `${muc_jid}/romeo`).pop());
expect(Strophe.serialize(pres)).toBe(
`<presence to="${muc_jid}/romeo" xmlns="jabber:client">`+
`<show>xa</show>`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="TfHz9vOOfqIG0Z9lW5CuPaWGnrQ=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`)
_converse.xmppstatus.set('status', 'dnd');
_converse.xmppstatus.set('status_message', 'Do not disturb');
while (sent_stanzas.length) sent_stanzas.pop();
const muc2_jid = 'cave@chat.shakespeare.lit';
const muc2 = await mock.openAndEnterChatRoom(_converse, muc2_jid, 'romeo');
pres = await u.waitUntil(() => sent_stanzas.filter(s => s.nodeName === 'presence').pop());
expect(Strophe.serialize(pres)).toBe(
`<presence from="${_converse.jid}" id="${pres.getAttribute('id')}" to="${muc2_jid}/romeo" xmlns="jabber:client">`+
`<x xmlns="http://jabber.org/protocol/muc"><history maxstanzas="0"/></x>`+
`<show>dnd</show>`+
`<status>Do not disturb</status>`+
`<c hash="sha-1" node="https://conversejs.org" ver="TfHz9vOOfqIG0Z9lW5CuPaWGnrQ=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`);
expect(muc2.getOwnOccupant().get('show')).toBe('dnd');
}));
it("reconnects when no-acceptable error is returned when sending a message",
mock.initConverse([], {}, async function (_converse) {
const muc_jid = 'coven@chat.shakespeare.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const model = _converse.chatboxes.get(muc_jid);
expect(model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
model.sendMessage({'body': 'hello world'});
const stanza = stx`
<message xmlns='jabber:client'
from='${muc_jid}'
type='error'
to='${_converse.bare_jid}'>
<error type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</message>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
let sent_stanzas = _converse.api.connection.get().sent_stanzas;
const iq = await u.waitUntil(() => sent_stanzas.filter(s => sizzle(`[xmlns="${Strophe.NS.PING}"]`, s).length).pop());
expect(Strophe.serialize(iq)).toBe(
`<iq id="${iq.getAttribute('id')}" to="coven@chat.shakespeare.lit/romeo" type="get" xmlns="jabber:client">`+
`<ping xmlns="urn:xmpp:ping"/>`+
`</iq>`);
const result = stx`
<iq from='${muc_jid}'
id='${iq.getAttribute('id')}'
to='${_converse.bare_jid}'
xmlns="jabber:server"
type='error'>
<error type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>`;
sent_stanzas = _converse.api.connection.get().sent_stanzas;
const index = sent_stanzas.length -1;
_converse.api.connection.get().IQ_stanzas = [];
_converse.api.connection.get()._dataRecv(mock.createRequest(result));
await mock.getRoomFeatures(_converse, muc_jid);
const pres = await u.waitUntil(
() => sent_stanzas.slice(index).filter(s => s.nodeName === 'presence').pop());
expect(Strophe.serialize(pres)).toBe(
`<presence from="${_converse.jid}" id="${pres.getAttribute('id')}" to="coven@chat.shakespeare.lit/romeo" xmlns="jabber:client">`+
`<x xmlns="http://jabber.org/protocol/muc"><history maxstanzas="0"/></x>`+
`<c hash="sha-1" node="https://conversejs.org" ver="TfHz9vOOfqIG0Z9lW5CuPaWGnrQ=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`);
}));
});
});