Compare commits

...

2 Commits

Author SHA1 Message Date
Rıdvan Akca
7c1d2c47ec feat(bluesky): add reply to post action 2024-05-28 10:05:47 +02:00
Rıdvan Akca
84ecf105c2 feat(bluesky): add search post by url action 2024-05-28 10:05:09 +02:00
5 changed files with 155 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
import createPost from './create-post/index.js';
import replyToPost from './reply-to-post/index.js';
import searchPostByUrl from './search-post-by-url/index.js';
export default [createPost];
export default [createPost, replyToPost, searchPostByUrl];

View File

@@ -0,0 +1,55 @@
import { BskyAgent, RichText } from '@atproto/api';
import defineAction from '../../../../helpers/define-action.js';
import getReplyRefs from '../../common/get-reply-refs.js';
export default defineAction({
name: 'Reply to post',
key: 'replyToPost',
description: 'Replies to a post.',
arguments: [
{
label: 'Post Url',
key: 'postUrl',
type: 'string',
required: true,
variables: true,
description: 'Enter whole post url you want to reply here.',
},
{
label: 'Text',
key: 'text',
type: 'string',
required: true,
variables: true,
description: 'Your post.',
},
],
async run($) {
const text = $.step.parameters.text;
const postUrl = $.step.parameters.postUrl;
const replyRefs = await getReplyRefs($, postUrl);
const agent = new BskyAgent({ service: 'https://bsky.social/xrpc' });
const richText = new RichText({ text });
await richText.detectFacets(agent);
const body = {
repo: $.auth.data.did,
collection: 'app.bsky.feed.post',
record: {
$type: 'app.bsky.feed.post',
text: richText.text,
facets: richText.facets,
createdAt: new Date().toISOString(),
reply: replyRefs,
},
};
const { data } = await $.http.post('/com.atproto.repo.createRecord', body);
$.setActionItem({ raw: data });
},
});

View File

@@ -0,0 +1,35 @@
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Search post by url',
key: 'searchPostByUrl',
description: 'Searches a post in a thread by url.',
arguments: [
{
label: 'Post Url',
key: 'postUrl',
type: 'string',
required: true,
variables: true,
description: 'Enter whole post url here.',
},
],
async run($) {
const postUrl = $.step.parameters.postUrl;
const urlParts = postUrl.split('/');
const handle = urlParts[urlParts.length - 3];
const postId = urlParts[urlParts.length - 1];
const uri = `at://${handle}/app.bsky.feed.post/${postId}`;
const params = {
uri,
};
const { data } = await $.http.get('/app.bsky.feed.getPostThread', {
params,
});
$.setActionItem({ raw: data });
},
});

View File

@@ -0,0 +1,58 @@
const parseUri = (uri) => {
const parts = uri.split('/');
return {
repo: parts[4],
collection: 'app.bsky.feed.post',
rkey: parts[6],
};
};
const getReplyRefs = async ($, parentUri) => {
const uriParts = parseUri(parentUri);
try {
const { data: parent } = await $.http.get('/com.atproto.repo.getRecord', {
params: uriParts,
});
const parentReply = parent.value?.reply;
let root;
if (parentReply) {
const rootUri = parentReply.root.uri;
const [rootRepo, rootCollection, rootRkey] = rootUri
.split('/')
.slice(2, 5);
const params = {
repo: rootRepo,
collection: rootCollection,
rkey: rootRkey,
};
const rootResp = await $.http.get('/com.atproto.repo.getRecord', {
params,
});
root = rootResp.data;
} else {
root = parent;
}
return {
root: {
uri: root.uri,
cid: root.cid,
},
parent: {
uri: parent.uri,
cid: parent.cid,
},
};
} catch (error) {
throw new Error('Error while fetching records');
}
};
export default getReplyRefs;

View File

@@ -3,6 +3,10 @@ favicon: /favicons/bluesky.svg
items:
- name: Create post
desc: Creates a new post.
- name: Reply to post
desc: Replies to a post.
- name: Search post by url
desc: Searches a post in a thread by url.
---
<script setup>