Skip to content

Commit

Permalink
feat(bsky): add filter support for profile feeds (#17295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuyumi25 authored Oct 24, 2024
1 parent 97d0369 commit b007418
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
25 changes: 22 additions & 3 deletions lib/routes/bsky/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import { parseDate } from '@/utils/parse-date';
import { resolveHandle, getProfile, getAuthorFeed } from './utils';
import { art } from '@/utils/render';
import path from 'node:path';
import querystring from 'querystring';

export const route: Route = {
path: '/profile/:handle',
path: '/profile/:handle/:routeParams?',
categories: ['social-media', 'popular'],
view: ViewType.SocialMedia,
example: '/bsky/profile/bsky.app',
parameters: { handle: 'User handle, can be found in URL' },
parameters: {
handle: 'User handle, can be found in URL',
routeParams: 'Filter parameter, Use filter to customize content types',
},
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -30,13 +34,28 @@ export const route: Route = {
name: 'Post',
maintainers: ['TonyRL'],
handler,
description: `
| Filter Value | Description |
|--------------|-------------|
| posts_with_replies | Includes Posts, Replies, and Reposts |
| posts_no_replies | Includes Posts and Reposts, without Replies |
| posts_with_media | Shows only Posts containing media |
| posts_and_author_threads | Shows Posts and Threads, without Replies and Reposts |
Default value for filter is \`posts_and_author_threads\` if not specified.
Example:
- \`/bsky/profile/bsky.app/filter=posts_with_replies\``,
};

async function handler(ctx) {
const handle = ctx.req.param('handle');
const routeParams = querystring.parse(ctx.req.param('routeParams'));
const filter = routeParams.filter || 'posts_and_author_threads';

const DID = await resolveHandle(handle, cache.tryGet);
const profile = await getProfile(DID, cache.tryGet);
const authorFeed = await getAuthorFeed(DID, cache.tryGet);
const authorFeed = await getAuthorFeed(DID, filter, cache.tryGet);

const items = authorFeed.feed.map(({ post }) => ({
title: post.record.text.split('\n')[0],
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/bsky/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const getProfile = (did, tryGet) =>
});

// https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/getAuthorFeed.json
const getAuthorFeed = (did, tryGet) =>
const getAuthorFeed = (did, filter, tryGet) =>
tryGet(
`bsky:authorFeed:${did}`,
`bsky:authorFeed:${did}:${filter}`,
async () => {
const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed', {
searchParams: {
actor: did,
filter: 'posts_and_author_threads',
filter,
limit: 30,
},
});
Expand Down

0 comments on commit b007418

Please sign in to comment.