Skip to content

Commit

Permalink
Get posts method (bluesky-social#876)
Browse files Browse the repository at this point in the history
* implement getPosts method

* tests

* bsky tests

* comment out test

* Handle stripping record-with-media viewer info in bsky tests

---------

Co-authored-by: Devin Ivy <[email protected]>
  • Loading branch information
dholms and devinivy authored Apr 24, 2023
1 parent 47890d6 commit 553f599
Show file tree
Hide file tree
Showing 27 changed files with 1,663 additions and 23 deletions.
34 changes: 34 additions & 0 deletions lexicons/app/bsky/feed/getPosts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"lexicon": 1,
"id": "app.bsky.feed.getPosts",
"defs": {
"main": {
"type": "query",
"description": "A view of an actor's feed.",
"parameters": {
"type": "params",
"required": ["uris"],
"properties": {
"uris": {
"type": "array",
"items": {"type": "string", "format": "at-uri"},
"maxLength": 25
}
}
},
"output": {
"encoding": "application/json",
"schema": {
"type": "object",
"required": ["posts"],
"properties": {
"posts": {
"type": "array",
"items": {"type": "ref", "ref": "app.bsky.feed.defs#postView"}
}
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions packages/api/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import * as AppBskyFeedDefs from './types/app/bsky/feed/defs'
import * as AppBskyFeedGetAuthorFeed from './types/app/bsky/feed/getAuthorFeed'
import * as AppBskyFeedGetLikes from './types/app/bsky/feed/getLikes'
import * as AppBskyFeedGetPostThread from './types/app/bsky/feed/getPostThread'
import * as AppBskyFeedGetPosts from './types/app/bsky/feed/getPosts'
import * as AppBskyFeedGetRepostedBy from './types/app/bsky/feed/getRepostedBy'
import * as AppBskyFeedGetTimeline from './types/app/bsky/feed/getTimeline'
import * as AppBskyFeedLike from './types/app/bsky/feed/like'
Expand Down Expand Up @@ -174,6 +175,7 @@ export * as AppBskyFeedDefs from './types/app/bsky/feed/defs'
export * as AppBskyFeedGetAuthorFeed from './types/app/bsky/feed/getAuthorFeed'
export * as AppBskyFeedGetLikes from './types/app/bsky/feed/getLikes'
export * as AppBskyFeedGetPostThread from './types/app/bsky/feed/getPostThread'
export * as AppBskyFeedGetPosts from './types/app/bsky/feed/getPosts'
export * as AppBskyFeedGetRepostedBy from './types/app/bsky/feed/getRepostedBy'
export * as AppBskyFeedGetTimeline from './types/app/bsky/feed/getTimeline'
export * as AppBskyFeedLike from './types/app/bsky/feed/like'
Expand Down Expand Up @@ -1118,6 +1120,17 @@ export class FeedNS {
})
}

getPosts(
params?: AppBskyFeedGetPosts.QueryParams,
opts?: AppBskyFeedGetPosts.CallOptions,
): Promise<AppBskyFeedGetPosts.Response> {
return this._service.xrpc
.call('app.bsky.feed.getPosts', params, undefined, opts)
.catch((e) => {
throw AppBskyFeedGetPosts.toKnownErr(e)
})
}

getRepostedBy(
params?: AppBskyFeedGetRepostedBy.QueryParams,
opts?: AppBskyFeedGetRepostedBy.CallOptions,
Expand Down
44 changes: 42 additions & 2 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2912,8 +2912,7 @@ export const schemaDict = {
latest: {
type: 'string',
format: 'cid',
description:
'The latest commit you in the commit range (inclusive',
description: 'The latest commit in the commit range (inclusive)',
},
},
},
Expand Down Expand Up @@ -4214,6 +4213,46 @@ export const schemaDict = {
},
},
},
AppBskyFeedGetPosts: {
lexicon: 1,
id: 'app.bsky.feed.getPosts',
defs: {
main: {
type: 'query',
description: "A view of an actor's feed.",
parameters: {
type: 'params',
required: ['uris'],
properties: {
uris: {
type: 'array',
items: {
type: 'string',
format: 'at-uri',
},
maxLength: 25,
},
},
},
output: {
encoding: 'application/json',
schema: {
type: 'object',
required: ['posts'],
properties: {
posts: {
type: 'array',
items: {
type: 'ref',
ref: 'lex:app.bsky.feed.defs#postView',
},
},
},
},
},
},
},
},
AppBskyFeedGetRepostedBy: {
lexicon: 1,
id: 'app.bsky.feed.getRepostedBy',
Expand Down Expand Up @@ -5036,6 +5075,7 @@ export const ids = {
AppBskyFeedGetAuthorFeed: 'app.bsky.feed.getAuthorFeed',
AppBskyFeedGetLikes: 'app.bsky.feed.getLikes',
AppBskyFeedGetPostThread: 'app.bsky.feed.getPostThread',
AppBskyFeedGetPosts: 'app.bsky.feed.getPosts',
AppBskyFeedGetRepostedBy: 'app.bsky.feed.getRepostedBy',
AppBskyFeedGetTimeline: 'app.bsky.feed.getTimeline',
AppBskyFeedLike: 'app.bsky.feed.like',
Expand Down
36 changes: 36 additions & 0 deletions packages/api/src/client/types/app/bsky/feed/getPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* GENERATED CODE - DO NOT MODIFY
*/
import { Headers, XRPCError } from '@atproto/xrpc'
import { ValidationResult, BlobRef } from '@atproto/lexicon'
import { isObj, hasProp } from '../../../../util'
import { lexicons } from '../../../../lexicons'
import { CID } from 'multiformats/cid'
import * as AppBskyFeedDefs from './defs'

export interface QueryParams {
uris: string[]
}

export type InputSchema = undefined

export interface OutputSchema {
posts: AppBskyFeedDefs.PostView[]
[k: string]: unknown
}

export interface CallOptions {
headers?: Headers
}

export interface Response {
success: boolean
headers: Headers
data: OutputSchema
}

export function toKnownErr(e: any) {
if (e instanceof XRPCError) {
}
return e
}
2 changes: 1 addition & 1 deletion packages/api/src/client/types/com/atproto/sync/getRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface QueryParams {
did: string
/** The earliest commit in the commit range (not inclusive) */
earliest?: string
/** The latest commit you in the commit range (inclusive */
/** The latest commit in the commit range (inclusive) */
latest?: string
}

Expand Down
41 changes: 41 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as common from '@atproto/common'
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import { AtUri } from '@atproto/uri'
import { PostView } from '@atproto/api/src/client/types/app/bsky/feed/defs'
import { authOptionalVerifier } from '../util'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getPosts({
auth: authOptionalVerifier,
handler: async ({ params, auth }) => {
const requester = auth.credentials.did

const feedService = ctx.services.feed(ctx.db)

const uris = common.dedupeStrs(params.uris)
const dids = common.dedupeStrs(
params.uris.map((uri) => new AtUri(uri).hostname),
)

const [actors, postViews, embeds] = await Promise.all([
feedService.getActorViews(Array.from(dids), requester),
feedService.getPostViews(Array.from(uris), requester),
feedService.embedsForPosts(Array.from(uris), requester),
])

const posts: PostView[] = []
for (const uri of uris) {
const post = feedService.formatPostView(uri, actors, postViews, embeds)
if (post) {
posts.push(post)
}
}

return {
encoding: 'application/json',
body: { posts },
}
},
})
}
2 changes: 2 additions & 0 deletions packages/bsky/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getTimeline from './app/bsky/feed/getTimeline'
import getAuthorFeed from './app/bsky/feed/getAuthorFeed'
import getLikes from './app/bsky/feed/getLikes'
import getPostThread from './app/bsky/feed/getPostThread'
import getPosts from './app/bsky/feed/getPosts'
import getProfile from './app/bsky/actor/getProfile'
import getProfiles from './app/bsky/actor/getProfiles'
import getRepostedBy from './app/bsky/feed/getRepostedBy'
Expand Down Expand Up @@ -38,6 +39,7 @@ export default function (server: Server, ctx: AppContext) {
getAuthorFeed(server, ctx)
getLikes(server, ctx)
getPostThread(server, ctx)
getPosts(server, ctx)
getProfile(server, ctx)
getProfiles(server, ctx)
getRepostedBy(server, ctx)
Expand Down
41 changes: 41 additions & 0 deletions packages/bsky/src/lexicon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import * as ComAtprotoRepoListRecords from './types/com/atproto/repo/listRecords
import * as ComAtprotoRepoPutRecord from './types/com/atproto/repo/putRecord'
import * as ComAtprotoRepoUploadBlob from './types/com/atproto/repo/uploadBlob'
import * as ComAtprotoServerCreateAccount from './types/com/atproto/server/createAccount'
import * as ComAtprotoServerCreateAppPassword from './types/com/atproto/server/createAppPassword'
import * as ComAtprotoServerCreateInviteCode from './types/com/atproto/server/createInviteCode'
import * as ComAtprotoServerCreateInviteCodes from './types/com/atproto/server/createInviteCodes'
import * as ComAtprotoServerCreateSession from './types/com/atproto/server/createSession'
Expand All @@ -45,10 +46,12 @@ import * as ComAtprotoServerDeleteSession from './types/com/atproto/server/delet
import * as ComAtprotoServerDescribeServer from './types/com/atproto/server/describeServer'
import * as ComAtprotoServerGetAccountInviteCodes from './types/com/atproto/server/getAccountInviteCodes'
import * as ComAtprotoServerGetSession from './types/com/atproto/server/getSession'
import * as ComAtprotoServerListAppPasswords from './types/com/atproto/server/listAppPasswords'
import * as ComAtprotoServerRefreshSession from './types/com/atproto/server/refreshSession'
import * as ComAtprotoServerRequestAccountDelete from './types/com/atproto/server/requestAccountDelete'
import * as ComAtprotoServerRequestPasswordReset from './types/com/atproto/server/requestPasswordReset'
import * as ComAtprotoServerResetPassword from './types/com/atproto/server/resetPassword'
import * as ComAtprotoServerRevokeAppPassword from './types/com/atproto/server/revokeAppPassword'
import * as ComAtprotoSyncGetBlob from './types/com/atproto/sync/getBlob'
import * as ComAtprotoSyncGetBlocks from './types/com/atproto/sync/getBlocks'
import * as ComAtprotoSyncGetCheckout from './types/com/atproto/sync/getCheckout'
Expand All @@ -69,6 +72,7 @@ import * as AppBskyActorSearchActorsTypeahead from './types/app/bsky/actor/searc
import * as AppBskyFeedGetAuthorFeed from './types/app/bsky/feed/getAuthorFeed'
import * as AppBskyFeedGetLikes from './types/app/bsky/feed/getLikes'
import * as AppBskyFeedGetPostThread from './types/app/bsky/feed/getPostThread'
import * as AppBskyFeedGetPosts from './types/app/bsky/feed/getPosts'
import * as AppBskyFeedGetRepostedBy from './types/app/bsky/feed/getRepostedBy'
import * as AppBskyFeedGetTimeline from './types/app/bsky/feed/getTimeline'
import * as AppBskyGraphGetFollowers from './types/app/bsky/graph/getFollowers'
Expand Down Expand Up @@ -419,6 +423,16 @@ export class ServerNS {
return this._server.xrpc.method(nsid, cfg)
}

createAppPassword<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
ComAtprotoServerCreateAppPassword.Handler<ExtractAuth<AV>>
>,
) {
const nsid = 'com.atproto.server.createAppPassword' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}

createInviteCode<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
Expand Down Expand Up @@ -484,6 +498,16 @@ export class ServerNS {
return this._server.xrpc.method(nsid, cfg)
}

listAppPasswords<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
ComAtprotoServerListAppPasswords.Handler<ExtractAuth<AV>>
>,
) {
const nsid = 'com.atproto.server.listAppPasswords' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}

refreshSession<AV extends AuthVerifier>(
cfg: ConfigOf<AV, ComAtprotoServerRefreshSession.Handler<ExtractAuth<AV>>>,
) {
Expand Down Expand Up @@ -517,6 +541,16 @@ export class ServerNS {
const nsid = 'com.atproto.server.resetPassword' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}

revokeAppPassword<AV extends AuthVerifier>(
cfg: ConfigOf<
AV,
ComAtprotoServerRevokeAppPassword.Handler<ExtractAuth<AV>>
>,
) {
const nsid = 'com.atproto.server.revokeAppPassword' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}
}

export class SyncNS {
Expand Down Expand Up @@ -725,6 +759,13 @@ export class FeedNS {
return this._server.xrpc.method(nsid, cfg)
}

getPosts<AV extends AuthVerifier>(
cfg: ConfigOf<AV, AppBskyFeedGetPosts.Handler<ExtractAuth<AV>>>,
) {
const nsid = 'app.bsky.feed.getPosts' // @ts-ignore
return this._server.xrpc.method(nsid, cfg)
}

getRepostedBy<AV extends AuthVerifier>(
cfg: ConfigOf<AV, AppBskyFeedGetRepostedBy.Handler<ExtractAuth<AV>>>,
) {
Expand Down
Loading

0 comments on commit 553f599

Please sign in to comment.