feat: add & delete emoji reacts in open groups

This commit is contained in:
Ryan Zhao 2022-07-28 16:32:51 +10:00
parent cc2bf11950
commit f7a6d0dbb0
2 changed files with 97 additions and 26 deletions

View File

@ -1022,7 +1022,7 @@ extension ConversationVC:
}
return OpenGroupAPI
.reactionDelete(
.reactionDeleteAll(
db,
emoji: emoji,
id: openGroupServerMessageId,
@ -1077,7 +1077,6 @@ extension ConversationVC:
.filter(id: thread.id)
.updateAll(db, SessionThread.Columns.shouldBeVisible.set(to: true))
// TODO: Need to handle open group specific logic
// Update the database
if remove {
_ = try Reaction
@ -1100,30 +1099,60 @@ extension ConversationVC:
Emoji.addRecent(db, emoji: emoji)
}
// Send the actual message
try MessageSender.send(
db,
message: VisibleMessage(
sentTimestamp: UInt64(sentTimestamp),
// TODO: Is the 'groupPublicKey' needed here?
groupPublicKey: (thread.variant == .closedGroup ? thread.id : nil),
text: nil,
reaction: VisibleMessage.VMReaction(
timestamp: UInt64(cellViewModel.timestampMs),
publicKey: {
guard cellViewModel.variant == .standardIncoming else {
return cellViewModel.currentUserPublicKey
}
return cellViewModel.authorId
}(),
emoji: emoji,
kind: (remove ? .remove : .react)
)
),
interactionId: cellViewModel.id,
in: thread
)
if let openGroup: OpenGroup = try? OpenGroup.fetchOne(db, id: cellViewModel.threadId) {
// Send reaction to open groups
guard
let openGroupServerMessageId: Int64 = try? Interaction
.select(.openGroupServerMessageId)
.filter(id: cellViewModel.id)
.asRequest(of: Int64.self)
.fetchOne(db)
else { return }
if remove {
_ = OpenGroupAPI
.reactionDelete(
db,
emoji: emoji,
id: openGroupServerMessageId,
in: openGroup.roomToken,
on: openGroup.server
)
} else {
_ = OpenGroupAPI
.reactionAdd(
db,
emoji: emoji,
id: openGroupServerMessageId,
in: openGroup.roomToken,
on: openGroup.server
)
}
} else {
// Send the actual message
try MessageSender.send(
db,
message: VisibleMessage(
sentTimestamp: UInt64(sentTimestamp),
text: nil,
reaction: VisibleMessage.VMReaction(
timestamp: UInt64(cellViewModel.timestampMs),
publicKey: {
guard cellViewModel.variant == .standardIncoming else {
return cellViewModel.currentUserPublicKey
}
return cellViewModel.authorId
}(),
emoji: emoji,
kind: (remove ? .remove : .react)
)
),
interactionId: cellViewModel.id,
in: thread
)
}
},
completion: { [weak self] _, _ in
self?.handleMessageSent()

View File

@ -593,6 +593,27 @@ public enum OpenGroupAPI {
// MARK: - Reactions
public static func reactionAdd(
_ db: Database,
emoji: String,
id: Int64,
in roomToken: String,
on server: String,
using dependencies: SMKDependencies = SMKDependencies()
) -> Promise<OnionRequestResponseInfoType> {
return OpenGroupAPI
.send(
db,
request: Request<NoBody, Endpoint>(
method: .put,
server: server,
endpoint: .reaction(roomToken, id: id, emoji: emoji)
),
using: dependencies
)
.map { responseInfo, _ in responseInfo }
}
public static func reactionDelete(
_ db: Database,
emoji: String,
@ -600,6 +621,27 @@ public enum OpenGroupAPI {
in roomToken: String,
on server: String,
using dependencies: SMKDependencies = SMKDependencies()
) -> Promise<OnionRequestResponseInfoType> {
return OpenGroupAPI
.send(
db,
request: Request<NoBody, Endpoint>(
method: .delete,
server: server,
endpoint: .reaction(roomToken, id: id, emoji: emoji)
),
using: dependencies
)
.map { responseInfo, _ in responseInfo }
}
public static func reactionDeleteAll(
_ db: Database,
emoji: String,
id: Int64,
in roomToken: String,
on server: String,
using dependencies: SMKDependencies = SMKDependencies()
) -> Promise<OnionRequestResponseInfoType> {
return OpenGroupAPI
.send(