add: reposts/quotes pages

add: repost notification
This commit is contained in:
trisua 2025-04-23 16:46:13 -04:00
parent 41250ef7ed
commit 276f25a496
17 changed files with 601 additions and 50 deletions

View file

@ -1,4 +1,4 @@
use super::{render_error, PaginatedQuery, SearchedQuery};
use super::{render_error, PaginatedQuery, RepostsQuery, SearchedQuery};
use crate::{assets::initial_context, get_lang, get_user_from_token, sanitize::clean_context, State};
use axum::{
Extension,
@ -327,12 +327,18 @@ pub async fn feed_request(
let (can_read, _) = check_permissions!(community, jar, data, user);
// ...
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
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) => match data.0.fill_posts(p, &ignore_users).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -340,7 +346,7 @@ pub async fn feed_request(
};
let pinned = match data.0.get_pinned_posts_by_community(community.id).await {
Ok(p) => match data.0.fill_posts(p).await {
Ok(p) => match data.0.fill_posts(p, &ignore_users).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -423,12 +429,18 @@ pub async fn questions_request(
let (can_read, _) = check_permissions!(community, jar, data, user);
// ...
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let feed = match data
.0
.get_questions_by_community(community.id, 12, props.page)
.await
{
Ok(p) => match data.0.fill_questions(p).await {
Ok(p) => match data.0.fill_questions(p, &ignore_users).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -570,8 +582,14 @@ pub async fn post_request(
}
// ...
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
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) => match data.0.fill_posts(p, &ignore_users).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -628,9 +646,132 @@ pub async fn post_request(
can_manage_questions,
);
// return
Ok(Html(data.1.render("post/post.html", &context).unwrap()))
}
/// `/post/{id}/reposts`
pub async fn reposts_request(
jar: CookieJar,
Path(id): Path<usize>,
Query(props): Query<RepostsQuery>,
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(p) => p,
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(c) => c,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// check repost
let reposting = data.0.get_post_reposting(&post).await;
// check question
let question = match data.0.get_post_question(&post).await {
Ok(q) => q,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// check permissions
let (can_read, _) = check_permissions!(community, jar, data, user);
if !can_read {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
// ...
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let list = if props.quotes {
match data
.0
.get_quoting_posts_by_quoting(post.id, 12, props.page)
.await
{
Ok(p) => match data.0.fill_posts(p, &ignore_users).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)),
}
} else {
match data.0.get_reposts_by_quoting(post.id, 12, props.page).await {
Ok(p) => match data.0.fill_posts(p, &ignore_users).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,
is_joined,
is_pending,
can_post,
can_manage_posts,
can_manage_community,
can_manage_roles,
can_manage_questions,
) = community_context_bools!(data, user, community);
context.insert("post", &post);
context.insert("reposting", &reposting);
context.insert("question", &question);
context.insert("list", &list);
context.insert("page", &props.page);
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,
is_pending,
can_post,
can_read,
can_manage_posts,
can_manage_community,
can_manage_roles,
can_manage_questions,
);
// return
Ok(Html(
data.1.render("communities/post.html", &context).unwrap(),
data.1
.render(
if props.quotes {
"post/quotes.html"
} else {
"post/reposts.html"
},
&context,
)
.unwrap(),
))
}
@ -761,12 +902,18 @@ pub async fn question_request(
}
// ...
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let feed = match data
.0
.get_posts_by_question(question.id, 12, props.page)
.await
{
Ok(p) => match data.0.fill_posts(p).await {
Ok(p) => match data.0.fill_posts(p, &ignore_users).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},

View file

@ -42,12 +42,18 @@ pub async fn index_request(
}
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let list = match data
.0
.get_posts_from_user_communities(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_posts_with_community(l, user.id).await {
Ok(l) => match data
.0
.fill_posts_with_community(l, user.id, &ignore_users)
.await
{
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &Some(user)).await),
},
@ -71,10 +77,20 @@ pub async fn popular_request(
let data = data.read().await;
let user = get_user_from_token!(jar, data.0);
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let list = match data.0.get_popular_posts(12, req.page, 604_800_000).await {
Ok(l) => match data
.0
.fill_posts_with_community(l, if let Some(ref ua) = user { ua.id } else { 0 })
.fill_posts_with_community(
l,
if let Some(ref ua) = user { ua.id } else { 0 },
&ignore_users,
)
.await
{
Ok(l) => l,
@ -107,12 +123,18 @@ pub async fn following_request(
}
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let list = match data
.0
.get_posts_from_user_following(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_posts_with_community(l, user.id).await {
Ok(l) => match data
.0
.fill_posts_with_community(l, user.id, &ignore_users)
.await
{
Ok(l) => l,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
},
@ -138,10 +160,20 @@ pub async fn all_request(
let data = data.read().await;
let user = get_user_from_token!(jar, data.0);
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let list = match data.0.get_latest_posts(12, req.page).await {
Ok(l) => match data
.0
.fill_posts_with_community(l, if let Some(ref ua) = user { ua.id } else { 0 })
.fill_posts_with_community(
l,
if let Some(ref ua) = user { ua.id } else { 0 },
&ignore_users,
)
.await
{
Ok(l) => l,
@ -172,12 +204,14 @@ pub async fn index_questions_request(
}
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let list = match data
.0
.get_questions_from_user_communities(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_questions(l).await {
Ok(l) => match data.0.fill_questions(l, &ignore_users).await {
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &Some(user)).await),
},
@ -210,12 +244,14 @@ pub async fn popular_questions_request(
}
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let list = match data
.0
.get_popular_global_questions(12, req.page, 604_800_000)
.await
{
Ok(l) => match data.0.fill_questions(l).await {
Ok(l) => match data.0.fill_questions(l, &ignore_users).await {
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &Some(user)).await),
},
@ -250,12 +286,14 @@ pub async fn following_questions_request(
}
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let list = match data
.0
.get_questions_from_user_following(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_questions(l).await {
Ok(l) => match data.0.fill_questions(l, &ignore_users).await {
Ok(l) => l,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
},
@ -283,8 +321,14 @@ pub async fn all_questions_request(
let data = data.read().await;
let user = get_user_from_token!(jar, data.0);
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let list = match data.0.get_latest_global_questions(12, req.page).await {
Ok(l) => match data.0.fill_questions(l).await {
Ok(l) => match data.0.fill_questions(l, &ignore_users).await {
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &user).await),
},
@ -353,24 +397,31 @@ pub async fn requests_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
let questions = match data
.0
.fill_questions({
let mut q = Vec::new();
.fill_questions(
{
let mut q = Vec::new();
for req in &requests {
if req.action_type != ActionType::Answer {
continue;
for req in &requests {
if req.action_type != ActionType::Answer {
continue;
}
q.push(match data.0.get_question_by_id(req.linked_asset).await {
Ok(p) => p,
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &Some(user)).await));
}
});
}
q.push(match data.0.get_question_by_id(req.linked_asset).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
});
}
q
})
q
},
&ignore_users,
)
.await
{
Ok(q) => q,

View file

@ -76,6 +76,7 @@ pub fn routes() -> Router {
get(communities::members_request),
)
.route("/post/{id}", get(communities::post_request))
.route("/post/{id}/reposts", get(communities::reposts_request))
.route("/question/{id}", get(communities::question_request))
}
@ -112,3 +113,11 @@ pub struct SearchedQuery {
#[serde(default)]
pub page: usize,
}
#[derive(Deserialize)]
pub struct RepostsQuery {
#[serde(default)]
pub quotes: bool,
#[serde(default)]
pub page: usize,
}

View file

@ -251,6 +251,12 @@ pub async fn posts_request(
}
// fetch data
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
let posts = match data
.0
.get_posts_by_user(other_user.id, 12, props.page)
@ -258,7 +264,11 @@ pub async fn posts_request(
{
Ok(p) => match data
.0
.fill_posts_with_community(p, if let Some(ref ua) = user { ua.id } else { 0 })
.fill_posts_with_community(
p,
if let Some(ref ua) = user { ua.id } else { 0 },
&ignore_users,
)
.await
{
Ok(p) => p,
@ -270,7 +280,11 @@ pub async fn posts_request(
let pinned = match data.0.get_pinned_posts_by_user(other_user.id).await {
Ok(p) => match data
.0
.fill_posts_with_community(p, if let Some(ref ua) = user { ua.id } else { 0 })
.fill_posts_with_community(
p,
if let Some(ref ua) = user { ua.id } else { 0 },
&ignore_users,
)
.await
{
Ok(p) => p,