tweak: local reaction entry logic change

pull/680/head
ryanzhao 3 years ago
parent eef56d47de
commit 82e55a8d5b

@ -1111,6 +1111,43 @@ extension ConversationVC:
.filter(id: thread.id) .filter(id: thread.id)
.updateAll(db, SessionThread.Columns.shouldBeVisible.set(to: true)) .updateAll(db, SessionThread.Columns.shouldBeVisible.set(to: true))
let pendingReaction: Reaction? = {
if remove {
return try? Reaction
.filter(Reaction.Columns.interactionId == cellViewModel.id)
.filter(Reaction.Columns.authorId == cellViewModel.currentUserPublicKey)
.filter(Reaction.Columns.emoji == emoji)
.fetchOne(db)
} else {
let sortId = Reaction.getSortId(
db,
interactionId: cellViewModel.id,
emoji: emoji
)
return Reaction(
interactionId: cellViewModel.id,
serverHash: nil,
timestampMs: sentTimestamp,
authorId: cellViewModel.currentUserPublicKey,
emoji: emoji,
count: 1,
sortId: sortId
)
}
}()
// Update the database
if remove {
try pendingReaction?.delete(db)
}
else {
try pendingReaction?.insert(db)
// Add it to the recent list
Emoji.addRecent(db, emoji: emoji)
}
if let openGroup: OpenGroup = try? OpenGroup.fetchOne(db, id: cellViewModel.threadId), if let openGroup: OpenGroup = try? OpenGroup.fetchOne(db, id: cellViewModel.threadId),
OpenGroupManager.isOpenGroupSupport(.reactions, on: openGroup.server) OpenGroupManager.isOpenGroupSupport(.reactions, on: openGroup.server)
{ {
@ -1140,18 +1177,21 @@ extension ConversationVC:
in: openGroup.roomToken, in: openGroup.roomToken,
on: openGroup.server on: openGroup.server
) )
.map { [weak self] _, response in .map { _, response in
OpenGroupManager OpenGroupManager
.updatePendingChange( .updatePendingChange(
pendingChange, pendingChange,
seqNo: response.seqNo seqNo: response.seqNo
) )
self?.handleReactionSent( }
cellViewModel, .catch { [weak self] _ in
with: emoji, OpenGroupManager.removePendingChange(pendingChange)
at: sentTimestamp,
self?.handleReactionSentFailure(
pendingReaction,
remove: remove remove: remove
) )
} }
.retainUntilComplete() .retainUntilComplete()
} else { } else {
@ -1171,16 +1211,18 @@ extension ConversationVC:
in: openGroup.roomToken, in: openGroup.roomToken,
on: openGroup.server on: openGroup.server
) )
.map { [weak self] _, response in .map { _, response in
OpenGroupManager OpenGroupManager
.updatePendingChange( .updatePendingChange(
pendingChange, pendingChange,
seqNo: response.seqNo seqNo: response.seqNo
) )
self?.handleReactionSent( }
cellViewModel, .catch { [weak self] _ in
with: emoji, OpenGroupManager.removePendingChange(pendingChange)
at: sentTimestamp,
self?.handleReactionSentFailure(
pendingReaction,
remove: remove remove: remove
) )
} }
@ -1189,7 +1231,7 @@ extension ConversationVC:
} else { } else {
// Send the actual message // Send the actual message
try MessageSender.sendNonDurably( try MessageSender.send(
db, db,
message: VisibleMessage( message: VisibleMessage(
sentTimestamp: UInt64(sentTimestamp), sentTimestamp: UInt64(sentTimestamp),
@ -1208,49 +1250,21 @@ extension ConversationVC:
) )
), ),
interactionId: cellViewModel.id, interactionId: cellViewModel.id,
in: thread) in: thread
.map { [weak self] in )
self?.handleReactionSent(
cellViewModel,
with: emoji,
at: sentTimestamp,
remove: remove
)
}
.retainUntilComplete()
} }
} }
) )
} }
private func handleReactionSent(_ cellViewModel: MessageViewModel, with emoji: String, at sentTimestamp: Int64, remove: Bool) { func handleReactionSentFailure(_ pendingReaction: Reaction?, remove: Bool) {
Storage.shared.writeAsync { db in Storage.shared.writeAsync { db in
// Update the database // Reverse the database
if remove { if remove {
_ = try Reaction try pendingReaction?.insert(db)
.filter(Reaction.Columns.interactionId == cellViewModel.id)
.filter(Reaction.Columns.authorId == cellViewModel.currentUserPublicKey)
.filter(Reaction.Columns.emoji == emoji)
.deleteAll(db)
} }
else { else {
let sortId = Reaction.getSortId( try pendingReaction?.delete(db)
db,
interactionId: cellViewModel.id,
emoji: emoji
)
try Reaction(
interactionId: cellViewModel.id,
serverHash: nil,
timestampMs: sentTimestamp,
authorId: cellViewModel.currentUserPublicKey,
emoji: emoji,
count: 1,
sortId: sortId
).insert(db)
// Add it to the recent list
Emoji.addRecent(db, emoji: emoji)
} }
} }
} }

@ -797,6 +797,17 @@ public final class OpenGroupManager: NSObject {
} }
} }
public static func removePendingChange(
_ pendingChange: OpenGroupAPI.PendingChange,
using dependencies: OGMDependencies = OGMDependencies()
) {
dependencies.mutableCache.mutate {
if let index = $0.pendingChanges.firstIndex(of: pendingChange) {
$0.pendingChanges.remove(at: index)
}
}
}
/// This method specifies if the given capability is supported on a specified Open Group /// This method specifies if the given capability is supported on a specified Open Group
public static func isOpenGroupSupport( public static func isOpenGroupSupport(
_ capability: Capability.Variant, _ capability: Capability.Variant,

@ -665,6 +665,7 @@ public final class MessageSender {
with error: MessageSenderError, with error: MessageSenderError,
interactionId: Int64? interactionId: Int64?
) { ) {
// TODO: Revert the local database change
// If the message was a reaction then we don't want to do anything to the original // If the message was a reaction then we don't want to do anything to the original
// interaciton (which the 'interactionId' is pointing to // interaciton (which the 'interactionId' is pointing to
guard (message as? VisibleMessage)?.reaction == nil else { return } guard (message as? VisibleMessage)?.reaction == nil else { return }

Loading…
Cancel
Save