Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appview: apply epoch for indexed-at times in views #2943

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/bsky/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ServerConfigValues {
modServiceDid: string
adminPasswords: string[]
labelsFromIssuerDids?: string[]
indexedAtEpoch?: Date
// misc/dev
blobCacheLocation?: string
statsigKey?: string
Expand Down Expand Up @@ -125,6 +126,13 @@ export class ServerConfig {
: process.env.BSKY_STATSIG_ENV || 'development'
const clientCheckEmailConfirmed =
process.env.BSKY_CLIENT_CHECK_EMAIL_CONFIRMED === 'true'
const indexedAtEpoch = process.env.BSKY_INDEXED_AT_EPOCH
? new Date(process.env.BSKY_INDEXED_AT_EPOCH)
: undefined
assert(
!indexedAtEpoch || !isNaN(indexedAtEpoch.getTime()),
'invalid BSKY_INDEXED_AT_EPOCH',
)
return new ServerConfig({
version,
debugMode,
Expand Down Expand Up @@ -161,6 +169,7 @@ export class ServerConfig {
statsigKey,
statsigEnv,
clientCheckEmailConfirmed,
indexedAtEpoch,
...stripUndefineds(overrides ?? {}),
})
}
Expand Down Expand Up @@ -317,6 +326,10 @@ export class ServerConfig {
get clientCheckEmailConfirmed() {
return this.cfg.clientCheckEmailConfirmed
}

get indexedAtEpoch() {
return this.cfg.indexedAtEpoch
}
}

function stripUndefineds(
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/hydration/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Actor = {
profileCid?: string
profileTakedownRef?: string
sortedAt?: Date
indexedAt?: Date
takedownRef?: string
isLabeler: boolean
allowIncomingChatsFrom?: string
Expand Down Expand Up @@ -126,6 +127,7 @@ export class ActorHydrator {
profileCid: profile?.cid,
profileTakedownRef: safeTakedownRef(profile),
sortedAt: profile?.sortedAt?.toDate(),
indexedAt: profile?.indexedAt?.toDate(),
takedownRef: safeTakedownRef(actor),
isLabeler: actor.labeler ?? false,
allowIncomingChatsFrom: actor.allowIncomingChatsFrom || undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/src/hydration/hydrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ export class Hydrator {
record: actor.profile,
cid: actor.profileCid,
sortedAt: actor.sortedAt ?? new Date(0), // @NOTE will be present since profile record is present
indexedAt: actor.indexedAt ?? new Date(0), // @NOTE will be present since profile record is present
takedownRef: actor.profileTakedownRef,
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/bsky/src/hydration/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type RecordInfo<T> = {
record: T
cid: string
sortedAt: Date
indexedAt: Date
takedownRef: string | undefined
}

Expand Down Expand Up @@ -65,6 +66,7 @@ export const parseRecord = <T>(
const record = parseRecordBytes<T>(entry.record)
const cid = entry.cid
const sortedAt = entry.sortedAt?.toDate() ?? new Date(0)
const indexedAt = entry.indexedAt?.toDate() ?? new Date(0)
if (!record || !cid) return
if (!isValidRecord(record)) {
return
Expand All @@ -73,6 +75,7 @@ export const parseRecord = <T>(
record,
cid,
sortedAt,
indexedAt,
takedownRef: safeTakedownRef(entry),
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/bsky/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export class BskyAppView {
rejectUnauthorized: !config.dataplaneIgnoreBadTls,
})
const hydrator = new Hydrator(dataplane, config.labelsFromIssuerDids)
const views = new Views(imgUriBuilder, videoUriBuilder)
const views = new Views({
imgUriBuilder: imgUriBuilder,
videoUriBuilder: videoUriBuilder,
indexedAtEpoch: config.indexedAtEpoch,
})

const bsyncClient = createBsyncClient({
baseUrl: config.bsyncUrl,
Expand Down
42 changes: 30 additions & 12 deletions packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ import { Notification } from '../proto/bsky_pb'
import { postUriToThreadgateUri, postUriToPostgateUri } from '../util/uris'

export class Views {
public imgUriBuilder: ImageUriBuilder = this.opts.imgUriBuilder
public videoUriBuilder: VideoUriBuilder = this.opts.videoUriBuilder
public indexedAtEpoch: Date | undefined = this.opts.indexedAtEpoch
constructor(
public imgUriBuilder: ImageUriBuilder,
public videoUriBuilder: VideoUriBuilder,
private opts: {
imgUriBuilder: ImageUriBuilder
videoUriBuilder: VideoUriBuilder
indexedAtEpoch: Date | undefined
},
) {}

// Actor
Expand Down Expand Up @@ -182,7 +188,13 @@ export class Views {
return {
...basicView,
description: actor.profile?.description || undefined,
indexedAt: actor.sortedAt?.toISOString(),
indexedAt:
actor.indexedAt && actor.sortedAt
? this.indexedAt({
sortedAt: actor.sortedAt,
indexedAt: actor.indexedAt,
}).toISOString()
: undefined,
}
}

Expand Down Expand Up @@ -330,7 +342,7 @@ export class Views {
creator,
description: list.record.description,
descriptionFacets: list.record.descriptionFacets,
indexedAt: list.sortedAt.toISOString(),
indexedAt: this.indexedAt(list).toISOString(),
}
}

Expand All @@ -356,7 +368,7 @@ export class Views {
)
: undefined,
listItemCount: listAgg?.listItems ?? 0,
indexedAt: list.sortedAt.toISOString(),
indexedAt: this.indexedAt(list).toISOString(),
labels,
viewer: listViewer
? {
Expand Down Expand Up @@ -386,7 +398,7 @@ export class Views {
joinedAllTimeCount: agg?.joinedAllTime ?? 0,
joinedWeekCount: agg?.joinedWeek ?? 0,
labels,
indexedAt: sp.sortedAt.toISOString(),
indexedAt: this.indexedAt(sp).toISOString(),
}
}

Expand Down Expand Up @@ -463,7 +475,7 @@ export class Views {
like: viewer.like,
}
: undefined,
indexedAt: labeler.sortedAt.toISOString(),
indexedAt: this.indexedAt(labeler).toISOString(),
labels,
}
}
Expand Down Expand Up @@ -551,7 +563,7 @@ export class Views {
like: viewer.like,
}
: undefined,
indexedAt: feedgen.sortedAt.toISOString(),
indexedAt: this.indexedAt(feedgen).toISOString(),
}
}

Expand Down Expand Up @@ -600,7 +612,7 @@ export class Views {
repostCount: aggs?.reposts ?? 0,
likeCount: aggs?.likes ?? 0,
quoteCount: aggs?.quotes ?? 0,
indexedAt: post.sortedAt.toISOString(),
indexedAt: this.indexedAt(post).toISOString(),
viewer: viewer
? {
repost: viewer.repost,
Expand Down Expand Up @@ -724,7 +736,7 @@ export class Views {
return {
$type: 'app.bsky.feed.defs#reasonRepost',
by: creator,
indexedAt: repost.sortedAt.toISOString(),
indexedAt: this.indexedAt(repost).toISOString(),
}
}

Expand Down Expand Up @@ -1170,11 +1182,12 @@ export class Views {
} else if (uri.collection === ids.AppBskyActorProfile) {
const actor = state.actors?.get(authorDid)
recordInfo =
actor && actor.profile && actor.profileCid && actor.sortedAt
actor && actor.profile && actor.profileCid
? {
record: actor.profile,
cid: actor.profileCid,
sortedAt: actor.sortedAt,
sortedAt: actor.sortedAt ?? new Date(0), // @NOTE will be present since profile record is present
indexedAt: actor.indexedAt ?? new Date(0), // @NOTE will be present since profile record is present
takedownRef: actor.profileTakedownRef,
}
: null
Expand Down Expand Up @@ -1202,6 +1215,11 @@ export class Views {
labels: [...labels, ...selfLabels],
}
}

indexedAt({ sortedAt, indexedAt }: { sortedAt: Date; indexedAt: Date }) {
if (!this.indexedAtEpoch) return sortedAt
return indexedAt && indexedAt > this.indexedAtEpoch ? indexedAt : sortedAt
}
}

const getRootUri = (uri: string, post: Post): string => {
Expand Down