acter/api/
pins.rs

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
use acter_core::{
    events::{
        pins::{self, PinBuilder},
        Display, RefDetails as CoreRefDetails, RefPreview,
    },
    models::{self, can_redact, ActerModel, AnyActerModel},
    referencing::{IndexKey, SectionIndex},
};
use anyhow::{bail, Result};
use futures::stream::StreamExt;
use matrix_sdk::room::Room;
use matrix_sdk::ruma::EventId;
use matrix_sdk_base::{
    ruma::{
        events::{room::message::TextMessageEventContent, MessageLikeEventType},
        OwnedEventId, OwnedRoomId, OwnedUserId,
    },
    RoomState,
};
use std::{
    collections::{hash_map::Entry, HashMap},
    ops::Deref,
};
use tokio::sync::broadcast::Receiver;
use tokio_stream::{wrappers::BroadcastStream, Stream};
use tracing::warn;

use crate::MsgContent;

use super::{client::Client, deep_linking::RefDetails, spaces::Space, RUNTIME};

impl Client {
    pub async fn wait_for_pin(&self, key: String, timeout: Option<u8>) -> Result<Pin> {
        let me = self.clone();
        RUNTIME
            .spawn(async move {
                let AnyActerModel::Pin(content) = me.wait_for(key.clone(), timeout).await? else {
                    bail!("{key} is not a pin");
                };
                let room = me.room_by_id_typed(content.room_id())?;
                Ok(Pin {
                    client: me.clone(),
                    room,
                    content,
                })
            })
            .await?
    }

    pub async fn pins(&self) -> Result<Vec<Pin>> {
        let me = self.clone();
        Ok(self
            .models_of_list_with_room(IndexKey::Section(SectionIndex::Pins))
            .await?
            .map(|(inner, room)| Pin {
                client: self.clone(),
                room,
                content: inner,
            })
            .collect())
    }

    pub async fn pin(&self, pin_id: String) -> Result<Pin> {
        let me = self.clone();
        self.model_with_room(EventId::parse(pin_id)?)
            .await
            .map(|(inner, room)| Pin {
                client: self.clone(),
                room,
                content: inner,
            })
    }
}

impl Space {
    pub async fn pins(&self) -> Result<Vec<Pin>> {
        let client = self.client.clone();
        let room = self.room.clone();
        Ok(client
            .models_of_list_with_room_under_check(
                IndexKey::RoomSection(room.room_id().to_owned(), SectionIndex::Pins),
                move |_r| Ok(room.clone()),
            )
            .await?
            .map(|(inner, room)| Pin {
                client: client.clone(),
                room,
                content: inner,
            })
            .collect())
    }
}

#[derive(Clone, Debug)]
pub struct Pin {
    client: Client,
    room: Room,
    content: models::Pin,
}

impl Deref for Pin {
    type Target = models::Pin;
    fn deref(&self) -> &Self::Target {
        &self.content
    }
}

/// helpers for content
impl Pin {
    pub fn title(&self) -> String {
        self.content.title.clone()
    }

    pub fn has_formatted_text(&self) -> bool {
        matches!(
            self.content.content(),
            Some(TextMessageEventContent {
                formatted: Some(_),
                ..
            })
        )
    }

    pub async fn ref_details(&self) -> Result<RefDetails> {
        let room = self.room.clone();
        let client = self.client.deref().clone();
        let target_id = self.content.event_id().to_owned();
        let room_id = self.room.room_id().to_owned();
        let title = self.content.title.clone();

        RUNTIME
            .spawn(async move {
                let via = room.route().await?;
                let room_display_name = room.cached_display_name();
                Ok(RefDetails::new(
                    client,
                    CoreRefDetails::Pin {
                        target_id,
                        room_id: Some(room_id),
                        via,
                        preview: RefPreview::new(Some(title), room_display_name),
                        action: Default::default(),
                    },
                ))
            })
            .await?
    }

    pub fn content_formatted(&self) -> Option<String> {
        self.content
            .content
            .as_ref()
            .and_then(|t| t.formatted.clone().map(|f| f.body))
    }

    pub fn content(&self) -> Option<MsgContent> {
        self.content.content.as_ref().map(MsgContent::from)
    }

    pub fn display(&self) -> Option<Display> {
        self.content.display.clone()
    }

    pub fn url(&self) -> Option<String> {
        self.content.url.clone()
    }

    pub fn event_id_str(&self) -> String {
        self.content.event_id().to_string()
    }

    pub fn room_id_str(&self) -> String {
        self.content.room_id().to_string()
    }

    pub fn sender(&self) -> OwnedUserId {
        self.content.sender().to_owned()
    }
}

/// Custom functions
impl Pin {
    pub async fn refresh(&self) -> Result<Pin> {
        let key = self.content.event_id().to_owned();
        let client = self.client.clone();
        let room = self.room.clone();

        RUNTIME
            .spawn(async move {
                let AnyActerModel::Pin(content) = client.store().get(&key).await? else {
                    bail!("Refreshing failed. {key} not a pin")
                };
                Ok(Pin {
                    client,
                    room,
                    content,
                })
            })
            .await?
    }

    pub async fn can_redact(&self) -> Result<bool> {
        let sender = self.content.sender().to_owned();
        let room = self.room.clone();

        RUNTIME
            .spawn(async move { Ok(can_redact(&room, &sender).await?) })
            .await?
    }

    fn is_joined(&self) -> bool {
        matches!(self.room.state(), RoomState::Joined)
    }

