summaryrefslogtreecommitdiff
path: root/src/loaders/pleroma.ts
blob: 73d11da9e1ccc649cb1b88281a4835a922c02ae2 (plain)
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
import type { Loader } from "astro/loaders";
import { marked } from "marked";
import TurndownService from "turndown";

interface PleromaFeedConfig {
	instanceUrl: string;
	username: string;
	maxPosts?: number;
	accountId?: string; // Optional: if provided, skips account lookup
	allowedTags?: string[]; // Optional: if provided, only posts with these tags are included
}

interface PleromaAccount {
	id: string;
	username: string;
	acct: string;
	display_name: string;
	url: string;
}

interface PleromaMediaAttachment {
	id: string;
	type: "image" | "video" | "gifv" | "audio" | "unknown";
	url: string;
	preview_url: string;
	description?: string;
}

interface PleromaStatus {
	id: string;
	created_at: string;
	content: string;
	url: string;
	reblog: PleromaStatus | null;
	in_reply_to_id: string | null;
	sensitive: boolean;
	media_attachments: PleromaMediaAttachment[];
	visibility: string;
}

async function getAccountId(
	instanceUrl: string,
	username: string,
	logger: any,
): Promise<string | null> {
	try {
		const searchUrl = `${instanceUrl}/api/v1/accounts/search?q=${encodeURIComponent(username)}&limit=1`;
		logger.info(`Looking up account ID for username: ${username}`);

		const controller = new AbortController();
		const timeoutId = setTimeout(() => controller.abort(), 10000);

		const response = await fetch(searchUrl, {
			headers: {
				"User-Agent": "Astro Blog (pleroma-loader)",
			},
			signal: controller.signal,
		});

		clearTimeout(timeoutId);

		if (!response.ok) {
			logger.warn(`Failed to search for account: HTTP ${response.status}`);
			return null;
		}

		const accounts: PleromaAccount[] = await response.json();

		if (accounts.length === 0 || !accounts[0]) {
			logger.warn(`No account found for username: ${username}`);
			return null;
		}

		const account = accounts[0];
		logger.info(`Found account ID: ${account.id} for @${account.acct}`);
		return account.id;
	} catch (error) {
		logger.warn(`Failed to lookup account ID: ${error}`);
		return null;
	}
}

async function fetchAccountStatuses(
	instanceUrl: string,
	accountId: string,
	maxPosts: number,
	logger: any,
): Promise<PleromaStatus[]> {
	let response: Response | undefined;
	let lastError: unknown;

	// Add retry logic for network issues
	for (let attempt = 1; attempt <= 3; attempt++) {
		try {
			logger.info(`Attempt ${attempt} to fetch statuses...`);

			const statusesUrl = `${instanceUrl}/api/v1/accounts/${accountId}/statuses?limit=${maxPosts}&exclude_replies=true&exclude_reblogs=true`;

			// Create timeout controller
			const controller = new AbortController();
			const timeoutId = setTimeout(() => controller.abort(), 10000);

			response = await fetch(statusesUrl, {
				headers: {
					"User-Agent": "Astro Blog (pleroma-loader)",
				},
				signal: controller.signal,
			});

			clearTimeout(timeoutId);

			if (response.ok) {
				break; // Success, exit retry loop
			}
			throw new Error(`HTTP ${response.status}: ${response.statusText}`);
		} catch (error) {
			lastError = error;
			logger.warn(`Attempt ${attempt} failed: ${error}`);

			if (attempt < 3) {
				logger.info("Retrying in 2 seconds...");
				await new Promise((resolve) => setTimeout(resolve, 2000));
			}
		}
	}

	if (!response || !response.ok) {
		throw new Error(`Failed to fetch statuses after 3 attempts. Last error: ${lastError}`);
	}

	const statuses: PleromaStatus[] = await response.json();
	return statuses;
}

function isFilteredStatus(status: PleromaStatus): boolean {
	// Filter out boosts/reblogs (already handled by API parameter, but double-check)
	if (status.reblog) {
		return true;
	}

	// Filter out replies (already handled by API parameter, but double-check)
	if (status.in_reply_to_id) {
		return true;
	}

	// Filter out NSFW/sensitive content
	if (status.sensitive) {
		return true;
	}

	return false;
}

function extractHashtags(htmlContent: string): string[] {
	// Extract hashtags from HTML spans and plain text
	const hashtagPattern = /#(\w+)/gi;
	const matches = htmlContent.match(hashtagPattern);
	return matches ? [...new Set(matches.map((tag) => tag.toLowerCase()))] : [];
}

