feat: send expire timer update message

pull/941/head
ryanzhao 3 years ago
parent 49c019a9d4
commit 14a853ab07

@ -349,16 +349,16 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
timestampMs: Int64(floor(Date().timeIntervalSince1970 * 1000)) timestampMs: Int64(floor(Date().timeIntervalSince1970 * 1000))
) )
.inserted(db) .inserted(db)
//
// try MessageSender.send( try MessageSender.send(
// db, db,
// message: ExpirationTimerUpdate( message: ExpirationTimerUpdate(
// syncTarget: nil, syncTarget: nil,
// duration: UInt32(floor(updatedConfig.isEnabled ? updatedConfig.durationSeconds : 0)) duration: UInt32(floor(updatedConfig.isEnabled ? updatedConfig.durationSeconds : 0))
// ), ),
// interactionId: interaction.id, interactionId: interaction.id,
// in: thread in: thread
// ) )
} }
} }
} }

@ -64,7 +64,7 @@ public final class ExpirationTimerUpdate: ControlMessage {
return ExpirationTimerUpdate( return ExpirationTimerUpdate(
syncTarget: dataMessageProto.syncTarget, syncTarget: dataMessageProto.syncTarget,
duration: dataMessageProto.expireTimer duration: proto.hasExpirationTimer ? proto.expirationTimer : 0
) )
} }
@ -85,6 +85,15 @@ public final class ExpirationTimerUpdate: ControlMessage {
return nil return nil
} }
let contentProto = SNProtoContent.builder() let contentProto = SNProtoContent.builder()
// DisappearingMessagesConfiguration
do {
try setDisappearingMessagesConfigurationIfNeeded(db, on: contentProto)
} catch {
SNLog("Couldn't construct visible message proto from: \(self).")
return nil
}
do { do {
contentProto.setDataMessage(try dataMessageProto.build()) contentProto.setDataMessage(try dataMessageProto.build())
return try contentProto.build() return try contentProto.build()

@ -70,12 +70,10 @@ public class Message: Codable {
else { return } else { return }
proto.setExpirationTimer(UInt32(disappearingMessagesConfiguration.durationSeconds)) proto.setExpirationTimer(UInt32(disappearingMessagesConfiguration.durationSeconds))
proto.setLastDisappearingMessageChangeTimestamp(UInt64(disappearingMessagesConfiguration.lastChangeTimestampMs))
if disappearingMessagesConfiguration.isEnabled, if disappearingMessagesConfiguration.isEnabled, let type = disappearingMessagesConfiguration.type {
let type = disappearingMessagesConfiguration.type
{
proto.setExpirationType(type.toProto()) proto.setExpirationType(type.toProto())
proto.setLastDisappearingMessageChangeTimestamp(UInt64(disappearingMessagesConfiguration.lastChangeTimestampMs))
} }
} }

@ -7,47 +7,47 @@ import SessionUtilitiesKit
extension MessageReceiver { extension MessageReceiver {
internal static func handleExpirationTimerUpdate(_ db: Database, message: ExpirationTimerUpdate) throws { internal static func handleExpirationTimerUpdate(_ db: Database, message: ExpirationTimerUpdate) throws {
// Get the target thread // Get the target thread
guard // guard
let targetId: String = MessageReceiver.threadInfo(db, message: message, openGroupId: nil)?.id, // let targetId: String = MessageReceiver.threadInfo(db, message: message, openGroupId: nil)?.id,
let sender: String = message.sender, // let sender: String = message.sender,
let thread: SessionThread = try? SessionThread.fetchOne(db, id: targetId) // let thread: SessionThread = try? SessionThread.fetchOne(db, id: targetId)
else { return } // else { return }
//
// Update the configuration // // Update the configuration
// // //
// Note: Messages which had been sent during the previous configuration will still // // Note: Messages which had been sent during the previous configuration will still
// use it's settings (so if you enable, send a message and then disable disappearing // // use it's settings (so if you enable, send a message and then disable disappearing
// message then the message you had sent will still disappear) // // message then the message you had sent will still disappear)
let config: DisappearingMessagesConfiguration = try thread.disappearingMessagesConfiguration // let config: DisappearingMessagesConfiguration = try thread.disappearingMessagesConfiguration
.fetchOne(db) // .fetchOne(db)
.defaulting(to: DisappearingMessagesConfiguration.defaultWith(thread.id)) // .defaulting(to: DisappearingMessagesConfiguration.defaultWith(thread.id))
.with( // .with(
// If there is no duration then we should disable the expiration timer // // If there is no duration then we should disable the expiration timer
isEnabled: ((message.duration ?? 0) > 0), // isEnabled: ((message.duration ?? 0) > 0),
durationSeconds: ( // durationSeconds: (
message.duration.map { TimeInterval($0) } ?? // message.duration.map { TimeInterval($0) } ??
DisappearingMessagesConfiguration.defaultDuration // DisappearingMessagesConfiguration.defaultDuration
) // )
) // )
//
// Add an info message for the user // // Add an info message for the user
_ = try Interaction( // _ = try Interaction(
serverHash: nil, // Intentionally null so sync messages are seen as duplicates // serverHash: nil, // Intentionally null so sync messages are seen as duplicates
threadId: thread.id, // threadId: thread.id,
authorId: sender, // authorId: sender,
variant: .infoDisappearingMessagesUpdate, // variant: .infoDisappearingMessagesUpdate,
body: config.messageInfoString( // body: config.messageInfoString(
with: (sender != getUserHexEncodedPublicKey(db) ? // with: (sender != getUserHexEncodedPublicKey(db) ?
Profile.displayName(db, id: sender) : // Profile.displayName(db, id: sender) :
nil // nil
), // ),
isPreviousOff: false // isPreviousOff: false
), // ),
timestampMs: Int64(message.sentTimestamp ?? 0) // Default to `0` if not set // timestampMs: Int64(message.sentTimestamp ?? 0) // Default to `0` if not set
).inserted(db) // ).inserted(db)
//
// Finally save the changes to the DisappearingMessagesConfiguration (If it's a duplicate // // Finally save the changes to the DisappearingMessagesConfiguration (If it's a duplicate
// then the interaction unique constraint will prevent the code from getting here) // // then the interaction unique constraint will prevent the code from getting here)
try config.save(db) // try config.save(db)
} }
} }

Loading…
Cancel
Save