add: profiles ui, communities ui, posts ui
This commit is contained in:
parent
00abbc8fa2
commit
eecf357325
36 changed files with 1460 additions and 147 deletions
|
@ -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(
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,15 +1,35 @@
|
|||
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::{ApiReturn, Error, communities::Community};
|
||||
|
||||
use crate::{
|
||||
State, get_user_from_token,
|
||||
routes::api::v1::{
|
||||
CreateCommunity, UpdateCommunityContext, UpdateJournalReadAccess, UpdateJournalTitle,
|
||||
UpdateJournalWriteAccess,
|
||||
CreateCommunity, UpdateCommunityContext, UpdateCommunityReadAccess, UpdateCommunityTitle,
|
||||
UpdateCommunityWriteAccess,
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn redirect_from_id(
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
match (&(data.read().await).0)
|
||||
.get_community_by_id(match id.parse::<usize>() {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Redirect::to("/"),
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(c) => Redirect::to(&format!("/community/{}", c.title)),
|
||||
Err(_) => Redirect::to("/"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
|
@ -25,10 +45,10 @@ pub async fn create_request(
|
|||
.create_community(Community::new(req.title, user.id))
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
Ok(id) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Community created".to_string(),
|
||||
payload: (),
|
||||
payload: Some(id.to_string()),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
|
@ -59,7 +79,7 @@ pub async fn update_title_request(
|
|||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateJournalTitle>,
|
||||
Json(req): Json<UpdateCommunityTitle>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
@ -103,7 +123,7 @@ pub async fn update_read_access_request(
|
|||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateJournalReadAccess>,
|
||||
Json(req): Json<UpdateCommunityReadAccess>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
@ -128,7 +148,7 @@ pub async fn update_write_access_request(
|
|||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateJournalWriteAccess>,
|
||||
Json(req): Json<UpdateCommunityWriteAccess>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
|
214
crates/app/src/routes/api/v1/communities/images.rs
Normal file
214
crates/app/src/routes/api/v1/communities/images.rs
Normal file
|
@ -0,0 +1,214 @@
|
|||
use axum::{Extension, Json, body::Body, extract::Path, response::IntoResponse};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use pathbufd::{PathBufD, pathd};
|
||||
use std::fs::exists;
|
||||
use tetratto_core::model::{ApiReturn, Error, permissions::FinePermission};
|
||||
|
||||
use crate::{
|
||||
State,
|
||||
avif::{Image, save_avif_buffer},
|
||||
get_user_from_token,
|
||||
routes::api::v1::auth::images::{MAXIUMUM_FILE_SIZE, read_image},
|
||||
};
|
||||
|
||||
/// Get a community's avatar image
|
||||
/// `/api/v1/communities/{id}/avatar`
|
||||
pub async fn avatar_request(
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
|
||||
let community = match data.get_community_by_id(id).await {
|
||||
Ok(ua) => ua,
|
||||
Err(_) => {
|
||||
return (
|
||||
[("Content-Type", "image/svg+xml")],
|
||||
Body::from(read_image(PathBufD::current().extend(&[
|
||||
data.0.dirs.media.as_str(),
|
||||
"images",
|
||||
"default-avatar.svg",
|
||||
]))),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let path = PathBufD::current().extend(&[
|
||||
"community_avatars",
|
||||
&data.0.dirs.media,
|
||||
&format!("{}.avif", &community.id),
|
||||
]);
|
||||
|
||||
if !exists(&path).unwrap() {
|
||||
return (
|
||||
[("Content-Type", "image/svg+xml")],
|
||||
Body::from(read_image(PathBufD::current().extend(&[
|
||||
data.0.dirs.media.as_str(),
|
||||
"images",
|
||||
"default-avatar.svg",
|
||||
]))),
|
||||
);
|
||||
}
|
||||
|
||||
(
|
||||
[("Content-Type", "image/avif")],
|
||||
Body::from(read_image(path)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Get a profile's banner image
|
||||
/// `/api/v1/auth/profile/{id}/banner`
|
||||
pub async fn banner_request(
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
|
||||
let community = match data.get_community_by_id(id).await {
|
||||
Ok(ua) => ua,
|
||||
Err(_) => {
|
||||
return (
|
||||
[("Content-Type", "image/svg+xml")],
|
||||
Body::from(read_image(PathBufD::current().extend(&[
|
||||
data.0.dirs.media.as_str(),
|
||||
"images",
|
||||
"default-banner.svg",
|
||||
]))),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let path = PathBufD::current().extend(&[
|
||||
"community_banners",
|
||||
&data.0.dirs.media,
|
||||
&format!("{}.avif", &community.id),
|
||||
]);
|
||||
|
||||
if !exists(&path).unwrap() {
|
||||
return (
|
||||
[("Content-Type", "image/svg+xml")],
|
||||
Body::from(read_image(PathBufD::current().extend(&[
|
||||
data.0.dirs.media.as_str(),
|
||||
"images",
|
||||
"default-banner.svg",
|
||||
]))),
|
||||
);
|
||||
}
|
||||
|
||||
(
|
||||
[("Content-Type", "image/avif")],
|
||||
Body::from(read_image(path)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Upload avatar
|
||||
pub async fn upload_avatar_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
img: Image,
|
||||
) -> impl IntoResponse {
|
||||
// get user from token
|
||||
let data = &(data.read().await).0;
|
||||
let auth_user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let community = match data.get_community_by_id(id).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if auth_user.id != community.owner {
|
||||
if !auth_user
|
||||
.permissions
|
||||
.check(FinePermission::MANAGE_COMMUNITIES)
|
||||
{
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
}
|
||||
|
||||
let path = pathd!(
|
||||
"{}/community_avatars/{}.avif",
|
||||
data.0.dirs.media,
|
||||
&auth_user.id
|
||||
);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
// upload image
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
for byte in img.0 {
|
||||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_avif_buffer(&path, bytes) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Avatar uploaded. It might take a bit to update".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(Error::MiscError(e.to_string()).into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Upload banner
|
||||
pub async fn upload_banner_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
img: Image,
|
||||
) -> impl IntoResponse {
|
||||
// get user from token
|
||||
let data = &(data.read().await).0;
|
||||
let auth_user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let community = match data.get_community_by_id(id).await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if auth_user.id != community.owner {
|
||||
if !auth_user
|
||||
.permissions
|
||||
.check(FinePermission::MANAGE_COMMUNITIES)
|
||||
{
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
}
|
||||
|
||||
let path = pathd!(
|
||||
"{}/community_banners/{}.avif",
|
||||
data.0.dirs.media,
|
||||
&auth_user.id
|
||||
);
|
||||
|
||||
// check file size
|
||||
if img.0.len() > MAXIUMUM_FILE_SIZE {
|
||||
return Json(Error::DataTooLong("image".to_string()).into());
|
||||
}
|
||||
|
||||
// upload image
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
for byte in img.0 {
|
||||
bytes.push(byte);
|
||||
}
|
||||
|
||||
match save_avif_buffer(&path, bytes) {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Banner uploaded. It might take a bit to update".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(Error::MiscError(e.to_string()).into()),
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
pub mod communities;
|
||||
pub mod images;
|
||||
pub mod posts;
|
||||
|
|
|
@ -4,13 +4,13 @@ use tetratto_core::model::{ApiReturn, Error, communities::Post};
|
|||
|
||||
use crate::{
|
||||
State, get_user_from_token,
|
||||
routes::api::v1::{CreateJournalEntry, UpdateJournalEntryContent, UpdateJournalEntryContext},
|
||||
routes::api::v1::{CreatePost, UpdatePostContent, UpdatePostContext},
|
||||
};
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<CreateJournalEntry>,
|
||||
Json(req): Json<CreatePost>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
@ -21,16 +21,26 @@ pub async fn create_request(
|
|||
match data
|
||||
.create_post(Post::new(
|
||||
req.content,
|
||||
req.journal,
|
||||
req.replying_to,
|
||||
match req.community.parse::<usize>() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
||||
},
|
||||
if let Some(rt) = req.replying_to {
|
||||
match rt.parse::<usize>() {
|
||||
Ok(x) => Some(x),
|
||||
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
},
|
||||
user.id,
|
||||
))
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
Ok(id) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Post created".to_string(),
|
||||
payload: (),
|
||||
payload: Some(id.to_string()),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
|
@ -61,7 +71,7 @@ pub async fn update_content_request(
|
|||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateJournalEntryContent>,
|
||||
Json(req): Json<UpdatePostContent>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
@ -83,7 +93,7 @@ pub async fn update_context_request(
|
|||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateJournalEntryContext>,
|
||||
Json(req): Json<UpdatePostContext>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
|
|
|
@ -18,7 +18,11 @@ pub fn routes() -> Router {
|
|||
.route("/reactions", post(reactions::create_request))
|
||||
.route("/reactions/{id}", get(reactions::get_request))
|
||||
.route("/reactions/{id}", delete(reactions::delete_request))
|
||||
// journal journals
|
||||
// communities
|
||||
.route(
|
||||
"/communities/find/{id}",
|
||||
get(communities::communities::redirect_from_id),
|
||||
)
|
||||
.route(
|
||||
"/communities",
|
||||
post(communities::communities::create_request),
|
||||
|
@ -36,13 +40,29 @@ pub fn routes() -> Router {
|
|||
post(communities::communities::update_context_request),
|
||||
)
|
||||
.route(
|
||||
"/journals/{id}/access/read",
|
||||
"/communities/{id}/access/read",
|
||||
post(communities::communities::update_read_access_request),
|
||||
)
|
||||
.route(
|
||||
"/journals/{id}/access/write",
|
||||
"/communities/{id}/access/write",
|
||||
post(communities::communities::update_write_access_request),
|
||||
)
|
||||
.route(
|
||||
"/communities/{id}/upload/avatar",
|
||||
post(communities::images::upload_avatar_request),
|
||||
)
|
||||
.route(
|
||||
"/communities/{id}/upload/banner",
|
||||
post(communities::images::upload_banner_request),
|
||||
)
|
||||
.route(
|
||||
"/communities/{id}/avatar",
|
||||
get(communities::images::avatar_request),
|
||||
)
|
||||
.route(
|
||||
"/communities/{id}/banner",
|
||||
get(communities::images::banner_request),
|
||||
)
|
||||
// posts
|
||||
.route("/posts", post(communities::posts::create_request))
|
||||
.route("/posts/{id}", delete(communities::posts::delete_request))
|
||||
|
@ -96,6 +116,10 @@ pub fn routes() -> Router {
|
|||
"/auth/profile/{id}/verified",
|
||||
post(auth::profile::update_profile_is_verified_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/profile/find/{id}",
|
||||
get(auth::profile::redirect_from_id),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -110,7 +134,7 @@ pub struct CreateCommunity {
|
|||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateJournalTitle {
|
||||
pub struct UpdateCommunityTitle {
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
|
@ -120,30 +144,30 @@ pub struct UpdateCommunityContext {
|
|||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateJournalReadAccess {
|
||||
pub struct UpdateCommunityReadAccess {
|
||||
pub access: CommunityReadAccess,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateJournalWriteAccess {
|
||||
pub struct UpdateCommunityWriteAccess {
|
||||
pub access: CommunityWriteAccess,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateJournalEntry {
|
||||
pub struct CreatePost {
|
||||
pub content: String,
|
||||
pub journal: usize,
|
||||
pub community: String,
|
||||
#[serde(default)]
|
||||
pub replying_to: Option<usize>,
|
||||
pub replying_to: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateJournalEntryContent {
|
||||
pub struct UpdatePostContent {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateJournalEntryContext {
|
||||
pub struct UpdatePostContext {
|
||||
pub context: PostContext,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue