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
|
{% if user.settings.display_name %} {{ user.settings.display_name }} {% else
|
||||||
%} {{ user.username }} {% endif %}
|
%} {{ user.username }} {% endif %}
|
||||||
</div>
|
</div>
|
||||||
{%- endmacro %} {% macro likes(id, asset_type, likes=0, dislikes=0) -%}
|
{%- endmacro %} {% macro likes(id, asset_type, likes=0, dislikes=0,
|
||||||
|
secondary=false) -%}
|
||||||
<button
|
<button
|
||||||
title="Like"
|
title="Like"
|
||||||
class="camo small"
|
class="{% if secondary %}quaternary{% else %}camo{% endif %} small"
|
||||||
hook_element="reaction.like"
|
hook_element="reaction.like"
|
||||||
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', true])"
|
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', true])"
|
||||||
>
|
>
|
||||||
|
@ -80,7 +81,7 @@ community %}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
title="Dislike"
|
title="Dislike"
|
||||||
class="camo small"
|
class="{% if secondary %}quaternary{% else %}camo{% endif %} small"
|
||||||
hook_element="reaction.dislike"
|
hook_element="reaction.dislike"
|
||||||
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', false])"
|
onclick="trigger('me::react', [event.target, '{{ id }}', '{{ asset_type }}', false])"
|
||||||
>
|
>
|
||||||
|
@ -705,29 +706,50 @@ secondary=false, show_community=true) -%}
|
||||||
show_community=show_community) }}
|
show_community=show_community) }}
|
||||||
|
|
||||||
<div
|
<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
|
<div
|
||||||
href="/question/{{ question[0].id }}"
|
class="flex gap-1 reactions_box"
|
||||||
class="button quaternary small"
|
hook="check_reactions"
|
||||||
|
hook-arg:id="{{ question[0].id }}"
|
||||||
>
|
>
|
||||||
{{ icon "external-link" }} {% if user %}
|
{{ components::likes(id=question[0].id, asset_type="Question",
|
||||||
<span>{{ text "requests:label.answer" }}</span>
|
likes=question[0].likes, dislikes=question[0].dislikes,
|
||||||
{% else %}
|
secondary=false) }}
|
||||||
<span>{{ text "general:action.open" }}</span>
|
</div>
|
||||||
{% endif %}
|
|
||||||
</a>
|
|
||||||
|
|
||||||
{% if user %} {% if can_manage_questions or is_helper or question[1].id
|
<div class="flex gap-1 buttons_box">
|
||||||
== user.id %}
|
<a href="/question/{{ question[0].id }}" class="button small">
|
||||||
<button
|
{{ icon "external-link" }} {% if user %}
|
||||||
class="quaternary small red"
|
<span>{{ text "requests:label.answer" }}</span>
|
||||||
onclick="trigger('me::remove_question', ['{{ question[0].id }}'])"
|
{% else %}
|
||||||
>
|
<span>{{ text "general:action.open" }}</span>
|
||||||
{{ icon "trash" }}
|
{% endif %}
|
||||||
<span>{{ text "general:action.delete" }}</span>
|
</a>
|
||||||
</button>
|
|
||||||
{% endif %} {% endif %}
|
{% 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>
|
||||||
</div>
|
</div>
|
||||||
{%- endmacro %}
|
{%- endmacro %}
|
||||||
|
|
|
@ -6,5 +6,8 @@ CREATE TABLE IF NOT EXISTS questions (
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
is_global INT NOT NULL,
|
is_global INT NOT NULL,
|
||||||
answer_count 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,
|
is_global: get!(x->5(i32)) as i8 == 1,
|
||||||
answer_count: get!(x->6(i32)) as usize,
|
answer_count: get!(x->6(i32)) as usize,
|
||||||
community: get!(x->7(i64)) 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!(
|
let res = execute!(
|
||||||
&conn,
|
&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![
|
params![
|
||||||
&(data.id as i64),
|
&(data.id as i64),
|
||||||
&(data.created as i64),
|
&(data.created as i64),
|
||||||
|
@ -292,7 +295,9 @@ impl DataManager {
|
||||||
&data.content,
|
&data.content,
|
||||||
&{ if data.is_global { 1 } else { 0 } },
|
&{ if data.is_global { 1 } else { 0 } },
|
||||||
&0_i32,
|
&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!(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!(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();
|
let post = self.get_post_by_id(data.asset).await.unwrap();
|
||||||
|
|
||||||
if post.owner != user.id {
|
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
|
self
|
||||||
.create_notification(Notification::new(
|
.create_notification(Notification::new(
|
||||||
"Your post has received a like!".to_string(),
|
"Your question has received a like!".to_string(),
|
||||||
format!(
|
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
|
user.username, user.id, data.asset
|
||||||
),
|
),
|
||||||
post.owner,
|
question.owner,
|
||||||
))
|
))
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
@ -174,23 +199,26 @@ impl DataManager {
|
||||||
// decr corresponding
|
// decr corresponding
|
||||||
match reaction.asset_type {
|
match reaction.asset_type {
|
||||||
AssetType::Community => {
|
AssetType::Community => {
|
||||||
{
|
if reaction.is_like {
|
||||||
if reaction.is_like {
|
self.decr_community_likes(reaction.asset).await
|
||||||
self.decr_community_likes(reaction.asset).await
|
} else {
|
||||||
} else {
|
self.decr_community_dislikes(reaction.asset).await
|
||||||
self.decr_community_dislikes(reaction.asset).await
|
}
|
||||||
}
|
}?,
|
||||||
}?
|
|
||||||
}
|
|
||||||
AssetType::Post => {
|
AssetType::Post => {
|
||||||
{
|
if reaction.is_like {
|
||||||
if reaction.is_like {
|
self.decr_post_likes(reaction.asset).await
|
||||||
self.decr_post_likes(reaction.asset).await
|
} else {
|
||||||
} else {
|
self.decr_post_dislikes(reaction.asset).await
|
||||||
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 => {
|
AssetType::User => {
|
||||||
return Err(Error::NotAllowed);
|
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
|
/// The ID of the community this question is asked to. This should only be > 0
|
||||||
/// if `is_global` is set to true.
|
/// if `is_global` is set to true.
|
||||||
pub community: usize,
|
pub community: usize,
|
||||||
|
// likes
|
||||||
|
#[serde(default)]
|
||||||
|
pub likes: isize,
|
||||||
|
#[serde(default)]
|
||||||
|
pub dislikes: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Question {
|
impl Question {
|
||||||
|
@ -319,6 +324,8 @@ impl Question {
|
||||||
is_global,
|
is_global,
|
||||||
answer_count: 0,
|
answer_count: 0,
|
||||||
community: 0,
|
community: 0,
|
||||||
|
likes: 0,
|
||||||
|
dislikes: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ pub enum AssetType {
|
||||||
Community,
|
Community,
|
||||||
#[serde(alias = "post")]
|
#[serde(alias = "post")]
|
||||||
Post,
|
Post,
|
||||||
|
#[serde(alias = "question")]
|
||||||
|
Question,
|
||||||
#[serde(alias = "user")]
|
#[serde(alias = "user")]
|
||||||
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