-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogged-in.js
597 lines (463 loc) · 25.1 KB
/
logged-in.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
function getAuthedUserID() {
// Check finnish_mastodon_user_authed_id from local storage if it's not there
if (localStorage.getItem('finnish_mastodon_users_authed_user_id') !== undefined && localStorage.getItem('finnish_mastodon_users_authed_user_acct') !== undefined && localStorage.getItem('finnish_mastodon_users_access_token')) {
// Get authed_user_instance from local storage
authed_user_instance_url = localStorage.getItem('finnish_mastodon_user_authed_instance_url');
// Get access token from local storage
access_token = localStorage.getItem('finnish_mastodon_users_access_token');
// Get authed user's ID
fetch(`${authed_user_instance_url}/api/v1/accounts/verify_credentials`, {
headers: {
'Authorization': `Bearer ${access_token}`
},
})
.then(response => response.json())
.then(json_me => {
// Save authed user's ID to local storage
localStorage.setItem('finnish_mastodon_users_authed_user_id', json_me.id);
localStorage.setItem('finnish_mastodon_users_authed_user_acct', json_me.acct);
localStorage.setItem('finnish_mastodon_users_authed_user_url', json_me.url);
// Get instance from URL in localStorage
localStorage.setItem('finnish_mastodon_users_authed_user_instance', json_me.url.split('/')[2]);
});
}
}
function checkMe() {
// Define user list li
let user_list_li = document.querySelectorAll('.user-list li');
// Get authed user acct
let authed_user = localStorage.getItem('finnish_mastodon_users_authed_user_acct') + '@' + localStorage.getItem('finnish_mastodon_users_authed_user_instance');
// Loop through user_list_li
for (let i = 0; i < user_list_li.length; i++) {
// Based on [data-user-name] and [data-user-instance], find me
let user = user_list_li[i].getAttribute('data-user-name') + '@' + user_list_li[i].getAttribute('data-user-instance');
// If it's me, add class
if ( authed_user === user ) {
// Replace .button-action with a
if ( user_list_li[i].querySelector('.button-action') !== null ) {
user_list_li[i].querySelector('.button-action').outerHTML = `<a href="${localStorage.getItem('finnish_mastodon_user_authed_instance_url')}/settings/profile" class="button button-action button-me">Muokkaa profiilia</a>`;
}
}
}
}
function preCheckUsers() {
// Get users from local storage
let listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users')) || [];
// Count all users that have finnish_mastodon_user_follow_status_checked_at_' + listedUsers[i].id set in localStorage
let usersChecked = 0;
// Reset users to be checked first
localStorage.setItem('finnish_mastodon_users_to_be_checked', JSON.stringify([]));
// Check me
checkMe();
// Loop through listedUsers
for (let i = 0; i < listedUsers.length; i++) {
// Get authed user acct
let authed_user = localStorage.getItem('finnish_mastodon_users_authed_user_acct') + '@' + localStorage.getItem('finnish_mastodon_users_authed_user_instance');
// Get listed user acct
let listed_user = listedUsers[i].acct + '@' + listedUsers[i].instance;
// Check if user has been checked
if ( localStorage.getItem(`finnish_mastodon_user_follow_status_checked_at_${listedUsers[i].id}`) !== null ) {
usersChecked++;
// If it's not me, save it to local storage
if ( authed_user !== listed_user ) {
localStorage.setItem('finnish_mastodon_users_checked_amount', usersChecked);
}
}
// Check not checked yet amount
if ( localStorage.getItem('finnish_mastodon_user_follow_status_checked_at_' + listedUsers[i].id) === null ) {
console.log('Quickly pre-checked user, we have not checked yet: ' + listedUsers[i].acct);
// Define amount of users to be checked
let usersToBeCheckedAmount = localStorage.getItem('finnish_mastodon_users_count') - localStorage.getItem('finnish_mastodon_users_checked_amount');
// Save users not checked amount to local storage
if ( authed_user !== listed_user ) {
localStorage.setItem('finnish_mastodon_users_not_checked_amount', usersToBeCheckedAmount);
}
// Define users to be checked list
usersToBeChecked = JSON.parse(localStorage.getItem('finnish_mastodon_users_to_be_checked')) || [];
// Get users we are not following and add them to a local storage
usersToBeChecked.push({
id: listedUsers[i].id,
acct: listedUsers[i].acct,
instance: listedUsers[i].instance
});
// Get current user count
let currentUserCount = i - localStorage.getItem('finnish_mastodon_users_checked_amount');
// Add user to local storage if the number of users to be checked is more than the max amount of users to be checked
if (currentUserCount <= usersToBeCheckedAmount) {
localStorage.setItem('finnish_mastodon_users_to_be_checked', JSON.stringify(usersToBeChecked));
}
}
// Transform checked at time to a format we can compare to Date.now()
let checkedAt = new Date(localStorage.getItem(`finnish_mastodon_user_follow_status_checked_at_${listedUsers[i].id}`)).getTime();
// Check again if hour has passed since last check
if ( localStorage.getItem(`finnish_mastodon_user_follow_status_checked_at_${listedUsers[i].id}`) !== null && checkedAt < Date.now() - 3600000 ) {
console.log('Quickly pre-checked user, we have not checked in an hour: ' + listedUsers[i].acct);
// Define amount of users to be checked
let usersToBeCheckedAmount = localStorage.getItem('finnish_mastodon_users_count') - localStorage.getItem('finnish_mastodon_users_checked_amount');
// Save users not checked amount to local storage
if ( authed_user !== listed_user ) {
localStorage.setItem('finnish_mastodon_users_not_checked_amount', usersToBeCheckedAmount);
}
// Define users to be checked list
usersToBeChecked = JSON.parse(localStorage.getItem('finnish_mastodon_users_to_be_checked')) || [];
// Get users we are not following and add them to a local storage
usersToBeChecked.push({
id: listedUsers[i].id,
acct: listedUsers[i].acct,
instance: listedUsers[i].instance
});
// Get current user count
let currentUserCount = i - localStorage.getItem('finnish_mastodon_users_checked_amount');
// Add user to local storage if the number of users to be checked is more than the max amount of users to be checked
if (currentUserCount <= usersToBeCheckedAmount) {
localStorage.setItem('finnish_mastodon_users_to_be_checked', JSON.stringify(usersToBeChecked));
}
} else {
// Check if user we are following the user
if ( localStorage.getItem(`finnish_mastodon_user_follow_status_${listedUsers[i].id}`) === 'true' ) {
console.log('Quickly pre-checked user, we are following: ' + listedUsers[i].acct);
// Add following class to user
if ( document.getElementById('user-'+ listedUsers[i].id) ) {
document.getElementById('user-'+ listedUsers[i].id).classList.add('following');
}
// Set following count to local storage
followingCount = document.getElementsByClassName('following').length;
localStorage.setItem('finnish_mastodon_users_following_count', followingCount);
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
// Remove has-no-action class from button
document.getElementById('button-action-'+ listedUsers[i].id).classList.remove('has-no-action');
// Add has-unfollow-action class to button
document.getElementById('button-action-'+ listedUsers[i].id).classList.add('has-unfollow-action');
// Update text to button
document.getElementById('button-action-'+ listedUsers[i].id).innerHTML = 'Lopeta seuraaminen';
}
} else if ( localStorage.getItem(`finnish_mastodon_user_follow_status_${listedUsers[i].id}`) === 'false' ) {
console.log('Quickly pre-cheked user, we are not following: ' + listedUsers[i].acct);
// Remove following class from user
document.getElementById('user-'+ listedUsers[i].id).classList.remove('following');
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
// Remove has-unfollow-action class from button
document.getElementById('button-action-'+ listedUsers[i].id).classList.remove('has-unfollow-action');
// Add has-follow-action class to button
document.getElementById('button-action-'+ listedUsers[i].id).classList.add('has-follow-action');
// Update text to button
document.getElementById('button-action-'+ listedUsers[i].id).innerHTML = 'Seuraa';
}
}
}
}
}
function lookupUsers() {
// Get users from local storage
let listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users')) || [];
// If users to be checked are in local storage
if ( localStorage.getItem('finnish_mastodon_users_to_be_checked') ) {
listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users_to_be_checked'));
} else {
// If users to be checked are not in local storage, get users from local storage
listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users'));
}
// Get milliseconds for 300 requests per 5 minutes
let milliSeondsBetweenUsers = 400 * 60 * 5 / 300;
// Get authed_user_instance from local storage
authed_user_instance_url = localStorage.getItem('finnish_mastodon_user_authed_instance_url');
// All endpoints and methods can be called 300 times within 5 minutes
// Run for loop one per second
for (let i = 0; i < listedUsers.length; i++) {
setTimeout(function() {
// Add styles to see which user is being checked
document.querySelectorAll('[data-user-name="'+ listedUsers[i].acct + '"]').forEach(function(element) {
// Add class and then remove it after 1 second
element.classList.add('checking-user');
setTimeout(function() {
element.classList.remove('checking-user');
}, 800);
});
// Update #heading-users-title title when checking users
document.getElementById('heading-users-title').innerHTML = `Tarkistetaan ketä seurataan...`;
// Get user amount
userAmount = document.querySelectorAll('.account-card').length;
// If there's users to be checked
if ( localStorage.getItem('finnish_mastodon_users_to_be_checked') ) {
// Count amount of users to be checked
userAmount = JSON.parse(localStorage.getItem('finnish_mastodon_users_to_be_checked')).length - 1;
}
// Add class checking to heading-users-title and user-count
document.getElementById('heading-users-title').classList.add('checking');
document.getElementById('user-count').classList.add('checking');
// Update #user-count title when checking users
document.getElementById('user-count').innerHTML = `${i}/${userAmount}`;
// Restore title to "Users" and user count to the original number when userAmount is same than calculated amount
if (i == userAmount) {
// Restore full user amount
userAmount = document.querySelectorAll('.account-card').length;
document.getElementById('heading-users-title').innerHTML = `Käyttäjät`;
document.getElementById('user-count').innerHTML = `${userAmount}`;
document.getElementById('heading-users-title').classList.remove('checking');
document.getElementById('user-count').classList.remove('checking');
}
// First use look up endpoint to webfinger
fetch(`${authed_user_instance_url}/api/v1/accounts/lookup?acct=${listedUsers[i].acct}@${listedUsers[i].instance}`)
.then(response => response.json())
.then(json => {
// Get access token from local storage
access_token = localStorage.getItem('finnish_mastodon_users_access_token');
// Then check following status from relationship endpoint
fetch(`${authed_user_instance_url}/api/v1/accounts/relationships?id[]=${json.id}`, {
headers: {
'Authorization': `Bearer ${access_token}`
},
})
.then(response => response.json())
.then(json_relationship => {
// Save checked_at time to local storage as separate item
localStorage.setItem('finnish_mastodon_user_follow_status_checked_at_'+ listedUsers[i].id, moment().format());
// Check if following is true
if ( json_relationship[0].following === true ) {
console.log('Checked relationship, found an user we are following: ' + listedUsers[i].acct);
// Update data-follow-id with relationship json id
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
document.getElementById('button-action-'+ listedUsers[i].id).setAttribute('data-follow-id', json_relationship[0].id);
}
// Update id with relationship json id
if (document.getElementById('user-'+ listedUsers[i].id)) {
document.getElementById('user-'+ listedUsers[i].id).setAttribute('id', 'user-'+ json_relationship[0].id);
}
// Add following class to user
// Remove following class from user that has the correct [data-user-name]
document.querySelectorAll('[data-user-name="'+ listedUsers[i].acct + '"]').forEach(function(element) {
element.classList.add('following');
});
// Remove has-no-action class from button
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
document.getElementById('button-action-'+ listedUsers[i].id).classList.remove('has-no-action');
// Add has-unfollow-action class to button
document.getElementById('button-action-'+ listedUsers[i].id).classList.add('has-unfollow-action');
// Update text to button
document.getElementById('button-action-'+ listedUsers[i].id).innerHTML = 'Lopeta seuraaminen';
}
// Add separate localStorage for this particular user so that we are following them
localStorage.setItem('finnish_mastodon_user_follow_status_'+ listedUsers[i].id, 'true');
} else {
console.log('Checked relationship, found an user we are not following: ' + listedUsers[i].acct);
// Timestamp checked
listedUsers[i].checked_at = moment().format();
// Update data-follow-id with relationship json id
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
document.getElementById('button-action-'+ listedUsers[i].id).setAttribute('data-follow-id', json_relationship[0].id);
}
// Update id with relationship json id
document.getElementById('user-'+ listedUsers[i].id).setAttribute('id', 'user-'+ json_relationship[0].id);
// Remove following class from user that has the correct [data-user-name]
document.querySelectorAll('[data-user-name="'+ listedUsers[i].acct + '"]').forEach(function(element) {
element.classList.remove('following');
});
if (document.getElementById('button-action-'+ listedUsers[i].id)) {
// Add has-no-action class to button
document.getElementById('button-action-'+ listedUsers[i].id).classList.remove('has-no-action');
// Remove has-unfollow-action class from button
document.getElementById('button-action-'+ listedUsers[i].id).classList.remove('has-unfollow-action');
// Add follow action to button
document.getElementById('button-action-'+ listedUsers[i].id).classList.add('has-follow-action');
// Update text to button
document.getElementById('button-action-'+ listedUsers[i].id).innerHTML = 'Seuraa';
}
// Add separate localStorage for this particular user so that we are following them
localStorage.setItem('finnish_mastodon_user_follow_status_'+ listedUsers[i].id, 'false');
}
// Get authed user instance URL just in case
let authed_user_instance_url = localStorage.getItem('finnish_mastodon_user_authed_instance_url');
// Get authed user acct
let authed_user = localStorage.getItem('finnish_mastodon_users_authed_user_acct') + '@' + localStorage.getItem('finnish_mastodon_users_authed_user_instance');
// Get listed user acct
let listed_user = listedUsers[i].acct + '@' + listedUsers[i].instance;
// If it's me, replace button with profile edit link
if (listed_user == authed_user) {
// Get .button-action under [data-user-name]
document.querySelectorAll('[data-user-name="'+ listedUsers[i].acct + '"] .account-card__actions__button').forEach(function(element) {
// Replace button with a
element.innerHTML = '<a href="'+ authed_user_instance_url +'/settings/profile" class="button button-action button-me">Muokkaa profiilia</a>';
});
}
});
});
}, milliSeondsBetweenUsers * i);
}
}
// Window load event used just in case window height is dependant upon images
window.addEventListener('load', function() {
// Get access_token from local storage
access_token = localStorage.getItem('finnish_mastodon_users_access_token');
if ( access_token) {
// Update #heading-users-title title when checking users
document.getElementById('heading-users-title').innerHTML = `Lasketaan käyttäjät...`;
// Get authed user's ID after one second
setTimeout(() => {
console.log('Launching authed user verification...');
getAuthedUserID();
}, 2000);
// After a few seconds, pre-check users
setTimeout(function() {
console.log('Launching pre check...');
preCheckUsers();
}, 3000);
// After a few seconds, lookup users
setTimeout(function() {
console.log('Launching user lookup...');
lookupUsers();
}, 5000);
// Restore users title
setTimeout(() => {
document.getElementById('heading-users-title').innerHTML = `Käyttäjät`;
}, 3000);
}
});
// Follow action
function followAction(e) {
// Get authed_user_instance from local storage
authed_user_instance_url = localStorage.getItem('finnish_mastodon_user_authed_instance_url');
// Get account ID from button
const account_id = e.target.getAttribute("data-follow-id");
// Follow
fetch(`${authed_user_instance_url}/api/v1/accounts/${account_id}/follow`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
}
})
.then(response => response.json())
.then(json => {
if ( json.error === "Record not found" ) {
let url = e.target.getAttribute("data-url");
// Open to new window
window.open(url, '_blank');
}
// Add following class to account-card that has the correct data-user-name
document.getElementById(`user-${json.id}`).classList.add('following');
// Calculate the amount of users we're following
let followingCount = document.getElementsByClassName('following').length;
// Add following count to local storage
localStorage.setItem('finnish_mastodon_users_following_count', followingCount);
// Change button element inside user-${json.id} .account-card__actions__button
document.getElementById(`user-${json.id}`).querySelector('.account-card__actions__button').innerHTML = `<button class="button button-action has-unfollow-action" data-follow-id="${json.id}" data-url="${json.url}">Lopeta seuraaminen</button>`;
// Get users from local storage
let listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users')) || [];
// Change listedUser following value to true
listedUsers.forEach(function(listedUser) {
if ( listedUser.id === json.id ) {
localStorage.setItem('finnish_mastodon_user_follow_status_'+ listedUser.id, 'true');
}
});
});
}
// unFollowAction
function unFollowAction(e) {
// Get authed_user_instance from local storage
authed_user_instance_url = localStorage.getItem('finnish_mastodon_user_authed_instance_url');
// Get account ID from button
const account_id = e.target.getAttribute("data-follow-id");
// Follow
fetch(`${authed_user_instance_url}/api/v1/accounts/${account_id}/unfollow`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
}
})
.then(response => response.json())
.then(json => {
if ( json.error === "Record not found" ) {
let url = e.target.getAttribute("data-url");
// Open to new window
window.open(url, '_blank');
}
// Add following class to account-card
document.getElementById(`user-${json.id}`).classList.remove('following');
// Calculate the amount of users we're following
let followingCount = document.getElementsByClassName('following').length;
// Add following count to local storage
localStorage.setItem('finnish_mastodon_users_following_count', followingCount);
// Change button element inside user-${json.id} .account-card__actions__button
document.getElementById(`user-${json.id}`).querySelector('.account-card__actions__button').innerHTML = `<button class="button button-action has-follow-action" data-follow-id="${json.id}" data-url="${json.url}">Seuraa</button>`;
// Get users from local storage
let listedUsers = JSON.parse(localStorage.getItem('finnish_mastodon_users')) || [];
// Change listedUser following value to false
listedUsers.forEach(function(listedUser) {
if ( listedUser.id === json.id ) {
localStorage.setItem('finnish_mastodon_user_follow_status_'+ listedUser.id, 'false');
}
});
});
}
function filterFollowedUsers() {
// Get following count from local storage
let followingCount = localStorage.getItem('finnish_mastodon_users_following_count');
// Get filterFollowed checkbox
let filterFollowed = document.getElementById('filter-followed');
// If filterFollowed is checked, filter out users we're already following
if ( filterFollowed.checked ) {
// Re-calculate the amount of users we're following
followingCount = document.getElementsByClassName('following').length;
localStorage.setItem('finnish_mastodon_users_following_count', followingCount);
// Hide all elements that have a following class
let following = document.getElementsByClassName('following');
for (let i = 0; i < following.length; i++) {
// Add hidden attribute to hide the element
following[i].setAttribute('hidden', 'hidden');
// Update user count by substracting amount from finnish_mastodon_users_following_count local storage
if ( !document.getElementById('user-count').classList.contains('checking') ) {
const followingCount = localStorage.getItem('finnish_mastodon_users_following_count');
const totalUsercount = localStorage.getItem('finnish_mastodon_users_count');
const userCount = document.getElementById('user-count');
userCount.innerHTML = totalUsercount - followingCount;
}
}
} else {
// Show all elements that have a following class
let following = document.getElementsByClassName('following');
for (let i = 0; i < following.length; i++) {
// Remove hidden attribute to show the element
following[i].removeAttribute('hidden');
// Restore user count number to the original number
if ( !document.getElementById('user-count').classList.contains('checking') ) {
const totalUsercount = localStorage.getItem('finnish_mastodon_users_count');
const userCount = document.getElementById('user-count');
userCount.innerHTML = totalUsercount;
}
}
}
}
// DOMContentLoaded event used to make sure all HTML is loaded
document.addEventListener('DOMContentLoaded', function() {
// Get access token from local storage
access_token = localStorage.getItem('finnish_mastodon_users_access_token');
// Delay, otherwise won't have time to get access token from local storage
setTimeout(function() {
if ( access_token ) {
// Remove hidden from #filter-followed-container
document.getElementById('filter-followed-container').removeAttribute('hidden');
// Add .has-filtering class to .heading class
document.querySelector('.heading').classList.add('has-filtering');
// Listener for filter-followed checkbox
document.getElementById('filter-followed').addEventListener('change', function() {
filterFollowedUsers();
});
}
}, 1000);
// If button has has-no-action class, add event listener to open profile in new window
document.addEventListener("click", function(e) {
if (e.target && e.target.classList.contains("has-no-action")) {
let url = e.target.getAttribute("data-url");
// Open to new window
window.open(url, '_blank');
}
// Follow and unfollow actions
if (e.target && e.target.classList.contains("has-follow-action")) {
followAction(e);
}
if (e.target && e.target.classList.contains("has-unfollow-action")) {
unFollowAction(e);
}
});
});