From 3af2a6f58d6daa16caf7ea0165645f19bf5f1cc3 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 29 Jun 2020 15:45:26 +0200 Subject: [PATCH] Can get the list of friends --- src/api_data/friend_api.rs | 35 +++++++++++++++++++++++++++ src/api_data/mod.rs | 3 ++- src/controllers/friends_controller.rs | 17 +++++++++++++ src/controllers/mod.rs | 1 + src/controllers/routes.rs | 5 +++- src/data/friend.rs | 13 ++++++++++ src/data/mod.rs | 3 ++- src/helpers/friends_helper.rs | 31 ++++++++++++++++++++++-- 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 src/api_data/friend_api.rs create mode 100644 src/controllers/friends_controller.rs create mode 100644 src/data/friend.rs diff --git a/src/api_data/friend_api.rs b/src/api_data/friend_api.rs new file mode 100644 index 0000000..181caa2 --- /dev/null +++ b/src/api_data/friend_api.rs @@ -0,0 +1,35 @@ +//! # Friend API +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::api_data::legacy_api_bool::LegacyBool; +use crate::data::friend::Friend; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct FriendAPI { + ID_friend: u64, + accepted: LegacyBool, + time_last_activity: u64, + following: LegacyBool, + canPostTexts: bool, +} + +impl FriendAPI { + /// Turn a friend object into an API entry + pub fn new(f: &Friend) -> FriendAPI { + FriendAPI { + ID_friend: f.friend_id.id(), + accepted: LegacyBool(f.accepted), + time_last_activity: f.last_activity_time, + following: LegacyBool(f.following), + canPostTexts: f.can_post_texts, + } + } + + /// Generate a new list of Friends + pub fn from_list(l: &Vec) -> Vec { + l.iter().map(Self::new).collect() + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index a393c89..8e76a02 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -27,4 +27,5 @@ pub mod res_create_group; pub mod group_api; pub mod advanced_group_api; pub mod res_change_group_logo; -pub mod group_member_api; \ No newline at end of file +pub mod group_member_api; +pub mod friend_api; \ No newline at end of file diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs new file mode 100644 index 0000000..829f07d --- /dev/null +++ b/src/controllers/friends_controller.rs @@ -0,0 +1,17 @@ +//! # Friends controller +//! +//! @author Pierre Hubert + +use crate::api_data::friend_api::FriendAPI; +use crate::controllers::routes::RequestResult; +use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::friends_helper; + +/// Get the list of friends of the current user +pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { + let list = friends_helper::get_list(&r.user_id()?)?; + + // TODO : update last activity (if allowed) + + r.set_response(FriendAPI::from_list(&list)) +} \ No newline at end of file diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index ed5300a..aba3353 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -4,6 +4,7 @@ pub mod server; pub mod server_controller; pub mod account_controller; pub mod user_controller; +pub mod friends_controller; pub mod conversations_controller; pub mod search_controller; pub mod groups_controller; diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 383024c..463f629 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_controller, conversations_controller, groups_controller, search_controller, server_controller, user_controller, virtual_directory_controller}; +use crate::controllers::{account_controller, conversations_controller, friends_controller, groups_controller, search_controller, server_controller, user_controller, virtual_directory_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -88,6 +88,9 @@ pub fn get_routes() -> Vec { Route::post_without_login("/user/getAdvancedUserInfo", Box::new(user_controller::get_advanced_info)), Route::post_without_login("/user/getAdvancedUserInfos", Box::new(user_controller::get_advanced_info)), + // Friends controller + Route::post("/friends/getList", Box::new(friends_controller::get_list)), + // Conversations controller Route::post("/conversations/create", Box::new(conversations_controller::create)), diff --git a/src/data/friend.rs b/src/data/friend.rs new file mode 100644 index 0000000..85db299 --- /dev/null +++ b/src/data/friend.rs @@ -0,0 +1,13 @@ +//! # Friend information +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct Friend { + pub friend_id: UserID, + pub accepted: bool, + pub following: bool, + pub last_activity_time: u64, + pub can_post_texts: bool, +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index b4e11b2..3a7ffc5 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -16,4 +16,5 @@ pub mod group_id; pub mod group; pub mod new_group; pub mod group_member; -pub mod global_search_result; \ No newline at end of file +pub mod global_search_result; +pub mod friend; \ No newline at end of file diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index cd436e3..0601f58 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -3,12 +3,28 @@ //! @author Pierre Hubert -use crate::data::user::UserID; +use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE}; use crate::data::error::ResultBoxError; +use crate::data::friend::Friend; +use crate::data::user::UserID; use crate::helpers::database; -use crate::constants::database_tables_names::FRIENDS_TABLE; use crate::helpers::database::QueryInfo; +/// Get the list of friends of a user +pub fn get_list(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(FRIENDS_TABLE) + .alias("f") + .cond_user_id("ID_personne", user_id) + .join(USERS_TABLE, "u", "f.ID_amis = u.ID") + .add_field("u.last_activity") + .add_field("f.ID_amis") + .add_field("f.actif") + .add_field("f.abonnement") + .add_field("f.autoriser_post_page") + .set_order("u.last_activity DESC") + .exec(db_to_friend) +} + /// Check out whether two users are friend or not pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError { Ok(database::count(QueryInfo::new(FRIENDS_TABLE) @@ -33,4 +49,15 @@ pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError< .add_field("autoriser_post_page") .query_row(|res| res.get_legacy_bool("autoriser_post_page")) .or(Ok(false)) +} + +/// Turn a database entry into a Friend structure +fn db_to_friend(row: &database::RowResult) -> ResultBoxError { + Ok(Friend { + friend_id: row.get_user_id("ID_amis")?, + accepted: row.get_legacy_bool("actif")?, + following: row.get_legacy_bool("abonnement")?, + last_activity_time: row.get_u64("last_activity")?, + can_post_texts: row.get_legacy_bool("autoriser_post_page")?, + }) } \ No newline at end of file