summaryrefslogtreecommitdiff
path: root/src/loaders/pleroma.ts
blob: caeeb320374daa2be8ab9deefa4b308aae64dd09 (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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import type { Loader } from "astro/loaders";
import { marked } from "marked";
import TurndownService from "turndown";

interface Logger {
	info: (message: string) => void;
	warn: (message: string) => void;
}

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
	mergeThreads?: boolean; // Optional: if true, merges thread posts into single entry (default: true)
}

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;
	account: PleromaAccount;
}

/**
 * Detect if a post is a thread starter by checking for thread markers
 * Matches patterns like: 🧵, 👇, ⬇️, 1/n, (1/n), [1/n], Thread:, etc.
 */
function isThreadStarter(content: string): boolean {
	// Check for thread emojis
	const threadEmojis = ["🧵", "👇", "⬇️", "📝", "📖", "⤵️", "🔽"];
	if (threadEmojis.some((emoji) => content.includes(emoji))) {
		return true;
	}

	// Check for numbered thread patterns:
	// - 1/n, 1/*, 1/2, 1/10 (plain)
	// - (1/n), (1/*), (1/2) (parentheses)
	// - [1/n], [1/*], [1/2] (brackets)
	const numberedPatterns = [
		/\b1\/([n*]|\d+)\b/i, // 1/n, 1/*, 1/2
		/\(1\/([n*]|\d+)\)/i, // (1/n), (1/*)
		/\[1\/([n*]|\d+)\]/i, // [1/n], [1/*]
	];

	if (numberedPatterns.some((pattern) => pattern.test(content))) {
		return true;
	}

	// Check for text markers (case insensitive)
	const textMarkers = [
		/\bthread:/i, // Thread:
		/\[thread\]/i, // [Thread]
		/^thread about/i, // Thread about... (start of text)
		/^a thread about/i, // A thread about...
	];

	if (textMarkers.some((pattern) => pattern.test(content))) {
		return true;
	}

	return false;
}

/**
 * Parse the Link header to extract the max_id for the next page
 * Link header format: <url?max_id=123>; rel="next", <url?since_id=456>; rel="prev"
 */
function parseNextPageMaxId(linkHeader: string | null): string | null {
	if (!linkHeader) {
		return null;
	}

	// Split by comma to get individual links
	const links = linkHeader.split(",");

	for (const link of links) {
		// Check if this is the "next" rel link
		if (link.includes('rel="next"')) {
			// Extract URL from angle brackets
			const urlMatch = link.match(/<([^>]+)>/);
			if (urlMatch?.[1]) {
				// Parse the URL to extract max_id parameter
				try {
					const url = new URL(urlMatch[1]);
					const maxId = url.searchParams.get("max_id");
					return maxId;
				} catch {}
			}
		}
	}

	return null;
}

/**
 * Fetch the context (ancestors and descendants) for a given status
 * Returns only the descendants array for thread building
 */
async function fetchStatusContext(
	instanceUrl: string,
	statusId: string,
	logger: Logger,
): Promise<PleromaStatus[]> {
	try {
		const contextUrl = `${instanceUrl}/api/v1/statuses/${statusId}/context`;
		logger.info(`Fetching context for status: ${statusId}`);

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

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

		clearTimeout(timeoutId);

		if (!response.ok) {
			logger.warn(`Failed to fetch context: HTTP ${response.status}`);
			return [];
		}

		const context: { ancestors: PleromaStatus[]; descendants: PleromaStatus[] } =
			await response.json();
		logger.info(`Fetched ${context.descendants.length} descendants for status ${statusId}`);
		return context.descendants;
	} catch (error) {
		logger.warn(`Failed to fetch status context: ${error}`);
		return [];
	}
}

/**
 * Build a direct author-to-author reply chain from the thread starter
 * Stops when encountering a reply from another user or a missing link
 */
function buildAuthorChain(starter: PleromaStatus, descendants: PleromaStatus[]): PleromaStatus[] {
	const chain: PleromaStatus[] = [starter];
	const authorAccountId = starter.account.id;
	let currentId = starter.id;

	// Keep following the chain as long as we find direct author replies
	while (true) {
		// Find the next post in the chain: it must be by the same author and reply to the current post
		const nextPost = descendants.find(
			(status) => status.in_reply_to_id === currentId && status.account.id === authorAccountId,
		);

		if (!nextPost) {
			// No more direct author replies found, chain ends here
			break;
		}

		chain.push(nextPost);
		currentId = nextPost.id;
	}

	return chain;
}

