1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00

Create new groups

This commit is contained in:
Pierre HUBERT 2020-06-24 09:08:31 +02:00
parent d38e2ee4ab
commit 5a14aaee31
9 changed files with 139 additions and 5 deletions

View File

@ -22,4 +22,5 @@ pub mod conversation_message_api;
pub mod conversations_refresh_api;
pub mod res_count_unread_conversations;
pub mod list_unread_conversations_api;
pub mod global_search_result_api;
pub mod global_search_result_api;
pub mod res_create_group;

View File

@ -0,0 +1,23 @@
//! # Group creation result
//!
//! Result of the creation of a new group
use serde::Serialize;
use crate::data::group_id::GroupID;
#[derive(Serialize)]
pub struct GroupCreationResult {
success: String,
id: u64,
}
impl GroupCreationResult {
/// Construct a new instance of this object
pub fn new(id: &GroupID) -> GroupCreationResult {
GroupCreationResult {
success: "The group has been successfully created!".to_string(),
id: id.id(),
}
}
}

View File

@ -27,6 +27,9 @@ pub mod database_tables_names {
/// Groups list table
pub const GROUPS_LIST_TABLE: &str = "comunic_groups";
/// Groups members table
pub const GROUPS_MEMBERS_TABLE: &str = "comunic_groups_members";
/// Conversations tables
pub const CONV_LIST_TABLE: &str = "comunic_conversations_list";
pub const CONV_USERS_TABLE: &str = "comunic_conversations_users";

View File

@ -2,10 +2,20 @@
//!
//! @author Pierre Hubert
use crate::api_data::res_create_group::GroupCreationResult;
use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::new_group::NewGroup;
use crate::helpers::groups_helper;
/// Create a new group
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
r.success("implement me")
let new_group = NewGroup {
name: r.post_string_opt("name", 3, true)?,
owner_id: r.user_id()?,
};
let group_id = groups_helper::create(&new_group)?;
r.set_response(GroupCreationResult::new(&group_id))
}

24
src/data/group_member.rs Normal file
View File

@ -0,0 +1,24 @@
//! # Group membership information
//!
//! @author Pierre Hubert
use crate::data::user::UserID;
use crate::data::group_id::GroupID;
pub enum GroupMembershipLevel {
ADMINISTRATOR = 0,
MODERATOR = 1,
MEMBER = 2,
INVITED = 3,
PENDING = 4, //When the group membership has not been approved yet
VISITOR = 5, //Simple visit
}
pub struct GroupMember {
pub id: u64,
pub user_id: UserID,
pub group_id: GroupID,
pub time_create: u64,
pub level: GroupMembershipLevel,
pub following: bool
}

View File

@ -14,4 +14,6 @@ pub mod new_conversation_message;
pub mod unread_conversation;
pub mod group_id;
pub mod group;
pub mod new_group;
pub mod group_member;
pub mod global_search_result;

10
src/data/new_group.rs Normal file
View File

@ -0,0 +1,10 @@
//! # New group information
//!
//! @author Pierre Hubert
use crate::data::user::UserID;
pub struct NewGroup {
pub name: String,
pub owner_id: UserID,
}

View File

@ -10,8 +10,8 @@ use mysql::prelude::Queryable;
use crate::data::config::DatabaseConfig;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::data::group_id::GroupID;
use crate::data::user::UserID;
/// Database access helper
///
@ -498,6 +498,12 @@ impl InsertQuery {
self
}
pub fn add_group_id(mut self, key: &str, value: &GroupID) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value.id()));
self
}
/// Legacy database boolean (1 = true / 0 = false)
pub fn add_legacy_bool(mut self, key: &str, value: bool) -> InsertQuery {
let num = match value {
@ -513,6 +519,12 @@ impl InsertQuery {
pub fn insert(self) -> ResultBoxError<Option<u64>> {
insert(self)
}
/// Process insert, drop the result of the operation
pub fn insert_drop_result(self) -> ResultBoxError<()> {
insert(self)?;
Ok(())
}
}
/// Insert a new entry into the database

View File

@ -2,11 +2,14 @@
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::GROUPS_LIST_TABLE;
use crate::data::error::ResultBoxError;
use crate::constants::database_tables_names::{GROUPS_LIST_TABLE, GROUPS_MEMBERS_TABLE};
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::GroupVisibilityLevel;
use crate::data::group_id::GroupID;
use crate::data::group_member::{GroupMember, GroupMembershipLevel};
use crate::data::new_group::NewGroup;
use crate::helpers::database;
use crate::utils::date_utils::time;
impl GroupVisibilityLevel {
pub fn to_db(&self) -> u64 {
@ -18,6 +21,52 @@ impl GroupVisibilityLevel {
}
}
impl GroupMembershipLevel {
pub fn to_db(&self) -> u32 {
match self {
GroupMembershipLevel::ADMINISTRATOR => 0,
GroupMembershipLevel::MODERATOR => 1,
GroupMembershipLevel::MEMBER => 2,
GroupMembershipLevel::INVITED => 3,
GroupMembershipLevel::PENDING => 4,
GroupMembershipLevel::VISITOR => 5,
}
}
}
/// Create a new group. Returns the ID of the new group
pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
// First, create the group
let group_id = database::InsertQuery::new(GROUPS_LIST_TABLE)
.add_u64("time_create", time())
.add_user_id("userid_create", group.owner_id)
.add_str("name", &group.name)
.insert()?.ok_or(ExecError::new("Could not get group ID!"))?;
let group_id = GroupID::new(group_id);
// Insert first member
insert_member(&GroupMember {
id: 0,
user_id: group.owner_id,
group_id: group_id.clone(),
time_create: time(),
level: GroupMembershipLevel::ADMINISTRATOR,
following: true,
})?;
Ok(group_id)
}
/// Insert a new group into the database
pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> {
database::InsertQuery::new(GROUPS_MEMBERS_TABLE)
.add_group_id("groups_id", &m.group_id)
.add_user_id("user_id", m.user_id)
.add_u64("time_create", m.time_create)
.add_u32("level", m.level.to_db())
.insert_drop_result()
}
/// Find a group id by virtual directory
pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError<GroupID> {
database::QueryInfo::new(GROUPS_LIST_TABLE)