generated from element-hq/.github
-
Notifications
You must be signed in to change notification settings - Fork 106
/
RoomTimelineController.swift
524 lines (440 loc) · 20.8 KB
/
RoomTimelineController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//
// Copyright 2022-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Combine
import Foundation
import IntentsUI
import MatrixRustSDK
import UIKit
class RoomTimelineController: RoomTimelineControllerProtocol {
private let roomProxy: JoinedRoomProxyProtocol
private let liveTimelineProvider: RoomTimelineProviderProtocol
private let timelineItemFactory: RoomTimelineItemFactoryProtocol
private let mediaProvider: MediaProviderProtocol
private let appSettings: AppSettings
private let serialDispatchQueue: DispatchQueue
let callbacks = PassthroughSubject<RoomTimelineControllerCallback, Never>()
private var activeTimeline: TimelineProxyProtocol
private var activeTimelineProvider: RoomTimelineProviderProtocol {
didSet {
configureActiveTimelineProvider()
}
}
private(set) var timelineItems = [RoomTimelineItemProtocol]()
var roomID: String {
roomProxy.id
}
var timelineKind: TimelineKind {
liveTimelineProvider.kind
}
init(roomProxy: JoinedRoomProxyProtocol,
timelineProxy: TimelineProxyProtocol,
initialFocussedEventID: String?,
timelineItemFactory: RoomTimelineItemFactoryProtocol,
mediaProvider: MediaProviderProtocol,
appSettings: AppSettings) {
self.roomProxy = roomProxy
liveTimelineProvider = timelineProxy.timelineProvider
self.timelineItemFactory = timelineItemFactory
self.mediaProvider = mediaProvider
self.appSettings = appSettings
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)
activeTimeline = timelineProxy
activeTimelineProvider = liveTimelineProvider
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange), name: UIContentSizeCategory.didChangeNotification, object: nil)
guard let initialFocussedEventID else {
configureActiveTimelineProvider()
return
}
Task {
callbacks.send(.paginationState(PaginationState(backward: .paginating, forward: .paginating)))
switch await focusOnEvent(initialFocussedEventID, timelineSize: 100) {
case .success:
break
case .failure:
// Setup the live timeline as a fallback.
configureActiveTimelineProvider()
}
}
}
func focusOnEvent(_ eventID: String, timelineSize: UInt16) async -> Result<Void, RoomTimelineControllerError> {
switch await roomProxy.timelineFocusedOnEvent(eventID: eventID, numberOfEvents: timelineSize) {
case .success(let timeline):
await timeline.subscribeForUpdates()
activeTimeline = timeline
activeTimelineProvider = timeline.timelineProvider
return .success(())
case .failure(let error):
if case .eventNotFound = error {
return .failure(.eventNotFound)
} else {
return .failure(.generic)
}
}
}
func focusLive() {
activeTimeline = roomProxy.timeline
activeTimelineProvider = liveTimelineProvider
}
func paginateBackwards(requestSize: UInt16) async -> Result<Void, RoomTimelineControllerError> {
MXLog.info("Started back pagination request")
switch await activeTimeline.paginateBackwards(requestSize: requestSize) {
case .success:
MXLog.info("Finished back pagination request")
return .success(())
case .failure(let error):
MXLog.error("Failed back pagination request with error: \(error)")
return .failure(.generic)
}
}
func paginateForwards(requestSize: UInt16) async -> Result<Void, RoomTimelineControllerError> {
MXLog.info("Started forward pagination request")
switch await activeTimeline.paginateForwards(requestSize: requestSize) {
case .success:
MXLog.info("Finished forward pagination request")
return .success(())
case .failure(let error):
MXLog.error("Failed forward pagination request with error: \(error)")
return .failure(.generic)
}
}
func sendReadReceipt(for itemID: TimelineItemIdentifier) async {
let receiptType: MatrixRustSDK.ReceiptType = appSettings.sharePresence ? .read : .readPrivate
// Mark the whole room as read if it's the last timeline item
if timelineItems.last?.id == itemID {
_ = await roomProxy.markAsRead(receiptType: receiptType)
} else {
guard let eventID = itemID.eventID else {
return
}
_ = await activeTimeline.sendReadReceipt(for: eventID, type: receiptType)
}
}
func processItemAppearance(_ itemID: TimelineItemIdentifier) async {
guard let timelineItem = timelineItems.firstUsingStableID(itemID) else {
return
}
if let messageTimelineItem = timelineItem as? EventBasedMessageTimelineItemProtocol {
fetchEventDetails(for: messageTimelineItem, refetchOnError: true)
}
}
func processItemDisappearance(_ itemID: TimelineItemIdentifier) { }
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)")
}
}
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 = switch roomProxy.details.avatar {
case .room(_, _, let avatarURL):
avatarURL
case .heroes(let userProfiles):
userProfiles.first?.avatarURL
}
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: EventOrTransactionId) async {
MXLog.info("Toggle reaction \(reaction) to \(eventOrTransactionID)")
switch await activeTimeline.toggleReaction(reaction, to: eventOrTransactionID) {
case .success:
MXLog.info("Finished toggling reaction")
case .failure(let error):
MXLog.error("Failed toggling reaction with error: \(error)")
}
}
func edit(_ eventOrTransactionID: EventOrTransactionId,
message: String,
html: String?,
intentionalMentions: IntentionalMentions) async {
MXLog.info("Edit message in \(roomID)")
MXLog.info("Editing timeline item: \(eventOrTransactionID)")
let messageContent = activeTimeline.buildMessageContentFor(message,
html: html,
intentionalMentions: intentionalMentions.toRustMentions())
switch await activeTimeline.edit(eventOrTransactionID, newContent: .roomMessage(content: messageContent)) {
case .success:
MXLog.info("Finished editing message by event")
case let .failure(error):
MXLog.error("Failed editing message by event with error: \(error)")
}
}
func editCaption(_ eventOrTransactionID: EventOrTransactionId,
message: String,
html: String?,
intentionalMentions: IntentionalMentions) async {
// We're waiting on an API for including mentions: https://github.com/matrix-org/matrix-rust-sdk/issues/4302
MXLog.info("Editing timeline item caption: \(eventOrTransactionID) in \(roomID)")
// When formattedCaption is nil, caption will be parsed as markdown and generate the HTML for us.
let newContent = createCaptionEdit(caption: message, formattedCaption: html.map { .init(format: .html, body: $0) })
switch await activeTimeline.edit(eventOrTransactionID, newContent: newContent) {
case .success:
MXLog.info("Finished editing caption")
case let .failure(error):
MXLog.error("Failed editing caption with error: \(error)")
}
}
func removeCaption(_ eventOrTransactionID: EventOrTransactionId) async {
// Set a `nil` caption to remove it from the event.
let newContent = createCaptionEdit(caption: nil, formattedCaption: nil)
switch await activeTimeline.edit(eventOrTransactionID, newContent: newContent) {
case .success:
MXLog.info("Finished removing caption.")
case let .failure(error):
MXLog.error("Failed removing caption with error: \(error)")
}
}
func redact(_ eventOrTransactionID: EventOrTransactionId) async {
MXLog.info("Send redaction in \(roomID)")
switch await activeTimeline.redact(eventOrTransactionID, reason: nil) {
case .success:
MXLog.info("Finished redacting message")
case .failure(let error):
MXLog.error("Failed redacting message with error: \(error)")
}
}
func pin(eventID: String) async {
MXLog.info("Pinning event \(eventID) in \(roomID)")
switch await activeTimeline.pin(eventID: eventID) {
case .success(let value):
if value {
MXLog.info("Finished pinning event \(eventID)")
} else {
MXLog.error("Failed pinning event \(eventID) because is already pinned")
}
case .failure(let error):
MXLog.error("Failed pinning event \(eventID) with error: \(error)")
}
}
func unpin(eventID: String) async {
MXLog.info("Unpinning event \(eventID) in \(roomID)")
switch await activeTimeline.unpin(eventID: eventID) {
case .success(let value):
if value {
MXLog.info("Finished unpinning event \(eventID)")
} else {
MXLog.error("Failed unpinning event \(eventID) because is not pinned")
}
case .failure(let error):
MXLog.error("Failed unpinning event \(eventID) with error: \(error)")
}
}
func messageEventContent(for timelineItemID: TimelineItemIdentifier) async -> RoomMessageEventContentWithoutRelation? {
await activeTimeline.messageEventContent(for: timelineItemID)
}
// Handle this parallel to the timeline items so we're not forced
// to bundle the Rust side objects within them
func debugInfo(for itemID: TimelineItemIdentifier) -> TimelineItemDebugInfo {
for timelineItemProxy in activeTimelineProvider.itemProxies {
switch timelineItemProxy {
case .event(let item):
if item.id == itemID {
return item.debugInfo
}
default:
continue
}
}
return .init(model: "Unknown item", originalJSON: nil, latestEditJSON: nil)
}
func sendHandle(for itemID: TimelineItemIdentifier) -> SendHandleProxy? {
for timelineItemProxy in activeTimelineProvider.itemProxies {
switch timelineItemProxy {
case .event(let item):
if item.id == itemID {
return item.sendHandle.map { .init(itemID: itemID, underlyingHandle: $0) }
}
default:
continue
}
}
return nil
}
func retryDecryption(for sessionID: String) async {
await activeTimeline.retryDecryption(for: sessionID)
}
// MARK: - Private
/// The cancellable used to update the timeline items.
private var updateTimelineItemsCancellable: AnyCancellable?
/// The controller is switching the `activeTimelineProvider`.
private var isSwitchingTimelines = false
/// Configures the controller to listen to `activeTimeline` for events.
/// - Parameter clearExistingItems: Whether or not to clear any existing items before loading the timeline's contents.
private func configureActiveTimelineProvider() {
updateTimelineItemsCancellable = nil
isSwitchingTimelines = true
// Inform the world that the initial items are loading from the store
callbacks.send(.paginationState(.init(backward: .paginating, forward: .paginating)))
callbacks.send(.isLive(activeTimelineProvider.kind == .live))
updateTimelineItemsCancellable = activeTimelineProvider
.updatePublisher
.receive(on: serialDispatchQueue)
.sink { [weak self] items, paginationState in
self?.updateTimelineItems(itemProxies: items, paginationState: paginationState)
}
}
@objc private func contentSizeCategoryDidChange() {
// Recompute all attributed strings on content size changes -> DynamicType support
serialDispatchQueue.async { [activeTimelineProvider] in
self.updateTimelineItems(itemProxies: activeTimelineProvider.itemProxies, paginationState: activeTimelineProvider.paginationState)
}
}
private func updateTimelineItems(itemProxies: [TimelineItemProxy], paginationState: PaginationState) {
var newTimelineItems = [RoomTimelineItemProtocol]()
let isNewTimeline = isSwitchingTimelines
isSwitchingTimelines = false
let collapsibleChunks = itemProxies.groupBy { isItemCollapsible($0) }
for (index, collapsibleChunk) in collapsibleChunks.enumerated() {
let isLastItem = index == collapsibleChunks.indices.last
let items = collapsibleChunk.compactMap { itemProxy in
let timelineItem = buildTimelineItem(for: itemProxy)
return timelineItem
}
if items.isEmpty {
continue
}
if items.count == 1, let timelineItem = items.first {
// Don't show the read marker if it's the last item in the timeline
// https://github.com/matrix-org/matrix-rust-sdk/issues/1546
guard !(timelineItem is ReadMarkerRoomTimelineItem && isLastItem) else {
continue
}
newTimelineItems.append(timelineItem)
} else {
newTimelineItems.append(CollapsibleTimelineItem(items: items))
}
}
// Check if we need to add anything to the top of the timeline.
switch paginationState.backward {
case .timelineEndReached:
if timelineKind != .pinned, !roomProxy.isEncryptedOneToOneRoom {
let timelineStart = TimelineStartRoomTimelineItem(name: roomProxy.infoPublisher.value.displayName)
newTimelineItems.insert(timelineStart, at: 0)
}
case .paginating:
newTimelineItems.insert(PaginationIndicatorRoomTimelineItem(position: .start), at: 0)
case .idle:
break
}
switch paginationState.forward {
case .paginating:
newTimelineItems.insert(PaginationIndicatorRoomTimelineItem(position: .end), at: newTimelineItems.count)
case .idle, .timelineEndReached:
break
}
DispatchQueue.main.sync {
timelineItems = newTimelineItems
}
callbacks.send(.updatedTimelineItems(timelineItems: newTimelineItems, isSwitchingTimelines: isNewTimeline))
callbacks.send(.paginationState(paginationState))
}
private func buildTimelineItem(for itemProxy: TimelineItemProxy) -> RoomTimelineItemProtocol? {
switch itemProxy {
case .event(let eventTimelineItem):
let timelineItem = timelineItemFactory.buildTimelineItem(for: eventTimelineItem, isDM: roomProxy.isEncryptedOneToOneRoom)
if let messageTimelineItem = timelineItem as? EventBasedMessageTimelineItemProtocol {
// Avoid fetching this over and over again as it changes states if it keeps failing to load
// Errors will be handled again on appearance
fetchEventDetails(for: messageTimelineItem, refetchOnError: false)
}
return timelineItem
case .virtual(let virtualItem, let uniqueID):
switch virtualItem {
case .dayDivider(let timestamp):
let date = Date(timeIntervalSince1970: TimeInterval(timestamp / 1000))
let dateString = date.formatted(date: .complete, time: .omitted)
return SeparatorRoomTimelineItem(id: .virtual(uniqueID: uniqueID), text: dateString)
case .readMarker:
return ReadMarkerRoomTimelineItem(id: .virtual(uniqueID: uniqueID))
}
case .unknown:
return nil
}
}
private func isItemCollapsible(_ item: TimelineItemProxy) -> Bool {
if case let .event(eventItem) = item {
switch eventItem.content {
case .profileChange, .roomMembership, .state:
return true
default:
return false
}
}
return false
}
private func fetchEventDetails(for timelineItem: EventBasedMessageTimelineItemProtocol, refetchOnError: Bool) {
guard let eventID = timelineItem.id.eventID else {
return
}
switch timelineItem.replyDetails {
case .notLoaded:
activeTimeline.fetchDetails(for: eventID)
case .error:
if refetchOnError {
activeTimeline.fetchDetails(for: eventID)
}
default:
break
}
}
func eventTimestamp(for itemID: TimelineItemIdentifier) -> Date? {
for itemProxy in activeTimelineProvider.itemProxies {
switch itemProxy {
case .event(let eventTimelineItemProxy):
if eventTimelineItemProxy.id == itemID {
return eventTimelineItemProxy.timestamp
}
case .virtual:
break
case .unknown:
break
}
}
return nil
}
}