Reactions: Use the hack like on riot-android and riot-web if the server has not yet the aggregations API

This commit is contained in:
manuroe
2019-05-20 16:04:54 +02:00
parent 71e8d104d6
commit 2824372877
2 changed files with 47 additions and 2 deletions
@@ -21,6 +21,7 @@ final class ReactionsMenuViewModel: ReactionsMenuViewModelType {
// MARK: - Properties
// MARK: Private
private let session: MXSession // TODO: To remove. Only required for reactUsingHack()
private let aggregations: MXAggregations
private let roomId: String
private let eventId: String
@@ -37,10 +38,11 @@ final class ReactionsMenuViewModel: ReactionsMenuViewModelType {
// MARK: - Setup
init(aggregations: MXAggregations, roomId: String, eventId: String) {
init(aggregations: MXAggregations, roomId: String, eventId: String, session: MXSession) {
self.aggregations = aggregations
self.roomId = roomId
self.eventId = eventId
self.session = session
self.loadData()
self.listenToDataUpdate()
@@ -140,6 +142,16 @@ final class ReactionsMenuViewModel: ReactionsMenuViewModelType {
return
}
// The server does not support support reaction yet
// Use a fallback mechanism
// TODO: To remove once the feature has landed on matrix.org homeserver
if let mxError = MXError(nsError: error) {
if mxError.errcode == kMXErrCodeStringUnrecognized {
sself.reactUsingHack(withReaction: reaction)
return
}
}
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionFailedWithError: error, reaction: reaction.rawValue, isAddReaction: true)
})
} else {
@@ -197,4 +209,37 @@ final class ReactionsMenuViewModel: ReactionsMenuViewModelType {
self.react(withReaction: unreaction, selected: false)
}
}
/// reactUsingHack directly sends a `m.reaction` room message instead of using the `/send_relation` api.
///
/// TODO: To remove once the feature has landed on matrix.org homeserver
///
/// - Parameter reaction: the reaction
private func reactUsingHack(withReaction reaction: ReactionsMenuReaction) {
print("[ReactionsMenuViewModel] reactUsingHack")
let reactionContent = [
"m.relates_to": [
"rel_type": "m.annotation",
"event_id": self.eventId,
"key": reaction.rawValue]
]
var nilEvent: MXEvent?
let room = self.session.room(withRoomId: self.roomId)
room?.sendEvent(.reaction, content: reactionContent, localEcho: &nilEvent, completion: { [weak self] ( completion) in
guard let sself = self else {
return
}
switch completion {
case .success:
print("[ReactionsMenuViewModel] reactUsingHack: Success")
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionComplete: reaction.rawValue, isAddReaction: true)
case.failure(let error):
print("[ReactionsMenuViewModel] reactUsingHack: Error: \(error)")
sself.coordinatorDelegate?.reactionsMenuViewModel(sself, didReactionFailedWithError: error, reaction: reaction.rawValue, isAddReaction: true)
}
})
}
}