From b5bcb3e8c7e0bca240d6573e8379ad14981a5dfb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 11:17:35 +0200 Subject: [PATCH] Can get the list of memberships of a user --- src/api_data/mod.rs | 4 +- src/api_data/notification_api.rs | 21 ++--------- src/api_data/type_container_api.rs | 24 ++++++++++++ src/api_data/user_membership_api.rs | 54 +++++++++++++++++++++++++++ src/controllers/web_app_controller.rs | 6 ++- src/data/mod.rs | 3 +- src/data/user_membership.rs | 13 +++++++ src/helpers/groups_helper.rs | 20 +++++++++- src/helpers/mod.rs | 3 +- src/helpers/webapp_helper.rs | 34 +++++++++++++++++ 10 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 src/api_data/type_container_api.rs create mode 100644 src/api_data/user_membership_api.rs create mode 100644 src/data/user_membership.rs create mode 100644 src/helpers/webapp_helper.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index a40a87a..7fde199 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -40,4 +40,6 @@ pub mod posts_targets_api; pub mod res_create_comment; pub mod res_number_unread_notifications; pub mod res_count_all_unreads; -pub mod notification_api; \ No newline at end of file +pub mod notification_api; +pub mod user_membership_api; +mod type_container_api; \ No newline at end of file diff --git a/src/api_data/notification_api.rs b/src/api_data/notification_api.rs index 29cb6e3..21eb6b7 100644 --- a/src/api_data/notification_api.rs +++ b/src/api_data/notification_api.rs @@ -2,24 +2,11 @@ //! //! @author Pierre Hubert -use serde::{Serialize, Serializer}; -use serde::ser::SerializeMap; +use serde::Serialize; +use crate::api_data::type_container_api::TypeContainerAPI; use crate::data::notification::Notification; -struct TypeContainer { - t: String, -} - -impl Serialize for TypeContainer { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where - S: Serializer { - let mut map = serializer.serialize_map(Some(1))?; - map.serialize_entry("type", &self.t)?; - map.end() - } -} - #[derive(Serialize)] pub struct NotificationAPI { id: u64, @@ -30,7 +17,7 @@ pub struct NotificationAPI { on_elem_id: u64, on_elem_type: String, #[serde(flatten)] - type_container: TypeContainer, + type_container: TypeContainerAPI, event_visibility: String, from_container_id: Option, from_container_type: Option, @@ -46,7 +33,7 @@ impl NotificationAPI { dest_user_id: n.dest_user_id.id(), on_elem_id: n.on_elem_id, on_elem_type: n.on_elem_type.to_api(), - type_container: TypeContainer { t: n.kind.to_api() }, + type_container: TypeContainerAPI::new(n.kind.to_api()), event_visibility: n.visibility.to_api(), from_container_id: n.container_id, from_container_type: n.container_type.as_ref().map(|f| f.to_api()), diff --git a/src/api_data/type_container_api.rs b/src/api_data/type_container_api.rs new file mode 100644 index 0000000..f3cfe94 --- /dev/null +++ b/src/api_data/type_container_api.rs @@ -0,0 +1,24 @@ +//! Use this structure for all structures that needs a "type" field +//! +//! @author Pierre Hubert +use serde::{Serialize, Serializer}; +use serde::ser::SerializeMap; + +pub struct TypeContainerAPI { + t: String, +} + +impl TypeContainerAPI { + pub fn new(t: String) -> TypeContainerAPI { + TypeContainerAPI { t } + } +} + +impl Serialize for TypeContainerAPI { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where + S: Serializer { + let mut map = serializer.serialize_map(Some(1))?; + map.serialize_entry("type", &self.t)?; + map.end() + } +} \ No newline at end of file diff --git a/src/api_data/user_membership_api.rs b/src/api_data/user_membership_api.rs new file mode 100644 index 0000000..bcd4d7d --- /dev/null +++ b/src/api_data/user_membership_api.rs @@ -0,0 +1,54 @@ +//! # User membership API entry +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::api_data::conversation_api::ConversationAPI; +use crate::api_data::friend_api::FriendAPI; +use crate::api_data::type_container_api::TypeContainerAPI; +use crate::data::user_membership::UserMembership; + +#[derive(Serialize)] +pub struct UserMembershipAPI { + #[serde(flatten)] + membership_type: TypeContainerAPI, + id: Option, + friend: Option, + last_activity: Option, + conv: Option, +} + +impl UserMembershipAPI { + pub fn new(m: &UserMembership) -> UserMembershipAPI { + match &m { + UserMembership::Group(group_id, last_activity) => UserMembershipAPI { + membership_type: TypeContainerAPI::new("group".to_string()), + id: Some(group_id.id()), + friend: None, + last_activity: Some(last_activity.clone()), + conv: None, + }, + + UserMembership::Friend(friend) => UserMembershipAPI { + membership_type: TypeContainerAPI::new("friend".to_string()), + id: None, + friend: Some(FriendAPI::new(friend)), + last_activity: None, + conv: None, + }, + + UserMembership::Conversation(conversation) => UserMembershipAPI { + membership_type: TypeContainerAPI::new("conversation".to_string()), + id: None, + friend: None, + last_activity: None, + conv: Some(ConversationAPI::new(conversation)), + } + } + } + + pub fn for_list(l: &Vec) -> Vec { + l.iter().map(Self::new).collect() + } +} \ No newline at end of file diff --git a/src/controllers/web_app_controller.rs b/src/controllers/web_app_controller.rs index 915bfa1..45e529f 100644 --- a/src/controllers/web_app_controller.rs +++ b/src/controllers/web_app_controller.rs @@ -2,10 +2,14 @@ //! //! @author Pierre Hubert +use crate::api_data::user_membership_api::UserMembershipAPI; use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::webapp_helper; /// Get the list of memberships of a given user pub fn get_memberships(r: &mut HttpRequestHandler) -> RequestResult { - r.success("Implement me") + let memberships = webapp_helper::get_user_memberships(r.user_id_ref()?)?; + + r.set_response(UserMembershipAPI::for_list(&memberships)) } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 9020438..2c039e1 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -24,4 +24,5 @@ pub mod movie; pub mod survey; pub mod comment; pub mod new_survey; -pub mod notification; \ No newline at end of file +pub mod notification; +pub mod user_membership; \ No newline at end of file diff --git a/src/data/user_membership.rs b/src/data/user_membership.rs new file mode 100644 index 0000000..2a11f69 --- /dev/null +++ b/src/data/user_membership.rs @@ -0,0 +1,13 @@ +//! # User membership +//! +//! @author Pierre Hubert + +use crate::data::friend::Friend; +use crate::data::conversation::Conversation; +use crate::data::group_id::GroupID; + +pub enum UserMembership { + Group(GroupID, u64), + Friend(Friend), + Conversation(Conversation) +} \ No newline at end of file diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index a2e7b90..2e59689 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -9,7 +9,7 @@ use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::new_group::NewGroup; use crate::data::user::UserID; -use crate::helpers::database; +use crate::helpers::{database, posts_helper}; use crate::utils::date_utils::time; impl GroupVisibilityLevel { @@ -346,6 +346,24 @@ pub fn check_directory_availability(dir: &str, group_id: Option) -> Res } } +/// Get the last activity time of a group +pub fn get_last_activity(user_id: &UserID, group_id: &GroupID) -> ResultBoxError { + let last_post = posts_helper::PostsQuery::new(user_id.as_option()) + .set_limit(1) + .get_group(group_id)?; + + match last_post.first() { + None => { + // No post was found + Ok(0) + } + + Some(p) => { + Ok(p.time_create) + } + } +} + /// Set new settings to the group, except group logo pub fn set_settings(g: &Group) -> ResultBoxError { database::UpdateInfo::new(GROUPS_LIST_TABLE) diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 7676e7f..3b24460 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -14,4 +14,5 @@ pub mod virtual_directory_helper; pub mod movies_helper; pub mod survey_helper; pub mod comments_helper; -pub mod notifications_helper; \ No newline at end of file +pub mod notifications_helper; +pub mod webapp_helper; \ No newline at end of file diff --git a/src/helpers/webapp_helper.rs b/src/helpers/webapp_helper.rs new file mode 100644 index 0000000..fecd458 --- /dev/null +++ b/src/helpers/webapp_helper.rs @@ -0,0 +1,34 @@ +//! # Web Application Helper +//! +//! Contains methods specific to Comunic Web applications +//! +//! @author Pierre Hubert + +use crate::data::error::ResultBoxError; +use crate::data::user::UserID; +use crate::data::user_membership::UserMembership; +use crate::helpers::{conversations_helper, friends_helper, groups_helper}; + +/// Get all the memberships of a user +pub fn get_user_memberships(user_id: &UserID) -> ResultBoxError> { + let friends = friends_helper::GetFriendsQuery::new(user_id) + .exec()?; + let groups = groups_helper::get_list_user(user_id, false)?; + let conversations_list = conversations_helper::get_list_user(user_id)?; + + let mut list = Vec::new(); + + for friend in friends { + list.push(UserMembership::Friend(friend)); + } + + for group in &groups { + list.push(UserMembership::Group(group.clone(), groups_helper::get_last_activity(user_id, group)?)); + } + + for conv in conversations_list { + list.push(UserMembership::Conversation(conv)) + } + + Ok(list) +} \ No newline at end of file