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