Fix editing items not in the timeline failing (#3075)

This commit is contained in:
Mauro
2024-07-22 13:15:57 +02:00
committed by GitHub
parent ab49274015
commit ff33c07596
10 changed files with 307 additions and 72 deletions
@@ -191,15 +191,38 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
html: String?,
intentionalMentions: IntentionalMentions) async {
MXLog.info("Edit message in \(roomID)")
MXLog.info("Editing timeline item: \(timelineItemID)")
switch await activeTimeline.edit(timelineItemID,
message: message,
html: html,
intentionalMentions: intentionalMentions) {
case .success:
MXLog.info("Finished editing message")
case .failure(let error):
MXLog.error("Failed editing message with error: \(error)")
let editMode: EditMode
if !timelineItemID.timelineID.isEmpty,
let timelineItem = liveTimelineProvider.itemProxies.firstEventTimelineItemUsingStableID(timelineItemID) {
editMode = .byEvent(timelineItem)
} else if let eventID = timelineItemID.eventID {
editMode = .byID(eventID)
} else {
MXLog.error("Unknown timeline item: \(timelineItemID)")
return
}
let messageContent = activeTimeline.buildMessageContentFor(message,
html: html,
intentionalMentions: intentionalMentions.toRustMentions())
switch editMode {
case let .byEvent(item):
switch await activeTimeline.edit(item, newContent: messageContent) {
case .success:
MXLog.info("Finished editing message by event")
case let .failure(error):
MXLog.error("Failed editing message by event with error: \(error)")
}
case let .byID(eventID):
switch await roomProxy.edit(eventID: eventID, newContent: messageContent) {
case .success:
MXLog.info("Finished editing message by event ID")
case let .failure(error):
MXLog.error("Failed editing message by event ID with error: \(error)")
}
}
}
@@ -408,3 +431,8 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
return nil
}
}
private enum EditMode {
case byEvent(EventTimelineItem)
case byID(String)
}
@@ -153,39 +153,17 @@ final class TimelineProxy: TimelineProxyProtocol {
}
}
func edit(_ timelineItemID: TimelineItemIdentifier,
message: String, html: String?,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
MXLog.info("Editing timeline item: \(timelineItemID)")
let timelineItem: EventTimelineItem? = if !timelineItemID.timelineID.isEmpty,
let timelineItem = await timelineProvider.itemProxies.firstEventTimelineItemUsingStableID(timelineItemID) {
timelineItem
} else if let eventID = timelineItemID.eventID {
nil // We need to edit by event ID which was removed.
} else {
nil
}
guard let timelineItem else {
MXLog.error("Unknown timeline item: \(timelineItemID)")
return .failure(.failedEditing)
}
let messageContent = buildMessageContentFor(message,
html: html,
intentionalMentions: intentionalMentions.toRustMentions())
func edit(_ timelineItem: EventTimelineItem, newContent: RoomMessageEventContentWithoutRelation) async -> Result<Void, TimelineProxyError> {
do {
guard try await timeline.edit(item: timelineItem, newContent: messageContent) == true else {
guard try await timeline.edit(item: timelineItem, newContent: newContent) == true else {
return .failure(.failedEditing)
}
MXLog.info("Finished editing timeline item: \(timelineItemID)")
MXLog.info("Finished editing timeline item: \(timelineItem.eventId() ?? timelineItem.transactionId() ?? "unknown")")
return .success(())
} catch {
MXLog.error("Failed editing timeline item: \(timelineItemID) with error: \(error)")
MXLog.error("Failed editing timeline item: \(timelineItem.eventId() ?? timelineItem.transactionId() ?? "unknown") with error: \(error)")
return .failure(.sdkError(error))
}
}
@@ -505,12 +483,10 @@ final class TimelineProxy: TimelineProxyProtocol {
return .failure(.sdkError(error))
}
}
// MARK: - Private
private func buildMessageContentFor(_ message: String,
html: String?,
intentionalMentions: Mentions) -> RoomMessageEventContentWithoutRelation {
func buildMessageContentFor(_ message: String,
html: String?,
intentionalMentions: Mentions) -> RoomMessageEventContentWithoutRelation {
let emoteSlashCommand = "/me "
let isEmote: Bool = message.starts(with: emoteSlashCommand)
@@ -533,6 +509,8 @@ final class TimelineProxy: TimelineProxyProtocol {
return content.withMentions(mentions: intentionalMentions)
}
// MARK: - Private
private func buildEmoteMessageContentFor(_ message: String, html: String?) -> RoomMessageEventContentWithoutRelation {
if let html {
return messageEventContentFromHtmlAsEmote(body: message, htmlBody: html)
@@ -41,10 +41,7 @@ protocol TimelineProxyProtocol {
func paginateBackwards(requestSize: UInt16) async -> Result<Void, TimelineProxyError>
func paginateForwards(requestSize: UInt16) async -> Result<Void, TimelineProxyError>
func edit(_ timelineItemID: TimelineItemIdentifier,
message: String,
html: String?,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError>
func edit(_ timelineItem: EventTimelineItem, newContent: RoomMessageEventContentWithoutRelation) async -> Result<Void, TimelineProxyError>
func redact(_ timelineItemID: TimelineItemIdentifier,
reason: String?) async -> Result<Void, TimelineProxyError>
@@ -109,6 +106,10 @@ protocol TimelineProxyProtocol {
func sendPollResponse(pollStartID: String, answers: [String]) async -> Result<Void, TimelineProxyError>
func getLoadedReplyDetails(eventID: String) async -> Result<InReplyToDetails, TimelineProxyError>
func buildMessageContentFor(_ message: String,
html: String?,
intentionalMentions: Mentions) -> RoomMessageEventContentWithoutRelation
}
extension TimelineProxyProtocol {