Something went wrong. Try again.
A live media wall for your Bluesky follows
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import type { AppBskyFeedDefs } from '@atproto/api';
export const postCountLabels = { likeCount: ['like', 'likes'], repostCount: ['repost', 'reposts'], replyCount: ['reply', 'replies'], quoteCount: ['quote', 'quotes'], bookmarkCount: ['bookmark', 'bookmarks']} as const;
type CountKey = keyof typeof postCountLabels;export type PostCounts = Pick<AppBskyFeedDefs.PostView, CountKey>;
export type PostInteractionChanges = { likeUri?: string | null; repostUri?: string | null; bookmarked?: boolean;};
export function applyPostInteractions( post: AppBskyFeedDefs.PostView, { likeUri, repostUri, bookmarked }: PostInteractionChanges): AppBskyFeedDefs.PostView { const viewer = { ...post.viewer }; const updated = { ...post, viewer }; if (likeUri !== undefined) viewer.like = likeUri ?? undefined; if (repostUri !== undefined) viewer.repost = repostUri ?? undefined; if (bookmarked !== undefined) viewer.bookmarked = bookmarked;
// Compare with the incoming viewer state so refreshed posts that already include // the change, or local posts processed again, don't count it twice. for (const [countKey, viewerKey] of [ ['likeCount', 'like'], ['repostCount', 'repost'], ['bookmarkCount', 'bookmarked'] ] as const) { const count = post[countKey]; const delta = Number(!!viewer[viewerKey]) - Number(!!post.viewer?.[viewerKey]); if (count !== undefined && Number.isSafeInteger(count) && count >= 0 && delta !== 0) updated[countKey] = Math.max(0, Math.min(Number.MAX_SAFE_INTEGER, count + delta)); } return updated;}
export function readPostCounts(post: AppBskyFeedDefs.PostView): PostCounts | undefined { const counts: PostCounts = {}; for (const key of Object.keys(postCountLabels) as CountKey[]) { const count = post[key]; if (typeof count === 'number' && Number.isSafeInteger(count) && count >= 0) counts[key] = count; } return Object.keys(counts).length ? counts : undefined;}