BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / channel_visibility.rs

channel_visibility.rs

channel_visibility.rs 2.8 KiB Download Raw
1use chatsvc::{ChannelKind, ChannelTarget};
2use chrono::Utc;
3
4use crate::engine::SyncEngine;
5use crate::error::{Error, Result};
6use crate::events::CoreEvent;
7use crate::message_link::{ChannelAddress, channel_link, team_link};
8use crate::remote::Remote;
9
10impl<R: Remote> SyncEngine<R> {
11 pub async fn set_channel_pinned(&self, channel_id: &str, pinned: bool) -> Result<()> {
12 let channel_ids = self
13 .folder_source()?
14 .set_channel_pinned(channel_id, pinned)
15 .await?;
16 self.store.replace_pinned_channels(&channel_ids)?;
17 let _ = self.events.send(CoreEvent::FoldersChanged);
18 Ok(())
19 }
20
21 pub async fn set_channel_shown(&self, channel_id: &str, shown: bool) -> Result<()> {
22 let channel = self
23 .store
24 .channel(channel_id)?
25 .ok_or_else(|| Error::UnknownConversation(channel_id.to_owned()))?;
26 let general = self.store.team_thread_id(&channel.team_id)?.as_deref() == Some(channel_id);
27 let own_user_id = self.my_user_id().await?;
28 let target = ChannelTarget {
29 channel_id,
30 general,
31 kind: ChannelKind::from_membership_type(channel.membership_type.as_deref()),
32 };
33 self.folder_source()?
34 .set_channel_shown(target, &own_user_id, shown, Utc::now().timestamp_millis())
35 .await?;
36 self.store.set_channel_hidden(channel_id, !shown)?;
37 let _ = self.events.send(CoreEvent::SidebarChanged);
38 Ok(())
39 }
40
41 pub async fn set_team_shown(&self, team_id: &str, shown: bool) -> Result<()> {
42 let team_thread_id = self.known_team_thread_id(team_id)?;
43 self.folder_source()?
44 .set_team_shown(&team_thread_id, shown)
45 .await?;
46 self.store.set_team_hidden(team_id, !shown)?;
47 let _ = self.events.send(CoreEvent::SidebarChanged);
48 Ok(())
49 }
50
51 pub async fn channel_link(&self, channel_id: &str) -> Result<String> {
52 let channel = self
53 .store
54 .channel(channel_id)?
55 .ok_or_else(|| Error::UnknownConversation(channel_id.to_owned()))?;
56 Ok(channel_link(&ChannelAddress {
57 tenant_id: self.my_tenant_id_from_any_chat().await?,
58 group_id: channel.team_id,
59 channel_id: channel.id,
60 channel_name: channel.name,
61 membership_type: channel.membership_type,
62 }))
63 }
64
65 pub async fn team_link(&self, team_id: &str) -> Result<String> {
66 let team_thread_id = self.known_team_thread_id(team_id)?;
67 let tenant_id = self.my_tenant_id_from_any_chat().await?;
68 Ok(team_link(&team_thread_id, team_id, &tenant_id))
69 }
70
71 fn known_team_thread_id(&self, team_id: &str) -> Result<String> {
72 self.store
73 .team_thread_id(team_id)?
74 .ok_or_else(|| Error::UnknownConversation(team_id.to_owned()))
75 }
76}