atagen

macltok

public
Star0
Watch0
fe4133dbcd47e9263b8915f2406de5b2cab7e65a
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git
src/engine/timeline.rsRust456 lines16.1 KB
1
use std::{collections::HashSet, future::Future, panic::AssertUnwindSafe, sync::Arc};
2
3
use futures_util::{FutureExt, StreamExt};
4
use matrix_sdk::SendOutsideWasm;
5
use matrix_sdk::{
6
    room::edit::EditedContent,
7
    room::reply::{EnforceThread, Reply as SdkReply},
8
    ruma::{
9
        api::client::receipt::create_receipt::v3::ReceiptType,
10
        events::{
11
            Mentions,
12
            room::message::{
13
                AddMentions, Relation, ReplyWithinThread, RoomMessageEventContentWithoutRelation,
14
            },
15
        },
16
    },
17
};
18
use matrix_sdk_ui::timeline::{Timeline, TimelineEventItemId};
19
use ruma::{OwnedEventId, OwnedUserId};
20
use tokio::sync::mpsc;
21
22
use super::executor::{EngineBoxFuture, JoinHandle, TaskSet, spawn};
23
use crate::{
24
    Error, ErrorKind, Event, EventRef, HistoryState, MessageContent, MessageEditContent,
25
    MessageMentions, RichText, TimelineCommand, TimelineId,
26
    engine::MediaService,
27
    normalise::{MediaScope, normalise_timeline, normalise_timeline_diffs},
28
    transport::{EventSender, Responder, SendEventError},
29
};
30
31
const PAGINATION_QUEUE: usize = 32;
32
33
/// long-lived tasks inside a room actor report panics here so the actor can die loudly
34
#[derive(Clone)]
35
pub(super) struct Supervisor(mpsc::UnboundedSender<&'static str>);
36
37
impl Supervisor {
38
    pub(super) fn new() -> (Self, mpsc::UnboundedReceiver<&'static str>) {
39
        let (sender, receiver) = mpsc::unbounded_channel();
40
        (Self(sender), receiver)
41
    }
42
43
    pub(super) fn spawn(
44
        &self,
45
        label: &'static str,
46
        task: impl Future<Output = ()> + SendOutsideWasm + 'static,
47
    ) -> JoinHandle<()> {
48
        let panics = self.0.clone();
49
        spawn(async move {
50
            if AssertUnwindSafe(task).catch_unwind().await.is_err() {
51
                let _ = panics.send(label);
52
            }
53
        })
54
    }
55
}
56
57
/// aborts its task when dropped, so an actor that dies mid-flight takes its helpers with it
58
pub(super) struct TaskGuard(pub(super) JoinHandle<()>);
59
60
impl Drop for TaskGuard {
61
    fn drop(&mut self) {
62
        self.0.abort();
63
    }
64
}
65
66
/// what a timeline session needs from the room that owns it
67
#[derive(Clone)]
68
pub(super) struct SessionContext {
69
    pub(super) events: EventSender,
70
    pub(super) media: Arc<MediaService>,
71
    pub(super) current_user_id: Option<OwnedUserId>,
72
    pub(super) initial_history_limit: u16,
73
    pub(super) supervisor: Supervisor,
74
}
75
76
pub(super) struct TimelineSession {
77
    pub(super) id: TimelineId,
78
    timeline: Arc<Timeline>,
79
    context: SessionContext,
80
    scope: MediaScope,
81
    watcher: JoinHandle<()>,
82
    pagination: mpsc::Sender<PaginationRequest>,
83
    pagination_worker: JoinHandle<()>,
84
}
85
86
struct PaginationRequest {
87
    limit: u16,
88
    reply: Option<Responder>,
89
}
90
91
impl TimelineSession {
92
    pub(super) async fn start(
93
        id: TimelineId,
94
        timeline: Timeline,
95
        context: SessionContext,
96
    ) -> Result<Self, SendEventError> {
97
        let timeline = Arc::new(timeline);
98
        let scope = MediaScope::new();
99
        let watcher = start_watcher(&id, &timeline, &context, &scope).await?;
100
        let (pagination, requests) = mpsc::channel(PAGINATION_QUEUE);
101
        let pagination_worker = context.supervisor.spawn(
102
            "pagination worker",
103
            run_pagination(
104
                requests,
105
                timeline.clone(),
106
                context.events.clone(),
107
                id.clone(),
108
            ),
109
        );
110
        let session = Self {
111
            id,
112
            timeline,
113
            scope,
114
            watcher,
115
            pagination,
116
            pagination_worker,
117
            context,
118
        };
119
        if session.context.initial_history_limit > 0 {
120
            session.paginate(session.context.initial_history_limit, None);
121
        }
122
        Ok(session)
123
    }
124
125
    /// swaps in a fresh watcher. the old one is dead before the reset goes out,
126
    /// so no stale diff can land after it
127
    pub(super) async fn resync(&mut self) -> Result<(), Error> {
128
        self.watcher.abort();
129
        let _ = (&mut self.watcher).await;
130
        self.watcher = start_watcher(&self.id, &self.timeline, &self.context, &self.scope)
131
            .await
132
            .map_err(|_| Error::new(ErrorKind::Timeline, "timeline reset could not be sent"))?;
133
        Ok(())
134
    }
135
136
    pub(super) fn paginate(&self, limit: u16, reply: Option<Responder>) {
137
        if let Err(error) = self.pagination.try_send(PaginationRequest { limit, reply }) {
138
            let (request, error) = match error {
139
                mpsc::error::TrySendError::Full(request) => (
140
                    request,
141
                    Error::new(ErrorKind::QueueFull, "pagination queue is full"),
142
                ),
143
                mpsc::error::TrySendError::Closed(request) => (
144
                    request,
145
                    Error::new(ErrorKind::TimelineNotOpen, "timeline is closing"),
146
                ),
147
            };
148
            if let Some(reply) = request.reply {
149
                reply.done(Err(error));
150
            }
151
        }
152
    }
153
154
    /// pending paginations answer `Interrupted` as their responders drop
155
    pub(super) async fn close(mut self) {
156
        self.pagination_worker.abort();
157
        let _ = (&mut self.pagination_worker).await;
158
        self.watcher.abort();
159
        let _ = (&mut self.watcher).await;
160
        // after the watcher, or a last diff could register media for a timeline that's gone
161
        self.context.media.release(&self.scope).await;
162
    }
163
164
    pub(super) async fn execute(
165
        &self,
166
        command: TimelineCommand,
167
        reply: Responder,
168
        work: &mut TaskSet<()>,
169
    ) {
170
        let timeline = self.timeline.clone();
171
        let thread_root = self.id.thread_root().cloned();
172
        let background: EngineBoxFuture<'static, Result<(), Error>> = match command {
173
            TimelineCommand::Paginate { limit, .. } => return self.paginate(limit, Some(reply)),
174
            // network-bound, so it runs beside the actor instead of queueing the room behind it
175
            TimelineCommand::Redact { target, reason, .. } => Box::pin(async move {
176
                timeline
177
                    .redact(&item_id(target), reason.as_deref())
178
                    .await
179
                    .map_err(timeline_error)
180
            }),
181
            TimelineCommand::ToggleReaction { target, key, .. } => Box::pin(async move {
182
                timeline
183
                    .toggle_reaction(&item_id(target), &key)
184
                    .await
185
                    .map(drop)
186
                    .map_err(timeline_error)
187
            }),
188
            TimelineCommand::MarkRead { event_id, .. } => Box::pin(async move {
189
                let receipt_failed =
190
                    |source| Error::from_sdk("sending read receipt failed", source);
191
                timeline
192
                    .send_single_receipt(ReceiptType::Read, event_id.clone())
193
                    .await
194
                    .map_err(receipt_failed)?;
195
                if thread_root.is_none() {
196
                    timeline
197
                        .send_single_receipt(ReceiptType::FullyRead, event_id)
198
                        .await
199
                        .map_err(receipt_failed)?;
200
                }
201
                Ok(())
202
            }),
203
            command => return reply.done(self.execute_serial(command).await),
204
        };
205
        work.spawn(async move { reply.done(background.await) });
206
    }
207
208
    /// sends stay on the actor so they hit the send queue in the order they were asked for
209
    async fn execute_serial(&self, command: TimelineCommand) -> Result<(), Error> {
210
        let timeline = &self.timeline;
211
        match command {
212
            TimelineCommand::Send { content, .. } => {
213
                let body = match &content {
214
                    MessageContent::Text { content, .. }
215
                    | MessageContent::Emote { content, .. }
216
                    | MessageContent::Reply { content, .. } => &content.body,
217
                };
218
                if body.trim().is_empty() {
219
                    return Err(Error::new(ErrorKind::EmptyBody, "message body is empty"));
220
                }
221
                let content = match content {
222
                    MessageContent::Text { content, mentions } => {
223
                        message_content(content, mentions, false).with_relation(None)
224
                    }
225
                    MessageContent::Emote { content, mentions } => {
226
                        message_content(content, mentions, true).with_relation(None)
227
                    }
228
                    MessageContent::Reply {
229
                        event_id,
230
                        content,
231
                        mentions,
232
                        notify_replied_to,
233
                    } => {
234
                        self.reply_content(event_id, content, mentions, notify_replied_to)
235
                            .await?
236
                    }
237
                };
238
                timeline
239
                    .send(content.into())
240
                    .await
241
                    .map(drop)
242
                    .map_err(timeline_error)
243
            }
244
            TimelineCommand::Edit {
245
                target, content, ..
246
            } => {
247
                let (content, mentions, emote) = match content {
248
                    MessageEditContent::Text { content, mentions } => (content, mentions, false),
249
                    MessageEditContent::Emote { content, mentions } => (content, mentions, true),
250
                };
251
                if content.body.trim().is_empty() {
252
                    return Err(Error::new(ErrorKind::EmptyBody, "message body is empty"));
253
                }
254
                timeline
255
                    .edit(
256
                        &item_id(target),
257
                        EditedContent::RoomMessage(message_content(content, mentions, emote)),
258
                    )
259
                    .await
260
                    .map_err(timeline_error)
261
            }
262
            _ => Err(Error::new(
263
                ErrorKind::Other,
264
                "timeline command was misrouted",
265
            )),
266
        }
267
    }
268
269
    async fn reply_content(
270
        &self,
271
        event_id: OwnedEventId,
272
        content: RichText,
273
        mentions: MessageMentions,
274
        notify_replied_to: bool,
275
    ) -> Result<matrix_sdk::ruma::events::room::message::RoomMessageEventContent, Error> {
276
        let thread_root = self.id.thread_root();
277
        let content = self
278
            .timeline
279
            .room()
280
            .make_reply_event(
281
                message_content(content, mentions, false),
282
                SdkReply {
283
                    event_id,
284
                    enforce_thread: if thread_root.is_some() {
285
                        EnforceThread::Threaded(ReplyWithinThread::Yes)
286
                    } else {
287
                        EnforceThread::MaybeThreaded
288
                    },
289
                    add_mentions: if notify_replied_to {
290
                        AddMentions::Yes
291
                    } else {
292
                        AddMentions::No
293
                    },
294
                },
295
            )
296
            .await
297
            .map_err(|source| {
298
                Error::with_source(ErrorKind::Timeline, "reply operation failed", source)
299
            })?;
300
        if let Some(thread_root) = thread_root
301
            && !matches!(
302
                content.relates_to.as_ref(),
303
                Some(Relation::Thread(thread)) if &thread.event_id == thread_root
304
            )
305
        {
306
            return Err(Error::new(
307
                ErrorKind::Invalid,
308
                "reply target does not belong to the selected thread",
309
            ));
310
        }
311
        Ok(content)
312
    }
313
}
314
315
impl Drop for TimelineSession {
316
    fn drop(&mut self) {
317
        self.watcher.abort();
318
        self.pagination_worker.abort();
319
    }
320
}
321
322
fn item_id(target: EventRef) -> TimelineEventItemId {
323
    match target {
324
        EventRef::Remote(event_id) => TimelineEventItemId::EventId(event_id),
325
        EventRef::Local(transaction_id) => TimelineEventItemId::TransactionId(transaction_id),
326
    }
327
}
328
329
/// the sdk leaves a reply's target unloaded until someone asks, so the watcher asks, once per event
330
struct ReplyFetcher {
331
    timeline: Arc<Timeline>,
332
    asked: HashSet<OwnedEventId>,
333
    fetches: TaskSet<()>,
334
}
335
336
impl ReplyFetcher {
337
    fn fetch(&mut self, missing: Vec<OwnedEventId>) {
338
        for event_id in missing {
339
            if self.asked.insert(event_id.clone()) {
340
                let timeline = self.timeline.clone();
341
                self.fetches.spawn(async move {
342
                    if let Err(error) = timeline.fetch_details_for_event(&event_id).await {
343
                        tracing::debug!(%event_id, %error, "fetching reply details failed");
344
                    }
345
                });
346
            }
347
        }
348
    }
349
}
350
351
async fn start_watcher(
352
    id: &TimelineId,
353
    timeline: &Arc<Timeline>,
354
    context: &SessionContext,
355
    scope: &MediaScope,
356
) -> Result<JoinHandle<()>, SendEventError> {
357
    let (items, mut stream) = timeline.subscribe().await;
358
    let current_user_id = context.current_user_id.clone();
359
    let normalised = normalise_timeline(items.iter(), current_user_id.as_deref(), scope);
360
    context.media.register(normalised.media).await;
361
    context
362
        .events
363
        .send(Event::TimelineReset {
364
            timeline_id: id.clone(),
365
            items: normalised.items,
366
        })
367
        .await?;
368
    let mut replies = ReplyFetcher {
369
        timeline: timeline.clone(),
370
        asked: HashSet::new(),
371
        fetches: TaskSet::new(),
372
    };
373
    replies.fetch(normalised.missing_replies);
374
    let id = id.clone();
375
    let events = context.events.clone();
376
    let media = context.media.clone();
377
    let scope = scope.clone();
378
    Ok(context.supervisor.spawn("timeline watcher", async move {
379
        loop {
380
            let diffs = tokio::select! {
381
                diffs = stream.next() => match diffs {
382
                    Some(diffs) => diffs,
383
                    None => return,
384
                },
385
                Some(_) = replies.fetches.join_next(), if !replies.fetches.is_empty() => continue,
386
            };
387
            let normalised = normalise_timeline_diffs(diffs, current_user_id.as_deref(), &scope);
388
            media.register(normalised.media).await;
389
            replies.fetch(normalised.missing_replies);
390
            let event = Event::TimelineChanged {
391
                timeline_id: id.clone(),
392
                diffs: normalised.diffs,
393
            };
394
            if events.send(event).await.is_err() {
395
                return;
396
            }
397
        }
398
    }))
399
}
400
401
async fn run_pagination(
402
    mut requests: mpsc::Receiver<PaginationRequest>,
403
    timeline: Arc<Timeline>,
404
    events: EventSender,
405
    timeline_id: TimelineId,
406
) {
407
    while let Some(PaginationRequest { limit, reply }) = requests.recv().await {
408
        let history = |state| Event::HistoryChanged {
409
            timeline_id: timeline_id.clone(),
410
            state,
411
        };
412
        if events.send(history(HistoryState::Loading)).await.is_err() {
413
            return;
414
        }
415
        let result = timeline
416
            .paginate_backwards(limit.max(1))
417
            .await
418
            .map_err(timeline_error);
419
        let state = match &result {
420
            Ok(true) => HistoryState::StartReached,
421
            Ok(false) => HistoryState::Idle,
422
            Err(error) => HistoryState::Failed(error.to_string()),
423
        };
424
        let _ = events.send(history(state)).await;
425
        if let Some(reply) = reply {
426
            reply.done(result.map(drop));
427
        }
428
    }
429
}
430
431
fn message_content(
432
    content: RichText,
433
    mentions: MessageMentions,
434
    emote: bool,
435
) -> RoomMessageEventContentWithoutRelation {
436
    let content = match content.formatted_body {
437
        Some(formatted_body) if emote => {
438
            RoomMessageEventContentWithoutRelation::emote_html(content.body, formatted_body)
439
        }
440
        Some(formatted_body) => {
441
            RoomMessageEventContentWithoutRelation::text_html(content.body, formatted_body)
442
        }
443
        None if emote => RoomMessageEventContentWithoutRelation::emote_plain(content.body),
444
        None => RoomMessageEventContentWithoutRelation::text_plain(content.body),
445
    };
446
    let content = content.add_mentions(Mentions::with_user_ids(mentions.user_ids));
447
    if mentions.room {
448
        content.add_mentions(Mentions::with_room_mention())
449
    } else {
450
        content
451
    }
452
}
453
454
pub(super) fn timeline_error(source: matrix_sdk_ui::timeline::Error) -> Error {
455
    Error::with_source(ErrorKind::Timeline, "timeline operation failed", source)
456
}