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