mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Return group information to API
This commit is contained in:
@ -170,6 +170,12 @@ impl QueryInfo {
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a custom u32 WHERE value
|
||||
pub fn add_custom_where_argument_u32(mut self, val: u32) -> QueryInfo {
|
||||
self.custom_where_ars.push(mysql::Value::UInt(val as u64));
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a custom string WHERE value
|
||||
pub fn add_custom_where_argument_str(mut self, val: &str) -> QueryInfo {
|
||||
self.custom_where_ars.push(mysql::Value::from(val));
|
||||
|
@ -172,6 +172,27 @@ pub fn search_group(query: &str, limit: u64) -> ResultBoxError<Vec<GroupID>> {
|
||||
.exec(|row| row.get_group_id("id"))
|
||||
}
|
||||
|
||||
/// Get information about the membership of a user over a group
|
||||
pub fn get_membership(group_id: &GroupID, user_id: Option<UserID>) -> ResultBoxError<GroupMember> {
|
||||
if user_id == None {
|
||||
return Ok(GroupMember {
|
||||
id: 0,
|
||||
user_id: 0,
|
||||
group_id: group_id.clone(),
|
||||
time_create: 0,
|
||||
level: GroupMembershipLevel::VISITOR,
|
||||
following: false,
|
||||
});
|
||||
}
|
||||
|
||||
let user_id = user_id.unwrap();
|
||||
|
||||
database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
|
||||
.cond_group_id("groups_id", group_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.query_row(db_to_group_member)
|
||||
}
|
||||
|
||||
/// Get the membership level of a user for a group
|
||||
pub fn get_membership_level(group_id: &GroupID, user_id: Option<UserID>) -> ResultBoxError<GroupMembershipLevel> {
|
||||
match user_id {
|
||||
@ -242,17 +263,18 @@ pub fn get_access_level(group_id: &GroupID, user_id: Option<UserID>) -> ResultBo
|
||||
pub fn count_members(group_id: &GroupID) -> ResultBoxError<usize> {
|
||||
database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
|
||||
.cond_group_id("groups_id", group_id)
|
||||
.set_custom_where("level <= ?")
|
||||
.add_custom_where_argument_u32(GroupMembershipLevel::MEMBER.to_db())
|
||||
.exec_count()
|
||||
}
|
||||
|
||||
/// Turn a database entry into a group object
|
||||
/// Turn a database entry into a group struct
|
||||
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")?),
|
||||
@ -262,4 +284,16 @@ fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> {
|
||||
description: row.get_optional_str("description")?,
|
||||
url: row.get_optional_str("url")?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn a database entry into a group member struct
|
||||
fn db_to_group_member(row: &database::RowResult) -> ResultBoxError<GroupMember> {
|
||||
Ok(GroupMember {
|
||||
id: row.get_u64("id")?,
|
||||
user_id: row.get_user_id("user_id")?,
|
||||
group_id: row.get_group_id("groups_id")?,
|
||||
time_create: row.get_u64("time_create")?,
|
||||
level: GroupMembershipLevel::from_db(row.get_u32("level")?),
|
||||
following: row.get_legacy_bool("following")?,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user