session-ios/SessionMessagingKit/Database/Models/InteractionAttachment.swift
Morgan Pretty 32304ae5dd Cleared out some of the legacy serialisation logic, further UI binding
Refactored the SignalApp class to Swift
Fixed a horizontal alignment issue in the ConversationTitleView
Fixed an issue where expiration timer update messages weren't migrated or rendering correctly
Fixed an issue where expiring messages weren't migrated correctly
Fixed an issue where closed groups which had been left were causing migration failures (due to data incorrectly being assumed to be required)
Shifted the Legacy Attachment types into the 'SMKLegacy' namespace
Moved all of the NSCoding logic for the TSMessage
2022-05-03 17:14:56 +10:00

54 lines
2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
public struct InteractionAttachment: Codable, FetchableRecord, PersistableRecord, TableRecord, ColumnExpressible {
public static var databaseTableName: String { "interactionAttachment" }
internal static let interactionForeignKey = ForeignKey([Columns.interactionId], to: [Interaction.Columns.id])
internal static let attachmentForeignKey = ForeignKey([Columns.attachmentId], to: [Attachment.Columns.id])
internal static let interaction = belongsTo(Interaction.self, using: interactionForeignKey)
internal static let attachment = belongsTo(Attachment.self, using: attachmentForeignKey)
public typealias Columns = CodingKeys
public enum CodingKeys: String, CodingKey, ColumnExpression {
case interactionId
case attachmentId
}
public let interactionId: Int64
public let attachmentId: String
// MARK: - Relationships
public var interaction: QueryInterfaceRequest<Interaction> {
request(for: InteractionAttachment.interaction)
}
public var attachment: QueryInterfaceRequest<Attachment> {
request(for: InteractionAttachment.attachment)
}
// MARK: - Custom Database Interaction
public func delete(_ db: Database) throws -> Bool {
// If we have an Attachment then check if this is the only type that is referencing it
// and delete the Attachment if so
let quoteUses: Int? = try? Quote
.select(.attachmentId)
.filter(Quote.Columns.attachmentId == attachmentId)
.fetchCount(db)
let linkPreviewUses: Int? = try? LinkPreview
.select(.attachmentId)
.filter(LinkPreview.Columns.attachmentId == attachmentId)
.fetchCount(db)
if (quoteUses ?? 0) == 0 && (linkPreviewUses ?? 0) == 0 {
try attachment.deleteAll(db)
}
return try performDelete(db)
}
}