function hasAllowedTag(status: PleromaStatus, allowedTags: string[]): boolean {
	if (!allowedTags || allowedTags.length === 0) {
		return true; // No filtering if no tags specified
	}

	const content = status.content || "";
	const hashtags = extractHashtags(content);
	const normalizedAllowedTags = allowedTags.map((tag) => tag.toLowerCase().replace(/^#/, ""));
	const normalizedHashtags = hashtags.map((tag) => tag.toLowerCase().replace(/^#/, ""));

	return normalizedHashtags.some((tag) => normalizedAllowedTags.includes(tag));
}

function cleanContent(htmlContent: string): string {
	const turndownService = new TurndownService({
		headingStyle: "atx",
		codeBlockStyle: "fenced",
	});

	// Remove or replace common Pleroma/Mastodon elements
	const cleanedContent = htmlContent
		.replace(/<span class="[^"]*mention[^"]*"[^>]*>/gi, "") // Remove mention spans but keep content
		.replace(/<\/span>/gi, "")
		.replace(/<span class="[^"]*hashtag[^"]*"[^>]*>/gi, "") // Remove hashtag spans but keep content
		.replace(/<span class="[^"]*ellipsis[^"]*"[^>]*>.*?<\/span>/gi, "") // Remove ellipsis
		.replace(/<span class="[^"]*invisible[^"]*"[^>]*>.*?<\/span>/gi, ""); // Remove invisible text

	// Convert to markdown
	const markdown = turndownService.turndown(cleanedContent);

	// Clean up extra whitespace
	return markdown.trim().replace(/\n\s*\n\s*\n/g, "\n\n");
}

function markdownToHtml(markdown: string): string {
	// Configure marked options for safe rendering
	marked.setOptions({
		breaks: true, // Convert line breaks to <br>
		gfm: true, // GitHub flavored markdown
	});

	// Convert markdown to HTML
	const html = marked.parse(markdown);

	// Return as string (marked.parse can return string or Promise<string>)
	return typeof html === "string" ? html : "";
}

function extractTitle(content: string): string {
	// Extract first line or first sentence as title
	const firstLine = content.split("\n")[0];
	if (!firstLine) return "Micro post";

	const firstSentence = firstLine.split(/[.!?]/)[0];
	if (!firstSentence) return "Micro post";

	// Limit title length and clean it up
	const title = (firstSentence.length > 60 ? `${firstSentence.substring(0, 57)}...` : firstSentence)
		.replace(/[#*_`]/g, "") // Remove markdown formatting
		.trim();

	return title || "Micro post";
}

export function pleromaLoader(config: PleromaFeedConfig): Loader {
	return {
		name: "pleroma-loader",
		load: async ({ store, logger }) => {
			try {
				const { instanceUrl, username, maxPosts = 20, accountId: configAccountId } = config;

				logger.info(`Fetching Pleroma posts via API for user: ${username}`);

				// Get account ID (use provided one or lookup by username)
				let accountId: string | undefined = configAccountId;
				if (!accountId) {
					const lookedUpAccountId = await getAccountId(instanceUrl, username, logger);
					if (!lookedUpAccountId) {
						logger.warn("Failed to get account ID. Continuing without Pleroma posts...");
						store.clear();
						return;
					}
					accountId = lookedUpAccountId;
				}

				// Fetch statuses from API
				const statuses = await fetchAccountStatuses(instanceUrl, accountId, maxPosts, logger);
				logger.info(`Fetched ${statuses.length} statuses from API`);

				// Filter statuses
				const validStatuses = statuses.filter((status) => {
					if (isFilteredStatus(status)) return false;
					if (config.allowedTags && !hasAllowedTag(status, config.allowedTags)) return false;
					return true;
				});
				logger.info(`After filtering: ${validStatuses.length} valid posts`);

				// Clear existing entries
				store.clear();

				// Process each status
				for (const status of validStatuses) {
					try {
						const content = status.content || "";
						const cleanedContent = cleanContent(content);
						const title = extractTitle(cleanedContent);

						// Extract post ID from status
						const postId = status.id;

						// Use status URL as source
						const sourceUrl = status.url;

						// Extract image attachments only
						const attachments = status.media_attachments
							.filter((attachment) => attachment.type === "image")
							.map((attachment) => ({
								url: attachment.url,
								type: `image/${attachment.url.split(".").pop() || "jpeg"}`,
							}));

						// Create note entry
						store.set({
							id: `pleroma-${postId}`,
							data: {
								title,
								description:
									cleanedContent.substring(0, 160) + (cleanedContent.length > 160 ? "..." : ""),
								publishDate: new Date(status.created_at),
								sourceUrl,
								attachments,
							},
							body: cleanedContent,
							rendered: {
								html: markdownToHtml(cleanedContent),
							},
						});

						logger.info(`Processed post: ${title.substring(0, 50)}...`);
					} catch (error) {
						logger.warn(`Failed to process status ${status.id}: ${error}`);
					}
				}

				logger.info(`Successfully loaded ${validStatuses.length} Pleroma posts`);
			} catch (error) {
				logger.warn(`Pleroma loader failed: ${error}`);
				logger.info("Continuing build without Pleroma posts...");
				// Don't throw error to prevent build failure
				store.clear();
			}
		},
	};
}