chore: switched to site.standard.comment a9ebd3dc
Steve · 2026-01-09 19:57 1 file(s) · +92 −40
packages/server/src/routes/now.ts +92 −40
398 398
		const parentData = await parentResponse.json();
399 399
		const parentCid = parentData.cid;
400 400
401 -
		// Create the reply record using app.bsky.feed.post lexicon
401 +
		// Create the comment record using site.standard.comment lexicon
402 402
		const createRecordUrl = `${c.env.PDS_URL}/xrpc/com.atproto.repo.createRecord`;
403 403
404 -
		const replyRecord = {
404 +
		const commentRecord = {
405 405
			repo: session.did,
406 -
			collection: "app.bsky.feed.post",
406 +
			collection: "site.standard.comment",
407 407
			record: {
408 -
				$type: "app.bsky.feed.post",
409 -
				text: body.content.trim(),
408 +
				$type: "site.standard.comment",
409 +
				parent: {
410 +
					uri: body.parentUri,
411 +
					cid: parentCid,
412 +
				},
413 +
				root: {
414 +
					uri: body.parentUri,
415 +
					cid: parentCid,
416 +
				},
417 +
				content: body.content.trim(),
410 418
				createdAt: new Date().toISOString(),
411 -
				reply: {
412 -
					root: {
413 -
						uri: body.parentUri,
414 -
						cid: parentCid,
415 -
					},
416 -
					parent: {
417 -
						uri: body.parentUri,
418 -
						cid: parentCid,
419 -
					},
420 -
				},
421 419
			},
422 420
		};
423 421
437 435
					Authorization: `DPoP ${session.accessToken}`,
438 436
					DPoP: dpopProof,
439 437
				},
440 -
				body: JSON.stringify(replyRecord),
438 +
				body: JSON.stringify(commentRecord),
441 439
			});
442 440
		};
443 441
484 482
	}
485 483
});
486 484
487 -
// Get replies for a post
485 +
// Get comments for a post
488 486
now.get("/replies/:uri", async (c) => {
489 487
	try {
490 488
		const encodedUri = c.req.param("uri");
491 489
		const uri = decodeURIComponent(encodedUri);
492 490
493 -
		// Use app.bsky.feed.getPostThread to fetch the post and its replies
494 -
		const threadUrl = `${PDS_URL}/xrpc/app.bsky.feed.getPostThread?uri=${encodeURIComponent(uri)}`;
491 +
		// Get the parent post to find its CID for matching
492 +
		const getRecordUrl =
493 +
			`${PDS_URL}/xrpc/com.atproto.repo.getRecord?` +
494 +
			new URLSearchParams({
495 +
				repo: uri.split("/")[2], // Extract DID from URI
496 +
				collection: uri.split("/")[3], // Extract collection
497 +
				rkey: uri.split("/")[4], // Extract rkey
498 +
			});
499 +
500 +
		const parentResponse = await fetch(getRecordUrl);
501 +
		if (!parentResponse.ok) {
502 +
			console.error("Failed to fetch parent post:", parentResponse.status);
503 +
			return c.json({ replies: [] });
504 +
		}
505 +
506 +
		const parentData = await parentResponse.json();
507 +
		const parentCid = parentData.cid;
495 508
496 -
		const response = await fetch(threadUrl);
509 +
		// Fetch all site.standard.comment records
510 +
		// Note: This is a simple implementation that fetches all comments
511 +
		// In production, you'd want to filter by parent URI server-side if possible
512 +
		const listUrl =
513 +
			`${PDS_URL}/xrpc/com.atproto.repo.listRecords?` +
514 +
			new URLSearchParams({
515 +
				repo: DID,
516 +
				collection: "site.standard.comment",
517 +
				limit: "100",
518 +
			});
519 +
520 +
		const response = await fetch(listUrl);
497 521
498 522
		if (!response.ok) {
499 -
			console.error("Failed to fetch thread:", response.status);
523 +
			console.error("Failed to fetch comments:", response.status);
500 524
			return c.json({ replies: [] });
501 525
		}
502 526
503 527
		const data = await response.json();
504 528
505 -
		// Extract replies from the thread
529 +
		// Filter comments that match the parent URI
506 530
		const replies: any[] = [];
507 531
508 -
		if (data.thread?.replies && Array.isArray(data.thread.replies)) {
509 -
			for (const reply of data.thread.replies) {
510 -
				if (reply.post) {
511 -
					replies.push({
512 -
						uri: reply.post.uri,
513 -
						cid: reply.post.cid,
514 -
						author: {
515 -
							did: reply.post.author.did,
516 -
							handle: reply.post.author.handle,
517 -
							displayName: reply.post.author.displayName,
518 -
							avatar: reply.post.author.avatar,
519 -
						},
520 -
						record: reply.post.record,
521 -
						indexedAt: reply.post.indexedAt,
522 -
						replyCount: reply.post.replyCount || 0,
523 -
						likeCount: reply.post.likeCount || 0,
524 -
					});
532 +
		for (const record of data.records) {
533 +
			const comment = record.value;
534 +
			// Check if this comment's parent matches our URI
535 +
			if (comment.parent?.uri === uri || comment.parent?.cid === parentCid) {
536 +
				// Fetch author profile info
537 +
				let handle = record.uri.split("/")[2]; // DID as fallback
538 +
				let displayName = undefined;
539 +
				let avatar = undefined;
540 +
541 +
				try {
542 +
					const profileUrl = `${PDS_URL}/xrpc/app.bsky.actor.getProfile?actor=${handle}`;
543 +
					const profileResponse = await fetch(profileUrl);
544 +
					if (profileResponse.ok) {
545 +
						const profileData = await profileResponse.json();
546 +
						handle = profileData.handle || handle;
547 +
						displayName = profileData.displayName;
548 +
						avatar = profileData.avatar;
549 +
					}
550 +
				} catch (err) {
551 +
					console.error("Failed to fetch profile:", err);
525 552
				}
553 +
554 +
				replies.push({
555 +
					uri: record.uri,
556 +
					cid: record.cid,
557 +
					author: {
558 +
						did: record.uri.split("/")[2],
559 +
						handle: handle,
560 +
						displayName: displayName,
561 +
						avatar: avatar,
562 +
					},
563 +
					record: {
564 +
						text: comment.content,
565 +
						createdAt: comment.createdAt,
566 +
					},
567 +
					indexedAt: record.indexedAt || comment.createdAt,
568 +
					replyCount: 0,
569 +
					likeCount: 0,
570 +
				});
526 571
			}
527 572
		}
528 573
574 +
		// Sort by creation date
575 +
		replies.sort(
576 +
			(a, b) =>
577 +
				new Date(a.record.createdAt).getTime() -
578 +
				new Date(b.record.createdAt).getTime(),
579 +
		);
580 +
529 581
		return c.json({ replies });
530 582
	} catch (error) {
531 -
		console.error("Error fetching replies:", error);
583 +
		console.error("Error fetching comments:", error);
532 584
		return c.json({ replies: [] });
533 585
	}
534 586
});