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, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 15 | login::LoginInput, |
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 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 115 | #[derive(Debug, Deserialize)] |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 116 | struct AccountDataQuery { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 117 | event_type: String, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 118 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 119 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 120 | #[derive(Debug, Deserialize)] |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 121 | struct AccountDataBody { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 122 | event_type: String, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 123 | content: serde_json::Value, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 124 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 125 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 126 | #[derive(Debug, Serialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | struct SoftLogoutInfo { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | user_id: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | homeserver: String, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 132 | #[derive(Debug, Serialize)] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | struct SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | user_id: Option<String>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | #[serde(skip_serializing_if = "Option::is_none")] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 136 | soft_logout: Option<SoftLogoutInfo>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | async fn auth( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | state: &AppState, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | headers: &HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | ) -> Result<(Arc<UserSession>, String), ApiError> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 143 | let Some(sid) = session_sid(headers) else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | return Err(ApiError::unauthorized("not signed in")); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 145 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | match state.get_or_restore(&sid).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | Ok(user) => Ok((user, sid)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | Err(RestoreError::NotFound) => Err(ApiError::unauthorized_stale( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | "session expired; sign in again", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | state.cookie_secure(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | )), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | Err(RestoreError::SoftLoggedOut { .. }) => Err(ApiError::unauthorized( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | "signed out by the server, sign in again", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | )), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | Err(RestoreError::Stale(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | state.remove_session(&sid, Retain::Nothing).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 157 | Err(ApiError::unauthorized_stale(message, state.cookie_secure())) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | Err(RestoreError::Unavailable(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 160 | Err(ApiError::new(StatusCode::BAD_GATEWAY, message)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 161 | } |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 165 | /// commands answer when they're done, so the browser gets the outcome in the response |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 166 | fn respond(result: Result<Reply, malice_core::error::CoreError>) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 167 | match result { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 168 | Ok(Reply::Done) => no_content(None), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 169 | Ok(Reply::Json(value)) => json_ok(&value, None), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 170 | Ok(Reply::Avatar { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 171 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 172 | content_type, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 173 | }) => ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 174 | [ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 175 | (axum::http::header::CONTENT_TYPE, content_type), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 176 | (axum::http::header::CACHE_CONTROL, "private, max-age=3600"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 177 | (axum::http::header::X_CONTENT_TYPE_OPTIONS, "nosniff"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 178 | ], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 179 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 180 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 181 | .into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 182 | Ok(Reply::Attachment { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 183 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 184 | content_type, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 185 | }) => ( |
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::CONTENT_TYPE, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 189 | content_type.unwrap_or("application/octet-stream"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 190 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 191 | ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 192 | axum::http::header::CONTENT_DISPOSITION, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 193 | if content_type.is_some() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 194 | "inline" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 195 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 196 | "attachment" |
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 | (axum::http::header::CACHE_CONTROL, "private, max-age=3600"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 200 | (axum::http::header::X_CONTENT_TYPE_OPTIONS, "nosniff"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 201 | (axum::http::header::CONTENT_SECURITY_POLICY, "sandbox"), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 202 | ], |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 203 | bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 204 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 205 | .into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 206 | Err(error) => ApiError::from(error).into_response(), |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 210 | async fn get_session(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 211 | let Some(sid) = session_sid(&headers) else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 212 | return json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 213 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 214 | user_id: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 215 | soft_logout: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 216 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 217 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 218 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 219 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 220 | match state.get_or_restore(&sid).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 221 | Ok(user) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 222 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 223 | user_id: Some(user.user_id.clone()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 224 | soft_logout: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 225 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 226 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 227 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 228 | Err(RestoreError::NotFound) => 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: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 232 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 233 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 234 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 235 | Err(RestoreError::SoftLoggedOut { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 236 | user_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 237 | homeserver, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 238 | }) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 239 | &SessionResponse { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 240 | user_id: None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 241 | soft_logout: Some(SoftLogoutInfo { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 242 | user_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 243 | homeserver, |
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 | None, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 247 | ), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 248 | Err(RestoreError::Stale(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 249 | state.remove_session(&sid, Retain::Nothing).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 250 | ApiError::unauthorized_stale(message, state.cookie_secure()).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 251 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 252 | Err(RestoreError::Unavailable(message)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 253 | ApiError::new(StatusCode::BAD_GATEWAY, message).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 254 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 255 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 256 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 257 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 258 | async fn post_login( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 259 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 260 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 261 | body: axum::Json<LoginInput>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 262 | ) -> Response { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 263 | let username = body.0.username.trim().to_owned(); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 264 | let password = body.0.password.clone(); |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 265 | let login = match body.0.into_login(state.allow_http_homeserver()) { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 266 | Ok(login) => login, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 267 | Err(error) => return ApiError::from(error).into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 268 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 269 | let cookie_sid = session_sid(&headers); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 270 | match state |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 271 | .login(cookie_sid.as_deref(), login, &username, password) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 272 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 273 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 274 | Ok(user) => json_ok( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 275 | &serde_json::json!({ "user_id": user.user_id }), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 276 | Some(set_cookie_value(&user.sid, state.cookie_secure())), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 277 | ), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 278 | Err(error) => ApiError::from(error).into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 279 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 280 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 281 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 282 | async fn post_logout(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 283 | if let Some(sid) = session_sid(&headers) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 284 | state.logout(&sid).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 285 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 286 | no_content(Some(crate::csrf::clear_cookie_value(state.cookie_secure()))) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 287 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 288 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 289 | async fn get_rooms(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 290 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 291 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 292 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 293 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 294 | respond(dispatch(&user.handle, &user.mirror, Request::Rooms).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 295 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 296 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 297 | async fn get_members( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 298 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 299 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 300 | Query(query): Query<MembersQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 301 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 302 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 303 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 304 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 305 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 306 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 307 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 308 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 309 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 310 | Request::Members { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 311 | room_id: query.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 312 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 313 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 314 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 318 | async fn rooms_open( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 319 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 320 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 321 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 322 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 323 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 324 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 325 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 326 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 327 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 328 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 329 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 330 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 331 | Request::Open { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 332 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 333 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 334 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 335 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 339 | async fn rooms_close( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 340 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 341 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 342 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 343 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 344 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 345 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 346 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 347 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 348 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 349 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 350 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 351 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 352 | Request::Close { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 353 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 354 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 355 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 356 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 360 | async fn rooms_send( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 361 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 362 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 363 | body: axum::Json<SendBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 364 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 365 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 366 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 367 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 368 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 369 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 370 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 371 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 372 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 373 | Request::Send { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 374 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 375 | body: body.0.body, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 376 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 377 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 378 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 382 | async fn rooms_upload( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 383 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 384 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 385 | mut upload: Multipart, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 386 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 387 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 388 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 389 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 390 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 391 | let mut room_id = None; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 392 | let mut file = None; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 393 | loop { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 394 | let field = match upload.next_field().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 395 | Ok(Some(field)) => field, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 396 | Ok(None) => break, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 397 | Err(_) => return ApiError::bad_request("invalid upload form").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 398 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 399 | match field.name() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 400 | Some("room_id") if room_id.is_none() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 401 | room_id = match field.text().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 402 | Ok(value) => Some(value), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 403 | Err(_) => return ApiError::bad_request("invalid room_id").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 404 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 405 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 406 | Some("file") if file.is_none() => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 407 | let filename = field.file_name().unwrap_or("attachment").to_owned(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 408 | let mimetype = field |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 409 | .content_type() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 410 | .unwrap_or("application/octet-stream") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 411 | .to_owned(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 412 | let bytes = match field.bytes().await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 413 | Ok(bytes) => bytes, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 414 | Err(_) => return ApiError::bad_request("invalid file").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 415 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 416 | if bytes.is_empty() || bytes.len() > 20 * 1024 * 1024 { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 417 | return ApiError::bad_request("file must be between 1 byte and 20 MiB") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 418 | .into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 419 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 420 | file = Some((filename, mimetype, bytes)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 421 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 422 | _ => return ApiError::bad_request("unexpected upload field").into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 423 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 424 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 425 | let Some(room_id) = room_id else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 426 | return ApiError::bad_request("room_id is required").into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 427 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 428 | let Some((filename, mimetype, bytes)) = file else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 429 | return ApiError::bad_request("file is required").into_response(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 430 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 431 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 432 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 433 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 434 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 435 | Request::upload(room_id, filename, mimetype, bytes.to_vec()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 436 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 437 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 438 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 439 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 440 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 441 | async fn rooms_paginate( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 442 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 443 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 444 | body: axum::Json<PaginateBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 445 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 446 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 447 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 448 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 449 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 450 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 451 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 452 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 453 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 454 | Request::Paginate { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 455 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 456 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 457 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 458 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 462 | async fn rooms_join( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 463 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 464 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 465 | body: axum::Json<JoinBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 466 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 467 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 468 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 469 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 470 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 471 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 472 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 473 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 474 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 475 | Request::Join { room: body.0.room }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 476 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 477 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 478 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 479 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 480 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 481 | async fn rooms_create( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 482 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 483 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 484 | body: axum::Json<CreateBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 485 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 486 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 487 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 488 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 489 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 490 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 491 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 492 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 493 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 494 | Request::Create { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 495 | name: body.0.name, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 496 | topic: body.0.topic, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 497 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 498 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 499 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 503 | async fn rooms_leave( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 504 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 505 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 506 | body: axum::Json<RoomIdBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 507 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 508 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 509 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 510 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 511 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 512 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 513 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 514 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 515 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 516 | Request::Leave { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 517 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 518 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 519 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 520 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 524 | async fn rooms_react( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 525 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 526 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 527 | body: axum::Json<ReactBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 528 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 529 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 530 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 531 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 532 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 533 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 534 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 535 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 536 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 537 | Request::React { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 538 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 539 | event_id: body.0.event_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 540 | key: body.0.key, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 541 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 542 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 543 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 547 | async fn rooms_read( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 548 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 549 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 550 | body: axum::Json<ReadBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 551 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 552 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 553 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 554 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 555 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 556 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 557 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 558 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 559 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 560 | Request::Read { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 561 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 562 | event_id: body.0.event_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 563 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 564 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 565 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 569 | async fn get_profile(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 570 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 571 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 572 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 573 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 574 | respond(dispatch(&user.handle, &user.mirror, Request::Profile).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 575 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 576 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 577 | async fn get_space_children( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 578 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 579 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 580 | Query(query): Query<MembersQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 581 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 582 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 583 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 584 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 585 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 586 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 587 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 588 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 589 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 590 | Request::SpaceChildren { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 591 | room_id: query.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 592 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 593 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 594 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 598 | async fn get_media( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 599 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 600 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 601 | Query(query): Query<MediaQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 602 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 603 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 604 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 605 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 606 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 607 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 608 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 609 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 610 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 611 | Request::Avatar { mxc: query.mxc }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 612 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 613 | .await, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 614 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 615 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 616 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 617 | async fn get_message_media( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 618 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 619 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 620 | Query(query): Query<MessageMediaQuery>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 621 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 622 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 623 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 624 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 625 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 626 | // the declared mimetype is the sender's claim, so the bytes get sniffed instead |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 627 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 628 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 629 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 630 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 631 | Request::Media { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 632 | media_id: query.media_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 633 | size: query.size, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 634 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 635 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 636 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 640 | async fn get_encryption(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 641 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 642 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 643 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 644 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 645 | respond(dispatch(&user.handle, &user.mirror, Request::Encryption).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 646 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 647 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 648 | async fn recover_encryption( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 649 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 650 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 651 | body: axum::Json<RecoverBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 652 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 653 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 654 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 655 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 656 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 657 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 658 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 659 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 660 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 661 | Request::Recover { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 662 | recovery_key: body.0.recovery_key, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 663 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 664 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 665 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 669 | async fn start_verification(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 670 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 671 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 672 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 673 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 674 | respond(dispatch(&user.handle, &user.mirror, Request::StartVerification).await) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 675 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 676 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 677 | async fn verify_encryption( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 678 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 679 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 680 | body: axum::Json<VerifyBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 681 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 682 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 683 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 684 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 685 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 686 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 687 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 688 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 689 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 690 | Request::Verify { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 691 | action: body.0.action, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 692 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 693 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 694 | .await, |
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 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 698 | async fn rooms_typing( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 699 | State(state): State<AppState>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 700 | headers: HeaderMap, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 701 | body: axum::Json<TypingBody>, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 702 | ) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 703 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 704 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 705 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 706 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 707 | respond( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 708 | dispatch( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 709 | &user.handle, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 710 | &user.mirror, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 711 | Request::Typing { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 712 | room_id: body.0.room_id, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 713 | typing: body.0.typing, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 714 | }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 715 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 716 | .await, |
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 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 720 | async fn get_account_data( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 721 | State(state): State<AppState>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 722 | headers: HeaderMap, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 723 | Query(query): Query<AccountDataQuery>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 724 | ) -> Response { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 725 | let (user, _) = match auth(&state, &headers).await { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 726 | Ok(pair) => pair, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 727 | Err(error) => return error.into_response(), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 728 | }; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 729 | respond( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 730 | dispatch( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 731 | &user.handle, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 732 | &user.mirror, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 733 | Request::GetAccountData { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 734 | event_type: query.event_type, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 735 | }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 736 | ) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 737 | .await, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 738 | ) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 739 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 740 | |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 741 | async fn set_account_data( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 742 | State(state): State<AppState>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 743 | headers: HeaderMap, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 744 | body: axum::Json<AccountDataBody>, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 745 | ) -> Response { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 746 | let (user, _) = match auth(&state, &headers).await { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 747 | Ok(pair) => pair, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 748 | Err(error) => return error.into_response(), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 749 | }; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 750 | let body = body.0; |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 751 | respond( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 752 | dispatch( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 753 | &user.handle, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 754 | &user.mirror, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 755 | Request::SetAccountData { |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 756 | event_type: body.event_type, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 757 | content: body.content, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 758 | }, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 759 | ) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 760 | .await, |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 761 | ) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 762 | } |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 763 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 764 | async fn get_events(State(state): State<AppState>, headers: HeaderMap) -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 765 | let (user, _) = match auth(&state, &headers).await { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 766 | Ok(pair) => pair, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 767 | Err(error) => return error.into_response(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 768 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 769 | let body = subscribe(&user.mirror).map(|event| { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 770 | let json = serde_json::to_string(&event).unwrap_or_else(|_| "{\"type\":\"error\"}".into()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 771 | Ok::<_, std::convert::Infallible>(SseEvent::default().data(json)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 772 | }); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 773 | Sse::new(body).into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 774 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 775 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 776 | async fn get_health() -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 777 | json_ok(&serde_json::json!({ "status": "ok" }), None) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 778 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 779 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 780 | async fn static_shell() -> Response { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 781 | ( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 782 | StatusCode::SERVICE_UNAVAILABLE, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 783 | 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 | 784 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 785 | .into_response() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 786 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 787 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 788 | pub fn router(state: AppState, web_dir: Option<String>) -> Router { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 789 | let api = Router::new() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 790 | .route("/session", get(get_session)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 791 | .route("/login", post(post_login)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 792 | .route("/logout", post(post_logout)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 793 | .route("/rooms", get(get_rooms)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 794 | .route("/events", get(get_events)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 795 | .route("/health", get(get_health)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 796 | .route("/rooms/open", post(rooms_open)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 797 | .route("/rooms/close", post(rooms_close)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 798 | .route("/rooms/send", post(rooms_send)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 799 | .route("/rooms/paginate", post(rooms_paginate)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 800 | .route( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 801 | "/rooms/upload", |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 802 | post(rooms_upload).layer(DefaultBodyLimit::max(21 * 1024 * 1024)), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 803 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 804 | .route("/rooms/join", post(rooms_join)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 805 | .route("/rooms/create", post(rooms_create)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 806 | .route("/rooms/leave", post(rooms_leave)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 807 | .route("/rooms/react", post(rooms_react)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 808 | .route("/rooms/read", post(rooms_read)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 809 | .route("/rooms/typing", post(rooms_typing)) |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 810 | .route( |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 811 | "/account-data", |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 812 | get(get_account_data).post(set_account_data), |
544a170ead2c | atagen | 2026-10-20 11:47 UTC | 813 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 814 | .route("/profile", get(get_profile)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 815 | .route("/media", get(get_media)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 816 | .route("/message-media", get(get_message_media)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 817 | .route("/spaces/children", get(get_space_children)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 818 | .route("/rooms/members", get(get_members)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 819 | .route("/encryption", get(get_encryption)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 820 | .route("/encryption/recover", post(recover_encryption)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 821 | .route("/encryption/start", post(start_verification)) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 822 | .route("/encryption/verify", post(verify_encryption)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 823 | let api = Router::new() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 824 | .nest("/api", api) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 825 | .layer(middleware::from_fn(csrf_guard)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 826 | let router = api.with_state(state).layer(TraceLayer::new_for_http()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 827 | match web_dir { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 828 | Some(dir) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 829 | let files = |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 830 | ServeDir::new(&dir).not_found_service(ServeFile::new(format!("{dir}/index.html"))); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 831 | router.fallback_service(files) |
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 | } |