-
Notifications
You must be signed in to change notification settings - Fork 3
/
embed_component.js
287 lines (214 loc) · 7.74 KB
/
embed_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
/**
* Renders an embed (e.g. image or quoted post) inside the post view.
*/
class EmbedComponent {
/** @param {Post} post, @param {Embed} embed */
constructor(post, embed) {
this.post = post;
this.embed = embed;
}
/** @returns {AnyElement} */
buildElement() {
if (this.embed instanceof RawRecordEmbed) {
let quoteView = this.quotedPostPlaceholder();
this.loadQuotedPost(this.embed.record.uri, quoteView);
return quoteView;
} else if (this.embed instanceof RawRecordWithMediaEmbed) {
let wrapper = $tag('div');
let mediaView = new EmbedComponent(this.post, this.embed.media).buildElement();
let quoteView = this.quotedPostPlaceholder();
this.loadQuotedPost(this.embed.record.uri, quoteView);
wrapper.append(mediaView, quoteView);
return wrapper;
} else if (this.embed instanceof InlineRecordEmbed) {
return this.buildQuotedPostElement(this.embed);
} else if (this.embed instanceof InlineRecordWithMediaEmbed) {
let wrapper = $tag('div');
let mediaView = new EmbedComponent(this.post, this.embed.media).buildElement();
let quoteView = this.buildQuotedPostElement(this.embed);
wrapper.append(mediaView, quoteView);
return wrapper;
} else if (this.embed instanceof RawImageEmbed || this.embed instanceof InlineImageEmbed) {
return this.buildImagesComponent(this.embed);
} else if (this.embed instanceof RawLinkEmbed || this.embed instanceof InlineLinkEmbed) {
return this.buildLinkComponent(this.embed);
} else {
return $tag('p', { text: `[${this.embed.type}]` });
}
}
/** @returns {AnyElement} */
quotedPostPlaceholder() {
return $tag('div.quote-embed', {
html: '<p class="post placeholder">Loading quoted post...</p>'
});
}
/** @param {InlineRecordEmbed | InlineRecordWithMediaEmbed} embed, @returns {AnyElement} */
buildQuotedPostElement(embed) {
let div = $tag('div.quote-embed');
if ([Post, BlockedPost, MissingPost, DetachedQuotePost].some(c => embed.post instanceof c)) {
let postView = new PostComponent(embed.post, 'quote').buildElement();
div.appendChild(postView);
} else if (embed.post instanceof FeedGeneratorRecord) {
return this.buildFeedGeneratorView(embed.post);
} else if (embed.post instanceof UserListRecord) {
return this.buildUserListView(embed.post);
} else if (embed.post instanceof StarterPackRecord) {
return this.buildStarterPackView(embed.post);
} else {
let p = $tag('p', { text: `[${embed.post.type}]` });
div.appendChild(p);
}
return div;
}
/** @params {RawLinkEmbed | InlineLinkEmbed} embed, @returns {AnyElement} */
buildLinkComponent(embed) {
let hostname;
try {
hostname = new URL(embed.url).hostname;
} catch (error) {
console.log("Invalid URL:" + error);
let a = $tag('a', { href: embed.url, text: embed.title || embed.url });
let p = $tag('p');
p.append('[Link: ', a, ']');
return p;
}
let a = $tag('a.link-card', { href: embed.url, target: '_blank' });
let box = $tag('div');
let domain = $tag('p.domain', { text: hostname });
let title = $tag('h2', { text: embed.title });
box.append(domain, title);
if (embed.description) {
let text;
if (embed.description.length <= 300) {
text = embed.description;
} else {
text = embed.description.slice(0, 300) + '…';
}
box.append($tag('p.description', { text: text }));
}
a.append(box);
return a;
}
/** @param {FeedGeneratorRecord} feedgen, @returns {AnyElement} */
buildFeedGeneratorView(feedgen) {
let link = this.linkToFeedGenerator(feedgen);
let a = $tag('a.link-card.record', { href: link, target: '_blank' });
let box = $tag('div');
if (feedgen.avatar) {
let avatar = $tag('img.avatar');
avatar.src = feedgen.avatar;
box.append(avatar);
}
let title = $tag('h2', { text: feedgen.title });
title.append($tag('span.handle', { text: `• Feed by @${feedgen.author.handle}` }));
box.append(title);
if (feedgen.description) {
let description = $tag('p.description', { text: feedgen.description });
box.append(description);
}
let stats = $tag('p.stats');
stats.append($tag('i', 'fa-solid fa-heart'), ' ');
stats.append($tag('output', { text: feedgen.likeCount }));
box.append(stats);
a.append(box);
return a;
}
/** @param {FeedGeneratorRecord} feedgen, @returns {string} */
linkToFeedGenerator(feedgen) {
let { repo, rkey } = atURI(feedgen.uri);
return `https://bsky.app/profile/${repo}/feed/${rkey}`;
}
/** @param {UserListRecord} list, @returns {AnyElement} */
buildUserListView(list) {
let link = this.linkToUserList(list);
let a = $tag('a.link-card.record', { href: link, target: '_blank' });
let box = $tag('div');
if (list.avatar) {
let avatar = $tag('img.avatar');
avatar.src = list.avatar;
box.append(avatar);
}
let listType;
switch (list.purpose) {
case 'app.bsky.graph.defs#curatelist':
listType = "User list";
break;
case 'app.bsky.graph.defs#modlist':
listType = "Mute list";
break;
default:
listType = "List";
}
let title = $tag('h2', { text: list.title });
title.append($tag('span.handle', { text: `• ${listType} by @${list.author.handle}` }));
box.append(title);
if (list.description) {
let description = $tag('p.description', { text: list.description });
box.append(description);
}
a.append(box);
return a;
}
/** @param {StarterPackRecord} pack, @returns {AnyElement} */
buildStarterPackView(pack) {
let { repo, rkey } = atURI(pack.uri);
let link = `https://bsky.app/starter-pack/${repo}/${rkey}`;
let a = $tag('a.link-card.record', { href: link, target: '_blank' });
let box = $tag('div');
let title = $tag('h2', { text: pack.title });
title.append($tag('span.handle', { text: `• Starter pack by @${pack.author.handle}` }));
box.append(title);
if (pack.description) {
let description = $tag('p.description', { text: pack.description });
box.append(description);
}
a.append(box);
return a;
}
/** @param {UserListRecord} list, @returns {string} */
linkToUserList(list) {
let { repo, rkey } = atURI(list.uri);
return `https://bsky.app/profile/${repo}/lists/${rkey}`;
}
/** @params {RawImageEmbed | InlineImageEmbed} embed, @returns {AnyElement} */
buildImagesComponent(embed) {
let wrapper = $tag('div');
for (let image of embed.images) {
let p = $tag('p');
p.append('[');
// TODO: load image
let a = $tag('a', { text: "Image" });
if (image.fullsize) {
a.href = image.fullsize;
} else {
let cid = image.image.ref['$link'];
a.href = `https://cdn.bsky.app/img/feed_fullsize/plain/${this.post.author.did}/${cid}@jpeg`;
}
p.append(a);
p.append('] ');
wrapper.append(p);
if (image.alt) {
let details = $tag('details.image-alt');
details.append(
$tag('summary', { text: 'Show alt' }),
image.alt
);
wrapper.appendChild(details);
}
}
return wrapper;
}
/** @param {string} uri, @param {AnyElement} div, @returns Promise<void> */
async loadQuotedPost(uri, div) {
let record = await api.loadPostIfExists(uri);
if (record) {
let post = new Post(record);
let postView = new PostComponent(post, 'quote').buildElement();
div.replaceChildren(postView);
} else {
let post = new MissingPost(this.embed.record);
let postView = new PostComponent(post, 'quote').buildElement();
div.replaceChildren(postView);
}
}
}