| 1 |
|
| 2 |
|
| 3 |
use axum::{ |
| 4 |
extract::{Path, State}, |
| 5 |
response::{IntoResponse, Response}, |
| 6 |
}; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
config::Config, |
| 10 |
db::{self, Username}, |
| 11 |
error::{AppError, Result}, |
| 12 |
}; |
| 13 |
use sqlx::PgPool; |
| 14 |
|
| 15 |
use super::item::set_embed_headers; |
| 16 |
|
| 17 |
#[tracing::instrument(skip_all, name = "embed::tip_button")] |
| 18 |
|
| 19 |
pub(super) async fn tip_button( |
| 20 |
State(db): State<PgPool>, |
| 21 |
State(config): State<Config>, |
| 22 |
Path(username): Path<String>, |
| 23 |
) -> Result<Response> { |
| 24 |
let username = Username::new(&username).map_err(|_| AppError::NotFound)?; |
| 25 |
let user = db::users::get_user_by_username(&db, &username) |
| 26 |
.await? |
| 27 |
.ok_or(AppError::NotFound)?; |
| 28 |
|
| 29 |
if user.is_suspended() || user.is_deactivated() || !user.tips_enabled { |
| 30 |
return Err(AppError::NotFound); |
| 31 |
} |
| 32 |
|
| 33 |
let display_name = user |
| 34 |
.display_name |
| 35 |
.as_deref() |
| 36 |
.unwrap_or(&user.username) |
| 37 |
.to_string(); |
| 38 |
let tip_url = format!("{}/u/{}/tip", config.host_url, user.username); |
| 39 |
|
| 40 |
let mut response = crate::templates::EmbedTipButtonTemplate { |
| 41 |
display_name, |
| 42 |
username: user.username.to_string(), |
| 43 |
tip_url, |
| 44 |
avatar_url: user.avatar_url, |
| 45 |
theme_css: crate::templates::embed_theme_css(), |
| 46 |
geometry_css: crate::templates::EMBED_GEOMETRY_CSS, |
| 47 |
} |
| 48 |
.into_response(); |
| 49 |
set_embed_headers(&mut response); |
| 50 |
Ok(response) |
| 51 |
} |
| 52 |
|