-
Notifications
You must be signed in to change notification settings - Fork 578
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
25 changed files
with
342 additions
and
581 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import stream from 'stream' | ||
import { CID } from 'multiformats/cid' | ||
import { BlobNotFoundError, BlobStore } from '@atproto/repo' | ||
import { InvalidRequestError } from '@atproto/xrpc-server' | ||
import { ActorDb } from '../actor-db' | ||
import { notSoftDeletedClause } from '../../db/util' | ||
|
||
export class BlobReader { | ||
constructor(public db: ActorDb, public blobstore: BlobStore) {} | ||
|
||
async getBlob( | ||
cid: CID, | ||
): Promise<{ size: number; mimeType?: string; stream: stream.Readable }> { | ||
const { ref } = this.db.db.dynamic | ||
const found = await this.db.db | ||
.selectFrom('blob') | ||
.selectAll() | ||
.innerJoin('repo_blob', 'repo_blob.cid', 'blob.cid') | ||
.where('blob.cid', '=', cid.toString()) | ||
.where(notSoftDeletedClause(ref('repo_blob'))) | ||
.executeTakeFirst() | ||
if (!found) { | ||
throw new InvalidRequestError('Blob not found') | ||
} | ||
let blobStream | ||
try { | ||
blobStream = await this.blobstore.getStream(cid) | ||
} catch (err) { | ||
if (err instanceof BlobNotFoundError) { | ||
throw new InvalidRequestError('Blob not found') | ||
} | ||
throw err | ||
} | ||
return { | ||
size: found.size, | ||
mimeType: found.mimeType, | ||
stream: blobStream, | ||
} | ||
} | ||
|
||
async listBlobs(opts: { | ||
since?: string | ||
cursor?: string | ||
limit: number | ||
}): Promise<string[]> { | ||
const { since, cursor, limit } = opts | ||
let builder = this.db.db | ||
.selectFrom('repo_blob') | ||
.select('cid') | ||
.orderBy('cid', 'asc') | ||
.limit(limit) | ||
if (since) { | ||
builder = builder.where('repoRev', '>', since) | ||
} | ||
if (cursor) { | ||
builder = builder.where('cid', '>', cursor) | ||
} | ||
const res = await builder.execute() | ||
return res.map((row) => row.cid) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ActorDb } from '../actor-db' | ||
|
||
export class PreferenceReader { | ||
constructor(public db: ActorDb) {} | ||
|
||
async getPreferences(namespace?: string): Promise<UserPreference[]> { | ||
const prefsRes = await this.db.db | ||
.selectFrom('user_pref') | ||
.orderBy('id') | ||
.selectAll() | ||
.execute() | ||
return prefsRes | ||
.filter((pref) => !namespace || prefMatchNamespace(namespace, pref.name)) | ||
.map((pref) => JSON.parse(pref.valueJson)) | ||
} | ||
} | ||
|
||
export type UserPreference = Record<string, unknown> & { $type: string } | ||
|
||
export const prefMatchNamespace = (namespace: string, fullname: string) => { | ||
return fullname === namespace || fullname.startsWith(`${namespace}.`) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.