1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can get the list of friends

This commit is contained in:
Pierre HUBERT 2020-06-29 15:45:26 +02:00
parent bef9dfffbc
commit 3af2a6f58d
8 changed files with 103 additions and 5 deletions

View File

@ -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<Friend>) -> Vec<FriendAPI> {
l.iter().map(Self::new).collect()
}
}

View File

@ -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;
pub mod group_member_api;
pub mod friend_api;

View File

@ -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))
}

View File

@ -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;

View File

@ -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> {
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)),

13
src/data/friend.rs Normal file
View File

@ -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,
}

View File

@ -16,4 +16,5 @@ pub mod group_id;
pub mod group;
pub mod new_group;
pub mod group_member;
pub mod global_search_result;
pub mod global_search_result;
pub mod friend;

View File

@ -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<Vec<Friend>> {
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<bool> {
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<Friend> {
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")?,
})
}