comparison roster.js @ 4:5e97e1808a35

Add support for the roster.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 22 Dec 2018 02:59:29 +0100
parents
children 45bd945c5722
comparison
equal deleted inserted replaced
3:5aa1bf7154b0 4:5e97e1808a35
1 'use strict';
2
3 function initRoster(connection) {
4 const roster_table = document.getElementById('roster-table');
5
6 const iq = $iq({type: 'get'})
7 .c('query', {xmlns: NS.roster});
8 connection.sendIQ(iq, onRoster, onRosterError.bind(null, 'roster query failed.'));
9
10 function onRoster(result_iq)
11 {
12 const items = parseXPath(result_iq, './roster:query/roster:item', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
13 while (true) {
14 const item = items.iterateNext();
15 if (!item)
16 break;
17 const jid = item.getAttributeNS(null, 'jid');
18 const subscription = item.getAttributeNS(null, 'subscription');
19 const name = item.getAttributeNS(null, 'name');
20 const groups = item.children;
21 const tr = document.createElementNS('http://www.w3.org/1999/xhtml', 'tr');
22 let td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
23 const a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
24 a.setAttributeNS(null, 'href', 'xmpp:' + jid);
25 a.textContent = jid;
26 td.appendChild(a);
27 tr.appendChild(td);
28 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
29 const input = document.createElementNS('http://www.w3.org/1999/xhtml', 'input');
30 input.value = name;
31 input.onblur = function (evt) {
32 const iq = $iq({type: 'set'})
33 .c('query', {xmlns: NS.roster})
34 .c('item', {jid: jid, name: evt.target.value});
35 for (let group of groups)
36 iq.c('group').t(group.textContent).up();
37 connection.sendIQ(iq, onRosterSet, onRosterSetError.bind(null, 'Roster set failed.'));
38 };
39 td.appendChild(input);
40 tr.appendChild(td);
41 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
42 td.textContent = subscription;
43 tr.appendChild(td);
44 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
45 for (let group of groups) {
46 const span = document.createElementNS('http://www.w3.org/1999/xhtml', 'span');
47 // TODO: use a tag system for the UI.
48 span.textContent = group.textContent;
49 td.appendChild(span);
50 }
51 tr.appendChild(td);
52 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
53 const button = document.createElementNS('http://www.w3.org/1999/xhtml', 'button');
54 button.textContent = 'Remove this contact';
55 button.onclick = function (evt) {
56 const iq = $iq({type: 'set'})
57 .c('query', {xmlns: NS.roster})
58 .c('item', {jid: jid, subscription: 'unsubscribe'});
59 connection.sendIQ(iq, onRosterSet.bind(node), onRosterSetError.bind(node, 'contact removal failed.'));
60 };
61 td.appendChild(button);
62 tr.appendChild(td);
63 roster_table.appendChild(tr);
64 }
65 }
66
67 function onRosterError(string)
68 {
69 console.log('Failed to retrieve your contact list: ' + string);
70 }
71
72 function onRosterSet(result_iq)
73 {
74 console.log(result_iq);
75 }
76
77 function onRosterSetError(string)
78 {
79 console.log('Failed to retrieve your contact list: ' + string);
80 }
81 }