mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-02-18 06:52:39 +00:00
Can get information about a single friendship
This commit is contained in:
parent
9a3b565f00
commit
31ed53aacf
@ -9,7 +9,7 @@ use crate::helpers::{account_helper, friends_helper, user_helper};
|
|||||||
|
|
||||||
/// Get the list of friends of the current user
|
/// Get the list of friends of the current user
|
||||||
pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult {
|
pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult {
|
||||||
let list = friends_helper::get_list(&r.user_id()?)?;
|
let list = friends_helper::GetFriendsQuery::new(&r.user_id()?).exec()?;
|
||||||
|
|
||||||
// Update last activity (if allowed)
|
// Update last activity (if allowed)
|
||||||
if !r.post_bool_opt("incognito", false) {
|
if !r.post_bool_opt("incognito", false) {
|
||||||
@ -19,6 +19,21 @@ pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult {
|
|||||||
r.set_response(FriendAPI::from_list(&list))
|
r.set_response(FriendAPI::from_list(&list))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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))
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the list of friends of another user
|
/// Get the list of friends of another user
|
||||||
pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult {
|
pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult {
|
||||||
let user_id = r.post_user_id("userID")?;
|
let user_id = r.post_user_id("userID")?;
|
||||||
@ -31,7 +46,7 @@ pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult {
|
|||||||
r.forbidden("The friend list of this user is not public!".to_string())?;
|
r.forbidden("The friend list of this user is not public!".to_string())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let friends = friends_helper::get_list(&user_id)?;
|
let friends = friends_helper::GetFriendsQuery::new(&user_id).exec()?;
|
||||||
|
|
||||||
r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::<Vec<u64>>())
|
r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::<Vec<u64>>())
|
||||||
}
|
}
|
@ -91,6 +91,8 @@ pub fn get_routes() -> Vec<Route> {
|
|||||||
// Friends controller
|
// Friends controller
|
||||||
Route::post("/friends/getList", Box::new(friends_controller::get_list)),
|
Route::post("/friends/getList", Box::new(friends_controller::get_list)),
|
||||||
|
|
||||||
|
Route::post("/friends/get_single_infos", Box::new(friends_controller::get_single_friendship_info)),
|
||||||
|
|
||||||
Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)),
|
Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)),
|
||||||
|
|
||||||
// Conversations controller
|
// Conversations controller
|
||||||
|
@ -4,25 +4,59 @@
|
|||||||
|
|
||||||
|
|
||||||
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
|
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
|
||||||
use crate::data::error::ResultBoxError;
|
use crate::data::error::{ExecError, ResultBoxError};
|
||||||
use crate::data::friend::Friend;
|
use crate::data::friend::Friend;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
use crate::helpers::database;
|
use crate::helpers::database;
|
||||||
use crate::helpers::database::QueryInfo;
|
use crate::helpers::database::QueryInfo;
|
||||||
|
|
||||||
|
/// Structure used to get a friend information
|
||||||
|
pub struct GetFriendsQuery {
|
||||||
|
target_user: UserID,
|
||||||
|
friend_id: Option<UserID>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GetFriendsQuery {
|
||||||
|
/// Construct a new request
|
||||||
|
pub fn new(target_user: &UserID) -> GetFriendsQuery {
|
||||||
|
GetFriendsQuery {
|
||||||
|
target_user: target_user.clone(),
|
||||||
|
friend_id: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the list of friends
|
||||||
|
pub fn exec(self) -> ResultBoxError<Vec<Friend>> {
|
||||||
|
get_list(&self)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get information about a single friendship
|
||||||
|
pub fn get_single_friend(mut self, friend_id: &UserID) -> ResultBoxError<Friend> {
|
||||||
|
self.friend_id = Some(friend_id.clone());
|
||||||
|
get_list(&self)?
|
||||||
|
.pop()
|
||||||
|
.ok_or(ExecError::boxed_new("Single friend not found!"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the list of friends of a user
|
/// Get the list of friends of a user
|
||||||
pub fn get_list(user_id: &UserID) -> ResultBoxError<Vec<Friend>> {
|
fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError<Vec<Friend>> {
|
||||||
database::QueryInfo::new(FRIENDS_TABLE)
|
let mut query = database::QueryInfo::new(FRIENDS_TABLE)
|
||||||
.alias("f")
|
.alias("f")
|
||||||
.cond_user_id("ID_personne", user_id)
|
.cond_user_id("ID_personne", &friend_query.target_user)
|
||||||
.join(USERS_TABLE, "u", "f.ID_amis = u.ID")
|
.join(USERS_TABLE, "u", "f.ID_amis = u.ID")
|
||||||
.add_field("u.last_activity")
|
.add_field("u.last_activity")
|
||||||
.add_field("f.ID_amis")
|
.add_field("f.ID_amis")
|
||||||
.add_field("f.actif")
|
.add_field("f.actif")
|
||||||
.add_field("f.abonnement")
|
.add_field("f.abonnement")
|
||||||
.add_field("f.autoriser_post_page")
|
.add_field("f.autoriser_post_page")
|
||||||
.set_order("u.last_activity DESC")
|
.set_order("u.last_activity DESC");
|
||||||
.exec(db_to_friend)
|
|
||||||
|
if let Some(friend_id) = friend_query.friend_id.as_ref() {
|
||||||
|
query = query.cond_user_id("ID_amis", friend_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
query.exec(db_to_friend)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check out whether two users are friend or not
|
/// Check out whether two users are friend or not
|
||||||
|
Loading…
x
Reference in New Issue
Block a user