    pub fn update_builder(&self) -> Result<PinUpdateBuilder> {
        if !self.is_joined() {
            bail!("Can only update pins in joined rooms");
        }
        Ok(PinUpdateBuilder {
            client: self.client.clone(),
            room: self.room.clone(),
            content: self.content.updater(),
        })
    }

    pub fn subscribe_stream(&self) -> impl Stream<Item = bool> {
        BroadcastStream::new(self.subscribe()).map(|_| true)
    }

    pub fn subscribe(&self) -> Receiver<()> {
        let key = self.content.event_id().to_owned();
        self.client.subscribe(key)
    }

    pub async fn comments(&self) -> Result<crate::CommentsManager> {
        let client = self.client.clone();
        let room = self.room.clone();
        let event_id = self.content.event_id().to_owned();
        crate::CommentsManager::new(client, room, event_id).await
    }

    pub async fn attachments(&self) -> Result<crate::AttachmentsManager> {
        let client = self.client.clone();
        let room = self.room.clone();
        let event_id = self.content.event_id().to_owned();
        crate::AttachmentsManager::new(client, room, event_id).await
    }
}

#[derive(Clone)]
pub struct PinDraft {
    client: Client,
    room: Room,
    content: PinBuilder,
}

impl PinDraft {
    pub fn title(&mut self, title: String) -> &mut Self {
        self.content.title(title);
        self
    }

    pub fn content_text(&mut self, body: String) -> &mut Self {
        self.content
            .content(Some(TextMessageEventContent::plain(body)));
        self
    }

    pub fn content_markdown(&mut self, body: String) -> &mut Self {
        self.content
            .content(Some(TextMessageEventContent::markdown(body)));
        self
    }

    pub fn content_html(&mut self, body: String, html_body: String) -> &mut Self {
        self.content
            .content(Some(TextMessageEventContent::html(body, html_body)));
        self
    }

    pub fn unset_content(&mut self) -> &mut Self {
        self.content.content(None);
        self
    }

    pub fn url(&mut self, url: String) -> &mut Self {
        self.content.url(Some(url));
        self
    }

    pub fn unset_url(&mut self) -> &mut Self {
        self.content.url(None);
        self
    }

    pub fn display(&mut self, display: Box<Display>) -> &mut Self {
        self.content.display(Some(*display));
        self
    }

    pub fn unset_display(&mut self) -> &mut Self {
        self.content.display(None);
        self
    }

    pub async fn send(&self) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let content = self.content.build()?;

        RUNTIME
            .spawn(async move {
                let permitted = room
                    .can_user_send_message(&my_id, MessageLikeEventType::RoomMessage)
                    .await?;
                if !permitted {
                    bail!("No permissions to send message in this room");
                }
                let response = room.send(content).await?;
                Ok(response.event_id)
            })
            .await?
    }
}

#[derive(Clone)]
pub struct PinUpdateBuilder {
    client: Client,
    room: Room,
    content: pins::PinUpdateBuilder,
}

impl PinUpdateBuilder {
    pub fn title(&mut self, title: String) -> &mut Self {
        self.content.title(Some(title));
        self
    }

    pub fn unset_title_update(&mut self) -> &mut Self {
        self.content.title(None);
        self
    }

    pub fn content_text(&mut self, body: String) -> &mut Self {
        self.content
            .content(Some(Some(TextMessageEventContent::plain(body))));
        self
    }

    pub fn content_markdown(&mut self, body: String) -> &mut Self {
        self.content
            .content(Some(Some(TextMessageEventContent::markdown(body))));
        self
    }

    pub fn content_html(&mut self, body: String, html_body: String) -> &mut Self {
        self.content
            .content(Some(Some(TextMessageEventContent::html(body, html_body))));
        self
    }

    pub fn unset_content(&mut self) -> &mut Self {
        self.content.content(Some(None));
        self
    }

    pub fn unset_content_update(&mut self) -> &mut Self {
        self.content
            .content(None::<Option<TextMessageEventContent>>);
        self
    }

    pub fn url(&mut self, url: String) -> &mut Self {
        self.content.url(Some(Some(url)));
        self
    }

    pub fn unset_url(&mut self) -> &mut Self {
        self.content.url(Some(None));
        self
    }

    pub fn unset_url_update(&mut self) -> &mut Self {
        self.content.url(None::<Option<String>>);
        self
    }

    pub fn display(&mut self, display: Box<Display>) -> &mut Self {
        self.content.display(Some(Some(*display)));
        self
    }

    pub fn unset_display(&mut self) -> &mut Self {
        self.content.display(Some(None));
        self
    }

    pub fn unset_display_update(&mut self) -> &mut Self {
        self.content.display(None::<Option<Display>>);
        self
    }

    pub async fn send(&self) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let content = self.content.build()?;

        RUNTIME
            .spawn(async move {
                let permitted = room
                    .can_user_send_message(&my_id, MessageLikeEventType::RoomMessage)
                    .await?;
                if !permitted {
                    bail!("No permissions to send message in this room");
                }
                let response = room.send(content).await?;
                Ok(response.event_id)
            })
            .await?
    }
}

impl Space {
    pub fn pin_draft(&self) -> Result<PinDraft> {
        if !self.is_joined() {
            bail!("Unable to create pins for spaces we are not part on");
        }
        Ok(PinDraft {
            client: self.client.clone(),
            room: self.inner.room.clone(),
            content: Default::default(),
        })
    }

    pub fn pin_draft_with_builder(&self, content: PinBuilder) -> Result<PinDraft> {
        if !self.is_joined() {
            bail!("Unable to create pins for spaces we are not part on");
        }
        Ok(PinDraft {
            client: self.client.clone(),
            room: self.inner.room.clone(),
            content,
        })
    }
}