1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-07 11:22:36 +00:00
comunicapiv3/src/controllers/likes_controller.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

2020-07-10 08:24:39 +00:00
//! # Likes controller
//!
//! @author Pierre Hubert
2021-02-13 15:15:25 +00:00
use crate::routes::RequestResult;
2021-02-05 08:11:30 +00:00
use crate::data::base_request_handler::BaseRequestHandler;
2020-07-10 08:24:39 +00:00
use crate::data::error::ExecError;
2020-07-10 08:37:02 +00:00
use crate::data::group::GroupAccessLevel;
2020-07-10 08:29:32 +00:00
use crate::data::post::PostAccessLevel;
2021-01-24 17:09:45 +00:00
use crate::helpers::{likes_helper, notifications_helper, user_helper};
2020-07-10 08:24:39 +00:00
use crate::helpers::likes_helper::LikeType;
struct LikeTarget(u64, LikeType);
/// Update like status
2021-02-05 12:45:40 +00:00
pub fn update<H: BaseRequestHandler>(r: &mut H) -> RequestResult {
2020-07-10 08:24:39 +00: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 08:29:32 +00:00
// In case of post
"post" => {
let post = r.post_post_with_access("id", PostAccessLevel::BASIC_ACCESS)?;
2021-01-24 17:09:45 +00:00
// Delete any notification targeting this user about the post
notifications_helper::delete_all_post_notifications_targeting_user(r.user_id_ref()?, post.id)?;
2020-07-10 08:29:32 +00:00
LikeTarget(post.id, LikeType::POST)
}
2020-07-10 08:35:24 +00:00
// In case of comment
"comment" => {
let comment = r.post_comment_with_access("id")?;
2021-01-24 17:09:45 +00:00
// Delete any notification targeting this user about the post
notifications_helper::delete_all_post_notifications_targeting_user(r.user_id_ref()?, comment.post_id)?;
2020-07-10 08:35:24 +00:00
LikeTarget(comment.id, LikeType::COMMENT)
}
2020-07-10 08:37:02 +00: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 08:24:39 +00: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("")
}