a51fc8e434
* Bump SDK * Use consistent type in profile query * Omit from constraint for profile shadow * Replace isRecord with isValidRecord in QuoteEmbed * Omit type from constraint for old ProfileCard * Omit type from constraint in profile queries where appropriate * Use correct type for update profile mutation * Conslidate and fix check for isValidRecord in Post.tsx * Replace isRecord with isValidRecord in PostThreadItem * Remove redundant cast in PostThreadFollowBtn * Ignore errors in DebugMod screen * Use matching type in ProfileFollows screen * Use matching type in ProfileFollowers screen * Migrate to isValidRecord in PostFeedItem * Use matching type if PostRepostedBy * Omit type from constraint in avatar props * Use matching types in NotificationFeedItem * Todo * Use isValidRecord in NotfyFeedItem * Improve MediaPreview types * Migrate another isValidRecord in NotificationFeedItem * Migrate to isValidView in queries/util * Migrate to isValidRecord in threadgate/util * Fix types in threadgates * Fix up types in starter-packs queries * Todo * Specify exact types in search-posts * Use internal type util to align types * Ditto last * Migrate postgate/index * Specify exact types in post-thread * Use correct type in post-quotes * FIX potential bug in post-thread * Use correct type in post-feed * Add correct type guards to notifications/feed * Migrate a guard in notifications/util * Migrate guard in Wizard/State * [@next] Profile handling, migrate `ProfileCard` (#7347) * Introduce new utils for profiles, migrate old ProfileCard * Rename, reorg * Add parseEmbed utils * Expand AnyProfileView to include chat profile view, update post shadow to reflect this * Cast for perf reasons * Tighten up types now that we have AnyProfileView * Add fastIsType util * Use `assertDid` Co-authored-by: Matthieu Sieben <matthieusieben@users.noreply.github.com> * Use util types * Comment * Use fastIsType where no validation was happening before * suggestions (#7382) * suggestions * Revert unneeded changes --------- Co-authored-by: Eric Bailey <git@esb.lol> * Use new util * Rename to dangerousIsType * Convert object shape * Use dangerous util * Use dangerous util * Use dangerous util, we can trust post records * Use dangerous util * Use AnyProfileVIew * Convert object shape * Clean up handling * Patch moderateProfile to accept known profile views, to discuss * Add AnyStarterPackView and related, implement in first usage * Remove validation, fix type, fix ref * Migrate over list-conversations * Clarify intent behind precacheProfile and its unstable query cache * Clean up unstable profile cache * Fix types during label creation in PwiOptOut (#7346) * Tighten types in queries/list * Chat: use correct profile views * Chat: fix log type check * Chat: construct lexically correct shape, even though it's only internal usage * Chat: use correct profile types * Chat: fix type check in logs * Starter: use correct profile types * Starter: use correct profile types * Starter: tighten types to match lex * Any profile type will work in blocked-and-muted * Use dangerous util * Use dangerousIsType * Update new ProfileCard to use AnyProfileView * Use dangerousIsType * Remove outdated todo * Use correct profile type * Use correct profile types * Tighten up types * Use dangerousIsType * Chat: more type fixes * Remove unused file * Add a few utils * Remove unused file * Ignore feedPost.__source * Clean up types, leave validation in critical path * Use dangerousIstype * Use ANyProfileView * Use isValidRecord * Use dangerousIsType * Fix types in ListCard * Fix FeedInterstitials types * Fix types in FeedCard * Fix types in dms ReportDialog * Fix types in SearchablePeopleList * Fix bad type in composer opts * Starter: ok these need to be loose too * Clarify docs Co-authored-by: Matthieu Sieben <matthieusieben@users.noreply.github.com> * Less code Co-authored-by: Matthieu Sieben <matthieusieben@users.noreply.github.com> * Use package exports Co-authored-by: Matthieu Sieben <matthieusieben@users.noreply.github.com> * Use package exports * Bump sdk, update $Typed imports * Format * Format * Fix weird TS error * Remove patch * Beter name * It's memo, can validate * Tighten up parseEmbed, dogfood * Bump sdk * Use asPredicate * Loosen types a bit * use asPredicate * Fix types * Use asPredicate * Use asPredicate * Use asPredicate * Clean up upsertProfile types * Use asPredicate * Use Un util * Fix types * Use new AnyProfileView * Use dangerousIsType * Use asPredicate * Use asPredicate * Add fallback content-type to pass typecheck * Clean up upsertProfile types * Align types * Use dangerousIsType * Use dangerousIsType * Use asPredicate * Align types * Convert findLast * Align types * Just ignore type errors and use findLast * Rename atproto -> bsky * Add validate util * Fix type error * Loosen types * Export post * rename atp bsky * Remove unused code * minor changes * Bump deps * Fix types * Tighten back up loose check * Tighten back up loose check * Fix small bug * Update comment * Revert change --------- Co-authored-by: Matthieu Sieben <matthieusieben@users.noreply.github.com> Co-authored-by: Matthieu Sieben <matthieu.sieben@gmail.com>
300 lines
7.3 KiB
TypeScript
300 lines
7.3 KiB
TypeScript
import React from 'react'
|
|
import {
|
|
AppBskyEmbedRecord,
|
|
AppBskyEmbedRecordWithMedia,
|
|
AppBskyFeedDefs,
|
|
AppBskyFeedPostgate,
|
|
AtUri,
|
|
BskyAgent,
|
|
} from '@atproto/api'
|
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
|
|
|
import {networkRetry, retry} from '#/lib/async/retry'
|
|
import {logger} from '#/logger'
|
|
import {updatePostShadow} from '#/state/cache/post-shadow'
|
|
import {STALE} from '#/state/queries'
|
|
import {useGetPosts} from '#/state/queries/post'
|
|
import {
|
|
createMaybeDetachedQuoteEmbed,
|
|
createPostgateRecord,
|
|
mergePostgateRecords,
|
|
POSTGATE_COLLECTION,
|
|
} from '#/state/queries/postgate/util'
|
|
import {useAgent} from '#/state/session'
|
|
import * as bsky from '#/types/bsky'
|
|
|
|
export async function getPostgateRecord({
|
|
agent,
|
|
postUri,
|
|
}: {
|
|
agent: BskyAgent
|
|
postUri: string
|
|
}): Promise<AppBskyFeedPostgate.Record | undefined> {
|
|
const urip = new AtUri(postUri)
|
|
|
|
if (!urip.host.startsWith('did:')) {
|
|
const res = await agent.resolveHandle({
|
|
handle: urip.host,
|
|
})
|
|
urip.host = res.data.did
|
|
}
|
|
|
|
try {
|
|
const {data} = await retry(
|
|
2,
|
|
e => {
|
|
/*
|
|
* If the record doesn't exist, we want to return null instead of
|
|
* throwing an error. NB: This will also catch reference errors, such as
|
|
* a typo in the URI.
|
|
*/
|
|
if (e.message.includes(`Could not locate record:`)) {
|
|
return false
|
|
}
|
|
return true
|
|
},
|
|
() =>
|
|
agent.api.com.atproto.repo.getRecord({
|
|
repo: urip.host,
|
|
collection: POSTGATE_COLLECTION,
|
|
rkey: urip.rkey,
|
|
}),
|
|
)
|
|
|
|
if (
|
|
data.value &&
|
|
bsky.validate(data.value, AppBskyFeedPostgate.validateRecord)
|
|
) {
|
|
return data.value
|
|
} else {
|
|
return undefined
|
|
}
|
|
} catch (e: any) {
|
|
/*
|
|
* If the record doesn't exist, we want to return null instead of
|
|
* throwing an error. NB: This will also catch reference errors, such as
|
|
* a typo in the URI.
|
|
*/
|
|
if (e.message.includes(`Could not locate record:`)) {
|
|
return undefined
|
|
} else {
|
|
throw e
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function writePostgateRecord({
|
|
agent,
|
|
postUri,
|
|
postgate,
|
|
}: {
|
|
agent: BskyAgent
|
|
postUri: string
|
|
postgate: AppBskyFeedPostgate.Record
|
|
}) {
|
|
const postUrip = new AtUri(postUri)
|
|
|
|
await networkRetry(2, () =>
|
|
agent.api.com.atproto.repo.putRecord({
|
|
repo: agent.session!.did,
|
|
collection: POSTGATE_COLLECTION,
|
|
rkey: postUrip.rkey,
|
|
record: postgate,
|
|
}),
|
|
)
|
|
}
|
|
|
|
export async function upsertPostgate(
|
|
{
|
|
agent,
|
|
postUri,
|
|
}: {
|
|
agent: BskyAgent
|
|
postUri: string
|
|
},
|
|
callback: (
|
|
postgate: AppBskyFeedPostgate.Record | undefined,
|
|
) => Promise<AppBskyFeedPostgate.Record | undefined>,
|
|
) {
|
|
const prev = await getPostgateRecord({
|
|
agent,
|
|
postUri,
|
|
})
|
|
const next = await callback(prev)
|
|
if (!next) return
|
|
await writePostgateRecord({
|
|
agent,
|
|
postUri,
|
|
postgate: next,
|
|
})
|
|
}
|
|
|
|
export const createPostgateQueryKey = (postUri: string) => [
|
|
'postgate-record',
|
|
postUri,
|
|
]
|
|
export function usePostgateQuery({postUri}: {postUri: string}) {
|
|
const agent = useAgent()
|
|
return useQuery({
|
|
staleTime: STALE.SECONDS.THIRTY,
|
|
queryKey: createPostgateQueryKey(postUri),
|
|
async queryFn() {
|
|
return (await getPostgateRecord({agent, postUri})) ?? null
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useWritePostgateMutation() {
|
|
const agent = useAgent()
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
postUri,
|
|
postgate,
|
|
}: {
|
|
postUri: string
|
|
postgate: AppBskyFeedPostgate.Record
|
|
}) => {
|
|
return writePostgateRecord({
|
|
agent,
|
|
postUri,
|
|
postgate,
|
|
})
|
|
},
|
|
onSuccess(_, {postUri}) {
|
|
queryClient.invalidateQueries({
|
|
queryKey: createPostgateQueryKey(postUri),
|
|
})
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useToggleQuoteDetachmentMutation() {
|
|
const agent = useAgent()
|
|
const queryClient = useQueryClient()
|
|
const getPosts = useGetPosts()
|
|
const prevEmbed = React.useRef<AppBskyFeedDefs.PostView['embed']>()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
post,
|
|
quoteUri,
|
|
action,
|
|
}: {
|
|
post: AppBskyFeedDefs.PostView
|
|
quoteUri: string
|
|
action: 'detach' | 'reattach'
|
|
}) => {
|
|
// cache here since post shadow mutates original object
|
|
prevEmbed.current = post.embed
|
|
|
|
if (action === 'detach') {
|
|
updatePostShadow(queryClient, post.uri, {
|
|
embed: createMaybeDetachedQuoteEmbed({
|
|
post,
|
|
quote: undefined,
|
|
quoteUri,
|
|
detached: true,
|
|
}),
|
|
})
|
|
}
|
|
|
|
await upsertPostgate({agent, postUri: quoteUri}, async prev => {
|
|
if (prev) {
|
|
if (action === 'detach') {
|
|
return mergePostgateRecords(prev, {
|
|
detachedEmbeddingUris: [post.uri],
|
|
})
|
|
} else if (action === 'reattach') {
|
|
return {
|
|
...prev,
|
|
detachedEmbeddingUris:
|
|
prev.detachedEmbeddingUris?.filter(uri => uri !== post.uri) ||
|
|
[],
|
|
}
|
|
}
|
|
} else {
|
|
if (action === 'detach') {
|
|
return createPostgateRecord({
|
|
post: quoteUri,
|
|
detachedEmbeddingUris: [post.uri],
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
async onSuccess(_data, {post, quoteUri, action}) {
|
|
if (action === 'reattach') {
|
|
try {
|
|
const [quote] = await getPosts({uris: [quoteUri]})
|
|
updatePostShadow(queryClient, post.uri, {
|
|
embed: createMaybeDetachedQuoteEmbed({
|
|
post,
|
|
quote,
|
|
quoteUri: undefined,
|
|
detached: false,
|
|
}),
|
|
})
|
|
} catch (e: any) {
|
|
// ok if this fails, it's just optimistic UI
|
|
logger.error(`Postgate: failed to get quote post for re-attachment`, {
|
|
safeMessage: e.message,
|
|
})
|
|
}
|
|
}
|
|
},
|
|
onError(_, {post, action}) {
|
|
if (action === 'detach' && prevEmbed.current) {
|
|
// detach failed, add the embed back
|
|
if (
|
|
AppBskyEmbedRecord.isView(prevEmbed.current) ||
|
|
AppBskyEmbedRecordWithMedia.isView(prevEmbed.current)
|
|
) {
|
|
updatePostShadow(queryClient, post.uri, {
|
|
embed: prevEmbed.current,
|
|
})
|
|
}
|
|
}
|
|
},
|
|
onSettled() {
|
|
prevEmbed.current = undefined
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useToggleQuotepostEnabledMutation() {
|
|
const agent = useAgent()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
postUri,
|
|
action,
|
|
}: {
|
|
postUri: string
|
|
action: 'enable' | 'disable'
|
|
}) => {
|
|
await upsertPostgate({agent, postUri: postUri}, async prev => {
|
|
if (prev) {
|
|
if (action === 'disable') {
|
|
return mergePostgateRecords(prev, {
|
|
embeddingRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
|
|
})
|
|
} else if (action === 'enable') {
|
|
return {
|
|
...prev,
|
|
embeddingRules: [],
|
|
}
|
|
}
|
|
} else {
|
|
if (action === 'disable') {
|
|
return createPostgateRecord({
|
|
post: postUri,
|
|
embeddingRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
})
|
|
}
|