Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into xrpc-passthru
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Oct 24, 2023
2 parents c043f1e + 825b2a0 commit b200007
Show file tree
Hide file tree
Showing 106 changed files with 692 additions and 573 deletions.
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const skeleton = async (

if (filter === 'posts_with_media') {
feedItemsQb = feedItemsQb
// and only your own posts/reposts
.where('post.creator', '=', actorDid)
// only your own posts
.where('type', '=', 'post')
// only posts with media
.whereExists((qb) =>
qb
Expand Down
55 changes: 33 additions & 22 deletions packages/bsky/src/api/app/bsky/notification/listNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ const skeleton = async (
}
let notifBuilder = db.db
.selectFrom('notification as notif')
.innerJoin('record', 'record.uri', 'notif.recordUri')
.innerJoin('actor as author', 'author.did', 'notif.author')
.where(notSoftDeletedClause(ref('record')))
.where(notSoftDeletedClause(ref('author')))
.where('notif.did', '=', viewer)
.where((clause) =>
clause
Expand All @@ -69,23 +65,20 @@ const skeleton = async (
),
)
.select([
'notif.author as authorDid',
'notif.recordUri as uri',
'notif.recordCid as cid',
'author.did as authorDid',
'author.handle as authorHandle',
'author.indexedAt as authorIndexedAt',
'author.takedownId as authorTakedownId',
'notif.reason as reason',
'notif.reasonSubject as reasonSubject',
'notif.sortAt as indexedAt',
'record.json as recordJson',
])

const keyset = new NotifsKeyset(ref('notif.sortAt'), ref('notif.recordCid'))
notifBuilder = paginate(notifBuilder, {
cursor,
limit,
keyset,
tryIndex: true,
})

const actorStateQuery = db.db
Expand All @@ -107,17 +100,18 @@ const skeleton = async (
}

const hydration = async (state: SkeletonState, ctx: Context) => {
const { graphService, actorService, labelService } = ctx
const { graphService, actorService, labelService, db } = ctx
const { params, notifs } = state
const { viewer } = params
const dids = notifs.map((notif) => notif.authorDid)
const uris = notifs.map((notif) => notif.uri)
const [actors, labels, bam] = await Promise.all([
const [actors, records, labels, bam] = await Promise.all([
actorService.views.profiles(dids, viewer),
getRecordMap(db, uris),
labelService.getLabelsForUris(uris),
graphService.getBlockAndMuteState(dids.map((did) => [viewer, did])),
])
return { ...state, actors, labels, bam }
return { ...state, actors, records, labels, bam }
}

const noBlockOrMutes = (state: HydrationState) => {
Expand All @@ -131,11 +125,11 @@ const noBlockOrMutes = (state: HydrationState) => {
}

const presentation = (state: HydrationState) => {
const { notifs, cursor, actors, labels, lastSeenNotifs } = state
const { notifs, cursor, actors, records, labels, lastSeenNotifs } = state
const notifications = mapDefined(notifs, (notif) => {
const author = actors[notif.authorDid]
if (!author) return undefined
const record = jsonStringToLex(notif.recordJson) as Record<string, unknown>
const record = records[notif.uri]
if (!author || !record) return undefined
const recordLabels = labels[notif.uri] ?? []
const recordSelfLabels = getSelfLabels({
uri: notif.uri,
Expand All @@ -157,6 +151,24 @@ const presentation = (state: HydrationState) => {
return { notifications, cursor }
}

const getRecordMap = async (
db: Database,
uris: string[],
): Promise<RecordMap> => {
if (!uris.length) return {}
const { ref } = db.db.dynamic
const recordRows = await db.db
.selectFrom('record')
.select(['uri', 'json'])
.where('uri', 'in', uris)
.where(notSoftDeletedClause(ref('record')))
.execute()
return recordRows.reduce((acc, { uri, json }) => {
acc[uri] = jsonStringToLex(json) as Record<string, unknown>
return acc
}, {} as RecordMap)
}

type Context = {
db: Database
actorService: ActorService
Expand All @@ -178,20 +190,19 @@ type SkeletonState = {
type HydrationState = SkeletonState & {
bam: BlockAndMuteState
actors: ActorInfoMap
records: RecordMap
labels: Labels
}

type RecordMap = { [uri: string]: Record<string, unknown> }

type NotifRow = {
indexedAt: string
cid: string
uri: string
authorDid: string
authorHandle: string | null
authorIndexedAt: string
authorTakedownId: number | null
uri: string
cid: string
reason: string
reasonSubject: string | null
recordJson: string
indexedAt: string
}

class NotifsKeyset extends TimeCidKeyset<NotifRow> {
Expand Down
8 changes: 8 additions & 0 deletions packages/bsky/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export const httpLogger: ReturnType<typeof subsystemLogger> =

export const loggerMiddleware = pinoHttp({
logger: httpLogger,
serializers: {
err: (err) => {
return {
code: err?.code,
message: err?.message,
}
},
},
})
8 changes: 7 additions & 1 deletion packages/bsky/src/services/actor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ export class ActorService {
qb = qb.orWhere('actor.did', 'in', dids)
}
if (handles.length) {
qb = qb.orWhere('actor.handle', 'in', handles)
qb = qb.orWhere(
'actor.handle',
'in',
handles.length === 1
? [handles[0], handles[0]] // a silly (but worthwhile) optimization to avoid usage of actor_handle_tgrm_idx
: handles,
)
}
return qb
})
Expand Down
1 change: 1 addition & 0 deletions packages/dev-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@atproto/xrpc-server": "workspace:^",
"@did-plc/lib": "^0.0.1",
"@did-plc/server": "^0.0.1",
"axios": "^0.27.2",
"better-sqlite3": "^7.6.2",
"chalk": "^5.0.1",
"dotenv": "^16.0.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/dev-env/src/bsky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class TestBsky {
})
// indexer
const ns = cfg.dbPostgresSchema
? await randomIntFromSeed(cfg.dbPostgresSchema, 10000)
? await randomIntFromSeed(cfg.dbPostgresSchema, 1000000)
: undefined
const indexerCfg = new bsky.IndexerConfig({
version: '0.0.0',
Expand Down Expand Up @@ -200,7 +200,7 @@ export async function getIngester(
opts: { name: string } & Partial<bsky.IngesterConfigValues>,
) {
const { name, ...config } = opts
const ns = name ? await randomIntFromSeed(name, 10000) : undefined
const ns = name ? await randomIntFromSeed(name, 1000000) : undefined
const cfg = new bsky.IngesterConfig({
version: '0.0.0',
redisHost: process.env.REDIS_HOST || '',
Expand Down Expand Up @@ -234,7 +234,7 @@ export async function getIndexers(
},
): Promise<BskyIndexers> {
const { name, ...config } = opts
const ns = name ? await randomIntFromSeed(name, 10000) : undefined
const ns = name ? await randomIntFromSeed(name, 1000000) : undefined
const baseCfg: bsky.IndexerConfigValues = {
version: '0.0.0',
didCacheStaleTTL: HOUR,
Expand Down
5 changes: 3 additions & 2 deletions packages/dev-env/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios'
import { IdResolver } from '@atproto/identity'
import { TestPds } from './pds'
import { TestBsky } from './bsky'
Expand Down Expand Up @@ -39,8 +40,8 @@ export const mockResolvers = (idResolver: IdResolver, pds: TestPds) => {

const url = `${pds.url}/.well-known/atproto-did`
try {
const res = await fetch(url, { headers: { host: handle } })
return await res.text()
const res = await axios.get(url, { headers: { host: handle } })
return res.data
} catch (err) {
return undefined
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dev-infra/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ get_container_id() {
exit 1
fi

# first line of jq normalizes for docker compose breaking change, see docker/compose#10958
docker compose -f $compose_file ps --format json --status running \
| jq -sc '.[] | if type=="array" then .[] else . end' | jq -s \
| jq -r '.[]? | select(.Service == "'${service}'") | .ID'
}

Expand Down
4 changes: 2 additions & 2 deletions packages/pds/src/api/app/bsky/actor/getPreferences.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import { AuthScope } from '../../../../auth'
import { AuthScope } from '../../../../auth-verifier'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.getPreferences({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async ({ auth }) => {
const requester = auth.credentials.did
const { services, db } = ctx
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/actor/getProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LocalRecords } from '../../../../services/local'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.getProfile({
auth: ctx.accessOrRoleVerifier,
auth: ctx.authVerifier.accessOrRole,
handler: async ({ req, auth, params }) => {
const requester =
auth.credentials.type === 'access' ? auth.credentials.did : null
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/actor/getProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { handleReadAfterWrite } from '../util/read-after-write'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.getProfiles({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async ({ auth, params }) => {
const requester = auth.credentials.did
const res = await ctx.appViewAgent.api.app.bsky.actor.getProfiles(
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.getSuggestions({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/actor/putPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InvalidRequestError } from '@atproto/xrpc-server'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.putPreferences({
auth: ctx.accessVerifierCheckTakedown,
auth: ctx.authVerifier.accessCheckTakedown,
handler: async ({ auth, input }) => {
const { preferences } = input.body
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/actor/searchActors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.searchActors({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.searchActorsTypeahead({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getActorFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getActorFeeds({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getActorLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LocalRecords } from '../../../../services/local'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getActorLikes({
auth: ctx.accessOrRoleVerifier,
auth: ctx.authVerifier.accessOrRole,
handler: async ({ req, params, auth }) => {
const requester =
auth.credentials.type === 'access' ? auth.credentials.did : null
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isReasonRepost } from '../../../../lexicon/types/app/bsky/feed/defs'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getAuthorFeed({
auth: ctx.accessOrRoleVerifier,
auth: ctx.authVerifier.accessOrRole,
handler: async ({ req, params, auth }) => {
const requester =
auth.credentials.type === 'access' ? auth.credentials.did : null
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeed({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { params, auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeedGenerator({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getFeedGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeedGenerators({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getLikes({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getListFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { proxy } from '@atproto/xrpc-server'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getListFeed({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getPostThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getPostThread({
auth: ctx.accessOrRoleVerifier,
auth: ctx.authVerifier.accessOrRole,
handler: async (request) => {
const { params, auth } = request
const requester =
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getPosts({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/api/app/bsky/feed/getRepostedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getRepostedBy({
auth: ctx.accessVerifier,
auth: ctx.authVerifier.access,
handler: async (request) => {
const { auth } = request
const requester = auth.credentials.did
Expand Down
Loading

0 comments on commit b200007

Please sign in to comment.