add: post likes page

fix: requests pkey
This commit is contained in:
trisua 2025-05-14 19:54:53 -04:00
parent bbb629336f
commit b63df2cb31
11 changed files with 274 additions and 14 deletions

View file

@ -72,6 +72,7 @@ pub const COMMUNITIES_QUESTIONS: &str = include_str!("./public/html/communities/
pub const POST_POST: &str = include_str!("./public/html/post/post.html"); pub const POST_POST: &str = include_str!("./public/html/post/post.html");
pub const POST_REPOSTS: &str = include_str!("./public/html/post/reposts.html"); pub const POST_REPOSTS: &str = include_str!("./public/html/post/reposts.html");
pub const POST_QUOTES: &str = include_str!("./public/html/post/quotes.html"); pub const POST_QUOTES: &str = include_str!("./public/html/post/quotes.html");
pub const POST_LIKES: &str = include_str!("./public/html/post/likes.html");
pub const TIMELINES_HOME: &str = include_str!("./public/html/timelines/home.html"); pub const TIMELINES_HOME: &str = include_str!("./public/html/timelines/home.html");
pub const TIMELINES_POPULAR: &str = include_str!("./public/html/timelines/popular.html"); pub const TIMELINES_POPULAR: &str = include_str!("./public/html/timelines/popular.html");
@ -270,6 +271,7 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"post/post.html"(crate::assets::POST_POST) -d "post" --config=config); write_template!(html_path->"post/post.html"(crate::assets::POST_POST) -d "post" --config=config);
write_template!(html_path->"post/reposts.html"(crate::assets::POST_REPOSTS) --config=config); write_template!(html_path->"post/reposts.html"(crate::assets::POST_REPOSTS) --config=config);
write_template!(html_path->"post/quotes.html"(crate::assets::POST_QUOTES) --config=config); write_template!(html_path->"post/quotes.html"(crate::assets::POST_QUOTES) --config=config);
write_template!(html_path->"post/likes.html"(crate::assets::POST_LIKES) --config=config);
write_template!(html_path->"timelines/home.html"(crate::assets::TIMELINES_HOME) -d "timelines" --config=config); write_template!(html_path->"timelines/home.html"(crate::assets::TIMELINES_HOME) -d "timelines" --config=config);
write_template!(html_path->"timelines/popular.html"(crate::assets::TIMELINES_POPULAR) --config=config); write_template!(html_path->"timelines/popular.html"(crate::assets::TIMELINES_POPULAR) --config=config);

View file

@ -90,6 +90,7 @@ version = "1.0.0"
"communities:label.replies" = "Replies" "communities:label.replies" = "Replies"
"communities:label.reposts" = "Reposts" "communities:label.reposts" = "Reposts"
"communities:label.quotes" = "Quotes" "communities:label.quotes" = "Quotes"
"communities:label.likes" = "Likes"
"communities:action.continue_thread" = "Continue thread" "communities:action.continue_thread" = "Continue thread"
"communities:tab.members" = "Members" "communities:tab.members" = "Members"
"communities:label.select_member" = "Select member" "communities:label.select_member" = "Select member"

View file

