-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
323 lines (297 loc) · 9.79 KB
/
index.js
File metadata and controls
323 lines (297 loc) · 9.79 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var pubnub = null
var user_avatar = ''
var selectedCustomAvatar = null
var loginModal = null;
const num_avatars = 16
const num_avatars_to_display = 5
const MAX_AVATAR_FILE_SIZE = 1024 * 1024 * 1
async function load () {
if (!(await testForLoggedInUser()))
{
showLogin()
}
developerMessage('This demo uses an example UI but your app would use its own UX, invoking the PubNub SDK --> You are NOT locked to a specific UI');
developerMessage('Your username and avatar URL are stored in PubNub App Context');
}
async function showLogin () {
if (!testPubNubKeys()) {
showLoginMsg(
'Cannot find PubNub keys. Please specify your PubNub keys in keys.js.',
true,
false
)
} else {
try {
pubnub = await createPubNubObject()
} catch (e) {
console.log(e)
}
}
document.getElementById('bottomNav').classList.add('blurred')
document.getElementById('title').classList.add('blurred')
document.getElementById('showcaseGrid').classList.add('blurred')
if (typeof GUIDED_DEMO !== 'undefined' && GUIDED_DEMO === true) { document.getElementById('guided-demo-info').style.display = 'block' }
loginModal = new bootstrap.Modal(document.getElementById('loginModal'), {
keyboard: false,
backdrop: 'static'
})
loginModal.show()
// Avatar logic
var random_array = Array.from({ length: num_avatars }, (x, i) => i)
random_array = shuffle(random_array)
for (var i = 0; i < num_avatars_to_display; i++) {
var avatar = document.getElementById('avatar-' + i)
avatar.src =
'./img/avatar/' + (random_array[i] + 1 + '').padStart(3, '0') + '.png'
if (i == 0) {
selectedAvatar(0, avatar.src)
}
}
document
.getElementById('txtNickname')
.addEventListener('keyup', function (e) {
setEnableButtonState();
})
setTimeout(setEnableButtonState, 1)
// This is the first time the user has logged in, test to see if there is channel metadata
// This is so, on first launch in a new keyset, the keyset can be populated with some default channels
pubnub.objects.getAllChannelMetadata({
include: {customFields: true},
limit: 50,
}).then (async (channels) => {
//console.log(channels)
if (channels.data.length === 0) {
// There ARE NO channels in the keyset, create some public channels
for (var i = 0; i < default_channels.public_channels.length; i++)
{
await pubnub.objects.setChannelMetadata({
channel: default_channels.public_channels[i].channel,
data: {
name: default_channels.public_channels[i].name,
description: default_channels.public_channels[i].description,
custom: {
profileIcon: default_channels.public_channels[i].profileIcon,
info: default_channels.public_channels[i].info
}
}
})
}
}
else{
// There WERE channels in the keyset, do not create default channels
}
})
}
function hideLogin()
{
document.getElementById('bottomNav').classList.remove('blurred')
document.getElementById('title').classList.remove('blurred')
document.getElementById('showcaseGrid').classList.remove('blurred')
if (loginModal !== null)
{
loginModal.hide();
}
}
function shuffle (array) {
var tmp,
current,
top = array.length
if (top)
while (--top) {
current = Math.floor(Math.random() * (top + 1))
tmp = array[current]
array[current] = array[top]
array[top] = tmp
}
return array
}
function setEnableButtonState () {
if (
document.getElementById('txtNickname').value.length == 0
) {
// Disable login button
document.getElementById('btnLogin').classList.add('disabled')
}
else if (!document.getElementById('avatar-5').classList.contains('hidden'))
{
// Enable login button
document.getElementById('btnLogin').classList.remove('disabled')
}
else if (!document.getElementById('imageUploadPane').classList.contains('hidden')) {
// Disable login button
document.getElementById('btnLogin').classList.add('disabled')
}
else if (
document.getElementById('txtNickname').value.length > 0
) {
// Enable login button
document.getElementById('btnLogin').classList.remove('disabled')
}
}
function selectedAvatar (avatarId, source) {
user_avatar = source
var id = 'avatar-' + avatarId
var avatar = document.getElementById(id)
for (var i = 0; (i < num_avatars_to_display + 1); i++) {
var tempId = 'avatar-' + i
if (tempId != id)
document.getElementById(tempId).classList.remove('avatar-selected')
}
avatar.classList.add('avatar-selected')
}
function selectCustomAvatar () {
var input = document.createElement('input')
input.type = 'file'
input.onchange = async e => {
selectedCustomAvatar = e.target.files[0]
if (customAvatarChecks(selectedCustomAvatar)) {
document.getElementById('imageToUploadName').innerText =
selectedCustomAvatar.name
// setting up the reader
var reader = new FileReader()
reader.readAsDataURL(selectedCustomAvatar, 'UTF-8')
reader.onload = readerEvent => {
var content = readerEvent.target.result // this is the content!
document.getElementById('imageToUpload').src = '' + content + ''
}
document.getElementById('imageUploadPane').classList.remove('hidden')
document.getElementById('btnUpload').classList.remove('disabled')
setEnableButtonState()
}
else
{
selectedCustomAvatar = null;
cancelSelectedImage();
}
}
input.click()
}
function cancelSelectedImage() {
document.getElementById('imageUploadPane').classList.add('hidden')
document.getElementById('btnUpload').classList.add('disabled')
setEnableButtonState()
}
function customAvatarChecks (avatarFile) {
if (avatarFile == null) {
showLoginMsg('You have not chosen a custom avatar file', true, true)
return false
} else if (avatarFile.size > MAX_AVATAR_FILE_SIZE) {
showLoginMsg('Your avatar should be under 1MB', true, true)
return false
} else if (
!(
avatarFile.type == 'image/png' ||
avatarFile.type == 'image/jpeg' ||
avatarFile.type == 'image/gif'
)
) {
showLoginMsg('Please choose a JPG, PNG or GIF file', true, true)
return false
}
return true
}
async function uploadCustomAvatar () {
var customAvatar = selectedCustomAvatar
uploadInProgress(true)
try {
// The image is OK, upload it to PubNub
const uploadedFile = await pubnub.sendFile({
channel: 'avatars',
file: customAvatar
})
const fileUrl = await pubnub.getFileUrl({
channel: 'avatars',
id: uploadedFile.id,
name: uploadedFile.name
})
// Upload was successful, test whether we should moderate the image
// IN PRODUCTION: You will want to have a PubNub function with event type 'Before Publish File'
// Then you can analyse and re-route files which have been moderated to follow up later, or
// for archive / record-keeping purposes
var response = await fetch("https://ps.pndsn.com/v1/blocks/sub-key/" + subscribe_key + "/moderate?" + new URLSearchParams({
url: fileUrl
}), {
method: 'GET'
})
if (response.ok)
{
// If response was not ok, the app was probably run against a custom keyset, in which case ignore moderation
response = await response.text()
if (response != "okay")
{
throw new Error ('Moderation failed')
}
}
var avatar = document.getElementById('avatar-5')
avatar.classList.remove('hidden')
avatar.src = fileUrl
selectedAvatar(5, avatar.src)
setEnableButtonState()
showLoginMsg("Image upload completed", false, true)
uploadInProgress(false)
} catch (err) {
showLoginMsg('Error uploading custom avatar for moderation. Try another image', true, true)
console.log('Error uploading custom avatar: ' + err)
uploadInProgress(false)
}
}
function uploadInProgress (inProgress) {
var spinner = document.getElementById('imageUploadSpinner')
var imageCancelIcon = document.getElementById('imageUploadCancel')
if (inProgress) {
imageCancelIcon.style.display = 'none'
spinner.style.display = 'inline-block'
} else {
imageCancelIcon.style.display = 'flex'
spinner.style.display = 'none'
}
}
async function login () {
var nickname = document.getElementById('txtNickname').value
var avatarUrl = user_avatar
sessionStorage.setItem('nickname', nickname)
sessionStorage.setItem('avatarUrl', avatarUrl)
const result = await pubnub.objects.setUUIDMetadata({
data: {
name: nickname,
profileUrl: avatarUrl
}
})
hideLogin();
}
function showLoginMsg (msg, isError, shouldFade) {
document.getElementById('login-message').style.display = 'block'
if (isError) {
document
.getElementById('login-message')
.classList.add('login-message-error')
document
.getElementById('login-message')
.classList.remove('login-message-success')
document
.getElementById('login-message-icon')
.classList.add('login-message-icon-error')
document
.getElementById('login-message-icon')
.classList.remove('login-message-icon-success')
} else {
document
.getElementById('login-message')
.classList.remove('login-message-error')
document
.getElementById('login-message')
.classList.add('login-message-success')
document
.getElementById('login-message-icon')
.classList.remove('login-message-icon-error')
document
.getElementById('login-message-icon')
.classList.add('login-message-icon-success')
}
document.getElementById('login-message-text').innerText = msg
if (shouldFade) {
setTimeout(function () {
document.getElementById('login-message').style.display = 'none'
}, 3000)
}
}