-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
rai.js
225 lines (191 loc) · 10.6 KB
/
rai.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*global mock, converse */
const { Strophe, u, stx } = converse.env;
// See: https://xmpp.org/rfcs/rfc3921.html
describe("XEP-0437 Room Activity Indicators", function () {
it("will be activated for a MUC that becomes hidden",
mock.initConverse(
[], {
'allow_bookmarks': false, // Hack to get the rooms list to render
'muc_subscribe_to_rai': true,
'view_mode': 'fullscreen'},
async function (_converse) {
expect(_converse.session.get('rai_enabled_domains')).toBe(undefined);
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);
expect(view.model.get('hidden')).toBe(false);
const sent_IQs = _converse.api.connection.get().IQ_stanzas;
const iq_get = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq query[xmlns="${Strophe.NS.MAM}"]`)).pop());
const first_msg_id = _converse.api.connection.get().getUniqueId();
const last_msg_id = _converse.api.connection.get().getUniqueId();
let message =
stx`<message xmlns="jabber:client"
to="romeo@montague.lit/orchard"
from="${muc_jid}">
<result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${first_msg_id}">
<forwarded xmlns="urn:xmpp:forward:0">
<delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:15:23Z"/>
<message from="${muc_jid}/some1" type="groupchat">
<body>1st MAM Message</body>
</message>
</forwarded>
</result>
</message>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(message));
message = stx`<message xmlns="jabber:client"
to="romeo@montague.lit/orchard"
from="${muc_jid}">
<result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${last_msg_id}">
<forwarded xmlns="urn:xmpp:forward:0">
<delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:16:23Z"/>
<message from="${muc_jid}/some1" type="groupchat">
<body>2nd MAM Message</body>
</message>
</forwarded>
</result>
</message>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(message));
const result =
stx`<iq type="result"
id="${iq_get.getAttribute("id")}"
xmlns="jabber:client">
<fin xmlns="urn:xmpp:mam:2">
<set xmlns="http://jabber.org/protocol/rsm">
<first index="0">${first_msg_id}</first>
<last>${last_msg_id}</last>
<count>2</count>
</set>
</fin>
</iq>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(result));
await u.waitUntil(() => view.model.messages.length === 2);
const sent_stanzas = [];
spyOn(_converse.api.connection.get(), 'send').and.callFake(s => sent_stanzas.push(s?.nodeTree ?? s));
view.model.save({'hidden': true});
await u.waitUntil(() => sent_stanzas.length === 3);
expect(Strophe.serialize(sent_stanzas[0])).toBe(
`<message from="${_converse.jid}" id="${sent_stanzas[0].getAttribute('id')}" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">`+
`<received id="${last_msg_id}" xmlns="urn:xmpp:chat-markers:0"/>`+
`</message>`
);
expect(Strophe.serialize(sent_stanzas[1])).toBe(
`<presence to="${muc_jid}/romeo" type="unavailable" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`
);
expect(Strophe.serialize(sent_stanzas[2])).toBe(
`<presence to="montague.lit" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`<rai xmlns="urn:xmpp:rai:0"/>`+
`</presence>`
);
await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED);
expect(view.model.get('has_activity')).toBe(false);
const room_el = await u.waitUntil(() => document.querySelector("converse-rooms-list .available-chatroom"));
expect(Array.from(room_el.classList).includes('unread-msgs')).toBeFalsy();
const activity_stanza = stx`
<message from="${Strophe.getDomainFromJid(muc_jid)}"
xmlns="jabber:client">
<rai xmlns="urn:xmpp:rai:0">
<activity>${muc_jid}</activity>
</rai>
</message>
`;
_converse.api.connection.get()._dataRecv(mock.createRequest(activity_stanza));
await u.waitUntil(() => view.model.get('has_activity'));
expect(Array.from(room_el.classList).includes('unread-msgs')).toBeTruthy();
}));
it("will be activated for a MUC that starts out hidden",
mock.initConverse(
[], {
'allow_bookmarks': false, // Hack to get the rooms list to render
'muc_subscribe_to_rai': true,
'view_mode': 'fullscreen'},
async function (_converse) {
const { api } = _converse;
expect(_converse.session.get('rai_enabled_domains')).toBe(undefined);
const muc_jid = 'lounge@montague.lit';
const nick = 'romeo';
const sent_stanzas = _converse.api.connection.get().sent_stanzas;
const muc_creation_promise = await api.rooms.open(muc_jid, {nick, 'hidden': true}, false);
await mock.getRoomFeatures(_converse, muc_jid, []);
await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
await muc_creation_promise;
const model = _converse.chatboxes.get(muc_jid);
await u.waitUntil(() => (model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
expect(model.get('hidden')).toBe(true);
const getSentPresences = () => sent_stanzas.filter(s => s.nodeName === 'presence');
await u.waitUntil(() => getSentPresences().length === 3, 500);
const sent_presences = getSentPresences();
expect(Strophe.serialize(sent_presences[1])).toBe(
`<presence to="${muc_jid}/romeo" type="unavailable" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`
);
expect(Strophe.serialize(sent_presences[2])).toBe(
`<presence to="montague.lit" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`<rai xmlns="urn:xmpp:rai:0"/>`+
`</presence>`
);
await u.waitUntil(() => model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED);
expect(model.get('has_activity')).toBe(false);
const room_el = await u.waitUntil(() => document.querySelector("converse-rooms-list .available-chatroom"));
expect(Array.from(room_el.classList).includes('unread-msgs')).toBeFalsy();
const activity_stanza = stx`
<message from="${Strophe.getDomainFromJid(muc_jid)}"
xmlns="jabber:client">
<rai xmlns="urn:xmpp:rai:0">
<activity>${muc_jid}</activity>
</rai>
</message>
`;
_converse.api.connection.get()._dataRecv(mock.createRequest(activity_stanza));
await u.waitUntil(() => model.get('has_activity'));
expect(Array.from(room_el.classList).includes('unread-msgs')).toBeTruthy();
}));
it("may not be activated due to server resource constraints",
mock.initConverse(
[], {
'allow_bookmarks': false, // Hack to get the rooms list to render
'muc_subscribe_to_rai': true,
'view_mode': 'fullscreen'},
async function (_converse) {
expect(_converse.session.get('rai_enabled_domains')).toBe(undefined);
const muc_jid = 'lounge@montague.lit';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
expect(model.get('hidden')).toBe(false);
const sent_stanzas = [];
spyOn(_converse.api.connection.get(), 'send').and.callFake(s => sent_stanzas.push(s?.nodeTree ?? s));
model.save({'hidden': true});
await u.waitUntil(() => sent_stanzas.filter(s => s.nodeName === 'presence').length === 2);
const sent_presences = sent_stanzas.filter(s => s.nodeName === 'presence');
expect(Strophe.serialize(sent_presences[0])).toBe(
`<presence to="${muc_jid}/romeo" type="unavailable" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`</presence>`
);
expect(Strophe.serialize(sent_presences[1])).toBe(
`<presence to="montague.lit" xmlns="jabber:client">`+
`<priority>0</priority>`+
`<c hash="sha-1" node="https://conversejs.org" ver="/5ng/Bnz6MXvkSDu6hjAlgQ8C60=" xmlns="http://jabber.org/protocol/caps"/>`+
`<rai xmlns="urn:xmpp:rai:0"/>`+
`</presence>`
);
// If an error presence with "resource-constraint" is returned, we rejoin
const activity_stanza = stx`
<presence type="error"
from="${Strophe.getDomainFromJid(muc_jid)}"
xmlns="jabber:client">
<error type="wait"><resource-constraint xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
</presence>
`;
_converse.api.connection.get()._dataRecv(mock.createRequest(activity_stanza));
await u.waitUntil(() => model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
}));
});