2020-06-29 13:45:26 +00:00
|
|
|
//! # Friends controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::api_data::friend_api::FriendAPI;
|
2020-06-30 07:50:31 +00:00
|
|
|
use crate::api_data::friendship_status_api::FriendshipStatusAPI;
|
2020-06-29 13:45:26 +00:00
|
|
|
use crate::controllers::routes::RequestResult;
|
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-06-30 05:49:23 +00:00
|
|
|
use crate::helpers::{account_helper, friends_helper, user_helper};
|
2020-06-29 13:45:26 +00:00
|
|
|
|
|
|
|
/// Get the list of friends of the current user
|
|
|
|
pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-30 06:14:58 +00:00
|
|
|
let list = friends_helper::GetFriendsQuery::new(&r.user_id()?).exec()?;
|
2020-06-29 13:45:26 +00:00
|
|
|
|
2020-06-29 13:53:39 +00:00
|
|
|
// Update last activity (if allowed)
|
|
|
|
if !r.post_bool_opt("incognito", false) {
|
|
|
|
account_helper::update_last_activity(&r.user_id()?)?;
|
|
|
|
}
|
2020-06-29 13:45:26 +00:00
|
|
|
|
|
|
|
r.set_response(FriendAPI::from_list(&list))
|
2020-06-30 05:49:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 06:14:58 +00:00
|
|
|
/// Get information about a single friendship
|
|
|
|
pub fn get_single_friendship_info(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_user_id("friendID")?;
|
|
|
|
|
|
|
|
let info = friends_helper::GetFriendsQuery::new(&r.user_id()?)
|
|
|
|
.get_single_friend(&friend_id);
|
|
|
|
|
|
|
|
let info = r.ok_or_not_found(
|
|
|
|
info,
|
|
|
|
"The friendship was not found!",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
r.set_response(FriendAPI::new(&info))
|
|
|
|
}
|
|
|
|
|
2020-06-30 05:49:23 +00:00
|
|
|
/// Get the list of friends of another user
|
|
|
|
pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let user_id = r.post_user_id("userID")?;
|
|
|
|
|
|
|
|
if !user_helper::can_see_user_page(&r.user_id_or_invalid(), &user_id)? {
|
|
|
|
r.forbidden("You can not access this user information!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user_helper::is_user_friends_list_public(&user_id)? {
|
|
|
|
r.forbidden("The friend list of this user is not public!".to_string())?;
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:14:58 +00:00
|
|
|
let friends = friends_helper::GetFriendsQuery::new(&user_id).exec()?;
|
2020-06-30 05:49:23 +00:00
|
|
|
|
|
|
|
r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::<Vec<u64>>())
|
2020-06-30 07:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the status of a friendship
|
|
|
|
pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_user_id("friendID")?;
|
|
|
|
let curr_user_id = r.user_id()?;
|
|
|
|
|
|
|
|
let status = friends_helper::get_status(&curr_user_id, &friend_id)?;
|
|
|
|
|
|
|
|
r.set_response(FriendshipStatusAPI::new(&status))
|
2020-06-30 08:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a new friendship request
|
|
|
|
pub fn send_request(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_user_id("friendID")?;
|
|
|
|
|
|
|
|
if friend_id == r.user_id()? {
|
|
|
|
r.forbidden("You can not sent friendship request to yourself!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = friends_helper::get_status(&r.user_id()?, &friend_id)?;
|
|
|
|
|
|
|
|
if status.are_friend {
|
|
|
|
r.forbidden("You are already a friend of this person!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.sent_request || status.received_request {
|
|
|
|
r.forbidden("A friendship request is already in progress!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
friends_helper::send_request(&r.user_id()?, &friend_id)?;
|
|
|
|
|
|
|
|
// TODO : create a notification
|
|
|
|
|
|
|
|
r.success("The friendship request was successfully sent!")
|
2020-06-30 08:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel a friendship request
|
|
|
|
pub fn cancel_request(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_user_id("friendID")?;
|
|
|
|
|
|
|
|
if !friends_helper::sent_request(&r.user_id()?, &friend_id)? {
|
|
|
|
r.forbidden("No friendship request was sent to this user!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
friends_helper::remove_request(&r.user_id()?, &friend_id)?;
|
|
|
|
|
|
|
|
// TODO : delete related notifications
|
|
|
|
|
|
|
|
r.success("Friendship request removed!")
|
2020-06-30 12:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Respond to a friendship request
|
|
|
|
pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_user_id("friendID")?;
|
|
|
|
let accept = r.post_bool("accept")?;
|
|
|
|
|
|
|
|
if !friends_helper::sent_request(&friend_id, r.user_id_ref()?)? {
|
|
|
|
r.forbidden("No friendship request was sent by this user!".to_string())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
friends_helper::respond_request(r.user_id_ref()?, &friend_id, accept)?;
|
|
|
|
|
|
|
|
// TODO : create a notification
|
|
|
|
|
|
|
|
r.set_response("Response to the friendship request successfully saved!")
|
2020-06-30 12:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a friend from the list
|
|
|
|
pub fn remove_friend(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-30 12:48:39 +00:00
|
|
|
let friend_id = r.post_friend_id("friendID")?;
|
2020-06-30 12:27:31 +00:00
|
|
|
|
|
|
|
friends_helper::remove_friendship(r.user_id_ref()?, &friend_id)?;
|
|
|
|
|
|
|
|
// TODO : Delete any related notification
|
|
|
|
|
|
|
|
r.success("The friend was removed from the list!")
|
2020-06-30 12:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update following status
|
|
|
|
pub fn set_following(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let friend_id = r.post_friend_id("friendID")?;
|
|
|
|
let follow = r.post_bool("follow")?;
|
|
|
|
|
|
|
|
friends_helper::set_following(r.user_id_ref()?, &friend_id, follow)?;
|
|
|
|
|
|
|
|
r.success("Following status updated!")
|
2020-06-29 13:45:26 +00:00
|
|
|
}
|