973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | use std::sync::Arc; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | use axum::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | Router, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | extract::{DefaultBodyLimit, Multipart, Query, State}, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 6 | http::{HeaderMap, StatusCode}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | middleware, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | response::{IntoResponse, Response, Sse, sse::Event as SseEvent}, |
dffd63b58af4 | atagen | 2026-09-28 09:10 UTC | 9 | routing::{get, post}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | use futures_util::StreamExt; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | use macltok::Retain; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | use malice_core::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | feed::subscribe, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | login::{LoginInput, sanitise_auth_error}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | request::{Reply, Request, dispatch}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | use serde::{Deserialize, Serialize}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | use tower_http::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | services::{ServeDir, ServeFile}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | trace::TraceLayer, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | use crate::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | csrf::{csrf_guard, session_sid, set_cookie_value}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | error::{ApiError, json_ok, no_content}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | state::{AppState, RestoreError, UserSession}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | struct RoomIdBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | struct SendBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | body: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | struct PaginateBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | struct JoinBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | room: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | struct CreateBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | name: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | topic: Option<String>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | struct ReactBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | event_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | key: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | struct ReadBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | event_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | struct MembersQuery { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | struct RecoverBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | recovery_key: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | struct VerifyBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | action: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | struct MediaQuery { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | mxc: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | struct MessageMediaQuery { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | media_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | size: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | #[derive(Debug, Deserialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | struct TypingBody { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | room_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | #[serde(default)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | typing: bool, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 113 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 114 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 115 | #[derive(Debug, Serialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 116 | struct SoftLogoutInfo { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | user_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | homeserver: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | #[derive(Debug, Serialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | struct SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | user_id: Option<String>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | #[serde(skip_serializing_if = "Option::is_none")] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | soft_logout: Option<SoftLogoutInfo>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 126 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | async fn auth( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | state: &AppState, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | headers: &HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | ) -> Result<(Arc<UserSession>, String), ApiError> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 132 | let Some(sid) = session_sid(headers) else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | return Err(ApiError::unauthorized("not signed in")); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | match state.get_or_restore(&sid).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 136 | Ok(user) => Ok((user, sid)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | Err(RestoreError::NotFound) => Err(ApiError::unauthorized_stale( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | "session expired; sign in again", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | state.cookie_secure(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | )), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | Err(RestoreError::SoftLoggedOut { .. }) => Err(ApiError::unauthorized( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | "signed out by the server, sign in again", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 143 | )), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | Err(RestoreError::Stale(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 145 | state.remove_session(&sid, Retain::Nothing).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | Err(ApiError::unauthorized_stale(message, state.cookie_secure())) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | Err(RestoreError::Unavailable(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | Err(ApiError::new(StatusCode::BAD_GATEWAY, message)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | /// commands answer when they're done, so the browser gets the outcome in the response |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | fn respond(result: Result<Reply, malice_core::error::CoreError>) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | match result { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 157 | Ok(Reply::Done) => no_content(None), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | Ok(Reply::Json(value)) => json_ok(&value, None), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | Ok(Reply::Avatar { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 160 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 161 | content_type, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 162 | }) => ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 163 | [ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 164 | (axum::http::header::CONTENT_TYPE, content_type), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 165 | (axum::http::header::CACHE_CONTROL, "private, max-age=3600"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 166 | (axum::http::header::X_CONTENT_TYPE_OPTIONS, "nosniff"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 167 | ], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 168 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 169 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 170 | .into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 171 | Ok(Reply::Attachment { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 172 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 173 | content_type, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 174 | }) => ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 175 | [ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 176 | ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 177 | axum::http::header::CONTENT_TYPE, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 178 | content_type.unwrap_or("application/octet-stream"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 179 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 180 | ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 181 | axum::http::header::CONTENT_DISPOSITION, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 182 | if content_type.is_some() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 183 | "inline" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 184 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 185 | "attachment" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 186 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 187 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 188 | (axum::http::header::CACHE_CONTROL, "private, max-age=3600"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 189 | (axum::http::header::X_CONTENT_TYPE_OPTIONS, "nosniff"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 190 | (axum::http::header::CONTENT_SECURITY_POLICY, "sandbox"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 191 | ], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 192 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 193 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 194 | .into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 195 | Err(error) => ApiError::from(error).into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 196 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 197 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 198 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 199 | async fn get_session(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 200 | let Some(sid) = session_sid(&headers) else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 201 | return json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 202 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 203 | user_id: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 204 | soft_logout: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 205 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 206 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 207 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 208 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 209 | match state.get_or_restore(&sid).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 210 | Ok(user) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 211 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 212 | user_id: Some(user.user_id.clone()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 213 | soft_logout: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 214 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 215 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 216 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 217 | Err(RestoreError::NotFound) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 218 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 219 | user_id: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 220 | soft_logout: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 221 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 222 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 223 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 224 | Err(RestoreError::SoftLoggedOut { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 225 | user_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 226 | homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 227 | }) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 228 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 229 | user_id: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 230 | soft_logout: Some(SoftLogoutInfo { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 231 | user_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 232 | homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 233 | }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 234 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 235 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 236 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 237 | Err(RestoreError::Stale(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 238 | state.remove_session(&sid, Retain::Nothing).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 239 | ApiError::unauthorized_stale(message, state.cookie_secure()).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 240 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 241 | Err(RestoreError::Unavailable(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 242 | ApiError::new(StatusCode::BAD_GATEWAY, message).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 243 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 244 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 245 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 246 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 247 | async fn post_login( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 248 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 249 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 250 | body: axum::Json<LoginInput>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 251 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 252 | let input = match body.0.validate(state.allow_http_homeserver()) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 253 | Ok(input) => input, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 254 | Err(error) => return ApiError::from(error).into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 255 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 256 | let cookie_sid = session_sid(&headers); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 257 | let password = input.password.clone(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 258 | match state |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 259 | .login( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 260 | cookie_sid.as_deref(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 261 | input.homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 262 | input.username, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 263 | input.password, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 264 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 265 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 266 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 267 | Ok(user) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 268 | &serde_json::json!({ "user_id": user.user_id }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 269 | Some(set_cookie_value(&user.sid, state.cookie_secure())), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 270 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 271 | Err(message) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 272 | let status = if message.contains("starting sync engine") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 273 | || message.contains("could not save the session") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 274 | || message.contains("another engine is using this session") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 275 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 276 | StatusCode::BAD_GATEWAY |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 277 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 278 | StatusCode::UNAUTHORIZED |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 279 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 280 | ApiError::new(status, sanitise_auth_error(&message, &password)).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 281 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 282 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 283 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 284 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 285 | async fn post_logout(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 286 | if let Some(sid) = session_sid(&headers) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 287 | state.logout(&sid).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 288 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 289 | no_content(Some(crate::csrf::clear_cookie_value(state.cookie_secure()))) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 290 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 291 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 292 | async fn get_rooms(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 293 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 294 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 295 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 296 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 297 | respond(dispatch(&user.handle, &user.mirror, Request::Rooms).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 298 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 299 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 300 | async fn get_members( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 301 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 302 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 303 | Query(query): Query<MembersQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 304 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 305 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 306 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 307 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 308 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 309 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 310 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 311 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 312 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 313 | Request::Members { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 314 | room_id: query.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 315 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 316 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 317 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 318 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 319 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 320 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 321 | async fn rooms_open( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 322 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 323 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 324 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 325 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 326 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 327 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 328 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 329 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 330 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 331 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 332 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 333 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 334 | Request::Open { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 335 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 336 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 337 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 338 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 339 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 340 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 341 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 342 | async fn rooms_close( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 343 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 344 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 345 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 346 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 347 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 348 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 349 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 350 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 351 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 352 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 353 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 354 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 355 | Request::Close { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 356 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 357 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 358 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 359 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 360 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 361 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 362 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 363 | async fn rooms_send( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 364 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 365 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 366 | body: axum::Json<SendBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 367 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 368 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 369 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 370 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 371 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 372 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 373 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 374 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 375 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 376 | Request::Send { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 377 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 378 | body: body.0.body, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 379 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 380 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 381 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 382 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 383 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 384 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 385 | async fn rooms_upload( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 386 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 387 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 388 | mut upload: Multipart, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 389 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 390 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 391 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 392 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 393 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 394 | let mut room_id = None; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 395 | let mut file = None; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 396 | loop { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 397 | let field = match upload.next_field().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 398 | Ok(Some(field)) => field, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 399 | Ok(None) => break, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 400 | Err(_) => return ApiError::bad_request("invalid upload form").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 401 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 402 | match field.name() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 403 | Some("room_id") if room_id.is_none() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 404 | room_id = match field.text().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 405 | Ok(value) => Some(value), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 406 | Err(_) => return ApiError::bad_request("invalid room_id").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 407 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 408 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 409 | Some("file") if file.is_none() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 410 | let filename = field.file_name().unwrap_or("attachment").to_owned(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 411 | let mimetype = field |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 412 | .content_type() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 413 | .unwrap_or("application/octet-stream") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 414 | .to_owned(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 415 | let bytes = match field.bytes().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 416 | Ok(bytes) => bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 417 | Err(_) => return ApiError::bad_request("invalid file").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 418 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 419 | if bytes.is_empty() || bytes.len() > 20 * 1024 * 1024 { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 420 | return ApiError::bad_request("file must be between 1 byte and 20 MiB") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 421 | .into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 422 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 423 | file = Some((filename, mimetype, bytes)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 424 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 425 | _ => return ApiError::bad_request("unexpected upload field").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 426 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 427 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 428 | let Some(room_id) = room_id else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 429 | return ApiError::bad_request("room_id is required").into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 430 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 431 | let Some((filename, mimetype, bytes)) = file else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 432 | return ApiError::bad_request("file is required").into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 433 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 434 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 435 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 436 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 437 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 438 | Request::upload(room_id, filename, mimetype, bytes.to_vec()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 439 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 440 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 441 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 442 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 443 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 444 | async fn rooms_paginate( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 445 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 446 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 447 | body: axum::Json<PaginateBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 448 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 449 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 450 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 451 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 452 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 453 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 454 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 455 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 456 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 457 | Request::Paginate { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 458 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 459 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 460 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 461 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 462 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 463 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 464 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 465 | async fn rooms_join( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 466 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 467 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 468 | body: axum::Json<JoinBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 469 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 470 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 471 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 472 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 473 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 474 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 475 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 476 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 477 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 478 | Request::Join { room: body.0.room }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 479 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 480 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 481 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 482 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 483 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 484 | async fn rooms_create( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 485 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 486 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 487 | body: axum::Json<CreateBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 488 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 489 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 490 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 491 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 492 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 493 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 494 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 495 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 496 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 497 | Request::Create { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 498 | name: body.0.name, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 499 | topic: body.0.topic, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 500 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 501 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 502 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 503 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 504 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 505 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 506 | async fn rooms_leave( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 507 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 508 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 509 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 510 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 511 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 512 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 513 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 514 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 515 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 516 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 517 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 518 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 519 | Request::Leave { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 520 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 521 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 522 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 523 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 524 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 525 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 526 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 527 | async fn rooms_react( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 528 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 529 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 530 | body: axum::Json<ReactBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 531 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 532 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 533 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 534 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 535 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 536 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 537 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 538 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 539 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 540 | Request::React { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 541 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 542 | event_id: body.0.event_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 543 | key: body.0.key, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 544 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 545 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 546 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 547 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 548 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 549 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 550 | async fn rooms_read( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 551 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 552 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 553 | body: axum::Json<ReadBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 554 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 555 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 556 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 557 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 558 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 559 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 560 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 561 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 562 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 563 | Request::Read { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 564 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 565 | event_id: body.0.event_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 566 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 567 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 568 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 569 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 570 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 571 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 572 | async fn get_profile(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 573 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 574 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 575 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 576 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 577 | respond(dispatch(&user.handle, &user.mirror, Request::Profile).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 578 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 579 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 580 | async fn get_space_children( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 581 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 582 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 583 | Query(query): Query<MembersQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 584 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 585 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 586 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 587 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 588 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 589 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 590 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 591 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 592 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 593 | Request::SpaceChildren { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 594 | room_id: query.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 595 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 596 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 597 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 598 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 599 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 600 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 601 | async fn get_media( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 602 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 603 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 604 | Query(query): Query<MediaQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 605 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 606 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 607 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 608 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 609 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 610 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 611 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 612 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 613 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 614 | Request::Avatar { mxc: query.mxc }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 615 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 616 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 617 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 618 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 619 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 620 | async fn get_message_media( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 621 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 622 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 623 | Query(query): Query<MessageMediaQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 624 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 625 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 626 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 627 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 628 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 629 | // the declared mimetype is the sender's claim, so the bytes get sniffed instead |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 630 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 631 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 632 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 633 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 634 | Request::Media { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 635 | media_id: query.media_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 636 | size: query.size, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 637 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 638 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 639 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 640 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 641 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 642 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 643 | async fn get_encryption(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 644 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 645 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 646 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 647 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 648 | respond(dispatch(&user.handle, &user.mirror, Request::Encryption).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 649 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 650 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 651 | async fn recover_encryption( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 652 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 653 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 654 | body: axum::Json<RecoverBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 655 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 656 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 657 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 658 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 659 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 660 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 661 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 662 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 663 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 664 | Request::Recover { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 665 | recovery_key: body.0.recovery_key, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 666 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 667 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 668 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 669 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 670 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 671 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 672 | async fn start_verification(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 673 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 674 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 675 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 676 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 677 | respond(dispatch(&user.handle, &user.mirror, Request::StartVerification).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 678 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 679 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 680 | async fn verify_encryption( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 681 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 682 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 683 | body: axum::Json<VerifyBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 684 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 685 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 686 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 687 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 688 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 689 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 690 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 691 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 692 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 693 | Request::Verify { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 694 | action: body.0.action, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 695 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 696 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 697 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 698 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 699 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 700 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 701 | async fn rooms_typing( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 702 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 703 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 704 | body: axum::Json<TypingBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 705 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 706 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 707 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 708 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 709 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 710 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 711 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 712 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 713 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 714 | Request::Typing { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 715 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 716 | typing: body.0.typing, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 717 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 718 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 719 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 720 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 721 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 722 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 723 | async fn get_events(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 724 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 725 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 726 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 727 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 728 | let body = subscribe(&user.mirror).map(|event| { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 729 | let json = serde_json::to_string(&event).unwrap_or_else(|_| "{\"type\":\"error\"}".into()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 730 | Ok::<_, std::convert::Infallible>(SseEvent::default().data(json)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 731 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 732 | Sse::new(body).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 733 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 734 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 735 | async fn get_health() -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 736 | json_ok(&serde_json::json!({ "status": "ok" }), None) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 737 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 738 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 739 | async fn static_shell() -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 740 | ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 741 | StatusCode::SERVICE_UNAVAILABLE, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 742 | axum::Json(serde_json::json!({ "message": "frontend assets missing; run npm --prefix web run build or use the Nix package" })), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 743 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 744 | .into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 745 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 746 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 747 | pub fn router(state: AppState, web_dir: Option<String>) -> Router { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 748 | let api = Router::new() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 749 | .route("/session", get(get_session)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 750 | .route("/login", post(post_login)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 751 | .route("/logout", post(post_logout)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 752 | .route("/rooms", get(get_rooms)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 753 | .route("/events", get(get_events)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 754 | .route("/health", get(get_health)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 755 | .route("/rooms/open", post(rooms_open)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 756 | .route("/rooms/close", post(rooms_close)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 757 | .route("/rooms/send", post(rooms_send)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 758 | .route("/rooms/paginate", post(rooms_paginate)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 759 | .route( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 760 | "/rooms/upload", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 761 | post(rooms_upload).layer(DefaultBodyLimit::max(21 * 1024 * 1024)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 762 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 763 | .route("/rooms/join", post(rooms_join)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 764 | .route("/rooms/create", post(rooms_create)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 765 | .route("/rooms/leave", post(rooms_leave)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 766 | .route("/rooms/react", post(rooms_react)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 767 | .route("/rooms/read", post(rooms_read)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 768 | .route("/rooms/typing", post(rooms_typing)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 769 | .route("/profile", get(get_profile)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 770 | .route("/media", get(get_media)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 771 | .route("/message-media", get(get_message_media)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 772 | .route("/spaces/children", get(get_space_children)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 773 | .route("/rooms/members", get(get_members)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 774 | .route("/encryption", get(get_encryption)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 775 | .route("/encryption/recover", post(recover_encryption)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 776 | .route("/encryption/start", post(start_verification)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 777 | .route("/encryption/verify", post(verify_encryption)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 778 | let api = Router::new() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 779 | .nest("/api", api) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 780 | .layer(middleware::from_fn(csrf_guard)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 781 | let router = api.with_state(state).layer(TraceLayer::new_for_http()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 782 | match web_dir { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 783 | Some(dir) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 784 | let files = |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 785 | ServeDir::new(&dir).not_found_service(ServeFile::new(format!("{dir}/index.html"))); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 786 | router.fallback_service(files) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 787 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 788 | None => router.fallback(static_shell), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 789 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 790 | } |