mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 15:29:21 +00:00
Can get the list of memberships of a user
This commit is contained in:
parent
59961129c5
commit
b5bcb3e8c7
@ -41,3 +41,5 @@ pub mod res_create_comment;
|
||||
pub mod res_number_unread_notifications;
|
||||
pub mod res_count_all_unreads;
|
||||
pub mod notification_api;
|
||||
pub mod user_membership_api;
|
||||
mod type_container_api;
|
@ -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<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::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<u64>,
|
||||
from_container_type: Option<String>,
|
||||
@ -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()),
|
||||
|
24
src/api_data/type_container_api.rs
Normal file
24
src/api_data/type_container_api.rs
Normal file
@ -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<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
|
||||
S: Serializer {
|
||||
let mut map = serializer.serialize_map(Some(1))?;
|
||||
map.serialize_entry("type", &self.t)?;
|
||||
map.end()
|
||||
}
|
||||
}
|
54
src/api_data/user_membership_api.rs
Normal file
54
src/api_data/user_membership_api.rs
Normal file
@ -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<u64>,
|
||||
friend: Option<FriendAPI>,
|
||||
last_activity: Option<u64>,
|
||||
conv: Option<ConversationAPI>,
|
||||
}
|
||||
|
||||
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<UserMembership>) -> Vec<UserMembershipAPI> {
|
||||
l.iter().map(Self::new).collect()
|
||||
}
|
||||
}
|
@ -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))
|
||||
}
|
@ -25,3 +25,4 @@ pub mod survey;
|
||||
pub mod comment;
|
||||
pub mod new_survey;
|
||||
pub mod notification;
|
||||
pub mod user_membership;
|
13
src/data/user_membership.rs
Normal file
13
src/data/user_membership.rs
Normal file
@ -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)
|
||||
}
|
@ -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<GroupID>) -> Res
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the last activity time of a group
|
||||
pub fn get_last_activity(user_id: &UserID, group_id: &GroupID) -> ResultBoxError<u64> {
|
||||
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)
|
||||
|
@ -15,3 +15,4 @@ pub mod movies_helper;
|
||||
pub mod survey_helper;
|
||||
pub mod comments_helper;
|
||||
pub mod notifications_helper;
|
||||
pub mod webapp_helper;
|
34
src/helpers/webapp_helper.rs
Normal file
34
src/helpers/webapp_helper.rs
Normal file
@ -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<Vec<UserMembership>> {
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user