add: profiles ui, communities ui, posts ui

This commit is contained in:
trisua 2025-03-29 00:26:56 -04:00
parent 00abbc8fa2
commit eecf357325
36 changed files with 1460 additions and 147 deletions

View file

@ -1,6 +1,12 @@
use axum::{Extension, Json, body::Body, extract::Path, response::IntoResponse};
use axum::{
Extension, Json,
body::Body,
extract::{Path, Query},
response::IntoResponse,
};
use axum_extra::extract::CookieJar;
use pathbufd::{PathBufD, pathd};
use serde::Deserialize;
use std::{
fs::{File, exists},
io::Read,
@ -23,15 +29,36 @@ pub fn read_image(path: PathBufD) -> Vec<u8> {
bytes
}
#[derive(Deserialize, PartialEq, Eq)]
pub enum AvatarSelectorType {
#[serde(alias = "username")]
Username,
#[serde(alias = "id")]
Id,
}
#[derive(Deserialize)]
pub struct AvatarSelectorQuery {
pub selector_type: AvatarSelectorType,
}
/// Get a profile's avatar image
/// `/api/v1/auth/profile/{id}/avatar`
pub async fn avatar_request(
Path(username): Path<String>,
Path(selector): Path<String>,
Extension(data): Extension<State>,
Query(req): Query<AvatarSelectorQuery>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match data.get_user_by_username(&username).await {
let user = match {
if req.selector_type == AvatarSelectorType::Id {
data.get_user_by_id(selector.parse::<usize>().unwrap())
.await
} else {
data.get_user_by_username(&selector).await
}
} {
Ok(ua) => ua,
Err(_) => {
return (
@ -88,7 +115,7 @@ pub async fn banner_request(
};
let path =
PathBufD::current().extend(&["avatars", &data.0.dirs.media, &format!("{}.avif", &user.id)]);
PathBufD::current().extend(&["banners", &data.0.dirs.media, &format!("{}.avif", &user.id)]);
if !exists(&path).unwrap() {
return (
@ -107,7 +134,7 @@ pub async fn banner_request(
)
}
static MAXIUMUM_FILE_SIZE: usize = 8388608;
pub static MAXIUMUM_FILE_SIZE: usize = 8388608;
/// Upload avatar
pub async fn upload_avatar_request(

View file

@ -3,13 +3,33 @@ use crate::{
model::{ApiReturn, Error},
routes::api::v1::UpdateUserIsVerified,
};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum::{
Extension, Json,
extract::Path,
response::{IntoResponse, Redirect},
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{
auth::{Token, UserSettings},
permissions::FinePermission,
};
pub async fn redirect_from_id(
Extension(data): Extension<State>,
Path(id): Path<String>,
) -> impl IntoResponse {
match (&(data.read().await).0)
.get_user_by_id(match id.parse::<usize>() {
Ok(id) => id,
Err(_) => return Redirect::to("/"),
})
.await
{
Ok(u) => Redirect::to(&format!("/user/{}", u.username)),
Err(_) => Redirect::to("/"),
}
}
/// Update the settings of the given user.
pub async fn update_profile_settings_request(
jar: CookieJar,