import type { MediaItem } from './feed.js'; import type { InteractionController } from './interaction-controller.js'; import { mediaAssetKey } from './media-identity.js'; export const POST_CONTEXT = Symbol('post-controls'); export type PostAction = 'like' | 'bookmark' | 'repost'; export type PostActionResult = 'activated' | 'deactivated' | 'ignored'; export type PostControls = { enabled: boolean; pending: ReadonlySet; errors: ReadonlyMap; change(item: MediaItem, action: PostAction): Promise; }; export function postActionKey(uri: string, action: PostAction) { return `${action}:${uri}`; } export function postActionTarget(item: MediaItem) { return JSON.stringify([item.uri, item.post?.cid, mediaAssetKey(item)]); } export async function writePostAction( controller: Pick< InteractionController, 'like' | 'unlike' | 'repost' | 'unrepost' | 'bookmark' | 'unbookmark' >, uri: string, state: NonNullable, action: PostAction ): Promise<'activated' | 'deactivated'> { const post = { uri, cid: state.cid }; if (action === 'like') { if (state.likeUri) { await controller.unlike(post, state.likeUri); return 'deactivated'; } await controller.like(post); } else if (action === 'repost') { if (state.repostUri) { await controller.unrepost(post, state.repostUri); return 'deactivated'; } await controller.repost(post); } else { if (state.bookmarked) { await controller.unbookmark(post); return 'deactivated'; } await controller.bookmark(post); } return 'activated'; }