1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Get information about groups

This commit is contained in:
Pierre HUBERT 2020-06-24 18:23:44 +02:00
parent ed498a73d6
commit dc58357b2e
3 changed files with 109 additions and 3 deletions

View File

@ -34,8 +34,9 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
/// Get information about a single group
pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult {
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::LIMITED_ACCESS)?;
let group = groups_helper::get_info(&group_id)?;
println!("Group to get: {:?}", group_id);
println!("Group info: {:#?}", group);
r.success("continue implementation")
}

View File

@ -2,14 +2,34 @@
//!
//! Group visibility level
use crate::data::group_id::GroupID;
#[allow(non_camel_case_types)]
#[derive(Eq, PartialEq, Hash)]
#[derive(Eq, PartialEq, Hash, Debug)]
pub enum GroupVisibilityLevel {
OPEN_GROUP,
PRIVATE_GROUP,
SECRETE_GROUP,
}
#[allow(non_camel_case_types)]
#[derive(Eq, PartialEq, Hash, Debug)]
pub enum GroupRegistrationLevel {
OPEN_REGISTRATION = 0,
MODERATED_REGISTRATION = 1,
CLOSED_REGISTRATION = 2,
}
#[allow(non_camel_case_types)]
#[derive(Eq, PartialEq, Hash, Debug)]
pub enum GroupPostsCreationLevel {
//Only the moderators and the administrator can create posts
POSTS_LEVEL_MODERATORS = 0,
//All the members of the group can create posts
POSTS_LEVEL_ALL_MEMBERS = 1,
}
#[allow(non_camel_case_types)]
#[derive(Eq, PartialEq, Hash, Debug, PartialOrd)]
pub enum GroupAccessLevel {
@ -32,6 +52,22 @@ pub enum GroupAccessLevel {
ADMIN_ACCESS = 5,
}
/// Group information
#[derive(Debug)]
pub struct Group {
pub id: GroupID,
pub name: String,
pub members_count: u64,
pub visibility: GroupVisibilityLevel,
pub registration_level: GroupRegistrationLevel,
pub posts_creation_level: GroupPostsCreationLevel,
pub logo: Option<String>,
pub virtual_directory: Option<String>,
pub time_create: u64,
pub description: Option<String>,
pub url: Option<String>,
}
#[cfg(test)]
mod tests {
use crate::data::group::GroupAccessLevel;

View File

@ -4,7 +4,7 @@
use crate::constants::database_tables_names::{GROUPS_LIST_TABLE, GROUPS_MEMBERS_TABLE};
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::{GroupAccessLevel, GroupVisibilityLevel};
use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel};
use crate::data::group_id::GroupID;
use crate::data::group_member::{GroupMember, GroupMembershipLevel};
use crate::data::new_group::NewGroup;
@ -56,6 +56,42 @@ impl GroupMembershipLevel {
}
}
impl GroupRegistrationLevel {
pub fn to_db(&self) -> u32 {
match self {
GroupRegistrationLevel::OPEN_REGISTRATION => 0,
GroupRegistrationLevel::MODERATED_REGISTRATION => 1,
GroupRegistrationLevel::CLOSED_REGISTRATION => 2,
}
}
pub fn from_db(level: u32) -> GroupRegistrationLevel {
match level {
0 => GroupRegistrationLevel::OPEN_REGISTRATION,
1 => GroupRegistrationLevel::MODERATED_REGISTRATION,
2 => GroupRegistrationLevel::CLOSED_REGISTRATION,
_ => GroupRegistrationLevel::CLOSED_REGISTRATION,
}
}
}
impl GroupPostsCreationLevel {
pub fn to_db(&self) -> u32 {
match self {
GroupPostsCreationLevel::POSTS_LEVEL_MODERATORS => 0,
GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS => 1,
}
}
pub fn from_db(level: u32) -> GroupPostsCreationLevel {
match level {
0 => GroupPostsCreationLevel::POSTS_LEVEL_MODERATORS,
1 => GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS,
_ => GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS,
}
}
}
/// Create a new group. Returns the ID of the new group
pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
// First, create the group
@ -102,6 +138,13 @@ pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError<Vec
query.exec(|row| row.get_group_id("groups_id"))
}
/// get information about a group
pub fn get_info(group_id: &GroupID) -> ResultBoxError<Group> {
database::QueryInfo::new(GROUPS_LIST_TABLE)
.cond_group_id("id", group_id)
.query_row(db_to_group)
}
/// Check out whether a group exists or not
pub fn exists(group_id: &GroupID) -> ResultBoxError<bool> {
database::QueryInfo::new(GROUPS_LIST_TABLE)
@ -193,4 +236,30 @@ pub fn get_access_level(group_id: &GroupID, user_id: Option<UserID>) -> ResultBo
// Else the user can not see the group
// Especially in the case of secrete group
Ok(GroupAccessLevel::NO_ACCESS)
}
/// Count the number of members of a group
pub fn count_members(group_id: &GroupID) -> ResultBoxError<usize> {
database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
.cond_group_id("groups_id", group_id)
.exec_count()
}
/// Turn a database entry into a group object
fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> {
let group_id = row.get_group_id("id")?;
Ok(Group {
id: group_id.clone(),
name: row.get_str("name")?,
members_count: count_members(&group_id)? as u64,
visibility: GroupVisibilityLevel::from_db(row.get_u32("visibility")?),
registration_level: GroupRegistrationLevel::from_db(row.get_u32("registration_level")?),
posts_creation_level: GroupPostsCreationLevel::from_db(row.get_u32("posts_level")?),
logo: row.get_optional_str("path_logo")?,
virtual_directory: row.get_optional_str("virtual_directory")?,
time_create: row.get_u64("time_create")?,
description: row.get_optional_str("description")?,
url: row.get_optional_str("url")?,
})
}