yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use std::collections::BTreeSet; |
| 2 | |
| 3 | use bytes::Bytes; |
| 4 | use common::{ |
| 5 | event::{Down, Event, Media}, |
| 6 | frame::{self, DOWN_MAX}, |
| 7 | model::{Id, Presence}, |
| 8 | }; |
| 9 | use tracing::warn; |
| 10 | |
| 11 | use crate::{ |
| 12 | db::{self, Connection}, |
| 13 | error::ServerError, |
| 14 | hub::ConnId, |
| 15 | network::Shared, |
| 16 | }; |
| 17 | |
| 18 | pub enum To { |
| 19 | User(Id), |
| 20 | Users(Vec<Id>), |
| 21 | Community(Id), |
| 22 | CommunityExcept(Id, Id), |
| 23 | Outsiders(Id), |
| 24 | Related(Id), |
| 25 | Builders, |
| 26 | Everyone, |
| 27 | Conn(ConnId), |
| 28 | } |
| 29 | |
| 30 | pub struct Broadcast { |
| 31 | pub to: To, |
| 32 | pub down: Down, |
| 33 | } |
| 34 | |
| 35 | impl Broadcast { |
| 36 | pub fn new(to: To, event: Event) -> Self { |
| 37 | Self { |
| 38 | to, |
| 39 | down: Down::Event(Box::new(event)), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn media(conn: ConnId, media: Media) -> Self { |
| 44 | Self { |
| 45 | to: To::Conn(conn), |
| 46 | down: Down::Media(media), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub(crate) async fn publish(shared: &Shared, db: &Connection, broadcasts: Vec<Broadcast>) { |
| 52 | for broadcast in broadcasts { |
| 53 | let mut ids = match resolve(shared, &broadcast.to, db).await { |
| 54 | Ok(ids) => ids, |
| 55 | Err(error) => { |
| 56 | warn!("resolve: {error}"); |
| 57 | continue; |
| 58 | } |
| 59 | }; |
| 60 | let community = matches!(broadcast.to, To::Community(_) | To::CommunityExcept(..)); |
| 61 | if let Some((community_id, user_id)) = voice_subject(&broadcast.down).filter(|_| community) |
| 62 | && hidden(user_id, community_id, db).await |
| 63 | { |
| 64 | let mut insiders = shared.services.rooms.room_users(community_id); |
| 65 | insiders.push(user_id); |
| 66 | let keep = shared.services.hub.conns_of(&insiders); |
| 67 | ids.retain(|id| keep.contains(id)); |
| 68 | } |
| 69 | let droppable = matches!(&broadcast.down, Down::Event(event) if matches!(**event, Event::Typing { .. })); |
| 70 | let Ok(framed) = frame::encode(&broadcast.down, DOWN_MAX) else { |
| 71 | warn!("event too large"); |
| 72 | continue; |
| 73 | }; |
| 74 | let bytes = Bytes::from(framed); |
| 75 | shared.services.hub.send(&ids, &bytes, droppable); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub(crate) async fn job( |
| 80 | shared: &Shared, |
| 81 | work: impl AsyncFnOnce(&Connection) -> Result<Vec<Broadcast>, ServerError>, |
| 82 | ) -> Result<(), ServerError> { |
| 83 | let db = shared.db.lock().await?; |
| 84 | let out = db::atomic(&db, work(&db)).await?; |
| 85 | publish(shared, &db, out).await; |
| 86 | |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | async fn resolve(shared: &Shared, to: &To, db: &Connection) -> Result<Vec<ConnId>, ServerError> { |
| 91 | let hub = &shared.services.hub; |
| 92 | let ids = match to { |
| 93 | To::Conn(id) => vec![*id], |
| 94 | To::Everyone => hub.all(), |
| 95 | To::User(user_id) => hub.conns_of(std::slice::from_ref(user_id)), |
| 96 | To::Users(user_ids) => hub.conns_of(user_ids), |
| 97 | To::Builders => hub.conns_of(&db::user::builder_ids(db).await?), |
| 98 | To::Community(community_id) => community_conns(shared, *community_id, db).await?, |
| 99 | To::CommunityExcept(community_id, user_id) => { |
| 100 | let mut ids = community_conns(shared, *community_id, db).await?; |
| 101 | let own = hub.conns_of(std::slice::from_ref(user_id)); |
| 102 | ids.retain(|id| !own.contains(id)); |
| 103 | ids |
| 104 | } |
| 105 | To::Related(user_id) => hub.conns_of(&related_user_ids(*user_id, db).await?), |
| 106 | To::Outsiders(community_id) => { |
| 107 | let mut ids = community_conns(shared, *community_id, db).await?; |
| 108 | let inside = hub.conns_of(&shared.services.rooms.room_users(*community_id)); |
| 109 | ids.retain(|id| !inside.contains(id)); |
| 110 | ids |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | Ok(ids) |
| 115 | } |
| 116 | |
| 117 | pub(crate) async fn hidden(user_id: Id, community_id: Id, db: &Connection) -> bool { |
| 118 | matches!(db::user::presence(user_id, db).await, Ok(Presence::Offline)) |
| 119 | && !matches!( |
| 120 | db::community::kind_of(community_id, db).await, |
| 121 | Ok(Some(db::community::Kind::Direct)) |
| 122 | ) |
| 123 | } |
| 124 | |
| 125 | fn voice_subject(down: &Down) -> Option<(Id, Id)> { |
| 126 | let Down::Event(event) = down else { |
| 127 | return None; |
| 128 | }; |
| 129 | match &**event { |
| 130 | Event::VoiceJoined { |
| 131 | community_id, |
| 132 | user_id, |
| 133 | } |
| 134 | | Event::VoiceLeft { |
| 135 | community_id, |
| 136 | user_id, |
| 137 | } |
| 138 | | Event::VoiceMuted { |
| 139 | community_id: Some(community_id), |
| 140 | user_id, |
| 141 | .. |
| 142 | } |
| 143 | | Event::ShareStopped { |
| 144 | community_id, |
| 145 | user_id, |
| 146 | .. |
| 147 | } => Some((*community_id, *user_id)), |
| 148 | Event::ShareStarted { |
| 149 | community_id, |
| 150 | share, |
| 151 | } => Some((*community_id, share.user_id)), |
| 152 | _ => None, |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | async fn community_conns( |
| 157 | shared: &Shared, |
| 158 | community_id: Id, |
| 159 | db: &Connection, |
| 160 | ) -> Result<Vec<ConnId>, ServerError> { |
| 161 | let hub = &shared.services.hub; |
| 162 | let mut ids = hub.conns_of(&db::member::member_ids(community_id, db).await?); |
| 163 | ids.extend(hub.watchers(community_id)); |
| 164 | ids.sort_unstable(); |
| 165 | ids.dedup(); |
| 166 | |
| 167 | Ok(ids) |
| 168 | } |
| 169 | |
| 170 | pub(crate) async fn related_user_ids(user_id: Id, db: &Connection) -> Result<Vec<Id>, ServerError> { |
| 171 | let mut user_ids = BTreeSet::from([user_id]); |
| 172 | for community in db::community::communities_of(user_id, db).await? { |
| 173 | user_ids.extend(db::member::member_ids(community.id, db).await?); |
| 174 | } |
| 175 | for friend in db::friend::state(user_id, db).await?.friends { |
| 176 | user_ids.insert(friend.id); |
| 177 | } |
| 178 | |
| 179 | Ok(user_ids.into_iter().collect()) |
| 180 | } |