annotate avatar.js @ 30:9ba4f8cc32f1

Display the returned error in the title of the spinner.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 23 Dec 2018 17:15:38 +0100
parents 07e33207e598
children aff7caa10489
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 'use strict';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 function initAvatar(connection) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 const DEFAULT_AVATAR = 'data:image/svg+xml,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 150 150"><rect width="150" height="150" fill="#888" stroke-width="1" stroke="#000"/><text x="75" y="100" text-anchor="middle" font-size="100">?</text></svg>';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 const avatar_data = {};
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 const avatar_img = document.getElementById('avatar');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 const avatar_size = document.getElementById('avatar-size');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 const avatar_file = document.getElementById('avatar-file');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 const avatar_upload = document.getElementById('avatar-upload');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 const avatar_change = document.getElementById('avatar-change');
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
12 const avatar_access = document.getElementById('avatar-access');
11
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
13 const spinner_img = document.getElementById('avatar-spinner');
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
14 const access_model_spinner_img = document.getElementById('access-model-avatar-spinner');
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 avatar_img.src = DEFAULT_AVATAR;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 const iq = $iq({type: 'get'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 .c('items', {node: 'urn:xmpp:avatar:metadata'});
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 connection.sendIQ(iq, onAvatarMetadata, onAvatarRetrievalError.bind(null, 'PubSub metadata query failed.'));
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
21 displaySpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 function onAvatarMetadata(result_iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 if (item == null)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 return onAvatarRetrievalError('no item found.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 const id = item.getAttributeNS(null, 'id');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 const info = parseXPath(item, './avatar_metadata:metadata/avatar_metadata:info');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 if (info == null)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 return onAvatarRetrievalError('no info found, your avatar metadata node is broken.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 if (id != info.getAttributeNS(null, 'id'))
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 return onAvatarRetrievalError('invalid id in metadata.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 const parsed_info = {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 id: id,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 type: info.getAttributeNS(null, 'type'),
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 bytes: info.getAttributeNS(null, 'bytes'),
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 width: info.getAttributeNS(null, 'width'),
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 height: info.getAttributeNS(null, 'height'),
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 };
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 const iq = $iq({type: 'get'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 .c('items', {node: 'urn:xmpp:avatar:data'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 .c('item', {id: id});
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 connection.sendIQ(iq, onAvatarData.bind(null, parsed_info), onAvatarRetrievalError.bind(null, 'PubSub data query failed.'));
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 function onAvatarData(info, result_iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 if (item == null)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 return onAvatarRetrievalError('no item found.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 if (info.id != item.getAttributeNS(null, 'id'))
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 return onAvatarRetrievalError('invalid id in data.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 const data = parseXPath(item, './avatar_data:data').textContent;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 const url = 'data:' + info.type + ';base64,' + data;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 // TODO: validate the bytes too.
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 /*
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 // TODO: figure out why this didn’t work.
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 avatar_img.onload = function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 const img = evt.target;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if (img.naturalWidth != info.width || img.naturalHeight != info.height)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 return onAvatarRetrievalError('invalid width or height in image data.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 avatar_img.onload = null;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 };
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 */
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 avatar_img.src = url;
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
70 hideSpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 function onAvatarRetrievalError(string)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 console.log('Failed to retrieve avatar, an empty one is displayed instead: ' + string);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 avatar_img.src = DEFAULT_AVATAR;
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
77 hideSpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 avatar_upload.addEventListener('click', function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 avatar_file.click();
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 });
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 avatar_change.addEventListener('click', function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 const metadata_iq = $iq({type: 'set'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 .c('publish', {node: 'urn:xmpp:avatar:metadata'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 .c('item', {id: avatar_data.id})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 .c('metadata', {xmlns: 'urn:xmpp:avatar:metadata'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 .c('info', {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 id: avatar_data.id,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 type: avatar_data.type,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 bytes: avatar_data.bytes,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 width: avatar_img.naturalWidth,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 height: avatar_img.naturalHeight,
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 });
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 connection.sendIQ(metadata_iq, onAvatarMetadataUpload, onAvatarUploadError);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
98 displaySpinner(spinner_img);
11
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
99 });
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
100
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
101 function onAvatarMetadataUpload(iq)
aedf80eefc19 Also use a spinner on avatar get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3
diff changeset
102 {
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 const data_iq = $iq({type: 'set'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 .c('publish', {node: 'urn:xmpp:avatar:data'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 .c('item', {id: avatar_data.id})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 .c('data', {xmlns: 'urn:xmpp:avatar:data'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 .t(avatar_data.data);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 connection.sendIQ(data_iq, onAvatarDataUpload, onAvatarUploadError);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 function onAvatarDataUpload(iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 console.log('Avatar successfully uploaded!', iq);
13
8724e28ccbd7 Improve styling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
115 avatar_change.hidden = true;
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 avatar_size.innerHTML = '';
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
117 spinnerOk(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 function onAvatarUploadError(iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 console.log("onAvatarUploadError", iq);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
123 spinnerError(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 avatar_file.addEventListener('change', function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 const file = evt.target.files[0];
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 avatar_data.type = file.type;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 avatar_data.bytes = file.size;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 // Set the preview.
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 avatar_img.src = URL.createObjectURL(file);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 const [size, unit] = friendlyDataSize(file.size);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 avatar_size.innerHTML = Math.round(size) + ' ' + unit;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 // Obtain the base64 version of this file.
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 const base64_reader = new FileReader();
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 base64_reader.onload = function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 const data = evt.target.result;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 avatar_data.data = data.substr(data.indexOf(',') + 1);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 base64_reader.readAsDataURL(file);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 // Compute the sha1 of this file.
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 const sha1_reader = new FileReader();
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 sha1_reader.onload = async function (evt) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 const data = evt.target.result;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 const digest = await window.crypto.subtle.digest('SHA-1', data);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 const sha1 = (Array
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 .from(new Uint8Array(digest))
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 .map(b => b.toString(16).padStart(2, "0"))
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 .join(""));
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 avatar_data.id = sha1;
13
8724e28ccbd7 Improve styling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
154 avatar_change.hidden = false;
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 sha1_reader.readAsArrayBuffer(file);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 });
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 function friendlyDataSize(bytes) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 let unit = 'B'
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 if (bytes >= 1024) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 bytes /= 1024;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 unit = 'KiB';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 if (bytes >= 1024) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 bytes /= 1024;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 unit = 'MiB';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 if (bytes >= 1024) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 bytes /= 1024;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 unit = 'GiB';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 if (bytes >= 1024) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 bytes /= 1024;
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 unit = 'TiB';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 return [bytes, unit];
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 }
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
179
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
180 avatar_access.addEventListener('change', function (evt) {
3
5aa1bf7154b0 Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2
diff changeset
181 const iq = configurePEPField('urn:xmpp:avatar:metadata', 'pubsub#access_model', evt.target.value);
21
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
182 connection.sendIQ(iq, onAvatarMetadataConfigured, onAvatarConfigureError.bind(null, 'PubSub configuration of metadata failed.'));
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
183 displaySpinner(access_model_spinner_img);
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
184 });
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
185
21
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
186 function onAvatarMetadataConfigured(result_iq)
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
187 {
22
07e33207e598 Fixup for an unitialised variable in the previous commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 21
diff changeset
188 const iq = configurePEPField('urn:xmpp:avatar:data', 'pubsub#access_model', avatar_access.value);
21
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
189 connection.sendIQ(iq, onAvatarConfigured, onAvatarConfigureError.bind(null, 'PubSub configuration of data failed.'));
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
190 }
cd35420457ac Also configure the avatar data node’s access model.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
191
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
192 function onAvatarConfigured(result_iq)
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
193 {
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
194 console.log('Successfully set avatar access model.')
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
195 spinnerOk(access_model_spinner_img);
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
196 }
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
197
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
198 function onAvatarConfigureError(string)
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
199 {
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
200 console.log('Failed to configure avatar node: ' + string);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
201 spinnerError(access_model_spinner_img);
2
db033e5eabcb Add pubsub#access_model configuration for avatars.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
202 }
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 }