1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-30 15:38:52 +00:00
comunicapiv3/src/routes.rs

356 lines
18 KiB
Rust
Raw Normal View History

2020-05-21 13:28:07 +00:00
use std::error::Error;
2021-04-18 12:42:45 +00:00
use crate::controllers::{account_controller, comments_controller, conversations_controller, forez_controller, friends_controller, groups_controller, likes_controller, notifications_controller, posts_controller, push_notifications_controller, search_controller, server_controller, settings_controller, surveys_controller, user_controller, user_ws_controller, virtual_directory_controller, web_app_controller};
2021-05-09 14:17:58 +00:00
use crate::controllers::admin::*;
2020-05-21 13:28:07 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
2021-02-13 15:15:25 +00:00
use crate::routes::Method::{GET, POST};
2020-05-21 13:28:07 +00:00
/// Project routes
///
/// @author Pierre Hubert
#[derive(PartialEq)]
pub enum Method {
GET,
POST,
}
2021-01-22 17:46:09 +00:00
/// Limitation policy of a request for a given IP address
///
/// All the limit are on a per-hour basis (the first request that triggers the limit is the one
/// that is recorded)
pub enum LimitPolicy {
// No limit is applied to the request
NONE,
// An acceptable threshold of successful requests (= 200) is defined, then the requests are
// rejected (they are not even processed)
SUCCESS(u64),
// An acceptable threshold of unsuccessful requests (!= 200) is defined, then the requests are
// rejected (they are not even processed)
FAILURE(u64),
// An acceptable threshold of request (successful or unsuccessful) is defined then the requests
// are rejected (they are not even processed)
ANY(u64),
}
2021-05-09 14:17:58 +00:00
/// Scope of the route
pub enum RouteScope {
// Route accessible by a "normal" user of Comunic
USER,
// Route accessible by an administrator of Comunic
ADMIN,
}
2021-01-23 08:44:34 +00:00
impl LimitPolicy {
pub fn is_none(&self) -> bool {
matches!(self, LimitPolicy::NONE)
}
pub fn get_count(&self) -> u64 {
match self {
LimitPolicy::NONE => 0,
LimitPolicy::SUCCESS(n) => n.clone(),
LimitPolicy::FAILURE(n) => n.clone(),
LimitPolicy::ANY(n) => n.clone(),
}
}
}
2020-05-21 13:28:07 +00:00
/// Define types
pub type RequestResult = Result<(), Box<dyn Error>>;
pub type RequestProcess = Box<dyn Fn(&mut HttpRequestHandler) -> RequestResult>;
pub struct Route {
2021-05-09 14:17:58 +00:00
/// Route scope
pub scope: RouteScope,
2020-05-21 13:28:07 +00:00
/// The Verb used for the request
pub method: Method,
/// The URI of the request, with the leading "/"
pub uri: &'static str,
/// If set to true, unauthenticated requests will be rejected
pub need_login: bool,
/// The function called to process a request
pub func: RequestProcess,
2021-01-22 17:46:09 +00:00
/// Request rate policy
pub limit_policy: LimitPolicy,
2020-05-21 13:28:07 +00:00
}
2020-05-21 13:39:20 +00:00
impl Route {
pub fn get_without_login(uri: &'static str, func: RequestProcess) -> Route {
Route {
2021-05-09 14:17:58 +00:00
scope: RouteScope::USER,
2020-05-21 13:39:20 +00:00
method: GET,
need_login: false,
uri,
2020-07-13 09:49:14 +00:00
func,
2021-01-22 17:46:09 +00:00
limit_policy: LimitPolicy::NONE,
2020-05-21 13:39:20 +00:00
}
}
2020-05-21 13:43:53 +00:00
pub fn post_without_login(uri: &'static str, func: RequestProcess) -> Route {
Route {
2021-05-09 14:17:58 +00:00
scope: RouteScope::USER,
2020-05-21 13:43:53 +00:00
method: POST,
need_login: false,
uri,
2020-07-13 09:49:14 +00:00
func,
2021-01-22 17:46:09 +00:00
limit_policy: LimitPolicy::NONE,
}
}
pub fn limited_post_without_login(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
Route {
2021-05-09 14:17:58 +00:00
scope: RouteScope::USER,
2021-01-22 17:46:09 +00:00
method: POST,
need_login: false,
uri,
func,
limit_policy,
2020-05-21 13:43:53 +00:00
}
}
2020-05-24 15:57:47 +00:00
pub fn post(uri: &'static str, func: RequestProcess) -> Route {
Route {
2021-05-09 14:17:58 +00:00
scope: RouteScope::USER,
2020-05-24 15:57:47 +00:00
method: POST,
need_login: true,
uri,
2020-07-13 09:49:14 +00:00
func,
2021-01-22 17:46:09 +00:00
limit_policy: LimitPolicy::NONE,
}
}
pub fn limited_post(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
Route {
2021-05-09 14:17:58 +00:00
scope: RouteScope::USER,
2021-01-22 17:46:09 +00:00
method: POST,
need_login: true,
uri,
func,
limit_policy,
2020-05-24 15:57:47 +00:00
}
}
2021-05-09 14:17:58 +00:00
pub fn limited_admin_post_without_login(uri: &'static str, func: RequestProcess, limit_policy: LimitPolicy) -> Route {
Route {
scope: RouteScope::ADMIN,
method: POST,
need_login: false,
uri,
func,
limit_policy,
}
}
2021-05-11 15:17:52 +00:00
pub fn admin_post(uri: &'static str, func: RequestProcess) -> Route {
Route {
scope: RouteScope::ADMIN,
method: POST,
need_login: true,
uri,
func,
limit_policy: LimitPolicy::NONE,
}
}
2020-05-21 13:39:20 +00:00
}
2020-05-21 13:28:07 +00:00
/// Get the list of routes available
pub fn get_routes() -> Vec<Route> {
vec![
// Server meta routes
2020-05-21 13:43:53 +00:00
Route::get_without_login("/", Box::new(server_controller::main_index)),
2021-02-15 16:03:25 +00:00
Route::post_without_login("/server/config", Box::new(server_controller::get_config)),
2020-05-21 13:43:53 +00:00
2021-02-05 09:37:37 +00:00
// Main user WebSocket
2021-02-05 12:21:10 +00:00
Route::post("/ws/token", Box::new(user_ws_controller::get_token)),
2021-02-05 09:37:37 +00:00
2020-05-21 13:43:53 +00:00
// Account controller
2021-01-22 17:46:09 +00:00
Route::limited_post_without_login("/account/create", Box::new(account_controller::create), LimitPolicy::SUCCESS(10)),
Route::limited_post_without_login("/account/login", Box::new(account_controller::login_user), LimitPolicy::FAILURE(10)),
2020-05-24 17:19:07 +00:00
Route::post("/account/logout", Box::new(account_controller::logout_user)),
Route::post("/account/disconnect_all_devices", Box::new(account_controller::disconnect_all_devices)),
2020-05-24 15:57:47 +00:00
Route::post("/account/id", Box::new(account_controller::user_id)),
Route::post("/account/mail", Box::new(account_controller::get_mail)),
2021-01-22 17:46:09 +00:00
Route::limited_post_without_login("/account/exists_email", Box::new(account_controller::exists_mail), LimitPolicy::ANY(30)),
Route::limited_post_without_login("/account/has_security_questions", Box::new(account_controller::has_security_questions), LimitPolicy::FAILURE(10)),
Route::limited_post_without_login("/account/get_security_questions", Box::new(account_controller::get_security_questions), LimitPolicy::FAILURE(10)),
Route::limited_post_without_login("/account/check_security_answers", Box::new(account_controller::check_security_answers), LimitPolicy::FAILURE(10)),
Route::limited_post_without_login("/account/check_password_reset_token", Box::new(account_controller::check_password_reset_token), LimitPolicy::FAILURE(10)),
Route::limited_post_without_login("/account/reset_user_passwd", Box::new(account_controller::reset_user_password), LimitPolicy::FAILURE(10)),
Route::limited_post("/account/export_data", Box::new(account_controller::export_data), LimitPolicy::ANY(10)),
2020-07-14 08:13:19 +00:00
Route::post("/account/delete", Box::new(account_controller::delete_account)),
2020-05-25 11:25:51 +00:00
// User controller
2021-01-22 17:46:09 +00:00
Route::post("/user/getInfo", Box::new(user_controller::get_single)),
Route::post("/user/getInfos", Box::new(user_controller::get_single)),
Route::post("/user/getInfoMultiple", Box::new(user_controller::get_multiple)),
Route::post("/user/getInfosMultiple", Box::new(user_controller::get_multiple)),
Route::post("/user/getAdvancedUserInfo", Box::new(user_controller::get_advanced_info)),
Route::post("/user/getAdvancedUserInfos", Box::new(user_controller::get_advanced_info)),
2020-06-01 15:17:18 +00:00
2020-07-14 09:15:20 +00:00
// Settings controller
Route::post("/settings/get_general", Box::new(settings_controller::get_general)),
2020-07-14 09:36:15 +00:00
Route::post("/settings/set_general", Box::new(settings_controller::set_general)),
Route::post("/settings/check_user_directory_availability", Box::new(settings_controller::check_virtual_directory)),
2020-07-14 11:16:52 +00:00
Route::post("/settings/get_language", Box::new(settings_controller::get_language)),
2020-07-14 11:33:17 +00:00
Route::post("/settings/set_language", Box::new(settings_controller::set_language)),
2021-01-19 16:27:54 +00:00
Route::post("/settings/get_security", Box::new(settings_controller::get_security)),
2021-01-19 16:47:48 +00:00
Route::post("/settings/set_security", Box::new(settings_controller::set_security)),
2021-01-19 16:52:13 +00:00
Route::post("/settings/check_password", Box::new(settings_controller::check_password)),
2021-01-19 16:57:07 +00:00
Route::post("/settings/update_password", Box::new(settings_controller::update_password)),
2021-01-19 17:14:17 +00:00
Route::post("/settings/get_account_image", Box::new(settings_controller::get_account_image_settings)),
2021-01-19 17:27:56 +00:00
Route::post("/settings/upload_account_image", Box::new(settings_controller::upload_account_image)),
2021-01-19 17:30:51 +00:00
Route::post("/settings/delete_account_image", Box::new(settings_controller::delete_account_image)),
2021-01-19 17:48:56 +00:00
Route::post("/settings/set_account_image_visibility", Box::new(settings_controller::set_account_image_visibility)),
2021-01-20 17:31:01 +00:00
Route::post("/settings/upload_custom_emoji", Box::new(settings_controller::upload_custom_emoji)),
2021-01-20 17:46:21 +00:00
Route::post("/settings/delete_custom_emoji", Box::new(settings_controller::delete_custom_emoji)),
2021-02-15 16:38:25 +00:00
Route::post("/settings/get_data_conservation_policy", Box::new(settings_controller::get_data_conservation_policy)),
2021-02-16 17:18:49 +00:00
Route::limited_post("/settings/set_data_conservation_policy", Box::new(settings_controller::set_data_conservation_policy), LimitPolicy::FAILURE(10)),
2021-04-10 17:12:46 +00:00
Route::post("/settings/get_notifications", Box::new(settings_controller::get_notifications)),
Route::post("/settings/set_notifications", Box::new(settings_controller::set_notifications)),
2021-01-19 16:57:07 +00:00
2020-07-14 09:15:20 +00:00
// Push notifications controller
Route::post("/push_notifications/status", Box::new(push_notifications_controller::get_status)),
Route::post("/push_notifications/configure", Box::new(push_notifications_controller::configure)),
2020-06-29 13:45:26 +00:00
// Friends controller
Route::post("/friends/getList", Box::new(friends_controller::get_list)),
Route::post("/friends/get_single_infos", Box::new(friends_controller::get_single_friendship_info)),
Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)),
2020-06-30 07:50:31 +00:00
Route::post("/friends/getStatus", Box::new(friends_controller::get_status)),
2020-06-30 08:06:53 +00:00
Route::post("/friends/sendRequest", Box::new(friends_controller::send_request)),
2020-06-30 08:24:18 +00:00
Route::post("/friends/removeRequest", Box::new(friends_controller::cancel_request)),
2020-06-30 12:05:36 +00:00
Route::post("/friends/respondRequest", Box::new(friends_controller::respond_request)),
2020-06-30 12:27:31 +00:00
Route::post("/friends/remove", Box::new(friends_controller::remove_friend)),
2020-06-30 12:48:39 +00:00
Route::post("/friends/setFollowing", Box::new(friends_controller::set_following)),
2020-06-30 12:54:45 +00:00
Route::post("/friends/set_can_post_texts", Box::new(friends_controller::set_can_post_texts)),
2020-06-30 08:24:18 +00:00
// Conversations controller
Route::post("/conversations/create", Box::new(conversations_controller::create)),
2020-06-04 15:51:22 +00:00
Route::post("/conversations/getList", Box::new(conversations_controller::get_list)),
2021-03-05 11:23:24 +00:00
Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)),
2020-06-12 07:23:02 +00:00
Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)),
2021-03-07 14:06:44 +00:00
Route::post("/conversations/change_image", Box::new(conversations_controller::change_image)),
2021-03-07 14:27:34 +00:00
Route::post("/conversations/delete_image", Box::new(conversations_controller::delete_image)),
2021-03-06 16:57:04 +00:00
Route::post("/conversations/addMember", Box::new(conversations_controller::add_member)),
Route::post("/conversations/setAdmin", Box::new(conversations_controller::set_admin)),
Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)),
Route::post("/conversations/getPrivate", Box::new(conversations_controller::find_private)),
2020-06-20 05:42:18 +00:00
Route::post("/conversations/refresh_single", Box::new(conversations_controller::refresh_single)),
2020-06-22 12:16:52 +00:00
Route::post("/conversations/get_older_messages", Box::new(conversations_controller::get_older_messages)),
2020-06-20 09:55:39 +00:00
Route::post("/conversations/sendMessage", Box::new(conversations_controller::send_message)),
Route::post("/conversations/get_number_unread", Box::new(conversations_controller::count_unread)),
2020-06-22 12:41:14 +00:00
Route::post("/conversations/get_list_unread", Box::new(conversations_controller::list_unread)),
2020-06-22 17:02:34 +00:00
Route::post("/conversations/delete", Box::new(conversations_controller::delete_conversation)),
Route::post("/conversations/updateMessage", Box::new(conversations_controller::update_message)),
Route::post("/conversations/deleteMessage", Box::new(conversations_controller::delete_message)),
2020-06-01 15:17:18 +00:00
2020-06-23 13:06:26 +00:00
// Search controller
Route::post("/search/user", Box::new(search_controller::search_user)),
Route::post("/user/search", Box::new(search_controller::search_user)),
2020-06-23 13:14:17 +00:00
Route::post("/search/global", Box::new(search_controller::search_global)),
2020-06-23 13:06:26 +00:00
2020-06-23 17:04:32 +00:00
// Groups controller
Route::post("/groups/create", Box::new(groups_controller::create)),
2020-06-24 07:21:24 +00:00
Route::post("/groups/get_my_list", Box::new(groups_controller::get_list_user)),
Route::post("/groups/get_info", Box::new(groups_controller::get_info_single)),
Route::post("/groups/get_multiple_info", Box::new(groups_controller::get_info_multiple)),
Route::post("/groups/get_advanced_info", Box::new(groups_controller::get_advanced_info)),
2020-06-25 11:45:21 +00:00
Route::post("/groups/get_settings", Box::new(groups_controller::get_settings)),
2020-06-25 15:55:46 +00:00
Route::post("/groups/set_settings", Box::new(groups_controller::set_settings)),
Route::post("/groups/checkVirtualDirectory", Box::new(groups_controller::check_virtual_dir)),
2020-06-26 07:55:53 +00:00
Route::post("/groups/upload_logo", Box::new(groups_controller::upload_logo)),
2020-06-26 08:35:54 +00:00
Route::post("/groups/delete_logo", Box::new(groups_controller::delete_logo)),
2021-04-03 17:00:18 +00:00
Route::post("/groups/create_conversation", Box::new(groups_controller::create_conversation)),
2021-04-05 13:25:14 +00:00
Route::post("/groups/set_conversation_visibility", Box::new(groups_controller::set_conversation_visibility)),
2021-04-03 17:14:32 +00:00
Route::post("/groups/delete_conversation", Box::new(groups_controller::delete_conversation)),
2020-06-26 09:00:12 +00:00
Route::post("/groups/get_members", Box::new(groups_controller::get_members)),
2020-06-27 05:29:23 +00:00
Route::post("/groups/invite", Box::new(groups_controller::invite_user)),
Route::post("/groups/cancel_invitation", Box::new(groups_controller::cancel_invitation)),
2020-06-27 11:19:56 +00:00
Route::post("/groups/respond_invitation", Box::new(groups_controller::respond_invitation)),
2020-06-27 12:37:49 +00:00
Route::post("/groups/send_request", Box::new(groups_controller::send_request)),
2020-06-27 12:50:14 +00:00
Route::post("/groups/cancel_request", Box::new(groups_controller::cancel_request)),
2020-06-27 16:06:13 +00:00
Route::post("/groups/delete_member", Box::new(groups_controller::delete_member)),
2020-06-27 16:36:53 +00:00
Route::post("/groups/update_membership_level", Box::new(groups_controller::update_membership)),
Route::post("/groups/respond_request", Box::new(groups_controller::respond_request)),
2020-06-29 06:39:21 +00:00
Route::post("/groups/get_membership", Box::new(groups_controller::get_membership)),
Route::post("/groups/remove_membership", Box::new(groups_controller::remove_membership)),
2020-06-29 07:03:32 +00:00
Route::post("/groups/set_following", Box::new(groups_controller::set_following)),
2021-01-23 08:56:19 +00:00
Route::limited_post("/groups/delete", Box::new(groups_controller::delete_group), LimitPolicy::FAILURE(10)),
2020-06-29 07:10:57 +00:00
2020-07-06 07:20:31 +00:00
2020-07-02 16:19:04 +00:00
// Posts controller
Route::post("/posts/get_user", Box::new(posts_controller::get_list_user)),
2020-07-06 07:20:31 +00:00
Route::post("/posts/get_group", Box::new(posts_controller::get_list_group)),
2020-07-06 08:01:54 +00:00
Route::post("/posts/get_latest", Box::new(posts_controller::get_latest)),
Route::post("/posts/get_single", Box::new(posts_controller::get_single)),
2020-07-06 15:31:23 +00:00
Route::post("/posts/create", Box::new(posts_controller::create_post)),
Route::post("/posts/set_visibility_level", Box::new(posts_controller::set_visibility_level)),
2020-07-09 07:27:47 +00:00
Route::post("/posts/update_content", Box::new(posts_controller::update_content)),
2020-07-09 07:32:45 +00:00
Route::post("/posts/delete", Box::new(posts_controller::delete)),
Route::post("/posts/getAvailableTargets", Box::new(posts_controller::get_targets)),
2020-06-23 17:04:32 +00:00
2020-07-10 06:21:40 +00:00
// Comments controller
Route::post("/comments/create", Box::new(comments_controller::create)),
Route::post("/comments/get_single", Box::new(comments_controller::get_single)),
2020-07-10 07:02:16 +00:00
Route::post("/comments/edit", Box::new(comments_controller::edit)),
Route::post("/comments/delete", Box::new(comments_controller::delete)),
2020-07-10 06:57:46 +00:00
2020-07-10 06:21:40 +00:00
2020-07-10 08:24:39 +00:00
// Likes controller
Route::post("/likes/update", Box::new(likes_controller::update)),
2020-07-10 09:20:11 +00:00
// Surveys controller
Route::post("/surveys/get_info", Box::new(surveys_controller::get_info_single)),
2020-07-10 09:14:59 +00:00
Route::post("/surveys/send_response", Box::new(surveys_controller::send_response)),
2020-07-10 09:20:11 +00:00
Route::post("/surveys/cancel_response", Box::new(surveys_controller::cancel_response)),
2020-07-10 09:59:18 +00:00
Route::post("/surveys/create_new_choice", Box::new(surveys_controller::create_new_choice)),
2020-07-10 10:03:11 +00:00
Route::post("/surveys/block_new_choices_creation", Box::new(surveys_controller::block_new_choices_creation)),
2020-07-10 09:59:18 +00:00
// Notifications controller
Route::post("/notifications/count_unread", Box::new(notifications_controller::count_unread)),
Route::post("/notifications/count_all_news", Box::new(notifications_controller::count_all_news)),
Route::post("/notifications/get_list_unread", Box::new(notifications_controller::get_list_unread)),
Route::post("/notifications/mark_seen", Box::new(notifications_controller::mark_seen)),
Route::post("/notifications/delete_all", Box::new(notifications_controller::delete_all)),
2020-06-01 15:17:18 +00:00
// Virtual directory controller
Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
Route::post("/virtualDirectory/find", Box::new(virtual_directory_controller::find)),
2020-07-12 12:32:18 +00:00
// Web application controller
Route::post("/webApp/getMemberships", Box::new(web_app_controller::get_memberships)),
2021-04-18 12:42:45 +00:00
// Forez controller
Route::post("/forez/get_groups", Box::new(forez_controller::get_list_groups)),
Route::post("/forez/get_member_info", Box::new(forez_controller::get_member_info)),
2021-05-09 14:17:58 +00:00
// === ADMIN ROUTES ===
// Admin accounts controller
Route::limited_admin_post_without_login("/admin/accounts/auth_options", Box::new(admin_account_controller::get_auth_options), LimitPolicy::FAILURE(5)),
Route::limited_admin_post_without_login("/admin/accounts/auth_with_reset_token", Box::new(admin_account_controller::auth_with_reset_token), LimitPolicy::FAILURE(5)),
2021-05-12 16:53:48 +00:00
Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)),
2021-05-11 15:17:52 +00:00
Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)),
2021-05-11 15:41:03 +00:00
Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)),
2020-05-21 13:28:07 +00:00
]
}