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 common::{ |
| 2 | call::{ |
| 3 | GetAvatar, GetSettings, GetUser, ListPresences, ListSessions, RemoveAvatar, RevokeSession, |
| 4 | UpdateAvatar, UpdateHue, UpdatePresence, UpdateStatus, UpdateUser, |
| 5 | }, |
| 6 | event::Event, |
| 7 | model::{AdminAction, Id, Presence, ServerSettings, SessionInfo, User}, |
| 8 | validate::validate_avatar_size, |
| 9 | }; |
| 10 | |
| 11 | use crate::{ |
| 12 | authentication, |
| 13 | call::{Cx, Handle, Me}, |
| 14 | db, |
| 15 | error::ServerError, |
| 16 | files, |
| 17 | publish::{To, related_user_ids}, |
| 18 | request::{effective_presence, managed, presence_changed}, |
| 19 | }; |
| 20 | |
| 21 | pub(super) fn user_updated(user: &User, cx: &mut Cx<'_>) { |
| 22 | cx.emit(To::Related(user.id), Event::UserUpdated(user.clone())); |
| 23 | } |
| 24 | |
| 25 | impl Handle for GetSettings { |
| 26 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<ServerSettings, ServerError> { |
| 27 | db::settings::read(cx.db).await |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Handle for GetUser { |
| 32 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 33 | db::user::read(self.user_id, cx.db).await |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Handle for UpdateUser { |
| 38 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 39 | let me = me.user()?; |
| 40 | let user = db::user::update(me.id, &self.username, cx.db).await?; |
| 41 | user_updated(&user, cx); |
| 42 | |
| 43 | Ok(user) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Handle for UpdateStatus { |
| 48 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 49 | let me = me.user()?; |
| 50 | let user = db::user::update_status(me.id, &self.status, cx.db).await?; |
| 51 | user_updated(&user, cx); |
| 52 | |
| 53 | Ok(user) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Handle for UpdateHue { |
| 58 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 59 | let me = me.user()?; |
| 60 | let user = db::user::update_hue(me.id, self.hue, cx.db).await?; |
| 61 | user_updated(&user, cx); |
| 62 | |
| 63 | Ok(user) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl Handle for UpdatePresence { |
| 68 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 69 | let me = me.user()?; |
| 70 | let was_offline = db::user::presence(me.id, cx.db).await? == Presence::Offline; |
| 71 | db::user::update_presence(me.id, self.presence, cx.db).await?; |
| 72 | cx.out |
| 73 | .extend(presence_changed(&cx.sh.services.hub, me.id, cx.db).await?); |
| 74 | let hiding = self.presence == Presence::Offline; |
| 75 | let rooms = &cx.sh.services.rooms; |
| 76 | let direct = match rooms.room_of(me.id) { |
| 77 | Some(community_id) => matches!( |
| 78 | db::community::kind_of(community_id, cx.db).await?, |
| 79 | Some(db::community::Kind::Direct) |
| 80 | ), |
| 81 | None => false, |
| 82 | }; |
| 83 | if let Some(community_id) = rooms |
| 84 | .room_of(me.id) |
| 85 | .filter(|_| was_offline != hiding && !direct) |
| 86 | && rooms.participants(community_id).contains(&me.id) |
| 87 | { |
| 88 | let shares = rooms |
| 89 | .shares(community_id) |
| 90 | .into_iter() |
| 91 | .filter(|share| share.user_id == me.id); |
| 92 | let mut events = Vec::new(); |
| 93 | if hiding { |
| 94 | events.extend(shares.map(|share| Event::ShareStopped { |
| 95 | community_id, |
| 96 | user_id: me.id, |
| 97 | kind: share.kind, |
| 98 | })); |
| 99 | events.push(Event::VoiceLeft { |
| 100 | community_id, |
| 101 | user_id: me.id, |
| 102 | }); |
| 103 | } else { |
| 104 | events.push(Event::VoiceJoined { |
| 105 | community_id, |
| 106 | user_id: me.id, |
| 107 | }); |
| 108 | if db::user::voice_muted(me.id, cx.db).await? { |
| 109 | events.push(Event::VoiceMuted { |
| 110 | community_id: Some(community_id), |
| 111 | user_id: me.id, |
| 112 | muted: true, |
| 113 | }); |
| 114 | } |
| 115 | events.extend(shares.map(|share| Event::ShareStarted { |
| 116 | community_id, |
| 117 | share, |
| 118 | })); |
| 119 | } |
| 120 | for event in events { |
| 121 | cx.emit(To::Outsiders(community_id), event); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | Ok(()) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl Handle for ListPresences { |
| 130 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<Vec<(Id, Presence)>, ServerError> { |
| 131 | let me = me.user()?; |
| 132 | let hub = &cx.sh.services.hub; |
| 133 | let mut presences = Vec::new(); |
| 134 | for user_id in related_user_ids(me.id, cx.db).await? { |
| 135 | let effective = effective_presence(hub, user_id, cx.db).await?; |
| 136 | if effective != Presence::Offline || user_id == me.id { |
| 137 | presences.push((user_id, effective)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | Ok(presences) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | impl Handle for UpdateAvatar { |
| 146 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 147 | let me = me.user()?; |
| 148 | let maximum = db::settings::read(cx.db).await?.max_avatar_bytes; |
| 149 | validate_avatar_size(&self.image, maximum)?; |
| 150 | files::check_avatar(&self.image)?; |
| 151 | db::avatar::set_user(me.id, Some(&self.image), cx.db).await?; |
| 152 | let user = db::user::read(me.id, cx.db).await?; |
| 153 | user_updated(&user, cx); |
| 154 | |
| 155 | Ok(user) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | impl Handle for RemoveAvatar { |
| 160 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<User, ServerError> { |
| 161 | let me = me.user()?; |
| 162 | db::avatar::set_user(me.id, None, cx.db).await?; |
| 163 | let user = db::user::read(me.id, cx.db).await?; |
| 164 | user_updated(&user, cx); |
| 165 | |
| 166 | Ok(user) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl Handle for GetAvatar { |
| 171 | async fn handle(self, _: Me, cx: &mut Cx<'_>) -> Result<Vec<u8>, ServerError> { |
| 172 | db::avatar::read(self.avatar_id, cx.db).await |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl Handle for ListSessions { |
| 177 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<Vec<SessionInfo>, ServerError> { |
| 178 | let me = me.user()?; |
| 179 | managed(&me, self.user_id, cx.db).await?; |
| 180 | |
| 181 | db::session::list(self.user_id, cx.db).await |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | impl Handle for RevokeSession { |
| 186 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 187 | let me = me.user()?; |
| 188 | managed(&me, self.user_id, cx.db).await?; |
| 189 | authentication::revoke( |
| 190 | &cx.sh.services.hub, |
| 191 | authentication::Revoke::Session { |
| 192 | user_id: self.user_id, |
| 193 | session_id: self.session_id, |
| 194 | }, |
| 195 | cx.db, |
| 196 | ) |
| 197 | .await?; |
| 198 | db::log::record_admin( |
| 199 | Some(me.id), |
| 200 | Some(self.user_id), |
| 201 | None, |
| 202 | &AdminAction::SessionsRevoked, |
| 203 | cx.db, |
| 204 | ) |
| 205 | .await?; |
| 206 | |
| 207 | Ok(()) |
| 208 | } |
| 209 | } |