WIP: refactoring the way of applying disappearing messages settings

pull/731/head
Ryan ZHAO 7 months ago
parent 7d0e0d5164
commit 9fc5f8fa68

@ -532,6 +532,8 @@ public class ConversationViewModel: OWSAudioPlayerDelegate {
].compactMap { $0 },
body: text
),
expiresInSeconds: threadData.disappearingMessagesConfiguration?.durationSeconds,
expiresStartedAtMs: (threadData.disappearingMessagesConfiguration?.type == .disappearAfterSend ? Double(sentTimestampMs) : nil),
linkPreviewUrl: linkPreviewDraft?.urlString
)
let optimisticAttachments: Attachment.PreparedData? = attachments

@ -144,19 +144,6 @@ public struct Interaction: Codable, Identifiable, Equatable, FetchableRecord, Mu
return false
}
}
fileprivate var shouldFollowDisappearingMessagesConfiguration: Bool {
switch self {
case .standardIncoming, .standardOutgoing,
.infoCall,
.infoDisappearingMessagesUpdate,
.infoClosedGroupCreated, .infoClosedGroupUpdated, .infoClosedGroupCurrentUserLeft, .infoClosedGroupCurrentUserLeaving,
.infoScreenshotNotification, .infoMediaSavedNotification:
return true
default: return false
}
}
}
/// The `id` value is auto incremented by the database, if the `Interaction` hasn't been inserted into
@ -373,23 +360,6 @@ public struct Interaction: Codable, Identifiable, Equatable, FetchableRecord, Mu
// Automatically mark interactions which can't be unread as read so the unread count
// isn't impacted
self.wasRead = (self.wasRead || !self.variant.canBeUnread)
// Automatically add disapeparing messages configuration
if self.variant.shouldFollowDisappearingMessagesConfiguration,
self.expiresInSeconds == nil,
self.expiresStartedAtMs == nil
{
guard
let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: self.threadId),
disappearingMessagesConfiguration.isEnabled
else {
self.expiresInSeconds = 0
return
}
self.expiresInSeconds = disappearingMessagesConfiguration.durationSeconds
}
}
public func aroundInsert(_ db: Database, insert: () throws -> InsertionSuccess) throws {
@ -848,8 +818,6 @@ public extension Interaction {
// MARK: - Variables
var isExpiringMessage: Bool {
guard variant.shouldFollowDisappearingMessagesConfiguration else { return false }
return (expiresInSeconds ?? 0 > 0)
}

@ -338,10 +338,7 @@ public final class ClosedGroupControlMessage: ControlMessage {
let contentProto = SNProtoContent.builder()
let dataMessageProto = SNProtoDataMessage.builder()
dataMessageProto.setClosedGroupControlMessage(try closedGroupControlMessage.build())
// DisappearingMessagesConfiguration
setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto, threadId: threadId)
contentProto.setDataMessage(try dataMessageProto.build())
return try contentProto.build()
} catch {

@ -652,6 +652,33 @@ public extension Message {
)
)
}
// MARK: - TTL for disappearing messages
internal static func getSpecifiedTTL(
message: Message,
isSyncMessage: Bool
) -> UInt64 {
// Not disappearing messages
guard let expiresInSeconds = message.expiresInSeconds else { return message.ttl }
// Sync message should be read already, it is the same for disappear after read and disappear after sent
guard !isSyncMessage else { return UInt64(expiresInSeconds * 1000) }
// Disappear after read messages that have not be read
guard let expiresStartedAtMs = message.expiresStartedAtMs else { return message.ttl }
// Disappear after read messages that have already be read
guard message.sentTimestamp == UInt64(expiresStartedAtMs) else { return message.ttl }
// Disappear after sent messages with expections
switch message {
case is ClosedGroupControlMessage, is UnsendRequest:
return message.ttl
default:
return UInt64(expiresInSeconds * 1000)
}
}
}
// MARK: - Mutation

@ -225,7 +225,7 @@ public extension VisibleMessage {
static func from(_ db: Database, interaction: Interaction) -> VisibleMessage {
let linkPreview: LinkPreview? = try? interaction.linkPreview.fetchOne(db)
return VisibleMessage(
var visibleMessage: VisibleMessage = VisibleMessage(
sender: interaction.authorId,
sentTimestamp: UInt64(interaction.timestampMs),
recipient: (try? interaction.recipientStates.fetchOne(db))?.recipientId,
@ -260,5 +260,10 @@ public extension VisibleMessage {
},
reaction: nil // Reactions are custom messages sent separately
)
visibleMessage.expiresInSeconds = interaction.expiresInSeconds
visibleMessage.expiresStartedAtMs = interaction.expiresStartedAtMs
return visibleMessage
}
}

@ -192,24 +192,4 @@ extension MessageSender {
}
.eraseToAnyPublisher()
}
// MARK: - Convenience
internal static func getSpecifiedTTL(
_ db: Database,
threadId: String,
message: Message,
isSyncMessage: Bool
) -> UInt64? {
guard
let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: threadId),
disappearingMessagesConfiguration.isEnabled,
(
disappearingMessagesConfiguration.type == .disappearAfterSend ||
isSyncMessage
)
else { return nil }
return UInt64(disappearingMessagesConfiguration.durationSeconds * 1000)
}
}

@ -348,9 +348,7 @@ public final class MessageSender {
let snodeMessage = SnodeMessage(
recipient: message.recipient!,
data: base64EncodedData,
ttl: MessageSender
.getSpecifiedTTL(db, threadId: threadId, message: message, isSyncMessage: isSyncMessage)
.defaulting(to: message.ttl),
ttl: Message.getSpecifiedTTL(message: message, isSyncMessage: isSyncMessage),
timestampMs: UInt64(messageSendTimestamp)
)

Loading…
Cancel
Save