-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,87 @@ | ||
import { InvalidRequestError } from '@atproto/xrpc-server' | ||
import { mapDefined } from '@atproto/common' | ||
import { Server } from '../../../../lexicon' | ||
import { QueryParams } from '../../../../lexicon/types/app/bsky/feed/getActorFeeds' | ||
import AppContext from '../../../../context' | ||
import { TimeCidKeyset, paginate } from '../../../../db/pagination' | ||
import { createPipelineNew, noRulesNew } from '../../../../pipeline' | ||
import { HydrationState, Hydrator } from '../../../../hydration/hydrator' | ||
import { Views } from '../../../../views' | ||
import { DataPlaneClient } from '../../../../data-plane' | ||
import { parseString } from '../../../../hydration/util' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
const getActorFeeds = createPipelineNew( | ||
skeleton, | ||
hydration, | ||
noRulesNew, | ||
presentation, | ||
) | ||
server.app.bsky.feed.getActorFeeds({ | ||
auth: ctx.authOptionalVerifier, | ||
handler: async ({ auth, params }) => { | ||
const { actor, limit, cursor } = params | ||
const viewer = auth.credentials.did | ||
|
||
const db = ctx.db.getReplica() | ||
const actorService = ctx.services.actor(db) | ||
const feedService = ctx.services.feed(db) | ||
|
||
const creatorRes = await actorService.getActor(actor) | ||
if (!creatorRes) { | ||
throw new InvalidRequestError(`Actor not found: ${actor}`) | ||
const result = await getActorFeeds({ ...params, viewer }, ctx) | ||
return { | ||
encoding: 'application/json', | ||
body: result, | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
const { ref } = db.db.dynamic | ||
let feedsQb = feedService | ||
.selectFeedGeneratorQb(viewer) | ||
.where('feed_generator.creator', '=', creatorRes.did) | ||
const skeleton = async (inputs: { | ||
ctx: Context | ||
params: Params | ||
}): Promise<Skeleton> => { | ||
const { ctx, params } = inputs | ||
const [did] = await ctx.hydrator.actor.getDids([params.actor]) | ||
if (!did) { | ||
throw new InvalidRequestError('Profile not found') | ||
} | ||
const feedsRes = await ctx.dataplane.getActorFeeds({ | ||
actorDid: did, | ||
cursor: params.cursor, | ||
limit: params.limit, | ||
}) | ||
return { | ||
feedUris: feedsRes.uris, | ||
cursor: parseString(feedsRes.cursor), | ||
} | ||
} | ||
|
||
const keyset = new TimeCidKeyset( | ||
ref('feed_generator.createdAt'), | ||
ref('feed_generator.cid'), | ||
) | ||
feedsQb = paginate(feedsQb, { | ||
limit, | ||
cursor, | ||
keyset, | ||
}) | ||
const hydration = async (inputs: { | ||
ctx: Context | ||
params: Params | ||
skeleton: Skeleton | ||
}) => { | ||
const { ctx, params, skeleton } = inputs | ||
return await ctx.hydrator.hydrateFeedGens(skeleton.feedUris, params.viewer) | ||
} | ||
|
||
const [feedsRes, profiles] = await Promise.all([ | ||
feedsQb.execute(), | ||
actorService.views.profiles([creatorRes], viewer), | ||
]) | ||
if (!profiles[creatorRes.did]) { | ||
throw new InvalidRequestError(`Actor not found: ${actor}`) | ||
} | ||
const presentation = (inputs: { | ||
ctx: Context | ||
skeleton: Skeleton | ||
hydration: HydrationState | ||
}) => { | ||
const { ctx, skeleton, hydration } = inputs | ||
const feeds = mapDefined(skeleton.feedUris, (uri) => | ||
ctx.views.feedGenerator(uri, hydration), | ||
) | ||
return { | ||
feeds, | ||
cursor: skeleton.cursor, | ||
} | ||
} | ||
|
||
const feeds = mapDefined(feedsRes, (row) => { | ||
const feed = { | ||
...row, | ||
viewer: viewer ? { like: row.viewerLike } : undefined, | ||
} | ||
return feedService.views.formatFeedGeneratorView(feed, profiles) | ||
}) | ||
type Context = { | ||
hydrator: Hydrator | ||
views: Views | ||
dataplane: DataPlaneClient | ||
} | ||
|
||
return { | ||
encoding: 'application/json', | ||
body: { | ||
cursor: keyset.packFromResult(feedsRes), | ||
feeds, | ||
}, | ||
} | ||
}, | ||
}) | ||
type Params = QueryParams & { viewer: string | null } | ||
|
||
type Skeleton = { | ||
feedUris: string[] | ||
cursor?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.