Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {
2 type AppBskyActorDefs,
3 AppBskyEmbedRecord,
4 AppBskyEmbedRecordWithMedia,
5 type AppBskyFeedDefs,
6 AppBskyFeedPost,
7 type AtUri,
8} from '@atproto/api'
9import {
10 type InfiniteData,
11 type QueryClient,
12 type QueryKey,
13} from '@tanstack/react-query'
14
15import * as bsky from '#/types/bsky'
16
17export async function truncateAndInvalidate<T = any>(
18 queryClient: QueryClient,
19 queryKey: QueryKey,
20) {
21 queryClient.setQueriesData<InfiniteData<T>>({queryKey}, data => {
22 if (data) {
23 return {
24 pageParams: data.pageParams.slice(0, 1),
25 pages: data.pages.slice(0, 1),
26 }
27 }
28 return data
29 })
30 return queryClient.invalidateQueries({queryKey})
31}
32
33// Given an AtUri, this function will check if the AtUri matches a
34// hit regardless of whether the AtUri uses a DID or handle as a host.
35//
36// AtUri should be the URI that is being searched for, while currentUri
37// is the URI that is being checked. currentAuthor is the author
38// of the currentUri that is being checked.
39export function didOrHandleUriMatches(
40 atUri: AtUri,
41 record: {uri: string; author: AppBskyActorDefs.ProfileViewBasic},
42) {
43 if (atUri.host.startsWith('did:')) {
44 return atUri.href === record.uri
45 }
46
47 return atUri.host === record.author.handle && record.uri.endsWith(atUri.rkey)
48}
49
50export function getEmbeddedPost(
51 v: unknown,
52): AppBskyEmbedRecord.ViewRecord | undefined {
53 if (
54 bsky.dangerousIsType<AppBskyEmbedRecord.View>(v, AppBskyEmbedRecord.isView)
55 ) {
56 if (
57 AppBskyEmbedRecord.isViewRecord(v.record) &&
58 AppBskyFeedPost.isRecord(v.record.value)
59 ) {
60 return v.record
61 }
62 }
63 if (
64 bsky.dangerousIsType<AppBskyEmbedRecordWithMedia.View>(
65 v,
66 AppBskyEmbedRecordWithMedia.isView,
67 )
68 ) {
69 if (
70 AppBskyEmbedRecord.isViewRecord(v.record.record) &&
71 AppBskyFeedPost.isRecord(v.record.record.value)
72 ) {
73 return v.record.record
74 }
75 }
76}
77
78export function embedViewRecordToPostView(
79 v: AppBskyEmbedRecord.ViewRecord,
80): AppBskyFeedDefs.PostView {
81 return {
82 uri: v.uri,
83 cid: v.cid,
84 author: v.author,
85 record: v.value,
86 indexedAt: v.indexedAt,
87 labels: v.labels,
88 embed: v.embeds?.[0],
89 likeCount: v.likeCount,
90 quoteCount: v.quoteCount,
91 replyCount: v.replyCount,
92 repostCount: v.repostCount,
93 }
94}