Something went wrong. Try again.
A live media wall for your Bluesky follows
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155import { ComAtprotoRepoStrongRef, lexicons, type Agent } from '@atproto/api';
export type InteractionAgent = Pick< Agent, 'like' | 'deleteLike' | 'repost' | 'deleteRepost' | 'follow' | 'deleteFollow' | 'mute' | 'unmute'> & { app: { bsky: { bookmark: Pick<Agent['app']['bsky']['bookmark'], 'createBookmark' | 'deleteBookmark'>; graph: { block: Pick<Agent['app']['bsky']['graph']['block'], 'create' | 'delete'> }; }; };};
export type InteractionPost = { uri: string; cid: string };
function validateDid(did: string) { const result = lexicons.validate('app.bsky.graph.block', { $type: 'app.bsky.graph.block', subject: did, createdAt: new Date().toISOString() }); if (!result.success) throw new Error('Invalid account DID');}
function recordUri(uri: string, collection: string, owner?: string) { const match = /^at:\/\/(did:[a-z]+:[A-Za-z0-9._:%-]+)\/([a-zA-Z0-9.]+)\/([A-Za-z0-9._~:-]{1,512})$/.exec(uri); if (!match || match[2] !== collection || match[3] === '.' || match[3] === '..') { throw new Error('Invalid record URI'); } validateDid(match[1]); if (owner && match[1] !== owner) throw new Error('Record is not owned by this account'); return match[3];}
function validatePost(post: InteractionPost) { recordUri(post.uri, 'app.bsky.feed.post'); if (!ComAtprotoRepoStrongRef.validateMain(post).success) throw new Error('Invalid post URI/CID');}
export function createInteractions(agent: InteractionAgent, accountDid: string) { validateDid(accountDid); const pending = new Set<string>(); let closed = false;
async function write<T>( family: string, subject: string, operation: () => Promise<T> ): Promise<T> { if (closed) throw new Error('Interactions are closed'); const key = `${family}:${subject}`; if (pending.has(key)) throw new Error('Interaction already pending'); pending.add(key); try { return await operation(); } finally { pending.delete(key); } }
function validateActor(did: string) { validateDid(did); if (did === accountDid) throw new Error('Cannot interact with yourself'); }
return { like(post: InteractionPost) { return write('like', post.uri, async () => { validatePost(post); return (await agent.like(post.uri, post.cid)).uri; }); }, unlike(postUri: string, likeUri: string) { return write('like', postUri, async () => { recordUri(postUri, 'app.bsky.feed.post'); recordUri(likeUri, 'app.bsky.feed.like', accountDid); await agent.deleteLike(likeUri); }); }, repost(post: InteractionPost) { return write('repost', post.uri, async () => { validatePost(post); return (await agent.repost(post.uri, post.cid)).uri; }); }, unrepost(postUri: string, repostUri: string) { return write('repost', postUri, async () => { recordUri(postUri, 'app.bsky.feed.post'); recordUri(repostUri, 'app.bsky.feed.repost', accountDid); await agent.deleteRepost(repostUri); }); }, bookmark(post: InteractionPost) { return write('bookmark', post.uri, async () => { validatePost(post); await agent.app.bsky.bookmark.createBookmark({ uri: post.uri, cid: post.cid }); }); }, unbookmark(postUri: string) { return write('bookmark', postUri, async () => { recordUri(postUri, 'app.bsky.feed.post'); await agent.app.bsky.bookmark.deleteBookmark({ uri: postUri }); }); }, follow(did: string) { return write('follow', did, async () => { validateActor(did); return (await agent.follow(did)).uri; }); }, unfollow(did: string, followUri: string) { return write('follow', did, async () => { validateActor(did); recordUri(followUri, 'app.bsky.graph.follow', accountDid); await agent.deleteFollow(followUri); }); }, mute(did: string) { return write('mute', did, async () => { validateActor(did); await agent.mute(did); }); }, unmute(did: string) { return write('mute', did, async () => { validateActor(did); await agent.unmute(did); }); }, block(did: string) { return write('block', did, async () => { validateActor(did); return ( await agent.app.bsky.graph.block.create( { repo: accountDid }, { subject: did, createdAt: new Date().toISOString() } ) ).uri; }); }, unblock(did: string, blockUri: string) { return write('block', did, async () => { validateActor(did); const rkey = recordUri(blockUri, 'app.bsky.graph.block', accountDid); await agent.app.bsky.graph.block.delete({ repo: accountDid, rkey }); }); }, close() { closed = true; } };}