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::{path::PathBuf, sync::Arc}; |
| 2 | |
| 3 | use common::{ |
| 4 | call::{Call, GetStorageUsage, Login, Logout, Register, Session}, |
| 5 | event::Event, |
| 6 | model::{Attachment, CommunityUsage, Id, Usage, User}, |
| 7 | open::Blob, |
| 8 | }; |
| 9 | use tokio::sync::{mpsc, oneshot}; |
| 10 | |
| 11 | mod datagram; |
| 12 | mod events; |
| 13 | mod fail; |
| 14 | mod fetch; |
| 15 | mod link; |
| 16 | mod media; |
| 17 | |
| 18 | pub use fail::{Fail, LinkError}; |
| 19 | pub(crate) use link::dial; |
| 20 | pub use link::start; |
| 21 | use media::Media; |
| 22 | |
| 23 | pub(crate) fn device_name() -> String { |
| 24 | let host = gethostname::gethostname() |
| 25 | .to_string_lossy() |
| 26 | .trim() |
| 27 | .to_string(); |
| 28 | let host = if host.is_empty() { |
| 29 | "unknown".to_string() |
| 30 | } else { |
| 31 | host |
| 32 | }; |
| 33 | |
| 34 | format!("{host} {}", std::env::consts::OS) |
| 35 | } |
| 36 | |
| 37 | #[derive(Clone)] |
| 38 | struct Shared { |
| 39 | media: Arc<Media>, |
| 40 | fetches: Arc<fetch::Fetches>, |
| 41 | } |
| 42 | |
| 43 | #[derive(Default)] |
| 44 | pub struct Levels { |
| 45 | pub voice: Vec<(Id, Vec<f32>)>, |
| 46 | pub own: Vec<f32>, |
| 47 | pub own_sending: bool, |
| 48 | pub watching: Vec<(Id, Vec<f32>)>, |
| 49 | pub sharing: Option<Vec<f32>>, |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug)] |
| 53 | pub enum NetworkEvent { |
| 54 | Connected, |
| 55 | VoiceRejoined, |
| 56 | VoiceEnded, |
| 57 | Disconnected(LinkError), |
| 58 | SessionLost, |
| 59 | Pushed(Box<Event>), |
| 60 | } |
| 61 | |
| 62 | enum Command { |
| 63 | Call(Box<dyn FnOnce(quinn::Connection) + Send>), |
| 64 | SignIn { |
| 65 | token: Option<String>, |
| 66 | reply: oneshot::Sender<Result<Option<User>, Fail>>, |
| 67 | }, |
| 68 | Upload { |
| 69 | community_id: Id, |
| 70 | path: PathBuf, |
| 71 | maximum: Option<u64>, |
| 72 | reply: oneshot::Sender<Result<Attachment, Fail>>, |
| 73 | }, |
| 74 | Download { |
| 75 | blob: Blob, |
| 76 | reply: oneshot::Sender<Result<Vec<u8>, Fail>>, |
| 77 | }, |
| 78 | Stage { |
| 79 | path: PathBuf, |
| 80 | progress: tokio::sync::watch::Sender<u64>, |
| 81 | reply: oneshot::Sender<Result<common::model::Staged, Fail>>, |
| 82 | }, |
| 83 | BackupDownload { |
| 84 | name: String, |
| 85 | path: PathBuf, |
| 86 | reply: oneshot::Sender<Result<(), Fail>>, |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | #[derive(Clone)] |
| 91 | pub struct NetworkHandle { |
| 92 | command_sender: mpsc::Sender<Command>, |
| 93 | server: tokio::sync::watch::Sender<crate::Server>, |
| 94 | shared: Shared, |
| 95 | } |
| 96 | |
| 97 | impl NetworkHandle { |
| 98 | pub fn server(&self) -> crate::Server { |
| 99 | self.server.borrow().clone() |
| 100 | } |
| 101 | |
| 102 | pub fn set_server(&self, server: crate::Server) -> bool { |
| 103 | self.server.send_if_modified(|current| { |
| 104 | let changed = *current != server; |
| 105 | *current = server; |
| 106 | changed |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | async fn ask<T>( |
| 111 | &self, |
| 112 | build: impl FnOnce(oneshot::Sender<Result<T, Fail>>) -> Command, |
| 113 | ) -> Result<T, Fail> { |
| 114 | let (reply, response) = oneshot::channel(); |
| 115 | |
| 116 | self.command_sender |
| 117 | .send(build(reply)) |
| 118 | .await |
| 119 | .map_err(|_| Fail::Offline)?; |
| 120 | |
| 121 | response.await.map_err(|_| Fail::Offline)? |
| 122 | } |
| 123 | |
| 124 | pub async fn call<C: Call>(&self, call: C) -> Result<C::Reply, Fail> { |
| 125 | call.validate()?; |
| 126 | |
| 127 | self.ask(|reply| { |
| 128 | Command::Call(Box::new(move |connection| { |
| 129 | link::answer(reply, async move { link::call(&connection, call).await }); |
| 130 | })) |
| 131 | }) |
| 132 | .await |
| 133 | } |
| 134 | |
| 135 | async fn switch(&self, token: Option<String>) -> Result<Option<User>, Fail> { |
| 136 | self.ask(|reply| Command::SignIn { token, reply }).await |
| 137 | } |
| 138 | |
| 139 | async fn sign_in(&self, session: Session) -> Result<(User, String), Fail> { |
| 140 | self.switch(Some(session.token.clone())).await?; |
| 141 | |
| 142 | Ok((session.user, session.token)) |
| 143 | } |
| 144 | |
| 145 | pub async fn resume(&self, token: &str) -> Result<(User, String), Fail> { |
| 146 | let user = self |
| 147 | .switch(Some(token.to_string())) |
| 148 | .await? |
| 149 | .ok_or(Fail::Protocol)?; |
| 150 | |
| 151 | Ok((user, token.to_string())) |
| 152 | } |
| 153 | |
| 154 | pub async fn detach(&self) -> Result<(), Fail> { |
| 155 | self.switch(None).await.map(|_| ()) |
| 156 | } |
| 157 | |
| 158 | pub async fn register( |
| 159 | &self, |
| 160 | username: &str, |
| 161 | password: &str, |
| 162 | invite: Option<&str>, |
| 163 | ) -> Result<(User, String), Fail> { |
| 164 | let session = self |
| 165 | .call(Register { |
| 166 | username: username.to_string(), |
| 167 | password: password.to_string(), |
| 168 | invite: invite.map(str::to_string), |
| 169 | }) |
| 170 | .await?; |
| 171 | |
| 172 | self.sign_in(session).await |
| 173 | } |
| 174 | |
| 175 | pub async fn login(&self, username: &str, password: &str) -> Result<(User, String), Fail> { |
| 176 | let session = self |
| 177 | .call(Login { |
| 178 | username: username.to_string(), |
| 179 | password: password.to_string(), |
| 180 | }) |
| 181 | .await?; |
| 182 | |
| 183 | self.sign_in(session).await |
| 184 | } |
| 185 | |
| 186 | pub async fn logout(&self) -> Result<(), Fail> { |
| 187 | let result = self.call(Logout).await; |
| 188 | self.detach().await?; |
| 189 | result |
| 190 | } |
| 191 | |
| 192 | pub async fn storage_usage(&self) -> Result<(Vec<Usage>, Vec<CommunityUsage>), Fail> { |
| 193 | let usage = self.call(GetStorageUsage).await?; |
| 194 | |
| 195 | Ok((usage.users, usage.communities)) |
| 196 | } |
| 197 | |
| 198 | pub async fn upload_attachment( |
| 199 | &self, |
| 200 | community_id: Id, |
| 201 | path: PathBuf, |
| 202 | maximum: Option<u64>, |
| 203 | ) -> Result<Attachment, Fail> { |
| 204 | self.ask(|reply| Command::Upload { |
| 205 | community_id, |
| 206 | path, |
| 207 | maximum, |
| 208 | reply, |
| 209 | }) |
| 210 | .await |
| 211 | } |
| 212 | |
| 213 | pub async fn download_attachment( |
| 214 | &self, |
| 215 | attachment_id: Id, |
| 216 | thumbnail: bool, |
| 217 | ) -> Result<Vec<u8>, Fail> { |
| 218 | let blob = if thumbnail { |
| 219 | Blob::Thumbnail(attachment_id) |
| 220 | } else { |
| 221 | Blob::Attachment(attachment_id) |
| 222 | }; |
| 223 | self.ask(|reply| Command::Download { blob, reply }).await |
| 224 | } |
| 225 | |
| 226 | pub async fn stage_release( |
| 227 | &self, |
| 228 | path: PathBuf, |
| 229 | progress: tokio::sync::watch::Sender<u64>, |
| 230 | ) -> Result<common::model::Staged, Fail> { |
| 231 | self.ask(|reply| Command::Stage { |
| 232 | path, |
| 233 | progress, |
| 234 | reply, |
| 235 | }) |
| 236 | .await |
| 237 | } |
| 238 | |
| 239 | pub async fn download_backup(&self, name: &str, path: PathBuf) -> Result<(), Fail> { |
| 240 | self.ask(|reply| Command::BackupDownload { |
| 241 | name: name.to_string(), |
| 242 | path, |
| 243 | reply, |
| 244 | }) |
| 245 | .await |
| 246 | } |
| 247 | } |