From f8413850ae73e4d59b64db8a7f4c5e5cda80d805 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 6 Jul 2020 10:01:54 +0200 Subject: [PATCH] Can get the latest posts --- src/controllers/groups_controller.rs | 2 +- src/controllers/posts_controller.rs | 12 +++++ src/controllers/routes.rs | 2 + src/helpers/database.rs | 6 +++ src/helpers/friends_helper.rs | 22 ++++++++++ src/helpers/groups_helper.rs | 4 +- src/helpers/posts_helper.rs | 65 ++++++++++++++++++++++++++++ 7 files changed, 110 insertions(+), 3 deletions(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 0ca8f58..29b7ebd 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -35,7 +35,7 @@ pub fn create(r: &mut HttpRequestHandler) -> RequestResult { /// Get the list of groups of the current user pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult { - let list = groups_helper::get_list_user(r.user_id()?, false)? + let list = groups_helper::get_list_user(r.user_id_ref()?, false)? .iter() .map(|f| f.id()) .collect::>(); diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 969708f..8028dbc 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -33,5 +33,17 @@ pub fn get_list_group(r: &mut HttpRequestHandler) -> RequestResult { .set_start_from(start_from) .get_group(&group_id)?; + r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?) +} + +/// Get the latest posts of a group +pub fn get_latest(r: &mut HttpRequestHandler) -> RequestResult { + let start_from = r.post_u64_opt("startFrom", 0)?; + let include_groups = r.post_bool_opt("include_groups", false); + + let posts = posts_helper::PostsQuery::new(r.user_id_opt()) + .set_start_from(start_from) + .get_latest(include_groups)?; + r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 2f20a62..885b755 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -202,6 +202,8 @@ pub fn get_routes() -> Vec { Route::post("/posts/get_group", Box::new(posts_controller::get_list_group)), + Route::post("/posts/get_latest", Box::new(posts_controller::get_latest)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 056bfe0..26fbf2d 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -201,6 +201,12 @@ impl QueryInfo { self } + /// Add a custom Group ID WHERE value + pub fn add_custom_where_argument_group_id(mut self, val: &GroupID) -> QueryInfo { + self.custom_where_ars.push(mysql::Value::UInt(val.id())); + self + } + /// Add a custom string WHERE value pub fn add_custom_where_argument_str(mut self, val: &str) -> QueryInfo { self.custom_where_ars.push(mysql::Value::from(val)); diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index d52539d..c809533 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -14,6 +14,8 @@ use crate::helpers::database::QueryInfo; /// Structure used to get a friend information pub struct GetFriendsQuery { + only_accepted: bool, + only_following: bool, target_user: UserID, friend_id: Option, } @@ -22,11 +24,23 @@ impl GetFriendsQuery { /// Construct a new request pub fn new(target_user: &UserID) -> GetFriendsQuery { GetFriendsQuery { + only_accepted: false, + only_following: false, target_user: target_user.clone(), friend_id: None, } } + pub fn set_only_accepted(mut self, accepted: bool) -> GetFriendsQuery { + self.only_accepted = accepted; + self + } + + pub fn set_only_following(mut self, following: bool) -> GetFriendsQuery { + self.only_following = following; + self + } + /// Get the list of friends pub fn exec(self) -> ResultBoxError> { get_list(&self) @@ -58,6 +72,14 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError> { query = query.cond_user_id("ID_amis", friend_id); } + if friend_query.only_accepted { + query = query.cond_legacy_bool("f.actif", true); + } + + if friend_query.only_following { + query = query.cond_legacy_bool("f.abonnement", true); + } + query.exec(db_to_friend) } diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 782fa55..24baff2 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -152,10 +152,10 @@ pub fn set_following(g: &GroupID, u: &UserID, follow: bool) -> ResultBoxError { } /// Get the list of groups of a user -pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError> { +pub fn get_list_user(user_id: &UserID, only_followed: bool) -> ResultBoxError> { let mut query = database::QueryInfo::new(GROUPS_MEMBERS_TABLE) .add_field("groups_id") - .cond_user_id("user_id", &user_id); + .cond_user_id("user_id", user_id); if only_followed { query = query.cond_legacy_bool("following", true); diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 3e40019..249ba93 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -78,6 +78,11 @@ impl PostsQuery { pub fn get_group(self, group_id: &GroupID) -> ResultBoxError> { get_group(&self, group_id) } + + /// Get the latest posts for a user + pub fn get_latest(self, include_groups_posts: bool) -> ResultBoxError> { + get_latest(&self, include_groups_posts) + } } /// Get the posts of `target_id` @@ -165,6 +170,66 @@ fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError .exec(db_to_post) } +/// Get the latest posts of a user +pub fn get_latest(query: &PostsQuery, include_group_posts: bool) -> ResultBoxError> { + let user_id = query.user_id.as_ref().ok_or(ExecError::new("Can not get latest posts of no user!"))?; + + let visibility_level = PostVisibilityLevel::VISIBILITY_FRIENDS; + + // Get the list of friends of the user + let friends_list = friends_helper::GetFriendsQuery::new(user_id) + .set_only_accepted(true) + .set_only_following(true) + .exec()?; + + // Prepare the request + let mut db_query = database::QueryInfo::new(POSTS_TABLE); + let mut custom_where = String::new(); + + // ================== MEMBERSHIP CONDITION ====================== + custom_where.push_str("("); + + // ========== FRIENDS POSTS =========== + custom_where.push_str("(group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?"); + db_query = db_query.add_custom_where_argument_u32(visibility_level.to_db()); + db_query = db_query.add_custom_where_argument_user_id(&user_id); + + for f in &friends_list { + custom_where.push_str(" OR ID_personne = ?"); + db_query = db_query.add_custom_where_argument_user_id(&f.friend_id); + }; + custom_where.push_str("))"); + // ========== /FRIENDS POSTS ========== + + // =========== GROUPS POSTS =========== + if include_group_posts { + let groups = groups_helper::get_list_user(user_id, true)?; + + for g in &groups { + custom_where.push_str(" OR group_id = ?"); + db_query = db_query.add_custom_where_argument_group_id(g); + }; + } + // ========== /GROUPS POSTS =========== + + custom_where.push_str(")"); + // ================== /MEMBERSHIP CONDITION ===================== + + + // ======================= START POINT ========================== + if query.start_from > 0 { + custom_where.push_str("AND ID <= ?"); + db_query = db_query.add_custom_where_argument_u64(query.start_from); + } + // ====================== /START POINT ========================== + + db_query + .set_custom_where(&custom_where) + .set_order("ID DESC") + .set_limit(query.limit) + .exec(db_to_post) +} + /// Get the access level of a user over a post pub fn get_access_level(p: &Post, user_id: &Option) -> ResultBoxError { if user_id == &p.user_id.as_option() {