1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-08 11:42:35 +00:00
comunicapiv3/src/controllers/likes_controller.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

2020-07-10 08:24:39 +00:00
//! # Likes controller
//!
//! @author Pierre Hubert
use crate::controllers::routes::RequestResult;
use crate::data::error::ExecError;
2020-07-10 08:29:32 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::post::PostAccessLevel;
use crate::helpers::{likes_helper, user_helper};
2020-07-10 08:24:39 +00:00
use crate::helpers::likes_helper::LikeType;
struct LikeTarget(u64, LikeType);
/// Update like status
pub fn update(r: &mut HttpRequestHandler) -> RequestResult {
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)?;
// TODO : delete any notification targeting this user about the post
LikeTarget(post.id, LikeType::POST)
}
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("")
}