acter/api/spaces/
categories.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
use acter_core::events::CategoriesStateEventContent;
use anyhow::{bail, Result};
use matrix_sdk_base::{deserialized_responses::RawSyncOrStrippedState, ruma::events::EventContent};
use tracing::warn;

use crate::{Categories, CategoriesBuilder, RUNTIME};

use super::Space;

impl Space {
    pub async fn categories(&self, cat_type: String) -> Result<Categories> {
        if !self.inner.is_joined() {
            bail!("Unable to read categories of a space you didn’t join");
        }
        let room = self.inner.room.clone();
        RUNTIME
            .spawn(async move {
                let inner = if let Some(RawSyncOrStrippedState::Sync(raw_state)) = room
                    .get_state_event_static_for_key::<CategoriesStateEventContent, _>(&cat_type)
                    .await?
                {
                    match raw_state
                        .get_field::<CategoriesStateEventContent>("content") {
                            Ok(u) => u,
                            Err(error) => {
                                warn!(room_id=?room.room_id(), ?raw_state, ?error, "Failed to deserialize categories.");
                                None
                            }
                        }
                } else {
                    None
                };
                Ok(Categories::new(inner))
            })
            .await?
    }
    pub async fn set_categories(
        &self,
        cat_type: String,
        builder: Box<CategoriesBuilder>,
    ) -> Result<bool> {
        let state_event = (*builder).build();
        if !self.inner.is_joined() {
            bail!("Unable to read categories of a space you didn’t join");
        }
        let room = self.inner.room.clone();
        let user_id = self.client.user_id()?;
        RUNTIME
            .spawn(async move {
                if (!room
                    .can_user_send_state(&user_id, state_event.event_type())
                    .await?)
                {
                    return Ok(false);
                }
                room.send_state_event_for_key(&cat_type, state_event)
                    .await?;
                Ok(true)
            })
            .await?
    }
}