@ -0,0 +1,74 @@
{% extends "root.html" %} {% block head %}
<title>Post quotes - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav() }}
<main class="flex flex-col gap-2">
{% if post.replying_to %}
<a href="/post/{{ post.replying_to }}" class="button">
{{ icon "arrow-up" }}
<span>{{ text "communities:action.continue_thread" }}</span>
</a>
{% endif %}
<!-- prettier-ignore -->
<div style="display: contents;">
{% if post.context.repost and post.context.repost.reposting %}
{{ components::repost(repost=reposting, post=post, owner=owner, community=community, show_community=true, can_manage_post=can_manage_posts) }}
{% else %}
{{ components::post(post=post, owner=owner, question=question, community=community, show_community=true, can_manage_post=can_manage_posts) }}
{% endif %}
</div>
<div class="pillmenu">
<a href="/post/{{ post.id }}#/replies">
{{ icon "newspaper" }}
<span>{{ text "communities:label.replies" }}</span>
</a>
<a href="/post/{{ post.id }}/reposts">
{{ icon "repeat-2" }}
<span>{{ text "communities:label.reposts" }}</span>
</a>
<a href="/post/{{ post.id }}/reposts?quotes=true">
{{ icon "quote" }}
<span>{{ text "communities:label.quotes" }}</span>
</a>
<a href="/post/{{ post.id }}/likes" class="active">
{{ icon "heart" }}
<span>{{ text "communities:label.likes" }}</span>
</a>
</div>
{% if (user and user.id == post.owner) or can_manage_posts %} {% if user and
user.id == post.owner %}
<div class="pillmenu">
<a href="/post/{{ post.id }}#/edit">
{{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span>
</a>
{% endif %}
<a href="/post/{{ post.id }}#/configure">
{{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span>
</a>
</div>
{% endif %}
<div class="card-nest w-full">
<div class="card small flex items-center gap-2">
{{ icon "heart" }}
<span>{{ text "communities:label.likes" }}</span>
</div>
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for user in list %}
{{ components::user_plate(user=user, secondary=true) }}
{% endfor %}
{{ components::pagination(page=page, items=list|length) }}
</div>
</div>
</main>
{% endblock %}

View file

@ -74,19 +74,26 @@
<span>{{ text "communities:label.quotes" }}</span> <span>{{ text "communities:label.quotes" }}</span>
</a> </a>
{% if user and user.id == post.owner %} <a href="/post/{{ post.id }}/likes">
{{ icon "heart" }}
<span>{{ text "communities:label.likes" }}</span>
</a>
</div>
{% if (user and user.id == post.owner) or can_manage_posts %} {% if user and
user.id == post.owner %}
<div class="pillmenu">
<a href="#/edit" data-tab-button="edit"> <a href="#/edit" data-tab-button="edit">
{{ icon "pen" }} {{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span> <span>{{ text "communities:label.edit_content" }}</span>
</a> </a>
{% endif %} {% if (user and user.id == post.owner) or can_manage_posts {% endif %}
%}
<a href="#/configure" data-tab-button="configure"> <a href="#/configure" data-tab-button="configure">
{{ icon "settings" }} {{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span> <span>{{ text "communities:action.configure" }}</span>
</a> </a>
{% endif %}
</div> </div>
{% endif %}
<div class="flex flex-col gap-2 hidden" data-tab="configure"> <div class="flex flex-col gap-2 hidden" data-tab="configure">
<div class="card-nest w-full"> <div class="card-nest w-full">

View file

@ -34,19 +34,26 @@
<span>{{ text "communities:label.quotes" }}</span> <span>{{ text "communities:label.quotes" }}</span>
</a> </a>
{% if user and user.id == post.owner %} <a href="/post/{{ post.id }}/likes">
{{ icon "heart" }}
<span>{{ text "communities:label.likes" }}</span>
</a>
</div>
{% if (user and user.id == post.owner) or can_manage_posts %} {% if user and
user.id == post.owner %}
<div class="pillmenu">
<a href="/post/{{ post.id }}#/edit"> <a href="/post/{{ post.id }}#/edit">
{{ icon "pen" }} {{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span> <span>{{ text "communities:label.edit_content" }}</span>
</a> </a>
{% endif %} {% if (user and user.id == post.owner) or can_manage_posts {% endif %}
%}
<a href="/post/{{ post.id }}#/configure"> <a href="/post/{{ post.id }}#/configure">
{{ icon "settings" }} {{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span> <span>{{ text "communities:action.configure" }}</span>
</a> </a>
{% endif %}
</div> </div>
{% endif %}
<div class="card-nest w-full"> <div class="card-nest w-full">
<div class="card small flex items-center gap-2"> <div class="card small flex items-center gap-2">

View file

@ -34,19 +34,26 @@
<span>{{ text "communities:label.quotes" }}</span> <span>{{ text "communities:label.quotes" }}</span>
</a> </a>
{% if user and user.id == post.owner %} <a href="/post/{{ post.id }}/likes">
{{ icon "heart" }}
<span>{{ text "communities:label.likes" }}</span>
</a>
</div>
{% if (user and user.id == post.owner) or can_manage_posts %} {% if user and
user.id == post.owner %}
<div class="pillmenu">
<a href="/post/{{ post.id }}#/edit"> <a href="/post/{{ post.id }}#/edit">
{{ icon "pen" }} {{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span> <span>{{ text "communities:label.edit_content" }}</span>
</a> </a>
{% endif %} {% if (user and user.id == post.owner) or can_manage_posts {% endif %}
%}
<a href="/post/{{ post.id }}#/configure"> <a href="/post/{{ post.id }}#/configure">
{{ icon "settings" }} {{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span> <span>{{ text "communities:action.configure" }}</span>
</a> </a>
{% endif %}
</div> </div>
{% endif %}
<div class="card-nest w-full"> <div class="card-nest w-full">
<div class="card small flex items-center gap-2"> <div class="card small flex items-center gap-2">

View file

@ -834,6 +834,120 @@ pub async fn reposts_request(
)) ))
} }
/// `/post/{id}/likes`
pub async fn likes_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)),
};
let ignore_users = if let Some(ref ua) = user {
data.0.get_userblocks_receivers(ua.id).await
} else {
Vec::new()
};
// ...
let owner = match data.0.get_user_by_id(post.owner).await {
Ok(ua) => ua,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
check_user_blocked_or_private!(user, owner, data, jar);
// check repost
let reposting = data.0.get_post_reposting(&post, &ignore_users).await;
// check question
let question = match data.0.get_post_question(&post, &ignore_users).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 = match data.0.get_reactions_by_asset(post.id, 12, props.page).await {
Ok(p) => match data.0.fill_reactions(&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("question", &question);
context.insert("reposting", &reposting);
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("post/likes.html", &context).unwrap()))
}
/// `/community/{title}/members` /// `/community/{title}/members`
pub async fn members_request( pub async fn members_request(
jar: CookieJar, jar: CookieJar,

View file

@ -89,6 +89,7 @@ pub fn routes() -> Router {
) )
.route("/post/{id}", get(communities::post_request)) .route("/post/{id}", get(communities::post_request))
.route("/post/{id}/reposts", get(communities::reposts_request)) .route("/post/{id}/reposts", get(communities::reposts_request))
.route("/post/{id}/likes", get(communities::likes_request))
.route("/question/{id}", get(communities::question_request)) .route("/question/{id}", get(communities::question_request))
// chats // chats
.route("/chats", get(chats::redirect_request)) .route("/chats", get(chats::redirect_request))

View file

@ -3,5 +3,6 @@ CREATE TABLE IF NOT EXISTS requests (
created BIGINT NOT NULL, created BIGINT NOT NULL,
owner BIGINT NOT NULL, owner BIGINT NOT NULL,
action_type TEXT NOT NULL, action_type TEXT NOT NULL,
linked_asset BIGINT NOT NULL linked_asset BIGINT NOT NULL,
PRIMARY KEY (id, owner, linked_asset)
) )

View file

@ -6,7 +6,7 @@ use crate::model::{
permissions::FinePermission, permissions::FinePermission,
reactions::{AssetType, Reaction}, reactions::{AssetType, Reaction},
}; };
use crate::{auto_method, execute, get, query_row, params}; use crate::{auto_method, execute, get, params, query_row, query_rows};
#[cfg(feature = "sqlite")] #[cfg(feature = "sqlite")]
use rusqlite::Row; use rusqlite::Row;
@ -32,6 +32,51 @@ impl DataManager {
auto_method!(get_reaction_by_id()@get_reaction_from_row -> "SELECT * FROM reactions WHERE id = $1" --name="reaction" --returns=Reaction --cache-key-tmpl="atto.reaction:{}"); auto_method!(get_reaction_by_id()@get_reaction_from_row -> "SELECT * FROM reactions WHERE id = $1" --name="reaction" --returns=Reaction --cache-key-tmpl="atto.reaction:{}");
/// Get all owner profiles from a reactions list.
pub async fn fill_reactions(
&self,
reactions: &Vec<Reaction>,
ignore_users: Vec<usize>,
) -> Result<Vec<User>> {
let mut out = Vec::new();
for reaction in reactions {
if ignore_users.contains(&reaction.owner) {
continue;
}
out.push(self.get_user_by_id(reaction.owner.to_owned()).await?);
}
Ok(out)
}
/// Get a reaction by `owner` and `asset`.
pub async fn get_reactions_by_asset(
&self,
asset: usize,
batch: usize,
page: usize,
) -> Result<Vec<Reaction>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM reactions WHERE asset = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(asset as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_reaction_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("reaction".to_string()));
}
Ok(res.unwrap())
}
/// Get a reaction by `owner` and `asset`. /// Get a reaction by `owner` and `asset`.
pub async fn get_reaction_by_owner_asset( pub async fn get_reaction_by_owner_asset(
&self, &self,

View file

@ -0,0 +1 @@
ALTER TABLE requests ADD CONSTRAINT requests_pkey PRIMARY KEY (id, owner, linked_asset);