refactor on the way that attaching disappearing messages config to messages

pull/731/head
Ryan ZHAO 1 year ago
parent 9fc5f8fa68
commit e38fb400ea

@ -235,6 +235,7 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate {
body: String(data: messageInfoData, encoding: .utf8),
timestampMs: timestampMs
)
.withDisappearingMessagesConfiguration(db)
.inserted(db)
self.callInteractionId = interaction?.id

@ -315,7 +315,10 @@ public enum PushRegistrationError: Error {
variant: .infoCall,
body: messageInfoString,
timestampMs: timestampMs
).inserted(db)
)
.withDisappearingMessagesConfiguration(db)
.inserted(db)
call.callInteractionId = interaction.id
return call

@ -476,6 +476,20 @@ public extension Interaction {
openGroupWhisperTo: self.openGroupWhisperTo
)
}
func withDisappearingMessagesConfiguration(
_ db: Database,
disappearingMessagesConfiguration: DisappearingMessagesConfiguration? = nil
) -> Interaction {
if let config = disappearingMessagesConfiguration ?? (try? DisappearingMessagesConfiguration.fetchOne(db, id: self.threadId)) {
return self.with(
expiresInSeconds: config.durationSeconds,
expiresStartedAtMs: (config.type == .disappearAfterSend ? Double(self.timestampMs) : nil)
)
}
return self
}
}
// MARK: - GRDB Interactions

@ -190,7 +190,7 @@ public final class CallMessage: ControlMessage {
let contentProto = SNProtoContent.builder()
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId)
setDisappearingMessagesConfigurationIfNeeded(on: contentProto)
do {
contentProto.setCallMessage(try callMessageProto.build())

@ -98,7 +98,7 @@ public final class DataExtractionNotification: ControlMessage {
let contentProto = SNProtoContent.builder()
contentProto.setDataExtractionNotification(try dataExtractionNotification.build())
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId)
setDisappearingMessagesConfigurationIfNeeded(on: contentProto)
return try contentProto.build()
} catch {
SNLog("Couldn't construct data extraction notification proto from: \(self).")

@ -78,7 +78,7 @@ public final class ExpirationTimerUpdate: ControlMessage {
let contentProto = SNProtoContent.builder()
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId)
setDisappearingMessagesConfigurationIfNeeded(on: contentProto)
do {
contentProto.setDataMessage(try dataMessageProto.build())

@ -65,7 +65,7 @@ public final class ReadReceipt: ControlMessage {
do {
contentProto.setReceiptMessage(try receiptProto.build())
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId)
setDisappearingMessagesConfigurationIfNeeded(on: contentProto)
return try contentProto.build()
} catch {
SNLog("Couldn't construct read receipt proto from: \(self).")

@ -66,17 +66,19 @@ public class Message: Codable {
preconditionFailure("toProto(_:) is abstract and must be overridden.")
}
public func setDisappearingMessagesConfigurationIfNeeded(_ db: Database, on proto: SNProtoContent.SNProtoContentBuilder, threadId: String) {
guard let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: threadId) else {
public func setDisappearingMessagesConfigurationIfNeeded(on proto: SNProtoContent.SNProtoContentBuilder) {
if let expiresInSeconds = self.expiresInSeconds {
proto.setExpirationTimer(UInt32(expiresInSeconds))
} else {
proto.setExpirationTimer(0)
proto.setExpirationType(.unknown)
return
}
let expireTimer: UInt32 = disappearingMessagesConfiguration.isEnabled ? UInt32(disappearingMessagesConfiguration.durationSeconds) : 0
proto.setExpirationTimer(expireTimer)
if disappearingMessagesConfiguration.isEnabled, let type = disappearingMessagesConfiguration.type {
proto.setExpirationType(type.toProto())
if let expiresStartedAtMs = self.expiresStartedAtMs, UInt64(expiresStartedAtMs) == self.sentTimestamp {
proto.setExpirationType(.deleteAfterSend)
} else {
proto.setExpirationType(.deleteAfterRead)
}
}

@ -185,7 +185,7 @@ public final class VisibleMessage: Message {
}
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: proto, threadId: threadId)
setDisappearingMessagesConfigurationIfNeeded(on: proto)
// Sync target
if let syncTarget = syncTarget {

@ -228,7 +228,9 @@ extension MessageReceiver {
timestampMs: (messageSentTimestamp * 1000),
userPublicKey: getUserHexEncodedPublicKey(db),
openGroup: nil
)
),
expiresInSeconds: message.expiresInSeconds,
expiresStartedAtMs: message.expiresStartedAtMs
)
.inserted(db)

Loading…
Cancel
Save