mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Start to implement posts creation
This commit is contained in:
		@@ -6,8 +6,8 @@ use crate::api_data::post_api::PostAPI;
 | 
				
			|||||||
use crate::controllers::routes::RequestResult;
 | 
					use crate::controllers::routes::RequestResult;
 | 
				
			||||||
use crate::data::group::GroupAccessLevel;
 | 
					use crate::data::group::GroupAccessLevel;
 | 
				
			||||||
use crate::data::http_request_handler::HttpRequestHandler;
 | 
					use crate::data::http_request_handler::HttpRequestHandler;
 | 
				
			||||||
use crate::data::post::PostAccessLevel;
 | 
					use crate::data::post::{PostAccessLevel, PostPageKind};
 | 
				
			||||||
use crate::helpers::{posts_helper, user_helper};
 | 
					use crate::helpers::{groups_helper, posts_helper, user_helper};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Get the list of posts of a user
 | 
					/// Get the list of posts of a user
 | 
				
			||||||
pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
@@ -55,3 +55,38 @@ pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    r.set_response(PostAPI::new(&post, &r.user_id_opt())?)
 | 
					    r.set_response(PostAPI::new(&post, &r.user_id_opt())?)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Create a new post
 | 
				
			||||||
 | 
					pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
 | 
					    // Process page target
 | 
				
			||||||
 | 
					    let target_page = match r.post_string("kind-page")?.as_str() {
 | 
				
			||||||
 | 
					        "user" => {
 | 
				
			||||||
 | 
					            let user_id = r.post_user_id("kind-id")?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if !user_helper::can_create_posts(r.user_id_ref()?, &user_id)? {
 | 
				
			||||||
 | 
					                r.forbidden("You are not allowed to create posts on this page!".to_string())?;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            PostPageKind::PAGE_KIND_USER(user_id)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        "group" => {
 | 
				
			||||||
 | 
					            let group_id = r.post_group_id_with_access("kind-id", GroupAccessLevel::MEMBER_ACCESS)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if !groups_helper::can_user_create_posts(&group_id, r.user_id_ref()?)? {
 | 
				
			||||||
 | 
					                r.forbidden("You are not allowed to create posts on this group!".to_string())?;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            PostPageKind::PAGE_KIND_GROUP(group_id)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        _ => {
 | 
				
			||||||
 | 
					            r.not_found("Unsupported target page type!".to_string())?;
 | 
				
			||||||
 | 
					            unreachable!();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("Create post on {:?}", target_page);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    r.success("Continue implementation")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -206,6 +206,8 @@ pub fn get_routes() -> Vec<Route> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        Route::post("/posts/get_single", Box::new(posts_controller::get_single)),
 | 
					        Route::post("/posts/get_single", Box::new(posts_controller::get_single)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Route::post("/posts/create", Box::new(posts_controller::create_post)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Virtual directory controller
 | 
					        // Virtual directory controller
 | 
				
			||||||
        Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
 | 
					        Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,6 +61,7 @@ impl PostAccessLevel {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[allow(non_camel_case_types)]
 | 
					#[allow(non_camel_case_types)]
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub enum PostPageKind {
 | 
					pub enum PostPageKind {
 | 
				
			||||||
    PAGE_KIND_USER(UserID),
 | 
					    PAGE_KIND_USER(UserID),
 | 
				
			||||||
    PAGE_KIND_GROUP(GroupID),
 | 
					    PAGE_KIND_GROUP(GroupID),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -414,6 +414,17 @@ pub fn respond_request(group_id: &GroupID, user_id: &UserID, accept: bool) -> Re
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// check out whether a user can create posts on a group or not
 | 
				
			||||||
 | 
					pub fn can_user_create_posts(group_id: &GroupID, user_id: &UserID) -> ResultBoxError<bool> {
 | 
				
			||||||
 | 
					    let membership_level = get_membership_level(group_id, user_id.as_option())?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    match membership_level {
 | 
				
			||||||
 | 
					        GroupMembershipLevel::ADMINISTRATOR | GroupMembershipLevel::MODERATOR => Ok(true),
 | 
				
			||||||
 | 
					        GroupMembershipLevel::MEMBER => Ok(get_info(group_id)?.posts_creation_level == GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS),
 | 
				
			||||||
 | 
					        _ => Ok(false),
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Turn a database entry into a group struct
 | 
					/// Turn a database entry into a group struct
 | 
				
			||||||
fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> {
 | 
					fn db_to_group(row: &database::RowResult) -> ResultBoxError<Group> {
 | 
				
			||||||
    let group_id = row.get_group_id("id")?;
 | 
					    let group_id = row.get_group_id("id")?;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user