forked from nodejs/bluesky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.js
More file actions
248 lines (214 loc) · 7.24 KB
/
posts.js
File metadata and controls
248 lines (214 loc) · 7.24 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
import AtpAgent, { AppBskyFeedPost, BlobRef, RichText } from "@atproto/api";
import assert from 'node:assert';
import * as cheerio from 'cheerio';
export const REPLY_IN_THREAD = Symbol('Reply in thread');
// URL format:
// 1. https://bsky.app/profile/${handle}/post/${postId}
// 2. https://bsky.app/profile/${did}/post/${postId}
// TODO(joyeecheung): consider supporting base other than bsky.app.
const kURLPattern = /https:\/\/bsky\.app\/profile\/(.+)\/post\/(.+)/;
/**
* @param {string} url
*/
export function validatePostURL(url) {
const match = url.match(kURLPattern);
assert(match, `Post URL ${url} does not match the expected pattern`);
return {
handle: match[1],
postId: match[2],
isDid: match[1].startsWith('did:')
};
}
/**
* @param {AtpAgent} agent
* @param {string} postUrl
*/
export async function getPostInfoFromUrl(agent, postUrl) {
const { handle, postId, isDid } = validatePostURL(postUrl);
let did;
if (isDid) {
did = handle;
} else {
const profile = await agent.resolveHandle({ handle });
did = profile.data.did;
}
const postView = await agent.getPost({ repo: did, rkey: postId });
const cid = postView.cid;
const uri = `at://${did}/app.bsky.feed.post/${postId}`;
return { uri, cid };
}
// URI format: at://${did}/app.bsky.feed.post/${postId}
const kURIPattern = /at:\/\/(.*)+\/app\.bsky\.feed\.post\/(.*)+/
export function validatePostURI(uri) {
const match = uri.match(kURIPattern);
assert(match, `Post URI ${uri} does not match the expected pattern`);
return {
did: match[1],
postId: match[2]
};
}
/**
* @param {AtpAgent} agent
* @param {string} uri
*/
export async function getPostURLFromURI(agent, uri) {
const { did, postId } = validatePostURI(uri);
const profile = await agent.getProfile({ actor: did });
const handle = profile.data.handle;
return `https://bsky.app/profile/${handle}/post/${postId}`;
}
/**
* TODO(joyeecheung): support 'imageFiles' field in JSON files.
* @param {AtpAgent} agent
* @param {ArrayBuffer} imgData
* @returns {BlobRef}
*/
async function uploadImage(agent, imgData) {
const res = await agent.uploadBlob(imgData, {
encoding: 'image/jpeg'
});
return res.data.blob;
}
// https://docs.bsky.app/docs/advanced-guides/posts#website-card-embeds
async function fetchEmbedUrlCard(url) {
console.log('Fetching embed card from', url);
// The required fields for every embed card
const card = {
uri: url,
title: '',
description: '',
};
try {
// Fetch the HTML
const resp = await fetch(url);
if (!resp.ok) {
throw new Error(`Failed to fetch URL: ${resp.status} ${resp.statusText}`);
}
const html = await resp.text();
const $ = cheerio.load(html);
// Parse out the "og:title" and "og:description" HTML meta tags
const titleTag = $('meta[property="og:title"]').attr('content');
if (titleTag) {
card.title = titleTag;
}
const descriptionTag = $('meta[property="og:description"]').attr('content');
if (descriptionTag) {
card.description = descriptionTag;
}
// If there is an "og:image" HTML meta tag, fetch and upload that image
const imageTag = $('meta[property="og:image"]').attr('content');
if (imageTag) {
let imgURL = imageTag;
// Naively turn a "relative" URL (just a path) into a full URL, if needed
if (!imgURL.includes('://')) {
imgURL = new URL(imgURL, url).href;
}
card.thumb = { $TO_BE_UPLOADED: imgURL };
}
return {
$type: 'app.bsky.embed.external',
external: card,
};
} catch (error) {
console.error('Error generating embed URL card:', error.message);
throw error;
}
}
/**
* @typedef ReplyRequest
* @property {string} richText
* @property {string} replyURL
* @property {{cid: string, uri: string}?} replyInfo
*/
/**
* @typedef PostRequest
* @property {string} richText
*/
/**
* @typedef QuotePostRequest
* @property {string} richText
* @property {string} repostURL
* @property {{cid: string, uri: string}?} repostInfo
*/
/**
* It should be possible to invoked this method on the same request at least twice -
* once to populate the facets and the embed without uploading any files if shouldUploadImage
* is false, and then again uploading files if shouldUploadImage is true.
* @param {AtpAgent} agent
* @param {ReplyRequest|PostRequest|QuotePostRequest} request
* @param {boolean} shouldUploadImage
* @returns {AppBskyFeedPost.Record}
*/
export async function populateRecord(agent, request, shouldUploadImage = false) {
console.log(`Generating record, shouldUploadImage = ${shouldUploadImage}, request = `, request);
if (request.repostURL && !request.repostInfo) {
request.repostInfo = await getPostInfoFromUrl(agent, request.repostURL);
}
if (request.replyURL && request.replyURL !== REPLY_IN_THREAD && !request.replyInfo) {
request.replyInfo = await getPostInfoFromUrl(agent, request.replyURL);
}
if (request.richText && !request.record) {
// TODO(joyeecheung): When Bluesky supports markdown or snippets, we should render the text
// as markdown.
const rt = new RichText({ text: request.richText });
await rt.detectFacets(agent); // automatically detects mentions and links
const record = {
$type: 'app.bsky.feed.post',
text: rt.text,
facets: rt.facets,
createdAt: new Date().toISOString(),
};
// https://docs.bsky.app/docs/tutorials/creating-a-post#quote-posts
if (request.repostInfo) {
record.embed = {
$type: 'app.bsky.embed.record',
record: request.repostInfo
};
} else if (request.replyInfo) {
record.reply = {
root: request.rootInfo || request.replyInfo,
parent: request.replyInfo,
};
}
// If there is already another embed, don't generate the card embed.
if (!record.embed) {
// Find the first URL, match until the first whitespace or punctuation.
const urlMatch = request.richText.match(/https?:\/\/[^\s\]\[\"\'\<\>]+/);
if (urlMatch !== null) {
const url = urlMatch[0];
const card = await fetchEmbedUrlCard(url);
record.embed = card;
}
}
request.record = record;
}
if (shouldUploadImage && request.record?.embed?.external?.thumb?.$TO_BE_UPLOADED) {
const card = request.record.embed.external;
const imgURL = card.thumb.$TO_BE_UPLOADED;
try {
console.log('Fetching image', imgURL);
const imgResp = await fetch(imgURL);
if (!imgResp.ok) {
throw new Error(`Failed to fetch image ${imgURL}: ${imgResp.status} ${imgResp.statusText}`);
}
const imgData = await imgResp.arrayBuffer();
console.log('Uploading image', imgURL, 'size = ', imgData.byteLength);
card.thumb = await uploadImage(agent, imgData);
} catch (e) {
// If image upload fails, post the embed card without the image, at worst we see a
// link card without an image which is not a big deal.
console.log(`Failed to fetch or upload image ${imgURL}`, e);
}
}
console.log('Generated record');
console.dir(request.record, { depth: 3 });
return request;
}
/**
* @param {AtpAgent} agent
* @param {object} request
*/
export async function post(agent, request) {
const { record } = await populateRecord(agent, request, true);
return agent.post(record);
}