acter/api/
deep_linking.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
use std::ops::Deref;
use std::sync::Arc;

use crate::{Client, RUNTIME};
use acter_core::events::{ObjRef as CoreObjRef, RefDetails as CoreRefDetails};
use acter_core::share_link::api;
use anyhow::{bail, Result};
use ruma::{OwnedEventId, OwnedRoomId, OwnedServerName};
use urlencoding::encode;

#[derive(Clone)]
pub struct ObjRef {
    client: Client,
    inner: CoreObjRef,
}

impl Deref for ObjRef {
    type Target = CoreObjRef;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl ObjRef {
    pub(crate) fn new(client: Client, inner: CoreObjRef) -> Self {
        Self { client, inner }
    }

    pub fn ref_details(&self) -> RefDetails {
        RefDetails {
            inner: self.inner.ref_details(),
            client: self.client.clone(),
        }
    }
}
pub struct RefDetails {
    inner: CoreRefDetails,
    client: Client,
}

impl Deref for RefDetails {
    type Target = CoreRefDetails;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

fn generate_object_link(
    room_id: &OwnedRoomId,
    path: &[(&str, &OwnedEventId)],
    via: &[OwnedServerName],
    params: &[(&str, Option<&String>)],
) -> String {
    let room_id = &room_id.to_string()[1..];
    format!(
        "acter:o/{room_id}/{}?{}",
        path.iter()
            .map(|(p, o)| format!("{p}/{}", &o.to_string()[1..]))
            .collect::<Vec<String>>()
            .join("/"),
        via.iter()
            .map(|v| format!("via={}", encode(v.as_str())))
            .chain(
                params
                    .iter()
                    .filter_map(|(key, v)| v.map(|i| format!("{key}={}", encode(i.as_str()))))
            )
            .collect::<Vec<String>>()
            .join("&")
    )
}

impl RefDetails {
    pub(crate) fn new(client: Client, inner: CoreRefDetails) -> Self {
        Self { client, inner }
    }

    pub fn can_generate_internal_link(&self) -> bool {
        match &self.inner {
            CoreRefDetails::Link { title, uri } => false,
            CoreRefDetails::Task { room_id, .. }
            | CoreRefDetails::TaskList { room_id, .. }
            | CoreRefDetails::Pin { room_id, .. }
            | CoreRefDetails::CalendarEvent { room_id, .. } => room_id.is_some(),
        }
    }

    pub fn generate_internal_link(&self, include_preview: bool) -> Result<String> {
        Ok(match &self.inner {
            CoreRefDetails::Link { title, uri } => bail!("Link can't be made into internal link"),
            CoreRefDetails::Task {
                target_id,
                room_id,
                via,
                preview,
                task_list,
                action,
            } => {
                let Some(room_id) = room_id else {
                    bail!("Object misses room_id")
                };
                let params = if include_preview {
                    vec![
                        ("roomDisplayName", preview.room_display_name.as_ref()),
                        ("title", preview.title.as_ref()),
                    ]
                } else {
                    vec![]
                };

                generate_object_link(
                    room_id,
                    &[("taskList", task_list), ("task", target_id)],
                    via.as_slice(),
                    params.as_slice(),
                )
            }
            CoreRefDetails::TaskList {
                target_id,
                room_id,
                via,
                preview,
                action,
            } => {
                let Some(room_id) = room_id else {
                    bail!("Object misses room_id")
                };
                let params = if include_preview {
                    vec![
                        ("roomDisplayName", preview.room_display_name.as_ref()),
                        ("title", preview.title.as_ref()),
                    ]
                } else {
                    vec![]
                };

                generate_object_link(
                    room_id,
                    &[("taskList", target_id)],
                    via.as_slice(),
                    params.as_slice(),
                )
            }
            CoreRefDetails::Pin {
                target_id,
                room_id,
                via,
                preview,
                action,
            } => {
                let Some(room_id) = room_id else {
                    bail!("Object misses room_id")
                };
                let params = if include_preview {
                    vec![
                        ("roomDisplayName", preview.room_display_name.as_ref()),
                        ("title", preview.title.as_ref()),
                    ]
                } else {
                    vec![]
                };

                generate_object_link(
                    room_id,
                    &[("pin", target_id)],
                    via.as_slice(),
                    params.as_slice(),
                )
            }
            CoreRefDetails::CalendarEvent {
                target_id,
                room_id,
                via,
                preview,
                action,
            } => {
                let Some(room_id) = room_id else {
                    bail!("Object misses room_id")
                };
                let participants = preview.participants.as_ref().map(ToString::to_string);
                let start_at = preview
                    .start_at_utc
                    .as_ref()
                    .map(|s| s.timestamp().to_string());
                let params = if include_preview {
                    vec![
                        ("roomDisplayName", preview.room_display_name.as_ref()),
                        ("title", preview.title.as_ref()),
                        ("participants", participants.as_ref()),
                        ("startAtUtc", start_at.as_ref()),
                    ]
                } else {
                    vec![]
                };

                generate_object_link(
                    room_id,
                    &[("calendarEvent", target_id)],
                    via.as_slice(),
                    params.as_slice(),
                )
            }
        })
    }

    pub async fn generate_external_link(&self) -> Result<String> {
        let c = self.client.core.client().clone();
        let inner = self.inner.clone();
        RUNTIME
            .spawn(async move {
                let req = api::create::Request::new(inner);
                let resp = c.send(req, None).await?;
                Ok(resp.url)
            })
            .await?
    }
}