1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-17 12:48:04 +00:00

Get information about groups

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

@ -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")?,
})
}