forked from nsideras/bluesky-js-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsky-comments.js
221 lines (189 loc) · 8.98 KB
/
bsky-comments.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
/*
MIT License
Copyright (c) 2024 Nicholas Sideras
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const rootElement = document.querySelector("#comments");
var atProto = ToAtProtoUri(rootElement.dataset.uri);
fetch(
"https://public.api.bsky.app/xrpc/app.bsky.feed.getPostThread?uri=" + atProto
)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
return response.json();
})
.then((data) => {
if (
typeof data.thread.replies != "undefined" &&
data.thread.replies.length > 0
) {
rootElement.appendChild(renderComments(data.thread));
} else {
const noReplies = document.createElement("em");
noReplies.innerText = "No replies.";
rootElement.appendChild(noReplies);
}
})
.catch((error) => {
const p = document.createElement("p");
p.appendChild(document.createTextNode(`Error: ${error.message}`));
document.body.appendChild(p, rootElement);
});
function ToBskyUrl(uri) {
var splitUri = uri.split('/');
if(splitUri[0] === 'at:')
{
return 'https://bsky.app/profile/' + splitUri[2] + '/post/' + splitUri[4];
}
else
{
return uri;
}
}
function ToAtProtoUri(url)
{
var splitUri = url.split('/');
if(splitUri[0] === 'https:' || splitUri[0] === 'http:')
{
return 'at://' + splitUri[4] + '/app.bsky.feed.post/' + splitUri[6];
}
else
{
return url;
}
}
function ToBskyImgUrl(did, blobLink, thumb) {
return `https://cdn.bsky.app/img/${thumb ? "feed_thumb" : "feed_fullsize"}/plain/${did}/${blobLink}`;
}
function renderComments(thread)
{
const commentsNode = document.createElement("div");
for (const comment of thread.replies) {
var renderedString = renderComment(comment);
var htmlContent = createElementFromHTML(renderedString);
htmlContent.querySelector(".replies").appendChild(renderComments(comment));
commentsNode.appendChild(htmlContent);
}
return commentsNode;
}
//https://stackoverflow.com/a/494348
function createElementFromHTML(htmlString) {
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
// Change this to div.childNodes to support multiple top-level nodes.
return div.firstChild;
}
// adjusted code from https://capscollective.com/blog/bluesky-blog-comments/
function renderRichText(record) {
let richText = ``
const textEncoder = new TextEncoder();
const utf8Decoder = new TextDecoder();
const utf8Text = new Uint8Array(record.text.length * 3);
textEncoder.encodeInto(record.text, utf8Text);
var charIdx = 0;
for (const facetIdx in record.facets) {
const facet = record.facets[facetIdx];
const facetFeature = facet.features[0];
const facetType = facetFeature.$type;
var facetLink = "#";
if (facetType == "app.bsky.richtext.facet#tag") {
facetLink = `https://bsky.app/hashtag/${facetFeature.tag}`;
} else if (facetType == "app.bsky.richtext.facet#link") {
facetLink = facetFeature.uri;
} else if (facetType == "app.bsky.richtext.facet#mention") {
facetLink = `https://bsky.app/profile/${facetFeature.did}`;
}
if (charIdx < facet.index.byteStart) {
const preFacetText = utf8Text.slice(charIdx, facet.index.byteStart);
richText += utf8Decoder.decode(preFacetText)
}
const facetText = utf8Text.slice(facet.index.byteStart, facet.index.byteEnd);
richText += `<a href="${facetLink}" target="_blank">` + utf8Decoder.decode(facetText) + '</a>';
charIdx = facet.index.byteEnd;
}
if (charIdx < utf8Text.length) {
const postFacetText = utf8Text.slice(charIdx, utf8Text.length);
richText += utf8Decoder.decode(postFacetText);
}
return richText;
}
function renderAttachment(post) {
let attachment = "";
if (post.embed) {
const embedType = post.embed.$type;
if (embedType === "app.bsky.embed.external#view") {
const {uri, title, description} = post.embed.external;
if (uri.includes(".gif?")) {
attachment = `<img src="${uri}" title="${title}" alt="${description}">`;
}
} else if (embedType === "app.bsky.embed.images#view") {
const images = post.record.embed.images;
attachment = images.map(image => {
const thumb = ToBskyImgUrl(post.author.did, image.image.ref.$link, true);
const orig = ToBskyImgUrl(post.author.did, image.image.ref.$link, false);
return `<a href="${orig}" target="_blank"><img src="${thumb}" alt="${image.alt}"></a>`;
}).join('');
}
}
return attachment;
}
function renderComment(comment) {
var replyDate = new Date(comment.post.record.createdAt);
return `<ul class="comment" style="display: flex; list-style: none;">
<li style="margin-right: 10px;">
<img src="${comment.post.author.avatar}" width="42px" height="42px" style="clip-path: circle()" />
</li>
<li>
<div><a href="https://bsky.app/profile/${comment.post.author.handle}" rel="ugc" style="color: #000; text-decoration: none;">
<strong class="display-name" style="white-space: nowrap;">${comment.post.author.displayName}</strong>
<span class="handle" style="white-space: nowrap;">@${comment.post.author.handle}</span>
<span style="white-space: nowrap;">${replyDate.toLocaleString()}</span>
</a></div>
<a href="${ToBskyUrl(comment.post.uri)}" rel="ugc" style="color: #000; text-decoration: none;">
<div>${renderRichText(comment.post.record)}</div>
<div>${renderAttachment(comment.post)}</div>
<div>
<!-- icons from https://www.systemuicons.com/ -->
<span style="margin-right: 1em;">
<svg style="position: relative; top: .3em;" xmlns="http://www.w3.org/2000/svg" width="21" height="21" viewBox="0 0 21 21">
<path fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round" d="M11 16.517c4.418 0 8-3.284 8-7.017S15.418 3 11 3S3 6.026 3 9.759c0 1.457.546 2.807 1.475 3.91L3.5 18.25l3.916-2.447a9.2 9.2 0 0 0 3.584.714" />
</svg>
${comment.post.replyCount}</span>
<span style="margin-right: 1em;">
<svg style="position: relative; top: .3em;" xmlns="http://www.w3.org/2000/svg" width="21" height="21" viewBox="0 0 21 21">
<g fill="none" fill-rule="evenodd" stroke="#000000" stroke-linecap="round" stroke-linejoin="round">
<path d="m13.5 13.5l3 3l3-3" />
<path d="M9.5 4.5h3a4 4 0 0 1 4 4v8m-9-9l-3-3l-3 3" />
<path d="M11.5 16.5h-3a4 4 0 0 1-4-4v-8" />
</g>
</svg>
${comment.post.repostCount}
</span>
<span style="margin-right: 1em;">
<svg style="position: relative; top: .3em;" xmlns="http://www.w3.org/2000/svg" width="21" height="21" viewBox="0 0 21 21">
<path fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round" d="M10.5 6.5c.5-2.5 4.343-2.657 6-1c1.603 1.603 1.5 4.334 0 6l-6 6l-6-6a4.243 4.243 0 0 1 0-6c1.55-1.55 5.5-1.5 6 1" />
</svg>
${comment.post.likeCount}
</span>
</div></a>
<div class="replies" style="margin-top: 1em;">
</div>
</li>
</ul>`;
}