Reorder the TimelineController send methods

This commit is contained in:
Stefan Ceriu
2025-07-22 14:16:40 +03:00
committed by Stefan Ceriu
parent cedb2936d0
commit a4c0b7f91c
3 changed files with 101 additions and 97 deletions
@@ -108,19 +108,6 @@ class MockTimelineController: TimelineControllerProtocol {
func processItemAppearance(_ itemID: TimelineItemIdentifier) async { }
func processItemDisappearance(_ itemID: TimelineItemIdentifier) async { }
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async { }
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError> {
if let timelineProxy {
return await timelineProxy.sendVoiceMessage(url: url, audioInfo: audioInfo, waveform: waveform) { _ in }
}
return .success(())
}
func toggleReaction(_ reaction: String, to eventID: TimelineItemIdentifier.EventOrTransactionID) async { }
@@ -161,6 +148,21 @@ class MockTimelineController: TimelineControllerProtocol {
timelineItemsTimestamp[itemID] ?? .now
}
// MARK: - Sending
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async { }
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError> {
if let timelineProxy {
return await timelineProxy.sendVoiceMessage(url: url, audioInfo: audioInfo, waveform: waveform) { _ in }
}
return .success(())
}
// MARK: - UI Test signalling
/// The cancellable used for UI Tests signalling.
@@ -145,83 +145,6 @@ class TimelineController: TimelineControllerProtocol {
func processItemDisappearance(_ itemID: TimelineItemIdentifier) { }
// MARK: - Sending
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async {
MXLog.info("Send message in \(roomID)")
switch await activeTimeline.sendMessage(message,
html: html,
inReplyToEventID: inReplyToEventID,
intentionalMentions: intentionalMentions) {
case .success:
MXLog.info("Finished sending message")
await donateSendMessageIntent()
case .failure(let error):
MXLog.error("Failed sending message with error: \(error)")
}
}
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError> {
await activeTimeline.sendVoiceMessage(url: url,
audioInfo: audioInfo,
waveform: waveform) { _ in }
}
private func donateSendMessageIntent() async {
guard let displayName = roomProxy.details.name ?? roomProxy.details.canonicalAlias, !displayName.isEmpty else {
MXLog.error("Failed donating send message intent, room missing name or alias.")
return
}
let groupName = INSpeakableString(spokenPhrase: displayName)
let sendMessageIntent = INSendMessageIntent(recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: nil,
speakableGroupName: groupName,
conversationIdentifier: roomProxy.id,
serviceName: nil,
sender: nil,
attachments: nil)
let avatarURL: URL? = switch roomProxy.details.avatar {
case .room(_, _, let avatarURL):
avatarURL
case .heroes(let userProfiles):
userProfiles.first?.avatarURL
case .tombstoned:
nil
}
func addPlacehoder() {
if let imageData = Avatars.generatePlaceholderAvatarImageData(name: displayName, id: roomProxy.id, size: .init(width: 100, height: 100)) {
sendMessageIntent.setImage(INImage(imageData: imageData), forParameterNamed: \.speakableGroupName)
}
}
if let avatarURL, let mediaSource = try? MediaSourceProxy(url: avatarURL, mimeType: nil) {
if case let .success(avatarData) = await mediaProvider.loadThumbnailForSource(source: mediaSource, size: .init(width: 100, height: 100)) {
sendMessageIntent.setImage(INImage(imageData: avatarData), forParameterNamed: \.speakableGroupName)
} else {
addPlacehoder()
}
} else {
addPlacehoder()
}
let interaction = INInteraction(intent: sendMessageIntent, response: nil)
do {
try await interaction.donate()
} catch {
MXLog.error("Failed donating send message intent with error: \(error)")
}
}
func toggleReaction(_ reaction: String, to eventOrTransactionID: TimelineItemIdentifier.EventOrTransactionID) async {
MXLog.info("Toggle reaction \(reaction) to \(eventOrTransactionID)")
@@ -358,6 +281,32 @@ class TimelineController: TimelineControllerProtocol {
return nil
}
// MARK: - Sending
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async {
MXLog.info("Send message in \(roomID)")
switch await activeTimeline.sendMessage(message,
html: html,
inReplyToEventID: inReplyToEventID,
intentionalMentions: intentionalMentions) {
case .success:
MXLog.info("Finished sending message")
await donateSendMessageIntent()
case .failure(let error):
MXLog.error("Failed sending message with error: \(error)")
}
}
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError> {
await activeTimeline.sendVoiceMessage(url: url,
audioInfo: audioInfo,
waveform: waveform) { _ in }
}
// MARK: - Private
/// The cancellable used to update the timeline items.
@@ -529,6 +478,57 @@ class TimelineController: TimelineControllerProtocol {
}
return nil
}
private func donateSendMessageIntent() async {
guard let displayName = roomProxy.details.name ?? roomProxy.details.canonicalAlias, !displayName.isEmpty else {
MXLog.error("Failed donating send message intent, room missing name or alias.")
return
}
let groupName = INSpeakableString(spokenPhrase: displayName)
let sendMessageIntent = INSendMessageIntent(recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: nil,
speakableGroupName: groupName,
conversationIdentifier: roomProxy.id,
serviceName: nil,
sender: nil,
attachments: nil)
let avatarURL: URL? = switch roomProxy.details.avatar {
case .room(_, _, let avatarURL):
avatarURL
case .heroes(let userProfiles):
userProfiles.first?.avatarURL
case .tombstoned:
nil
}
func addPlacehoder() {
if let imageData = Avatars.generatePlaceholderAvatarImageData(name: displayName, id: roomProxy.id, size: .init(width: 100, height: 100)) {
sendMessageIntent.setImage(INImage(imageData: imageData), forParameterNamed: \.speakableGroupName)
}
}
if let avatarURL, let mediaSource = try? MediaSourceProxy(url: avatarURL, mimeType: nil) {
if case let .success(avatarData) = await mediaProvider.loadThumbnailForSource(source: mediaSource, size: .init(width: 100, height: 100)) {
sendMessageIntent.setImage(INImage(imageData: avatarData), forParameterNamed: \.speakableGroupName)
} else {
addPlacehoder()
}
} else {
addPlacehoder()
}
let interaction = INInteraction(intent: sendMessageIntent, response: nil)
do {
try await interaction.donate()
} catch {
MXLog.error("Failed donating send message intent with error: \(error)")
}
}
}
private extension TimelineItemProxy {
@@ -63,13 +63,6 @@ protocol TimelineControllerProtocol {
func sendReadReceipt(for itemID: TimelineItemIdentifier) async
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError>
func edit(_ eventOrTransactionID: TimelineItemIdentifier.EventOrTransactionID,
message: String,
html: String?,
@@ -97,4 +90,13 @@ protocol TimelineControllerProtocol {
func sendHandle(for itemID: TimelineItemIdentifier) -> SendHandleProxy?
func eventTimestamp(for itemID: TimelineItemIdentifier) -> Date?
// MARK: - Sending
func sendMessage(_ message: String,
html: String?,
inReplyToEventID: String?,
intentionalMentions: IntentionalMentions) async
func sendVoiceMessage(url: URL, audioInfo: AudioInfo, waveform: [UInt16]) async -> Result<Void, TimelineProxyError>
}