add: question reactions

This commit is contained in:
trisua 2025-04-13 12:44:08 -04:00
parent 9ae9b6d4ea
commit f452875fbe
7 changed files with 122 additions and 45 deletions

View file

@ -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
)

View file

@ -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);
}

View file

@ -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);
}