fix issue of syncing disappearing messages

pull/941/head
ryanzhao 1 year ago
parent dca70058e3
commit 6658c5edd5

@ -373,14 +373,21 @@ public struct Interaction: Codable, Identifiable, Equatable, FetchableRecord, Mu
self.wasRead = (self.wasRead || !self.variant.canBeUnread)
// Automatically add disapeparing messages configuration
if self.variant.shouldFollowDisappearingMessagesConfiguration,
self.expiresInSeconds == nil, self.expiresStartedAtMs == nil,
let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: self.threadId),
disappearingMessagesConfiguration.isEnabled
if
self.variant.shouldFollowDisappearingMessagesConfiguration &&
self.expiresInSeconds == nil &&
self.expiresStartedAtMs == nil
{
self.expiresInSeconds = disappearingMessagesConfiguration.durationSeconds
if disappearingMessagesConfiguration.type == .disappearAfterSend {
self.expiresStartedAtMs = Double(self.timestampMs)
if
let disappearingMessagesConfiguration = try? DisappearingMessagesConfiguration.fetchOne(db, id: self.threadId),
disappearingMessagesConfiguration.isEnabled
{
self.expiresInSeconds = disappearingMessagesConfiguration.durationSeconds
if disappearingMessagesConfiguration.type == .disappearAfterSend {
self.expiresStartedAtMs = Double(self.timestampMs)
}
} else {
self.expiresInSeconds = 0
}
}
}

@ -55,7 +55,7 @@ public extension DisappearingMessagesJob {
// If there is another expiring message then update the job to run 1 second after it's meant to expire
let nextExpirationTimestampMs: Double? = try? Interaction
.filter(Interaction.Columns.expiresStartedAtMs != nil)
.filter(Interaction.Columns.expiresInSeconds != nil)
.filter(Interaction.Columns.expiresInSeconds > 0)
.select(Interaction.Columns.expiresStartedAtMs + (Interaction.Columns.expiresInSeconds * 1000))
.order((Interaction.Columns.expiresStartedAtMs + (Interaction.Columns.expiresInSeconds * 1000)).asc)
.asRequest(of: Double.self)
@ -83,7 +83,7 @@ public extension DisappearingMessagesJob {
.filter(
Interaction.Columns.threadId == threadId &&
Interaction.Columns.timestampMs <= lastReadTimestampMs &&
Interaction.Columns.expiresInSeconds != nil &&
Interaction.Columns.expiresInSeconds > 0 &&
Interaction.Columns.expiresStartedAtMs == nil
)
.select(Interaction.Columns.serverHash)
@ -149,7 +149,7 @@ public extension DisappearingMessagesJob {
let interactionExpirationInfosByExpiresInSeconds: [TimeInterval: [ExpirationInfo]] = (try? Interaction
.filter(interactionIds.contains(Interaction.Columns.id))
.filter(
Interaction.Columns.expiresInSeconds != nil &&
Interaction.Columns.expiresInSeconds > 0 &&
Interaction.Columns.expiresStartedAtMs == nil
)
.select(
@ -166,7 +166,7 @@ public extension DisappearingMessagesJob {
let changeCount: Int? = try? Interaction
.filter(interactionIds.contains(Interaction.Columns.id))
.filter(
Interaction.Columns.expiresInSeconds != nil &&
Interaction.Columns.expiresInSeconds > 0 &&
Interaction.Columns.expiresStartedAtMs == nil
)
.updateAll(

@ -295,7 +295,7 @@ public enum GarbageCollectionJob: JobExecutor {
/// Remove interactions which should be disappearing after read but never be read within 14 days
if finalTypesToCollect.contains(.expiredUnreadDisappearingMessages) {
_ = try Interaction
.filter(Interaction.Columns.expiresInSeconds != nil)
.filter(Interaction.Columns.expiresInSeconds > 0)
.filter(Interaction.Columns.expiresStartedAtMs == nil)
.filter(Interaction.Columns.timestampMs < (timestampNow - fourteenDaysInSeconds) * 1000)
.deleteAll(db)

@ -105,30 +105,30 @@ extension MessageReceiver {
// Finally save the changes to the DisappearingMessagesConfiguration (If it's a duplicate
// then the interaction unique constraint will prevent the code from getting here)
try remoteConfig.save(db)
// Remove previous info messages
_ = try Interaction
.filter(Interaction.Columns.threadId == threadId)
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
.deleteAll(db)
// Add an info message for the user
_ = try Interaction(
serverHash: nil, // Intentionally null so sync messages are seen as duplicates
threadId: threadId,
authorId: sender,
variant: .infoDisappearingMessagesUpdate,
body: remoteConfig.messageInfoString(
with: (sender != getUserHexEncodedPublicKey(db) ?
Profile.displayName(db, id: sender) :
nil
),
isPreviousOff: false
),
timestampMs: timestampMs,
expiresInSeconds: (remoteConfig.isEnabled ? nil : localConfig.durationSeconds)
).inserted(db)
}
// Remove previous info messages
_ = try Interaction
.filter(Interaction.Columns.threadId == threadId)
.filter(Interaction.Columns.variant == Interaction.Variant.infoDisappearingMessagesUpdate)
.deleteAll(db)
// Add an info message for the user
_ = try Interaction(
serverHash: nil, // Intentionally null so sync messages are seen as duplicates
threadId: threadId,
authorId: sender,
variant: .infoDisappearingMessagesUpdate,
body: remoteConfig.messageInfoString(
with: (sender != getUserHexEncodedPublicKey(db) ?
Profile.displayName(db, id: sender) :
nil
),
isPreviousOff: false
),
timestampMs: timestampMs,
expiresInSeconds: (remoteConfig.isEnabled ? nil : localConfig.durationSeconds)
).inserted(db)
}
internal static func updateDisappearingMessagesConfigurationIfNeeded(
@ -171,7 +171,7 @@ extension MessageReceiver {
let isEnable: Bool = (durationSeconds != 0)
let disappearingType: DisappearingMessagesConfiguration.DisappearingMessageType? = (proto.hasExpirationType ?
.init(protoType: proto.expirationType) :
nil
.unknown
)
let remoteConfig: DisappearingMessagesConfiguration = localConfig.with(
isEnabled: isEnable,

@ -24,6 +24,7 @@ extension MessageReceiver {
let messageSentTimestamp: TimeInterval = (TimeInterval(message.sentTimestamp ?? 0) / 1000)
let isMainAppActive: Bool = (UserDefaults.sharedLokiProject?[.isMainAppActive]).defaulting(to: false)
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db, dependencies: dependencies)
let expiresInSeconds: TimeInterval? = proto.hasExpirationTimer ? TimeInterval(proto.expirationTimer) : nil
/// Only process the message if the thread `shouldBeVisible` or it was sent after the libSession buffer period
guard
@ -178,6 +179,7 @@ extension MessageReceiver {
body: message.text,
quoteAuthorId: dataMessage.quote?.author
),
expiresInSeconds: expiresInSeconds,
// OpenGroupInvitations are stored as LinkPreview's in the database
linkPreviewUrl: (message.linkPreview?.url ?? message.openGroupInvitation?.url),
// Keep track of the open group server message ID message ID relationship

Loading…
Cancel
Save