Skip to content

Commit

Permalink
Merge pull request #1307 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Dec 28, 2023
2 parents 94fe4c1 + f90e5d5 commit 273f135
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/v2/bsky/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
'/keyword/:keyword': ['untitaker'],
'/profile/:handle': ['TonyRL'],
};
42 changes: 42 additions & 0 deletions lib/v2/bsky/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { parseDate } = require('@/utils/parse-date');
const { resolveHandle, getProfile, getAuthorFeed } = require('./utils');
const { art } = require('@/utils/render');
const { join } = require('path');

module.exports = async (ctx) => {
const { handle } = ctx.params;
const DID = await resolveHandle(handle, ctx.cache.tryGet);
const profile = await getProfile(DID, ctx.cache.tryGet);
const authorFeed = await getAuthorFeed(DID, ctx.cache.tryGet);

const items = authorFeed.feed.map(({ post }) => ({
title: post.record.text.split('\n')[0],
description: art(join(__dirname, 'templates/post.art'), {
text: post.record.text.replace(/\n/g, '<br>'),
embed: post.embed,
// embed.$type "app.bsky.embed.record#view" and "app.bsky.embed.recordWithMedia#view"
// are not handled
}),
author: post.author.displayName,
pubDate: parseDate(post.record.createdAt),
link: `https://bsky.app/profile/${post.author.handle}/post/${post.uri.split('app.bsky.feed.post/')[1]}`,
upvotes: post.likeCount,
comments: post.replyCount,
}));

ctx.state.data = {
title: `${profile.displayName} (@${profile.handle}) — Bluesky`,
description: profile.description.replace(/\n/g, ' '),
link: `https://bsky.app/profile/${profile.handle}`,
image: profile.banner,
icon: profile.avatar,
logo: profile.avatar,
item: items,
};

ctx.state.json = {
DID,
profile,
authorFeed,
};
};
6 changes: 6 additions & 0 deletions lib/v2/bsky/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module.exports = {
source: '/search',
target: (params, url) => `/bsky/keyword/${new URL(url).searchParams.get('q')}`,
},
{
title: 'Post',
docs: 'https://docs.rsshub.app/routes/social-media#bluesky-bsky',
source: '/profile/:handle',
target: '/bsky/profile/:handle',
},
],
},
};
1 change: 1 addition & 0 deletions lib/v2/bsky/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = (router) => {
router.get('/keyword/:keyword', require('./keyword'));
router.get('/profile/:handle', require('./posts'));
};
15 changes: 15 additions & 0 deletions lib/v2/bsky/templates/post.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{ if text }}
{{@ text }}<br>
{{ /if }}

{{ if embed }}
{{ if embed.$type == 'app.bsky.embed.images#view'}}
{{ each embed.images i }}
<img src="{{ i.fullsize }}" alt="{{ i.alt }}"><br>
{{ /each }}
{{ else if embed.$type == 'app.bsky.embed.external#view' }}
<a href="{{ embed.external.uri }}"><b>{{ embed.external.title }}</b><br>
{{ embed.external.description }}
</a>
{{ /if }}
{{ /if }}
52 changes: 52 additions & 0 deletions lib/v2/bsky/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const got = require('@/utils/got');
const config = require('@/config').value;

/**
* docs: https://atproto.com/lexicons/app-bsky
*/

// https://github.com/bluesky-social/atproto/blob/main/lexicons/com/atproto/identity/resolveHandle.json
const resolveHandle = (handle, tryGet) =>
tryGet(`bsky:${handle}`, async () => {
const { data } = await got('https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle', {
searchParams: {
handle,
},
});
return data.did;
});

// https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/getProfile.json
const getProfile = (did, tryGet) =>
tryGet(`bsky:profile:${did}`, async () => {
const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile', {
searchParams: {
actor: did,
},
});
return data;
});

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

module.exports = {
resolveHandle,
getProfile,
getAuthorFeed,
};
4 changes: 4 additions & 0 deletions website/docs/routes/social-media.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@

## Bluesky (bsky) {#bluesky-bsky}

### Post {#bluesky-bsky-post}

<Route author="TonyRL" example="/bsky/profile/bsky.app" path="/bsky/profile/:handle" paramsDesc={['User handle, can be found in URL']} radar="1" />

### Keywords {#bluesky-bsky-keywords}

<Route author="untitaker" example="/bsky/keyword/hello" path="/bsky/keyword/:keyword" radar="1" />
Expand Down
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@docusaurus/types": "3.0.1",
"@types/markdown-it": "^13.0.7",
"@types/mdx-js__react": "^1.5.8",
"@types/react": "^18.2.45",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"typescript": "^5.3.3"
},
Expand Down
54 changes: 27 additions & 27 deletions website/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 273f135

Please sign in to comment.