/**
 * Strip thread markers from content (1/n, 2/n, 3/4, etc.)
 */
function stripThreadMarkers(content: string): string {
	return content
		.replace(/\s*\d+\/[n*\d]+\s*/gi, " ")
		.replace(/🧵/g, "")
		.trim();
}

/**
 * Extract trailing hashtags from content
 * Handles both hashtag-only lines and hashtags at the end of text lines
 * Returns the main content without trailing tags and the extracted tags
 */
function extractTrailingHashtags(content: string): {
	mainContent: string;
	tags: string[];
} {
	const tags: string[] = [];
	let modifiedContent = content;

	// Regex patterns
	const hashtagOnlyLine = /^\s*((\[#\w+\]\([^)]+\)|#\w+)\s*)+\s*$/;
	const trailingHashtags = /((?:\s*(?:\[#\w+\]\([^)]+\)|#\w+))+)\s*$/;
	const hashtagExtract = /\[#(\w+)\]\([^)]+\)|#(\w+)/g;

	// First, handle hashtag-only lines at the end
	const lines = modifiedContent.split("\n");
	while (lines.length > 0) {
		const lastLine = lines[lines.length - 1]?.trim() || "";
		if (!lastLine) {
			lines.pop(); // Remove empty trailing lines
			continue;
		}
		if (hashtagOnlyLine.test(lastLine)) {
			// Extract tag names from this line
			let match: RegExpExecArray | null = hashtagExtract.exec(lastLine);
			while (match !== null) {
				const tag = match[1] || match[2];
				if (tag) {
					tags.push(tag.toLowerCase());
				}
				match = hashtagExtract.exec(lastLine);
			}
			hashtagExtract.lastIndex = 0;
			lines.pop();
		} else {
			break;
		}
	}
	modifiedContent = lines.join("\n");

	// Second, handle trailing hashtags at the end of the last line (even if there's other text)
	const trailingMatch = modifiedContent.match(trailingHashtags);
	if (trailingMatch?.[1]) {
		const trailingText = trailingMatch[1];
		// Extract tag names from trailing hashtags
		let match: RegExpExecArray | null = hashtagExtract.exec(trailingText);
		while (match !== null) {
			const tag = match[1] || match[2];
			if (tag) {
				tags.push(tag.toLowerCase());
			}
			match = hashtagExtract.exec(trailingText);
		}
		hashtagExtract.lastIndex = 0;
		// Remove trailing hashtags from content
		modifiedContent = modifiedContent.replace(trailingHashtags, "").trim();
	}

	return {
		mainContent: modifiedContent,
		tags: [...new Set(tags)], // Deduplicate within this segment
	};
}

/**
 * Merge thread posts into a single content structure with image grids per segment
 */
function mergeThreadContent(chain: PleromaStatus[]): {
	content: string;
	attachments: Array<{ url: string; type: string }>;
} {
	const segments: string[] = [];
	const allAttachments: Array<{ url: string; type: string }> = [];
	const allTags = new Set<string>(); // Collect all tags from all segments

	for (const post of chain) {
		// Clean and strip thread markers from content
		const cleanedContent = cleanContent(post.content || "");
		const contentWithoutMarkers = stripThreadMarkers(cleanedContent);

		// Extract trailing hashtags from content
		const { mainContent, tags } = extractTrailingHashtags(contentWithoutMarkers);
		tags.forEach((tag) => allTags.add(tag));

		// Build segment with text (without trailing hashtags)
		let segment = mainContent;

		// Add image attachments as HTML grid after the text
		const imageAttachments = post.media_attachments.filter(
			(attachment) => attachment.type === "image",
		);

		if (imageAttachments.length > 0) {
			// Build HTML grid for images
			const imageGrid = `
<div class="mt-4 mb-4 grid grid-cols-1 gap-4 sm:grid-cols-2">
${imageAttachments
	.map((attachment) => {
		const description = attachment.description || "Image";
		allAttachments.push({
			url: attachment.url,
			type: `image/${attachment.url.split(".").pop() || "jpeg"}`,
		});
		return `<a href="${attachment.url}" target="_blank" rel="noopener noreferrer" class="block overflow-hidden rounded-lg border border-gray-200 transition-colors hover:border-gray-300 dark:border-gray-700 dark:hover:border-gray-600">
<img src="${attachment.url}" alt="${description}" class="h-48 w-full object-cover" loading="lazy" />
</a>`;
	})
	.join("\n")}
</div>`;

			segment = `${segment}\n\n${imageGrid}`;
		}

		segments.push(segment);
	}

	// Join segments with horizontal rule separator
	let content = segments.join("\n\n---\n\n");

	// Append consolidated tags at the end as markdown links
	if (allTags.size > 0) {
		// Get the instance URL from the first post to construct tag URLs
		const instanceUrl = chain[0]?.account.url.split("/@")[0] || "https://social.craftknight.com";
		const tagLine = [...allTags].map((t) => `[#${t}](${instanceUrl}/tag/${t})`).join(" ");
		content = `${content}\n\n${tagLine}`;
	}

	return { content, attachments: [] }; // Return empty attachments to avoid duplicate grid at end
}

async function getAccountId(
	instanceUrl: string,
	username: string,
	logger: Logger,
): 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: Logger,
): Promise<PleromaStatus[]> {
	const allStatuses: PleromaStatus[] = [];
	let maxId: string | null = null;
	let pageCount = 0;
	const pageLimit = 40; // Mastodon/Pleroma API max per page
	const fetchAll = maxPosts === -1;

	// Fetch pages until we have enough posts or no more pages available
	while (fetchAll || allStatuses.length < maxPosts) {
		pageCount++;
		let response: Response | undefined;
		let lastError: unknown;

		// Build URL with pagination parameters
		// If fetching all, always use pageLimit; otherwise calculate remaining
		const requestLimit = fetchAll ? pageLimit : Math.min(pageLimit, maxPosts - allStatuses.length);
		const params = new URLSearchParams({
			limit: String(requestLimit),
			exclude_replies: "true",
			exclude_reblogs: "true",
		});

		if (maxId) {
			params.set("max_id", maxId);
		}

		const statusesUrl = `${instanceUrl}/api/v1/accounts/${accountId}/statuses?${params.toString()}`;

		// Add retry logic for network issues
		for (let attempt = 1; attempt <= 3; attempt++) {
			try {
				const modeMsg = fetchAll ? " [fetching all posts]" : ` [target: ${maxPosts}]`;
				logger.info(
					`Attempt ${attempt} to fetch statuses page ${pageCount}${maxId ? ` (max_id: ${maxId})` : ""}${modeMsg}...`,
				);

				// 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();
		logger.info(`Fetched ${statuses.length} statuses from page ${pageCount}`);

		// If no statuses returned, we've reached the end
		if (statuses.length === 0) {
			logger.info("No more statuses available");
			break;
		}

		// Add statuses to our accumulated list
		allStatuses.push(...statuses);

		// Parse Link header to get next page max_id
		const linkHeader = response.headers.get("link");
		const nextMaxId = parseNextPageMaxId(linkHeader);

		if (!nextMaxId) {
			logger.info("No more pages available (no next link in header)");
			break;
		}

		// If the max_id hasn't changed, we're stuck in a loop - break
		if (nextMaxId === maxId) {
			logger.warn("Pagination returned same max_id, stopping to prevent infinite loop");
			break;
		}

		maxId = nextMaxId;
	}

	const summaryMsg = fetchAll
		? `Total fetched: ${allStatuses.length} statuses (all available) across ${pageCount} page(s)`
		: `Total fetched: ${allStatuses.length} statuses (target: ${maxPosts}) across ${pageCount} page(s)`;
	logger.info(summaryMsg);
	return allStatuses;
}

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 || "";
						let cleanedContent: string;
						let attachments: Array<{ url: string; type: string }>;
						let postId: string;
						let sourceUrl: string;

						// Check if this is a thread starter and thread merging is enabled
						if (config.mergeThreads !== false && isThreadStarter(content)) {
							logger.info(`Detected thread starter: ${status.id}`);

							// Fetch context and build the author chain
							const descendants = await fetchStatusContext(instanceUrl, status.id, logger);
							const chain = buildAuthorChain(status, descendants);

							logger.info(`Built chain with ${chain.length} post(s) for thread ${status.id}`);

						// Merge thread content
						const merged = mergeThreadContent(chain);
						cleanedContent = merged.content;
						attachments = merged.attachments;
							postId = status.id;
							sourceUrl = status.url;
						} else {
							// Process as single post
							cleanedContent = cleanContent(content);
							postId = status.id;
							sourceUrl = status.url;

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

						const title = extractTitle(cleanedContent);

						// 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();
			}
		},
	};
}