Skip to content

Commit

Permalink
get some tests passing!
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 8, 2023
1 parent b1dfbde commit 2298406
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 33 deletions.
4 changes: 4 additions & 0 deletions packages/bsky/src/data-plane/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import mutes from './mutes'
import notifs from './notifs'
import posts from './posts'
import profile from './profile'
import records from './records'
import relationships from './relationships'
import reposts from './reposts'
import search from './search'
import suggestions from './suggestions'
Expand All @@ -34,6 +36,8 @@ export default (db: Database) => (router: ConnectRouter) =>
...notifs(db),
...posts(db),
...profile(db),
...records(db),
...relationships(db),
...reposts(db),
...search(db),
...suggestions(db),
Expand Down
6 changes: 3 additions & 3 deletions packages/bsky/src/data-plane/server/routes/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import * as ui8 from 'uint8arrays'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getLabels(req) {
const { subjects, issuers } = req
if (subjects.length === 0 || issuers.length === 0) {
// @TODO add in issues param
const { subjects } = req
if (subjects.length === 0) {
return { records: [] }
}
const res = await db.db
.selectFrom('label')
.where('src', 'in', issuers)
.where('uri', 'in', subjects)
.selectAll()
.execute()
Expand Down
14 changes: 4 additions & 10 deletions packages/bsky/src/data-plane/server/routes/notifs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Database } from '../../../db'
import { countAll, excluded } from '../../../db/util'
import { sql } from 'kysely'
import { TimeCidKeyset, paginate } from '../../../db/pagination'
import { Timestamp } from '@bufbuild/protobuf'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getNotifications(req) {
Expand Down Expand Up @@ -46,9 +47,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
const notifications = notifsRes.map((notif) => ({
uri: notif.uri,
reason: notif.reason,
timestamp: {
nanos: new Date(notif.sortAt).getTime() * 1000,
},
timestamp: Timestamp.fromDate(new Date(notif.sortAt)),
}))
return {
notifications,
Expand All @@ -65,11 +64,8 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
if (!res) {
return {}
}
const nanos = new Date(res.lastSeenNotifs).getTime() * 1000
return {
timestamp: {
nanos,
},
timestamp: Timestamp.fromDate(new Date(res.lastSeenNotifs)),
}
},

Expand Down Expand Up @@ -101,9 +97,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
if (!timestamp) {
return
}
const lastSeenNotifs = new Date(
Math.floor(timestamp.nanos / 1000),
).toISOString()
const lastSeenNotifs = timestamp.toDate().toISOString()
await db.db
.insertInto('actor_state')
.values({ did: actorDid, lastSeenNotifs })
Expand Down
2 changes: 1 addition & 1 deletion packages/bsky/src/data-plane/server/routes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
const actors = dids.map((did, i) => {
return {
handle: byDid[did]?.handle ?? undefined,
profile: profiles[i],
profile: profiles.records[i],
}
})
return { actors }
Expand Down
5 changes: 2 additions & 3 deletions packages/bsky/src/data-plane/server/routes/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ui8 from 'uint8arrays'
import { Database } from '../../../db'
import { keyBy } from '@atproto/common'
import { Record } from '../../gen/bsky_pb'
import { Timestamp } from '@bufbuild/protobuf'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
getBlockRecords: getRecords(db),
Expand Down Expand Up @@ -38,9 +39,7 @@ export const getRecords =
return new Record({
record: recordBytes,
cid: row.cid,
indexedAt: {
nanos: new Date(row.indexedAt).getTime() * 1000,
},
indexedAt: Timestamp.fromDate(new Date(row.indexedAt)),
})
})
return { records }
Expand Down
10 changes: 2 additions & 8 deletions packages/bsky/src/hydration/actor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { CID } from 'multiformats/cid'
import { DataPlaneClient } from '../data-plane/client'
import { Record as ProfileRecord } from '../lexicon/types/app/bsky/actor/profile'
import {
HydrationMap,
parseCid,
parseRecordBytes,
parseString,
parseTimestamp,
} from './util'
import { HydrationMap, parseCid, parseRecordBytes, parseString } from './util'

export type Actor = {
did: string
Expand Down Expand Up @@ -69,7 +63,7 @@ export class ActorHydrator {
handle: parseString(actor.handle),
profile: parseRecordBytes<ProfileRecord>(actor.profile?.record),
profileCid: parseCid(actor.profile?.cid),
indexedAt: parseTimestamp(actor.profile?.indexedAt),
indexedAt: actor.profile?.indexedAt?.toDate(),
})
}, new HydrationMap<Actor>())
}
Expand Down
8 changes: 1 addition & 7 deletions packages/bsky/src/hydration/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type RecordInfo<T> = {
export const parseRecord = <T>(entry: Record): RecordInfo<T> | undefined => {
const record = parseRecordBytes<T>(entry.record)
const cid = parseCid(entry.cid)
const indexedAt = parseTimestamp(entry.indexedAt)
const indexedAt = entry.indexedAt?.toDate()
if (!record || !cid) return
return { record, cid, indexedAt }
}
Expand Down Expand Up @@ -54,9 +54,3 @@ export const parseCid = (cidStr: string | undefined): CID | undefined => {
return
}
}

export const parseTimestamp = (ts: Timestamp | undefined): Date | undefined => {
if (!ts) return undefined
const ms = Math.floor(ts.nanos / 1000)
return new Date(ms)
}
30 changes: 29 additions & 1 deletion packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AtUri, INVALID_HANDLE } from '@atproto/syntax'
import { AtUri, INVALID_HANDLE, normalizeDatetimeAlways } from '@atproto/syntax'
import { ImageUriBuilder } from '../image/uri'
import { HydrationState } from '../hydration/hydrator'
import { ids } from '../lexicon/lexicons'
Expand All @@ -16,6 +16,7 @@ import { ListView, ListViewBasic } from '../lexicon/types/app/bsky/graph/defs'
import { compositeTime, creatorFromUri } from './util'
import { mapDefined } from '@atproto/common'
import { isListRule } from '../lexicon/types/app/bsky/feed/threadgate'
import { isSelfLabels } from '../lexicon/types/com/atproto/label/defs'
import {
Embed,
EmbedBlocked,
Expand All @@ -35,6 +36,7 @@ import {
isRecordEmbed,
isRecordWithMedia,
} from './types'
import { Label } from '../hydration/label'

export class Views {
constructor(public imgUriBuilder: ImageUriBuilder) {}
Expand Down Expand Up @@ -93,6 +95,11 @@ export class Views {
const labels = [
...(state.labels?.get(did) ?? []),
...(state.labels?.get(profileUri) ?? []),
...this.selfLabels({
uri: profileUri,
cid: actor.profileCid?.toString(),
record: actor.profile,
}),
]
return {
did,
Expand Down Expand Up @@ -181,6 +188,27 @@ export class Views {
}
}

// Labels
// ------------

selfLabels(details: {
uri?: string
cid?: string
record?: Record<string, unknown>
}): Label[] {
const { uri, cid, record } = details
if (!uri || !cid || !record) return []
if (!isSelfLabels(record.labels)) return []
const src = new AtUri(uri).host // record creator
const cts =
typeof record.createdAt === 'string'
? normalizeDatetimeAlways(record.createdAt)
: new Date(0).toISOString()
return record.labels.values.map(({ val }) => {
return { src, uri, cid, val, cts, neg: false }
})
}

// Feed
// ------------

Expand Down

0 comments on commit 2298406

Please sign in to comment.