acter/api/
reactions.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
use acter_core::models::{self, ActerModel, AnyActerModel};
use anyhow::{bail, Result};
use futures::stream::StreamExt;
use matrix_sdk::room::Room;
use matrix_sdk_base::ruma::{
    events::MessageLikeEventType, OwnedEventId, OwnedTransactionId, OwnedUserId, UserId,
};
use std::ops::Deref;
use tokio::sync::broadcast::Receiver;
use tokio_stream::{wrappers::BroadcastStream, Stream};

use super::{client::Client, RUNTIME};

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

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

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

impl Reaction {
    pub fn event_id_str(&self) -> String {
        self.inner.event_id().to_string()
    }

    pub fn sender(&self) -> OwnedUserId {
        self.inner.meta.sender.clone()
    }

    pub fn origin_server_ts(&self) -> u64 {
        self.inner.meta.origin_server_ts.get().into()
    }

    pub fn relates_to(&self) -> String {
        self.inner.relates_to.event_id.to_string()
    }
}

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

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

impl ReactionManager {
    pub(crate) async fn new(
        client: Client,
        room: Room,
        event_id: OwnedEventId,
    ) -> Result<ReactionManager> {
        RUNTIME
            .spawn(async move {
                let inner =
                    models::ReactionManager::from_store_and_event_id(client.store(), &event_id)
                        .await;
                Ok(ReactionManager {
                    client,
                    room,
                    inner,
                })
            })
            .await?
    }

    pub async fn reload(&self) -> Result<ReactionManager> {
        ReactionManager::new(
            self.client.clone(),
            self.room.clone(),
            self.inner.event_id(),
        )
        .await
    }

    pub async fn send_like(&self) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let event = self.inner.construct_like_event();

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

    pub async fn send_reaction(&self, key: String) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let event = self.inner.construct_reaction_event(key);

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

    pub async fn redact_like(
        &self,
        reason: Option<String>,
        txn_id: Option<String>,
    ) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let stats = self.inner.stats();
        let Some(event_id) = stats.user_likes.last().cloned() else {
            bail!("User hasn’t liked")
        };
        let txn_id = txn_id.map(OwnedTransactionId::from);

        RUNTIME
            .spawn(async move {
                let evt = room.event(&event_id, None).await?;
                let Some(sender) = evt.kind.raw().get_field::<OwnedUserId>("sender")? else {
                    bail!("Could not determine the sender of the previous event");
                };
                let permitted = if sender == my_id {
                    room.can_user_redact_own(&my_id).await?
                } else {
                    room.can_user_redact_other(&my_id).await?
                };
                if !permitted {
                    bail!("No permission to redact this reaction");
                }
                let response = room.redact(&event_id, reason.as_deref(), txn_id).await?;
                Ok(response.event_id)
            })
            .await?
    }

    pub async fn redact_reaction(
        &self,
        sender_id: String,
        key: String,
        reason: Option<String>,
        txn_id: Option<String>,
    ) -> Result<OwnedEventId> {
        let room = self.room.clone();
        let my_id = self.client.user_id()?;
        let sender_id = UserId::parse(sender_id)?;
        let inner = self.inner.clone();
        let txn_id = txn_id.map(OwnedTransactionId::from);

        RUNTIME
            .spawn(async move {
                let permitted = if sender_id == my_id {
                    room.can_user_redact_own(&my_id).await?
                } else {
                    room.can_user_redact_other(&my_id).await?
                };
                if !permitted {
                    bail!("No permission to redact this reaction");
                }
                for (user_id, reaction) in inner.reaction_entries().await? {
                    if user_id == sender_id && reaction.relates_to.key == key {
                        let response = room
                            .redact(reaction.event_id(), reason.as_deref(), txn_id)
                            .await?;
                        return Ok(response.event_id);
                    }
                }
                bail!("User hasn’t reacted")
            })
            .await?
    }

    pub fn stats(&self) -> models::ReactionStats {
        self.inner.stats().clone()
    }

    pub fn likes_count(&self) -> u32 {
        self.inner.stats().total_like_reactions
    }

    pub fn liked_by_me(&self) -> bool {
        self.inner.stats().user_has_liked
    }

    pub fn reacted_by_me(&self) -> bool {
        self.inner.stats().user_has_reacted
    }

    pub fn has_reaction_entries(&self) -> bool {
        self.stats().has_reaction_entries
    }

    pub fn total_reaction_count(&self) -> u32 {
        self.stats().total_reaction_count
    }

    pub async fn reaction_entries(&self) -> Result<Vec<Reaction>> {
        let manager = self.inner.clone();
        let client = self.client.clone();
        let room = self.room.clone();

        RUNTIME
            .spawn(async move {
                let res = manager
                    .reaction_entries()
                    .await?
                    .into_iter()
                    .map(|(user_id, inner)| Reaction {
                        client: client.clone(),
                        room: room.clone(),
                        inner,
                    })
                    .collect();
                Ok(res)
            })
            .await?
    }

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

    pub fn subscribe(&self) -> Receiver<()> {
        self.client.subscribe(self.inner.update_key())
    }
}