Mercurial > xmpp-account-manager
annotate nickname.js @ 48:021185105e2f
Move the node configuration retrieval to a util promise.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 23 May 2020 20:37:20 +0200 |
parents | b76146a09e07 |
children | 2f45bee88b47 |
rev | line source |
---|---|
0 | 1 'use strict'; |
2 | |
3 function initNickname(connection) { | |
4 const nick_input = document.getElementById('nick'); | |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
5 const nick_access = document.getElementById('nick-access'); |
10
e47b9bd55192
Also use a spinner on nickname get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
6 const spinner_img = document.getElementById('nick-spinner'); |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
7 const access_spinner_img = document.getElementById('nick-access-spinner'); |
0 | 8 |
9 const iq = $iq({type: 'get'}) | |
10 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
11 .c('items', {node: 'http://jabber.org/protocol/nick'}); | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
12 connection.sendIQ(iq, onNickname, onNicknameRetrievalError); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
13 displaySpinner(spinner_img); |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
14 getNicknameAccessModel(); |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
15 displaySpinner(access_spinner_img); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
16 |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
17 function getNicknameAccessModel() |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
18 { |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
19 retrieveConfiguration(connection).then((access_model) => { |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
20 console.log('yay', access_model); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
21 if (access_model !== null) { |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
22 if (access_model === 'open') |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
23 nick_access.value = 'open'; |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
24 else if (access_model === 'presence') |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
25 nick_access.value = 'presence'; |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
26 else |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
27 console.log('Unsupported nickname access model: ' + access_model); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
28 nick_access.disabled = false; |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
29 } |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
30 hideSpinner(access_spinner_img); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
31 }, (reason) => { |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
32 console.log('Failed to retrieve nickname configuration: ' + reason); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
33 hideSpinner(access_spinner_img); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
34 }); |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
35 } |
0 | 36 |
37 function onNickname(result_iq) | |
38 { | |
39 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
40 if (item == null) { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
41 console.log('Failed to retrieve nickname: no item published.'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
42 hideSpinner(spinner_img); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
43 return; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
44 } |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
45 |
0 | 46 const id = item.getAttributeNS(null, 'id'); |
47 const nick = parseXPath(item, './nickname:nick'); | |
48 nick_input.value = nick.textContent; | |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
49 hideSpinner(spinner_img); |
0 | 50 } |
51 | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
52 function onNicknameRetrievalError(iq) |
0 | 53 { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
54 console.log('Failed to retrieve nickname: ' + parseErrorIq(iq)); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
55 hideSpinner(spinner_img); |
0 | 56 } |
57 | |
34
45bd945c5722
Change all blur events to change, prevents a stanza when no change occured.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
58 nick_input.addEventListener('change', function (evt) { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
59 const new_nickname = evt.target.value; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
60 let iq; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
61 let changed_callback; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
62 if (new_nickname) { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
63 iq = $iq({type: 'set'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
64 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
65 .c('publish', {node: 'http://jabber.org/protocol/nick'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
66 .c('item', {id: 'current'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
67 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
68 .t(new_nickname); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
69 changed_callback = onNicknameChanged.bind(null, 'changed'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
70 } else { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
71 iq = $iq({type: 'set'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
72 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
73 .c('delete', {node: 'http://jabber.org/protocol/nick'}); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
74 changed_callback = onNicknameChanged.bind(null, 'deleted'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
75 } |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
76 connection.sendIQ(iq, changed_callback, onNicknameChangeError); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
77 displaySpinner(spinner_img); |
0 | 78 }); |
79 | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
80 function onNicknameChanged(action, result_iq) |
0 | 81 { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
82 console.log('Nickname successfully ' + action + '.'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
83 nick_access.disabled = true; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
84 if (action === 'changed') |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
85 getNicknameAccessModel(); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
86 spinnerOk(spinner_img); |
0 | 87 } |
88 | |
89 function onNicknameChangeError(iq) | |
90 { | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
91 console.log('onNicknameChangeError', iq); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
92 spinnerError(spinner_img); |
0 | 93 } |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
94 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
95 nick_access.addEventListener('change', function (evt) { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
96 const iq = configurePEPField('http://jabber.org/protocol/nick', 'pubsub#access_model', evt.target.value); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
97 connection.sendIQ(iq, onNicknameConfigured, onNicknameConfigureError); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
98 displaySpinner(access_spinner_img); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
99 }); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
100 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
101 function onNicknameConfigured(iq) |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
102 { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
103 console.log('Successfully set nickname access model.') |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
104 spinnerOk(access_spinner_img); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
105 } |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
106 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
107 function onNicknameConfigureError(iq) |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
108 { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
109 const string = 'Failed to configure nick node: ' + parseErrorIq(iq); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
110 console.log(string); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
111 spinnerError(access_spinner_img, string); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
112 } |
0 | 113 } |