feat: cache remove all for open group emoji reacts

pull/680/head
Ryan Zhao 3 years ago
parent 4a3fe4cdeb
commit 31dcb3c985

@ -1040,6 +1040,15 @@ extension ConversationVC:
return Promise(error: StorageError.objectNotFound) return Promise(error: StorageError.objectNotFound)
} }
let pendingChange = OpenGroupManager
.addPendingReaction(
emoji: emoji,
id: openGroupServerMessageId,
in: openGroup.roomToken,
on: openGroup.server,
type: .removeAll
)
return OpenGroupAPI return OpenGroupAPI
.reactionDeleteAll( .reactionDeleteAll(
db, db,
@ -1048,7 +1057,13 @@ extension ConversationVC:
in: openGroup.roomToken, in: openGroup.roomToken,
on: openGroup.server on: openGroup.server
) )
.map { _ in () } .map { _, response in
OpenGroupManager
.updatePendingChange(
pendingChange,
seqNo: response.seqNo
)
}
} }
.done { _ in .done { _ in
Storage.shared.writeAsync { db in Storage.shared.writeAsync { db in
@ -1168,7 +1183,7 @@ extension ConversationVC:
id: openGroupServerMessageId, id: openGroupServerMessageId,
in: openGroup.roomToken, in: openGroup.roomToken,
on: openGroup.server, on: openGroup.server,
type: .react type: .add
) )
OpenGroupAPI OpenGroupAPI
.reactionAdd( .reactionAdd(

@ -369,12 +369,20 @@ public extension Message {
rawReaction.count > 0, rawReaction.count > 0,
let reactors = rawReaction.reactors let reactors = rawReaction.reactors
{ {
// Decide whether we need to ignore all reactions
let pendingChangeRemoveAllReaction: Bool = associatedPendingChanges.contains { pendingChange in
if case .reaction(_, let emoji, let action) = pendingChange.metadata {
return emoji == decodedEmoji && action == .removeAll
}
return false
}
// Decide whether we need to add an extra reaction from current user // Decide whether we need to add an extra reaction from current user
let pendingChangeSelfReaction: Bool? = { let pendingChangeSelfReaction: Bool? = {
// Find the newest 'PendingChange' entry with a matching emoji, if one exists, and // Find the newest 'PendingChange' entry with a matching emoji, if one exists, and
// set the "self reaction" value based on it's action // set the "self reaction" value based on it's action
let maybePendingChange: OpenGroupAPI.PendingChange? = associatedPendingChanges let maybePendingChange: OpenGroupAPI.PendingChange? = associatedPendingChanges
.sorted(by: { lhs, rhs -> Bool in (lhs.seqNo ?? Int64.max) > (rhs.seqNo ?? Int64.max) }) .sorted(by: { lhs, rhs -> Bool in (lhs.seqNo ?? Int64.max) >= (rhs.seqNo ?? Int64.max) })
.first { pendingChange in .first { pendingChange in
if case .reaction(_, let emoji, _) = pendingChange.metadata { if case .reaction(_, let emoji, _) = pendingChange.metadata {
return emoji == decodedEmoji return emoji == decodedEmoji
@ -390,11 +398,11 @@ public extension Message {
else { return nil } else { return nil }
// Otherwise add/remove accordingly // Otherwise add/remove accordingly
return (action == .react) return action == .add
}() }()
let shouldAddSelfReaction: Bool = ( let shouldAddSelfReaction: Bool = (
pendingChangeSelfReaction ?? pendingChangeSelfReaction ??
(rawReaction.you || reactors.contains(userPublicKey)) ((rawReaction.you || reactors.contains(userPublicKey)) && !pendingChangeRemoveAllReaction)
) )
let count: Int64 = rawReaction.you ? rawReaction.count - 1 : rawReaction.count let count: Int64 = rawReaction.you ? rawReaction.count - 1 : rawReaction.count
@ -408,6 +416,8 @@ public extension Message {
results = results results = results
.appending( // Add the first reaction (with the count) .appending( // Add the first reaction (with the count)
pendingChangeRemoveAllReaction ?
nil :
desiredReactorIds.first desiredReactorIds.first
.map { reactor in .map { reactor in
Reaction( Reaction(
@ -422,7 +432,7 @@ public extension Message {
} }
) )
.appending( // Add all other reactions .appending( // Add all other reactions
contentsOf: desiredReactorIds.count <= 1 ? contentsOf: desiredReactorIds.count <= 1 || pendingChangeRemoveAllReaction ?
[] : [] :
desiredReactorIds desiredReactorIds
.suffix(from: 1) .suffix(from: 1)

@ -4,12 +4,18 @@ import Foundation
extension OpenGroupAPI { extension OpenGroupAPI {
public struct PendingChange: Equatable { public struct PendingChange: Equatable {
enum ChangeType { public enum ChangeType {
case reaction case reaction
} }
public enum ReactAction: Equatable {
case add
case remove
case removeAll
}
enum Metadata { enum Metadata {
case reaction(messageId: Int64, emoji: String, action: VisibleMessage.VMReaction.Kind) case reaction(messageId: Int64, emoji: String, action: ReactAction)
} }
let server: String let server: String

@ -764,7 +764,7 @@ public final class OpenGroupManager: NSObject {
id: Int64, id: Int64,
in roomToken: String, in roomToken: String,
on server: String, on server: String,
type: VisibleMessage.VMReaction.Kind, type: OpenGroupAPI.PendingChange.ReactAction,
using dependencies: OGMDependencies = OGMDependencies() using dependencies: OGMDependencies = OGMDependencies()
) -> OpenGroupAPI.PendingChange { ) -> OpenGroupAPI.PendingChange {
let pendingChange = OpenGroupAPI.PendingChange( let pendingChange = OpenGroupAPI.PendingChange(

Loading…
Cancel
Save