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,
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,38 @@
|
|||
use super::render_error;
|
||||
use super::{PaginatedQuery, render_error};
|
||||
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
|
||||
use axum::{
|
||||
Extension,
|
||||
extract::{Path, Query},
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::Error;
|
||||
use tera::Context;
|
||||
use tetratto_core::model::{
|
||||
Error,
|
||||
auth::User,
|
||||
communities::{Community, CommunityReadAccess},
|
||||
};
|
||||
|
||||
macro_rules! check_permissions {
|
||||
($community:ident, $jar:ident, $data:ident, $user:ident) => {
|
||||
match $community.read_access {
|
||||
CommunityReadAccess::Private => {
|
||||
if let Some(ref ua) = $user {
|
||||
if ua.id != $community.owner {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &$jar, &$data, &$user).await,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &$jar, &$data, &$user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/// `/communities`
|
||||
pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
|
||||
|
@ -19,14 +46,22 @@ pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) ->
|
|||
}
|
||||
};
|
||||
|
||||
let posts = match data.0.get_memberships_by_owner(user.id).await {
|
||||
let list = match data.0.get_memberships_by_owner(user.id).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
let mut communities: Vec<Community> = Vec::new();
|
||||
for membership in &list {
|
||||
match data.0.get_community_by_id(membership.community).await {
|
||||
Ok(c) => communities.push(c),
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &None).await)),
|
||||
}
|
||||
}
|
||||
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
||||
context.insert("posts", &posts);
|
||||
context.insert("list", &communities);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
|
@ -35,3 +70,146 @@ pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) ->
|
|||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn community_context(
|
||||
context: &mut Context,
|
||||
community: &Community,
|
||||
is_owner: bool,
|
||||
is_joined: bool,
|
||||
) {
|
||||
context.insert("community", &community);
|
||||
context.insert("is_owner", &is_owner);
|
||||
context.insert("is_joined", &is_joined);
|
||||
}
|
||||
|
||||
/// `/community/{title}`
|
||||
pub async fn feed_request(
|
||||
jar: CookieJar,
|
||||
Path(title): Path<String>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = get_user_from_token!(jar, data.0);
|
||||
|
||||
let community = match data.0.get_community_by_title(&title).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// check permissions
|
||||
check_permissions!(community, jar, data, user);
|
||||
|
||||
// ...
|
||||
let feed = match data
|
||||
.0
|
||||
.get_posts_by_community(community.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(p) => match data.0.fill_posts(p).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// init context
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &user).await;
|
||||
|
||||
let is_owner = if let Some(ref ua) = user {
|
||||
ua.id == community.owner
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_joined = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_membership_by_owner_community(ua.id, community.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
context.insert("feed", &feed);
|
||||
community_context(&mut context, &community, is_owner, is_joined);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1
|
||||
.render("communities/feed.html", &mut context)
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
/// `/post/{id}`
|
||||
pub async fn post_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = get_user_from_token!(jar, data.0);
|
||||
|
||||
let post = match data.0.get_post_by_id(id).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
let community = match data.0.get_community_by_id(post.community).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// check permissions
|
||||
check_permissions!(community, jar, data, user);
|
||||
|
||||
// ...
|
||||
let feed = match data.0.get_post_comments(post.id, 12, props.page).await {
|
||||
Ok(p) => match data.0.fill_posts(p).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// init context
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &user).await;
|
||||
|
||||
let is_owner = if let Some(ref ua) = user {
|
||||
ua.id == community.owner
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_joined = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_membership_by_owner_community(ua.id, community.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
context.insert("post", &post);
|
||||
context.insert("replies", &feed);
|
||||
context.insert(
|
||||
"owner",
|
||||
&data
|
||||
.0
|
||||
.get_user_by_id(post.owner)
|
||||
.await
|
||||
.unwrap_or(User::deleted()),
|
||||
);
|
||||
community_context(&mut context, &community, is_owner, is_joined);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1
|
||||
.render("communities/post.html", &mut context)
|
||||
.unwrap(),
|
||||
))
|
||||
}
|
||||
|
|
|
@ -24,6 +24,8 @@ pub fn routes() -> Router {
|
|||
.route("/user/{username}", get(profile::posts_request))
|
||||
// communities
|
||||
.route("/communities", get(communities::list_request))
|
||||
.route("/community/{title}", get(communities::feed_request))
|
||||
.route("/post/{id}", get(communities::post_request))
|
||||
}
|
||||
|
||||
pub async fn render_error(
|
||||
|
|
|
@ -7,10 +7,17 @@ use axum::{
|
|||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tera::Context;
|
||||
use tetratto_core::model::{Error, auth::User};
|
||||
use tetratto_core::model::{Error, auth::User, communities::Community};
|
||||
|
||||
pub fn profile_context(context: &mut Context, profile: &User, is_self: bool, is_following: bool) {
|
||||
pub fn profile_context(
|
||||
context: &mut Context,
|
||||
profile: &User,
|
||||
communities: &Vec<Community>,
|
||||
is_self: bool,
|
||||
is_following: bool,
|
||||
) {
|
||||
context.insert("profile", &profile);
|
||||
context.insert("communities", &communities);
|
||||
context.insert("is_self", &is_self);
|
||||
context.insert("is_following", &is_following);
|
||||
}
|
||||
|
@ -30,15 +37,6 @@ pub async fn posts_request(
|
|||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
let posts = match data
|
||||
.0
|
||||
.get_posts_by_user(other_user.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// check if we're blocked
|
||||
if let Some(ref ua) = user {
|
||||
if data
|
||||
|
@ -53,6 +51,27 @@ pub async fn posts_request(
|
|||
}
|
||||
}
|
||||
|
||||
// fetch data
|
||||
let posts = match data
|
||||
.0
|
||||
.get_posts_by_user(other_user.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(p) => match data.0.fill_posts_with_community(p).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
let communities = match data.0.get_memberships_by_owner(other_user.id).await {
|
||||
Ok(m) => match data.0.fill_communities(m).await {
|
||||
Ok(m) => m,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// init context
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &user).await;
|
||||
|
@ -73,7 +92,13 @@ pub async fn posts_request(
|
|||
};
|
||||
|
||||
context.insert("posts", &posts);
|
||||
profile_context(&mut context, &other_user, is_self, is_following);
|
||||
profile_context(
|
||||
&mut context,
|
||||
&other_user,
|
||||
&communities,
|
||||
is_self,
|
||||
is_following,
|
||||
);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue