Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Dec 17, 2024
1 parent 4f2552a commit fa64d4d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 69 deletions.
18 changes: 9 additions & 9 deletions packages/bsky/src/hydration/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
HydrationMap,
RecordInfo,
parseRecord,
parseRecordBytes,
parseString,
safeTakedownRef,
} from './util'
Expand Down Expand Up @@ -116,18 +115,19 @@ export class ActorHydrator {
) {
return acc.set(did, null)
}
const profile =
includeTakedowns || !actor.profile?.takenDown
? actor.profile
: undefined

const profile = actor.profile
? parseRecord<ProfileRecord>(actor.profile, includeTakedowns)
: undefined

return acc.set(did, {
did,
handle: parseString(actor.handle),
profile: parseRecordBytes<ProfileRecord>(profile?.record),
profile: profile?.record,
profileCid: profile?.cid,
profileTakedownRef: safeTakedownRef(profile),
sortedAt: profile?.sortedAt?.toDate(),
indexedAt: profile?.indexedAt?.toDate(),
profileTakedownRef: profile?.takedownRef,
sortedAt: profile?.sortedAt,
indexedAt: profile?.indexedAt,
takedownRef: safeTakedownRef(actor),
isLabeler: actor.labeler ?? false,
allowIncomingChatsFrom: actor.allowIncomingChatsFrom || undefined,
Expand Down
5 changes: 3 additions & 2 deletions packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
ThreadgateView,
isPostView,
} from '../lexicon/types/app/bsky/feed/defs'
import { isValidRecord as isValidPostRecord } from '../lexicon/types/app/bsky/feed/post'
import { isRecord as isPostRecord } from '../lexicon/types/app/bsky/feed/post'
import {
ListView,
ListViewBasic,
Expand Down Expand Up @@ -719,10 +719,11 @@ export class Views {
let grandparentAuthor: ProfileViewBasic | undefined
if (
isPostView(parent) &&
isValidPostRecord(parent.record) &&
isPostRecord(parent.record) &&
parent.record.reply
) {
grandparentAuthor = this.profileBasic(
// @ts-expect-error isValidPostRecord(parent.record) should be used but the "parent" is not IPDL decoded
creatorFromUri(parent.record.reply.parent.uri),
state,
)
Expand Down
46 changes: 23 additions & 23 deletions packages/bsky/tests/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
isThreadViewPost,
} from '../src/lexicon/types/app/bsky/feed/defs'
import { isViewRecord } from '../src/lexicon/types/app/bsky/embed/record'
import { isView as isRecordView } from '../src/lexicon/types/app/bsky/embed/record'
import { isView as isRecordWithMediaView } from '../src/lexicon/types/app/bsky/embed/recordWithMedia'
import { isView as isEmbedRecordView } from '../src/lexicon/types/app/bsky/embed/record'
import { isView as isEmbedRecordWithMediaView } from '../src/lexicon/types/app/bsky/embed/recordWithMedia'
import { AppBskyFeedGetPostThread } from '@atproto/api'
import {
LabelerView,
Expand Down Expand Up @@ -209,28 +209,13 @@ export function stripViewerFromPost(
delete post.$type
}
post.author = stripViewer(post.author)
const recordEmbed = isRecordView(post.embed)
? isViewRecord(post.embed.record)
? post.embed.record
: undefined
: isRecordWithMediaView(post.embed)
? isViewRecord(post.embed.record.record)
? post.embed.record.record
: undefined
: undefined

const recordEmbed = extractRecordEmbed(post.embed)

if (recordEmbed) {
recordEmbed.author = stripViewer(recordEmbed.author)
recordEmbed.embeds?.forEach((deepEmbed) => {
const deepRecordEmbed = isRecordView(deepEmbed)
? isViewRecord(deepEmbed.record)
? deepEmbed.record
: undefined
: isRecordWithMediaView(deepEmbed)
? isViewRecord(deepEmbed.record.record)
? deepEmbed.record.record
: undefined
: undefined
const deepRecordEmbed = extractRecordEmbed(deepEmbed)
if (deepRecordEmbed) {
deepRecordEmbed.author = stripViewer(deepRecordEmbed.author)
}
Expand All @@ -239,11 +224,26 @@ export function stripViewerFromPost(
return stripViewer(post)
}

const extractRecordEmbed = (embed: PostView['embed']) =>
isEmbedRecordView(embed)
? isViewRecord(embed.record)
? embed.record
: undefined
: isEmbedRecordWithMediaView(embed)
? isViewRecord(embed.record.record)
? embed.record.record
: undefined
: undefined

// @NOTE mutates
export const stripViewerFromThread = <T>(thread: T): T => {
if (!isThreadViewPost(thread)) return thread
// @ts-expect-error
export const stripViewerFromThread = <T>(threadUnknown: T): T => {
if (!isThreadViewPost(threadUnknown)) return threadUnknown

const thread = threadUnknown as typeof threadUnknown & ThreadViewPost

// @ts-expect-error "viewer" does not exist on type 'ThreadViewPost'
delete thread.viewer

thread.post = stripViewerFromPost(thread.post)
if (isThreadViewPost(thread.parent)) {
thread.parent = stripViewerFromThread(thread.parent)
Expand Down
14 changes: 11 additions & 3 deletions packages/bsky/tests/views/author-feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
stripViewer,
stripViewerFromPost,
} from '../_util'
import { ReplyRef, isRecord } from '../../src/lexicon/types/app/bsky/feed/post'
import {
ReplyRef,
isValidReplyRef,
isRecord,
} from '../../src/lexicon/types/app/bsky/feed/post'
import { isView as isEmbedRecordWithMedia } from '../../src/lexicon/types/app/bsky/embed/recordWithMedia'
import { isView as isImageEmbed } from '../../src/lexicon/types/app/bsky/embed/images'
import { isPostView } from '../../src/lexicon/types/app/bsky/feed/defs'
Expand Down Expand Up @@ -366,15 +370,19 @@ describe('pds author feed views', () => {
// does not include eve's replies to fred, even within her own thread.
expect(
eveFeed.feed.every(({ post, reply }) => {
if (!post || !isRecord(post.record) || !post.record.reply) {
if (
!post ||
!isRecord(post.record) ||
!isValidReplyRef(post.record.reply)
) {
return true // not a reply
}
const replyToEve = isReplyTo(post.record.reply, eve)
const replyToReplyByEve =
reply &&
isPostView(reply.parent) &&
isRecord(reply.parent.record) &&
(!reply.parent.record.reply ||
(!isValidReplyRef(reply.parent.record.reply) ||
isReplyTo(reply.parent.record.reply, eve))
return replyToEve && replyToReplyByEve
}),
Expand Down
40 changes: 21 additions & 19 deletions packages/bsky/tests/views/feed-view-post.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert'
import { AtpAgent, AppBskyFeedDefs, AtUri } from '@atproto/api'
import { TestNetwork, SeedClient, basicSeed } from '@atproto/dev-env'
import { ids } from '../../src/lexicon/lexicons'
Expand Down Expand Up @@ -87,8 +88,8 @@ describe('pds thread views', () => {
throw new Error('sliceA or sliceB is undefined')
}

expect(AppBskyFeedDefs.isBlockedPost(sliceB.reply?.parent)).toBe(true)
expect(AppBskyFeedDefs.isBlockedPost(sliceB.reply?.root)).toBe(true)
assert(AppBskyFeedDefs.isBlockedPost(sliceB.reply?.parent))
assert(AppBskyFeedDefs.isBlockedPost(sliceB.reply?.root))

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -129,10 +130,10 @@ describe('pds thread views', () => {
throw new Error('sliceC is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceC.reply.parent))
expect(sliceC.reply.parent.uri).toEqual(B.ref.uriStr)
assert(AppBskyFeedDefs.isBlockedPost(sliceC.reply.root))
expect(sliceC.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceC.reply.parent)).toBe(true)
expect(AppBskyFeedDefs.isBlockedPost(sliceC.reply.root)).toBe(true)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -249,10 +250,10 @@ describe('pds thread views', () => {
throw new Error('sliceD is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceD.reply.parent))
expect(sliceD.reply.parent.uri).toEqual(C.ref.uriStr)
assert(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root))
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceD.reply.parent)).toBe(true)
expect(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root)).toBe(true)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -292,10 +293,10 @@ describe('pds thread views', () => {
throw new Error('sliceD is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceD.reply.parent))
expect(sliceD.reply.parent.uri).toEqual(C.ref.uriStr)
assert(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root))
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceD.reply.parent)).toBe(true)
expect(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root)).toBe(true)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -338,13 +339,13 @@ describe('pds thread views', () => {
throw new Error('sliceD is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceD.reply.parent))
expect(sliceD.reply.parent.uri).toEqual(C.ref.uriStr)
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceD.reply.parent)).toBe(true)
/*
* We don't walk the reply ancestors past whats available in the ReplyRef
*/
expect(AppBskyFeedDefs.isPostView(sliceD.reply.root)).toBe(true)
assert(AppBskyFeedDefs.isPostView(sliceD.reply.root))
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -384,13 +385,14 @@ describe('pds thread views', () => {
throw new Error('sliceD is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceD.reply.parent))
expect(sliceD.reply.parent.uri).toEqual(C.ref.uriStr)
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceD.reply.parent)).toBe(true)

/*
* We don't walk the reply ancestors past whats available in the ReplyRef
*/
expect(AppBskyFeedDefs.isPostView(sliceD.reply.root)).toBe(true)
assert(AppBskyFeedDefs.isPostView(sliceD.reply.root))
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: bob, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -434,9 +436,9 @@ describe('pds thread views', () => {
throw new Error('sliceD or sliceC is undefined')
}

expect(AppBskyFeedDefs.isBlockedPost(sliceD.reply?.root)).toBe(true)
expect(AppBskyFeedDefs.isPostView(sliceC.reply?.parent)).toBe(true)
expect(AppBskyFeedDefs.isPostView(sliceC.reply?.root)).toBe(true)
assert(AppBskyFeedDefs.isBlockedPost(sliceD.reply?.root))
assert(AppBskyFeedDefs.isPostView(sliceC.reply?.parent))
assert(AppBskyFeedDefs.isPostView(sliceC.reply?.root))

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down Expand Up @@ -488,10 +490,10 @@ describe('pds thread views', () => {
throw new Error('sliceD is undefined')
}

assert(AppBskyFeedDefs.isPostView(sliceD.reply.parent))
expect(sliceD.reply.parent.uri).toEqual(C.ref.uriStr)
assert(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root))
expect(sliceD.reply.root.uri).toEqual(A.ref.uriStr)
expect(AppBskyFeedDefs.isPostView(sliceD.reply.parent)).toBe(true)
expect(AppBskyFeedDefs.isBlockedPost(sliceD.reply.root)).toBe(true)

await pdsAgent.api.app.bsky.graph.block.delete(
{ repo: alice, rkey: new AtUri(block.uri).rkey },
Expand Down
4 changes: 3 additions & 1 deletion packages/bsky/tests/views/labels-takedown.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AtpAgent } from '@atproto/api'
import assert from 'node:assert'
import { AppBskyLabelerDefs, AtpAgent } from '@atproto/api'
import { TestNetwork, SeedClient, basicSeed, RecordRef } from '@atproto/dev-env'
import { ids } from '../../src/lexicon/lexicons'

Expand Down Expand Up @@ -203,6 +204,7 @@ describe('bsky takedown labels', () => {
dids: [sc.dids.labeler1, sc.dids.labeler2],
})
expect(res.data.views.length).toBe(1)
assert(AppBskyLabelerDefs.isLabelerView(res.data.views[0])) // OR isLabelerViewDetailed ?
expect(res.data.views[0].creator?.['did']).toBe(sc.dids.labeler2)
})

Expand Down
6 changes: 3 additions & 3 deletions packages/bsky/tests/views/starter-packs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'node:assert'
import { AtpAgent } from '@atproto/api'
import { TestNetwork, SeedClient, RecordRef, basicSeed } from '@atproto/dev-env'
import { isRecord as isProfile } from '../../src/lexicon/types/app/bsky/actor/profile'
import { isValidRecord as isValidProfile } from '../../src/lexicon/types/app/bsky/actor/profile'
import { forSnapshot } from '../_util'
import assert from 'assert'
import { ids } from '../../src/lexicon/lexicons'

describe('starter packs', () => {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('starter packs', () => {
expect(notif.reason).toBe('starterpack-joined')
expect(notif.reasonSubject).toBe(sp1.uriStr)
expect(notif.uri).toMatch(/\/app\.bsky\.actor\.profile\/self$/)
assert(isProfile(notif.record), 'record is not profile')
assert(isValidProfile(notif.record), 'record is not profile')
expect(notif.record.joinedViaStarterPack?.uri).toBe(sp1.uriStr)
})
expect(forSnapshot(notifications)).toMatchSnapshot()
Expand Down
11 changes: 2 additions & 9 deletions packages/bsky/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
{
"include": [],
"references": [
{ "path": "./tsconfig.build.json" }

// The following files validate properly in VSCode, but for some reason,
// generate errors when compiled with tsc ("pnpm verify:types"). I'm not
// sure why, but I'm not going to spend time figuring it out right now:
// - "./tests/views/thread.test.ts"
// - "./tests/views/threadgating.test.ts"

// { "path": "./tsconfig.tests.json" }
{ "path": "./tsconfig.build.json" },
{ "path": "./tsconfig.tests.json" }
]
}

0 comments on commit fa64d4d

Please sign in to comment.