1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-03-14 01:42:37 +00:00
comunicapiv3/src/controllers/likes_controller.rs

76 lines
2.3 KiB
Rust
Raw Normal View History

2020-07-10 10:24:39 +02:00
//! # Likes controller
//!
//! @author Pierre Hubert
2021-02-05 09:11:30 +01:00
use crate::data::base_request_handler::BaseRequestHandler;
2020-07-10 10:24:39 +02:00
use crate::data::error::ExecError;
2020-07-10 10:37:02 +02:00
use crate::data::group::GroupAccessLevel;
2020-07-10 10:29:32 +02:00
use crate::data::post::PostAccessLevel;
2020-07-10 10:24:39 +02:00
use crate::helpers::likes_helper::LikeType;
2023-05-31 19:10:05 +02:00
use crate::helpers::{likes_helper, notifications_helper, user_helper};
2022-03-12 07:47:22 +01:00
use crate::routes::RequestResult;
2020-07-10 10:24:39 +02:00
struct LikeTarget(u64, LikeType);
/// Update like status
2022-03-12 07:47:22 +01:00
pub async fn update<H: BaseRequestHandler>(r: &mut H) -> RequestResult {
2020-07-10 10:24:39 +02:00
let req_type = r.post_string("type")?;
let is_liking = r.post_bool("like")?;
let target = match req_type.as_str() {
// In case of user
"user" => {
let user_id = r.post_user_id("id")?;
if !user_helper::can_see_user_page(&r.user_id_or_invalid(), &user_id)? {
r.forbidden("You cannot access this user information!".to_string())?;
}
LikeTarget(user_id.id(), LikeType::USER)
}
2020-07-10 10:29:32 +02:00
// In case of post
"post" => {
let post = r.post_post_with_access("id", PostAccessLevel::BASIC_ACCESS)?;
2021-01-24 18:09:45 +01:00
// Delete any notification targeting this user about the post
2023-05-31 19:10:05 +02:00
notifications_helper::delete_all_post_notifications_targeting_user(
r.user_id_ref()?,
post.id,
)
.await?;
2020-07-10 10:29:32 +02:00
LikeTarget(post.id, LikeType::POST)
}
2020-07-10 10:35:24 +02:00
// In case of comment
"comment" => {
let comment = r.post_comment_with_access("id")?;
2021-01-24 18:09:45 +01:00
// Delete any notification targeting this user about the post
2023-05-31 19:10:05 +02:00
notifications_helper::delete_all_post_notifications_targeting_user(
r.user_id_ref()?,
comment.post_id,
)
.await?;
2021-01-24 18:09:45 +01:00
2020-07-10 10:35:24 +02:00
LikeTarget(comment.id, LikeType::COMMENT)
}
2020-07-10 10:37:02 +02:00
// In case of group
"group" => {
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::VIEW_ACCESS)?;
LikeTarget(group_id.id(), LikeType::GROUP)
}
2020-07-10 10:24:39 +02:00
_ => {
r.internal_error(ExecError::boxed_new("Unsupported like type!"))?;
unreachable!();
}
};
likes_helper::update(r.user_id_ref()?, is_liking, target.0, target.1)?;
r.success("")
2023-05-31 19:10:05 +02:00
}