add: question reactions
This commit is contained in:
parent
9ae9b6d4ea
commit
f452875fbe
7 changed files with 122 additions and 45 deletions
|
@ -66,10 +66,11 @@ community %}
|
|||
{% if user.settings.display_name %} {{ user.settings.display_name }} {% else
|
||||
%} {{ user.username }} {% endif %}
|
||||
</div>
|
||||
{%- endmacro %} {% macro likes(id, asset_type, likes=0, dislikes=0) -%}
|
||||
{%- endmacro %} {% macro likes(id, asset_type, likes=0, dislikes=0,
|
||||
secondary=false) -%}
|
||||
<button
|
||||
title="Like"
|
||||
class="camo small"
|
||||
class="{% if secondary %}quaternary{% else %}camo{% endif %} small"
|
||||
hook_element="reaction.like"
|
||||
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', true])"
|
||||
>
|
||||
|
@ -80,7 +81,7 @@ community %}
|
|||
|
||||
<button
|
||||
title="Dislike"
|
||||
class="camo small"
|
||||
class="{% if secondary %}quaternary{% else %}camo{% endif %} small"
|
||||
hook_element="reaction.dislike"
|
||||
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', false])"
|
||||
>
|
||||
|
@ -705,29 +706,50 @@ secondary=false, show_community=true) -%}
|
|||
show_community=show_community) }}
|
||||
|
||||
<div
|
||||
class="card flex flex-wrap gap-2{% if secondary %} secondary{% endif %}"
|
||||
class="small card flex justify-between flex-wrap gap-2{% if secondary %} secondary{% endif %}"
|
||||
>
|
||||
<a
|
||||
href="/question/{{ question[0].id }}"
|
||||
class="button quaternary small"
|
||||
<div
|
||||
class="flex gap-1 reactions_box"
|
||||
hook="check_reactions"
|
||||
hook-arg:id="{{ question[0].id }}"
|
||||
>
|
||||
{{ icon "external-link" }} {% if user %}
|
||||
<span>{{ text "requests:label.answer" }}</span>
|
||||
{% else %}
|
||||
<span>{{ text "general:action.open" }}</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
{{ components::likes(id=question[0].id, asset_type="Question",
|
||||
likes=question[0].likes, dislikes=question[0].dislikes,
|
||||
secondary=false) }}
|
||||
</div>
|
||||
|
||||
{% if user %} {% if can_manage_questions or is_helper or question[1].id
|
||||
== user.id %}
|
||||
<button
|
||||
class="quaternary small red"
|
||||
onclick="trigger('me::remove_question', ['{{ question[0].id }}'])"
|
||||
>
|
||||
{{ icon "trash" }}
|
||||
<span>{{ text "general:action.delete" }}</span>
|
||||
</button>
|
||||
{% endif %} {% endif %}
|
||||
<div class="flex gap-1 buttons_box">
|
||||
<a href="/question/{{ question[0].id }}" class="button small">
|
||||
{{ icon "external-link" }} {% if user %}
|
||||
<span>{{ text "requests:label.answer" }}</span>
|
||||
{% else %}
|
||||
<span>{{ text "general:action.open" }}</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
|
||||
{% if user %} {% if can_manage_questions or is_helper or
|
||||
question[1].id == user.id %}
|
||||
<div class="dropdown">
|
||||
<button
|
||||
class="camo small"
|
||||
onclick="trigger('atto::hooks::dropdown', [event])"
|
||||
exclude="dropdown"
|
||||
>
|
||||
{{ icon "ellipsis" }}
|
||||
</button>
|
||||
|
||||
<div class="inner">
|
||||
<button
|
||||
class="camo small red"
|
||||
onclick="trigger('me::remove_question', ['{{ question[0].id }}'])"
|
||||
>
|
||||
{{ icon "trash" }}
|
||||
<span>{{ text "general:action.delete" }}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %} {% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
|
|
@ -6,5 +6,8 @@ CREATE TABLE IF NOT EXISTS questions (
|
|||
content TEXT NOT NULL,
|
||||
is_global INT NOT NULL,
|
||||
answer_count INT NOT NULL,
|
||||
community BIGINT NOT NULL
|
||||
community BIGINT NOT NULL,
|
||||
-- likes
|
||||
likes INT NOT NULL,
|
||||
dislikes INT NOT NULL
|
||||
)
|
||||
|
|
|
@ -33,6 +33,9 @@ impl DataManager {
|
|||
is_global: get!(x->5(i32)) as i8 == 1,
|
||||
answer_count: get!(x->6(i32)) as usize,
|
||||
community: get!(x->7(i64)) as usize,
|
||||
// likes
|
||||
likes: get!(x->8(i32)) as isize,
|
||||
dislikes: get!(x->9(i32)) as isize,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -283,7 +286,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO questions VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
||||
"INSERT INTO questions VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
|
@ -292,7 +295,9 @@ impl DataManager {
|
|||
&data.content,
|
||||
&{ if data.is_global { 1 } else { 0 } },
|
||||
&0_i32,
|
||||
&(data.community as i64)
|
||||
&(data.community as i64),
|
||||
&0_i32,
|
||||
&0_i32
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -396,4 +401,9 @@ impl DataManager {
|
|||
|
||||
auto_method!(incr_question_answer_count() -> "UPDATE questions SET answer_count = answer_count + 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --incr);
|
||||
auto_method!(decr_question_answer_count() -> "UPDATE questions SET answer_count = answer_count - 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --decr);
|
||||
|
||||
auto_method!(incr_question_likes() -> "UPDATE questions SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --incr);
|
||||
auto_method!(incr_question_dislikes() -> "UPDATE questions SET dislikes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --incr);
|
||||
auto_method!(decr_question_likes() -> "UPDATE questions SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --decr);
|
||||
auto_method!(decr_question_dislikes() -> "UPDATE questions SET dislikes = dislikes - 1 WHERE id = $1" --cache-key-tmpl="atto.question:{}" --decr);
|
||||
}
|
||||
|
|
|
@ -125,14 +125,39 @@ impl DataManager {
|
|||
let post = self.get_post_by_id(data.asset).await.unwrap();
|
||||
|
||||
if post.owner != user.id {
|
||||
self.create_notification(Notification::new(
|
||||
"Your post has received a like!".to_string(),
|
||||
format!(
|
||||
"[@{}](/api/v1/auth/user/find/{}) has liked your [post](/post/{})!",
|
||||
user.username, user.id, data.asset
|
||||
),
|
||||
post.owner,
|
||||
))
|
||||
.await?
|
||||
}
|
||||
}
|
||||
}
|
||||
AssetType::Question => {
|
||||
if let Err(e) = {
|
||||
if data.is_like {
|
||||
self.incr_question_likes(data.asset).await
|
||||
} else {
|
||||
self.incr_question_dislikes(data.asset).await
|
||||
}
|
||||
} {
|
||||
return Err(e);
|
||||
} else if data.is_like {
|
||||
let question = self.get_question_by_id(data.asset).await.unwrap();
|
||||
|
||||
if question.owner != user.id {
|
||||
self
|
||||
.create_notification(Notification::new(
|
||||
"Your post has received a like!".to_string(),
|
||||
"Your question has received a like!".to_string(),
|
||||
format!(
|
||||
"[@{}](/api/v1/auth/user/find/{}) has liked your [post](/post/{})!",
|
||||
"[@{}](/api/v1/auth/user/find/{}) has liked your [question](/question/{})!",
|
||||
user.username, user.id, data.asset
|
||||
),
|
||||
post.owner,
|
||||
question.owner,
|
||||
))
|
||||
.await?
|
||||
}
|
||||
|
@ -174,23 +199,26 @@ impl DataManager {
|
|||
// decr corresponding
|
||||
match reaction.asset_type {
|
||||
AssetType::Community => {
|
||||
{
|
||||
if reaction.is_like {
|
||||
self.decr_community_likes(reaction.asset).await
|
||||
} else {
|
||||
self.decr_community_dislikes(reaction.asset).await
|
||||
}
|
||||
}?
|
||||
}
|
||||
if reaction.is_like {
|
||||
self.decr_community_likes(reaction.asset).await
|
||||
} else {
|
||||
self.decr_community_dislikes(reaction.asset).await
|
||||
}
|
||||
}?,
|
||||
AssetType::Post => {
|
||||
{
|
||||
if reaction.is_like {
|
||||
self.decr_post_likes(reaction.asset).await
|
||||
} else {
|
||||
self.decr_post_dislikes(reaction.asset).await
|
||||
}
|
||||
}?
|
||||
}
|
||||
if reaction.is_like {
|
||||
self.decr_post_likes(reaction.asset).await
|
||||
} else {
|
||||
self.decr_post_dislikes(reaction.asset).await
|
||||
}
|
||||
}?,
|
||||
AssetType::Question => {
|
||||
if reaction.is_like {
|
||||
self.decr_question_likes(reaction.asset).await
|
||||
} else {
|
||||
self.decr_question_dislikes(reaction.asset).await
|
||||
}
|
||||
}?,
|
||||
AssetType::User => {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
|
|
@ -302,6 +302,11 @@ pub struct Question {
|
|||
/// The ID of the community this question is asked to. This should only be > 0
|
||||
/// if `is_global` is set to true.
|
||||
pub community: usize,
|
||||
// likes
|
||||
#[serde(default)]
|
||||
pub likes: isize,
|
||||
#[serde(default)]
|
||||
pub dislikes: isize,
|
||||
}
|
||||
|
||||
impl Question {
|
||||
|
@ -319,6 +324,8 @@ impl Question {
|
|||
is_global,
|
||||
answer_count: 0,
|
||||
community: 0,
|
||||
likes: 0,
|
||||
dislikes: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ pub enum AssetType {
|
|||
Community,
|
||||
#[serde(alias = "post")]
|
||||
Post,
|
||||
#[serde(alias = "question")]
|
||||
Question,
|
||||
#[serde(alias = "user")]
|
||||
User,
|
||||
}
|
||||
|
|
5
sql_changes/questions_likes.sql
Normal file
5
sql_changes/questions_likes.sql
Normal file
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE questions
|
||||
ADD COLUMN likes INT NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE questions
|
||||
ADD COLUMN dislikes INT NOT NULL DEFAULT 0;
|
Loading…
Add table
Add a link
Reference in a new issue