-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpost_component.js
586 lines (461 loc) · 16.9 KB
/
post_component.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
/**
* Renders a post/thread view and its subviews.
*/
class PostComponent {
/**
Contexts:
- thread - a post in the thread tree
- parent - parent reference above the thread root
- quote - a quote embed
- quotes - a post on the quotes page
- feed - a post on the hashtag feed page
@typedef {'thread' | 'parent' | 'quote' | 'quotes' | 'feed'} PostContext
@param {AnyPost} post, @param {PostContext} context
*/
constructor(post, context) {
this.post = /** @type {Post}, TODO */ (post);
this.context = context;
}
/** @returns {boolean} */
get isRoot() {
return this.post.isRoot;
}
/** @returns {string} */
get linkToAuthor() {
return 'https://bsky.app/profile/' + this.post.author.handle;
}
/** @returns {string} */
get linkToPost() {
return this.linkToAuthor + '/post/' + this.post.rkey;
}
/** @returns {string} */
get didLinkToAuthor() {
let { repo } = atURI(this.post.uri);
return `https://bsky.app/profile/${repo}`;
}
/** @returns {string} */
get didLinkToPost() {
let { repo, rkey } = atURI(this.post.uri);
return `https://bsky.app/profile/${repo}/post/${rkey}`;
}
/** @returns {string} */
get authorName() {
if (this.post.author.displayName) {
return this.post.author.displayName;
} else if (this.post.author.handle.endsWith('.bsky.social')) {
return this.post.author.handle.replace(/\.bsky\.social$/, '');
} else {
return this.post.author.handle;
}
}
/** @returns {json} */
get timeFormatForTimestamp() {
if (this.context == 'quotes' || this.context == 'feed') {
return { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' };
} else if (this.isRoot || this.context != 'thread') {
return { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' };
} else if (this.post.pageRoot && !sameDay(this.post.createdAt, this.post.pageRoot.createdAt)) {
return { day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric' };
} else {
return { hour: 'numeric', minute: 'numeric' };
}
}
/** @returns {AnyElement} */
buildElement() {
let div = $tag('div.post');
if (this.post.muted) {
div.classList.add('muted');
}
if (this.post instanceof BlockedPost) {
this.buildBlockedPostElement(div);
return div;
} else if (this.post instanceof DetachedQuotePost) {
this.buildDetachedQuoteElement(div);
return div;
} else if (this.post instanceof MissingPost) {
this.buildMissingPostElement(div);
return div;
}
let header = this.buildPostHeader();
div.appendChild(header);
let content = $tag('div.content');
if (this.context == 'thread' && !this.isRoot) {
let edge = $tag('div.edge');
let line = $tag('div.line');
edge.appendChild(line);
div.appendChild(edge);
let plus = $tag('img.plus', { src: 'icons/subtract-square.png' });
div.appendChild(plus);
[edge, plus].forEach(x => {
x.addEventListener('click', (e) => {
e.preventDefault();
this.toggleSectionFold(div);
});
});
}
let wrapper;
if (this.post.muted) {
let details = $tag('details');
let summary = $tag('summary');
summary.innerText = this.post.muteList ? `Muted (${this.post.muteList})` : 'Muted - click to show';
details.appendChild(summary);
content.appendChild(details);
wrapper = details;
} else {
wrapper = content;
}
let p = this.buildPostBody();
wrapper.appendChild(p);
if (this.post.embed) {
let embed = new EmbedComponent(this.post, this.post.embed).buildElement();
wrapper.appendChild(embed);
}
if (this.post.likeCount !== undefined && this.post.repostCount !== undefined) {
let stats = this.buildStatsFooter();
wrapper.appendChild(stats);
}
if (this.post.replies.length == 1 && this.post.replies[0].author?.did == this.post.author.did) {
let component = new PostComponent(this.post.replies[0], 'thread');
let element = component.buildElement();
element.classList.add('flat');
content.appendChild(element);
} else {
for (let reply of this.post.replies) {
if (reply instanceof MissingPost) { continue }
if (reply instanceof BlockedPost && window.biohazardEnabled === false) { continue }
let component = new PostComponent(reply, 'thread');
content.appendChild(component.buildElement());
}
}
if (this.context == 'thread') {
if (this.post.hasMoreReplies) {
let loadMore = this.buildLoadMoreLink();
content.appendChild(loadMore);
} else if (this.post.hasHiddenReplies && window.biohazardEnabled !== false) {
let loadMore = this.buildHiddenRepliesLink();
content.appendChild(loadMore);
}
}
div.appendChild(content);
return div;
}
/** @returns {AnyElement} */
buildPostHeader() {
let timeFormat = this.timeFormatForTimestamp;
let formattedTime = this.post.createdAt.toLocaleString(window.dateLocale, timeFormat);
let isoTime = this.post.createdAt.toISOString();
let h = $tag('h2');
h.innerHTML = `${escapeHTML(this.authorName)} `;
if (this.post.isFediPost) {
let handle = this.post.authorFediHandle;
h.innerHTML += `<a class="handle" href="${this.linkToAuthor}" target="_blank">@${handle}</a> ` +
`<img src="icons/mastodon.svg" class="mastodon"> `;
} else {
h.innerHTML += `<a class="handle" href="${this.linkToAuthor}" target="_blank">@${this.post.author.handle}</a> `;
}
h.innerHTML += `<span class="separator">•</span> ` +
`<a class="time" href="${this.linkToPost}" target="_blank" title="${isoTime}">${formattedTime}</a> `;
if (this.post.replyCount > 0 && !this.isRoot || ['quote', 'quotes', 'feed'].includes(this.context)) {
h.innerHTML +=
`<span class="separator">•</span> ` +
`<a href="${linkToPostThread(this.post)}" class="action" title="Load this subtree">` +
`<i class="fa-solid fa-arrows-split-up-and-left fa-rotate-180"></i></a> `;
}
if (this.post.muted) {
h.prepend($tag('i', 'missing fa-regular fa-circle-user fa-2x'));
} else if (this.post.author.avatar) {
h.prepend(this.buildUserAvatar(this.post.author.avatar));
} else {
h.prepend($tag('i', 'missing fa-regular fa-face-smile fa-2x'));
}
return h;
}
/** @param {string} url, @returns {HTMLImageElement} */
buildUserAvatar(url) {
let avatar = $tag('img.avatar', { src: url });
let tries = 0;
let errorHandler = function(e) {
if (tries < 3) {
tries++;
setTimeout(() => { avatar.src = url }, Math.random() * 5 * tries);
} else {
avatar.removeEventListener('error', errorHandler);
}
};
avatar.addEventListener('error', errorHandler);
return avatar;
}
/** @returns {AnyElement} */
buildPostBody() {
if (this.post.originalFediContent) {
return $tag('div.body', { html: sanitizeHTML(this.post.originalFediContent) });
}
let p = $tag('p.body');
let richText = new RichText({ text: this.post.text, facets: this.post.facets });
for (let seg of richText.segments()) {
if (seg.mention) {
p.append($tag('a', { href: `https://bsky.app/profile/${seg.mention.did}`, text: seg.text }));
} else if (seg.link) {
p.append($tag('a', { href: seg.link.uri, text: seg.text }));
} else if (seg.tag) {
let url = new URL(getLocation());
url.searchParams.set('hash', seg.tag.tag);
p.append($tag('a', { href: url.toString(), text: seg.text }));
} else if (seg.text.includes("\n")) {
let span = $tag('span', { text: seg.text });
span.innerHTML = span.innerHTML.replaceAll("\n", "<br>");
p.append(span);
} else {
p.append(seg.text);
}
}
if (this.post.isTruncatedFediPost) {
if (this.post.embed && ('url' in this.post.embed) && typeof this.post.embed.url == 'string') {
let link = this.buildLoadFediPostLink(this.post.embed.url, p);
p.append(' ', link);
}
}
return p;
}
/** @returns {AnyElement} */
buildStatsFooter() {
let stats = $tag('p.stats');
let span = $tag('span');
let heart = $tag('i', 'fa-solid fa-heart ' + (this.post.liked ? 'liked' : ''));
heart.addEventListener('click', (e) => this.onHeartClick(heart));
span.append(heart, ' ', $tag('output', { text: this.post.likeCount }));
stats.append(span);
if (this.post.repostCount > 0) {
let span = $tag('span', { html: `<i class="fa-solid fa-retweet"></i> ${this.post.repostCount}` });
stats.append(span);
}
return stats;
}
/** @param {string} originalURL, @param {HTMLElement} p, @returns {AnyElement} */
buildLoadFediPostLink(originalURL, p) {
let link = $tag('a', {
href: originalURL,
text: "(Load full post)"
});
link.addEventListener('click', (e) => {
e.preventDefault();
link.remove();
this.loadFediPost(originalURL, p);
});
return link;
}
/** @returns {AnyElement} */
buildLoadMoreLink() {
let loadMore = $tag('p');
let link = $tag('a', {
href: linkToPostThread(this.post),
text: "Load more replies…"
});
link.addEventListener('click', (e) => {
e.preventDefault();
loadMore.innerHTML = `<img class="loader" src="icons/sunny.png">`;
loadSubtree(this.post, loadMore.closest('.post'));
});
loadMore.appendChild(link);
return loadMore;
}
/** @returns {AnyElement} */
buildHiddenRepliesLink() {
let loadMore = $tag('p.hidden-replies');
let link = $tag('a', {
href: linkToPostThread(this.post),
text: "Load hidden replies…"
});
link.addEventListener('click', (e) => {
e.preventDefault();
if (window.biohazardEnabled === true) {
this.loadHiddenReplies(loadMore);
} else {
window.loadInfohazard = () => this.loadHiddenReplies(loadMore);
showDialog($id('biohazard_dialog'));
}
});
loadMore.append("☣️ ", link);
return loadMore;
}
/** @param {HTMLLinkElement} loadMoreButton */
loadHiddenReplies(loadMoreButton) {
loadMoreButton.innerHTML = `<img class="loader" src="icons/sunny.png">`;
loadHiddenSubtree(this.post, loadMoreButton.closest('.post'));
}
/** @param {HTMLLinkElement} authorLink */
loadReferencedPostAuthor(authorLink) {
let did = atURI(this.post.uri).repo;
api.fetchHandleForDid(did).then(handle => {
if (this.post.author) {
this.post.author.handle = handle;
} else {
this.post.author = { did, handle };
}
authorLink.href = this.linkToAuthor;
authorLink.innerText = `@${handle}`;
});
}
/** @param {AnyElement} div, @returns {AnyElement} */
buildBlockedPostElement(div) {
let p = $tag('p.blocked-header');
p.innerHTML = `<i class="fa-solid fa-ban"></i> <span>Blocked post</span>`;
if (window.biohazardEnabled === false) {
div.appendChild(p);
div.classList.add('blocked');
return p;
}
let blockStatus = this.post.blockedByUser ? 'has blocked you' : this.post.blocksUser ? "you've blocked them" : '';
blockStatus = blockStatus ? `, ${blockStatus}` : '';
let authorLink = $tag('a', { href: this.didLinkToAuthor, target: '_blank', text: 'see author' });
p.append(' (', authorLink, blockStatus, ') ');
div.appendChild(p);
this.loadReferencedPostAuthor(authorLink);
let loadPost = $tag('p.load-post');
let a = $tag('a', { href: '#', text: "Load post…" });
a.addEventListener('click', (e) => {
e.preventDefault();
loadPost.innerHTML = ' ';
this.loadBlockedPost(this.post.uri, div);
});
loadPost.appendChild(a);
div.appendChild(loadPost);
div.classList.add('blocked');
return div;
}
/** @param {AnyElement} div, @returns {AnyElement} */
buildDetachedQuoteElement(div) {
let p = $tag('p.blocked-header');
p.innerHTML = `<i class="fa-solid fa-ban"></i> <span>Hidden quote</span>`;
if (window.biohazardEnabled === false) {
div.appendChild(p);
div.classList.add('blocked');
return p;
}
let authorLink = $tag('a', { href: this.didLinkToAuthor, target: '_blank', text: 'see author' });
p.append(' (', authorLink, ') ');
div.appendChild(p);
this.loadReferencedPostAuthor(authorLink);
let loadPost = $tag('p.load-post');
let a = $tag('a', { href: '#', text: "Load post…" });
a.addEventListener('click', (e) => {
e.preventDefault();
loadPost.innerHTML = ' ';
this.loadBlockedPost(this.post.uri, div);
});
loadPost.appendChild(a);
div.appendChild(loadPost);
div.classList.add('blocked');
return div;
}
/** @param {AnyElement} div, @returns {AnyElement} */
buildMissingPostElement(div) {
let p = $tag('p.blocked-header');
p.innerHTML = `<i class="fa-solid fa-ban"></i> <span>Deleted post</span>`;
let authorLink = $tag('a', { href: this.didLinkToAuthor, target: '_blank', text: 'see author' });
p.append(' (', authorLink, ') ');
this.loadReferencedPostAuthor(authorLink);
div.appendChild(p);
div.classList.add('blocked');
return div;
}
/** @param {string} uri, @param {AnyElement} div, @returns Promise<void> */
async loadBlockedPost(uri, div) {
let record = await appView.loadPostIfExists(this.post.uri);
if (!record) {
let post = new MissingPost({ uri: this.post.uri });
let postView = new PostComponent(post, 'quote').buildElement();
div.replaceWith(postView);
return;
}
this.post = new Post(record);
let userView = await api.getRequest('app.bsky.actor.getProfile', { actor: this.post.author.did });
if (!userView.viewer || !(userView.viewer.blockedBy || userView.viewer.blocking)) {
let { repo, rkey } = atURI(this.post.uri);
let a = $tag('a', {
href: linkToPostById(repo, rkey),
className: 'action',
title: "Load thread",
html: `<i class="fa-solid fa-arrows-split-up-and-left fa-rotate-180"></i>`
});
let header = div.querySelector('p.blocked-header');
let separator = $tag('span.separator', { html: '•' });
header.append(separator, ' ', a);
}
div.querySelector('p.load-post').remove();
if (this.isRoot && this.post.parentReference) {
let { repo, rkey } = atURI(this.post.parentReference.uri);
let url = linkToPostById(repo, rkey);
let handle = api.findHandleByDid(repo);
let link = handle ? `See parent post (@${handle})` : "See parent post";
let p = $tag('p.back', { html: `<i class="fa-solid fa-reply"></i><a href="${url}">${link}</a>` });
div.appendChild(p);
}
let p = this.buildPostBody();
div.appendChild(p);
if (this.post.embed) {
let embed = new EmbedComponent(this.post, this.post.embed).buildElement();
div.appendChild(embed);
// TODO
Array.from(div.querySelectorAll('a.link-card')).forEach(x => x.remove());
}
}
/** @param {string} url, @param {HTMLElement} p, @returns Promise<void> */
async loadFediPost(url, p) {
let host = new URL(url).host;
let postId = url.replace(/\/$/, '').split('/').reverse()[0];
let statusURL = `https://${host}/api/v1/statuses/${postId}`;
let response = await fetch(statusURL);
let json = await response.json();
if (json.content) {
let div = $tag('div.body', { html: sanitizeHTML(json.content) });
p.replaceWith(div);
}
}
/** @param {AnyElement} div */
toggleSectionFold(div) {
let plus = div.querySelector('.plus');
if (div.classList.contains('collapsed')) {
div.classList.remove('collapsed');
plus.src = 'icons/subtract-square.png'
} else {
div.classList.add('collapsed');
plus.src = 'icons/add-square.png'
}
}
/** @param {AnyElement} heart */
onHeartClick(heart) {
if (!this.post.hasViewerInfo) {
if (accountAPI.isLoggedIn) {
accountAPI.loadPost(this.post.uri).then(data => {
this.post = new Post(data);
if (this.post.liked) {
heart.classList.add('liked');
} else {
this.onHeartClick(heart);
}
}).catch(error => {
console.log(error);
alert("Sorry, this post is blocked.");
});
} else {
showDialog(loginDialog);
}
return;
}
let count = heart.nextElementSibling;
if (!heart.classList.contains('liked')) {
accountAPI.likePost(this.post).then((like) => {
this.post.viewerLike = like.uri;
heart.classList.add('liked');
count.innerText = String(parseInt(count.innerText, 10) + 1);
}).catch(showError);
} else {
accountAPI.removeLike(this.post.viewerLike).then(() => {
this.post.viewerLike = undefined;
heart.classList.remove('liked');
count.innerText = String(parseInt(count.innerText, 10) - 1);
}).catch(showError);
}
}
}