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

41 lines
1.1 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::http_request_handler::HttpRequestHandler;
use crate::data::error::ExecError;
use crate::helpers::likes_helper::LikeType;
use crate::helpers::{user_helper, likes_helper};
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)
}
_ => {
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("")
}