Add method to fetch attachments by media name (has UNIQUE constraint and therefore has index)

This commit is contained in:
Harry 2024-10-01 12:56:58 -07:00 committed by GitHub
parent 1a6aa23512
commit 950d6343db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 0 deletions

View File

@ -33,6 +33,12 @@ public protocol AttachmentStore {
tx: DBReadTransaction
) -> Attachment?
/// Fetch attachment by media name. There can be only one match.
func fetchAttachment(
mediaName: String,
tx: DBReadTransaction
) -> Attachment?
/// Enumerate all references to a given attachment id, calling the block for each one.
/// Blocks until all references have been enumerated.
func enumerateAllReferences(

View File

@ -33,6 +33,17 @@ public class AttachmentStoreImpl: AttachmentStore {
)
}
public func fetchAttachment(
mediaName: String,
tx: DBReadTransaction
) -> Attachment? {
try? fetchAttachment(
mediaName: mediaName,
db: SDSDB.shimOnlyBridge(tx).unwrapGrdbRead.database,
tx: tx
)
}
public func enumerateAllReferences(
toAttachmentId attachmentId: Attachment.IDType,
tx: DBReadTransaction,
@ -306,6 +317,17 @@ public class AttachmentStoreImpl: AttachmentStore {
.map(Attachment.init(record:))
}
func fetchAttachment(
mediaName: String,
db: GRDB.Database,
tx: DBReadTransaction
) throws -> Attachment? {
return try Attachment.Record
.filter(Column(Attachment.Record.CodingKeys.mediaName) == mediaName)
.fetchOne(db)
.map(Attachment.init(record:))
}
func allQuotedReplyAttachments(
forOriginalAttachmentId originalAttachmentId: Attachment.IDType,
db: GRDB.Database,

View File

@ -31,6 +31,13 @@ open class AttachmentStoreMock: AttachmentStore {
return nil
}
open func fetchAttachment(
mediaName: String,
tx: DBReadTransaction
) -> Attachment? {
return nil
}
open func enumerateAllReferences(
toAttachmentId: Attachment.IDType,
tx: DBReadTransaction,