1 use crate::api::comment::*;
2 use crate::api::community::*;
3 use crate::api::post::*;
4 use crate::api::site::*;
5 use crate::api::user::*;
6 use crate::api::{Oper, Perform};
7 use actix_web::{web, HttpResponse};
8 use diesel::r2d2::{ConnectionManager, Pool};
9 use diesel::PgConnection;
13 type DbParam = web::Data<Pool<ConnectionManager<PgConnection>>>;
16 pub fn config(cfg: &mut web::ServiceConfig) {
19 .route("/api/v1/site", web::get().to(route_get::<GetSite, GetSiteResponse>))
20 .route("/api/v1/categories", web::get().to(route_get::<ListCategories, ListCategoriesResponse>))
21 .route("/api/v1/modlog", web::get().to(route_get::<GetModlog, GetModlogResponse>))
22 .route("/api/v1/search", web::get().to(route_get::<Search, SearchResponse>))
24 .route("/api/v1/community", web::post().to(route_post::<CreateCommunity, CommunityResponse>))
25 .route("/api/v1/community", web::get().to(route_get::<GetCommunity, GetCommunityResponse>))
26 .route("/api/v1/community", web::put().to(route_post::<EditCommunity, CommunityResponse>))
27 .route("/api/v1/community/list", web::get().to(route_get::<ListCommunities, ListCommunitiesResponse>))
28 .route("/api/v1/community/follow", web::post().to(route_post::<FollowCommunity, CommunityResponse>))
30 .route("/api/v1/post", web::post().to(route_post::<CreatePost, PostResponse>))
31 .route("/api/v1/post", web::put().to(route_post::<EditPost, PostResponse>))
32 .route("/api/v1/post", web::get().to(route_get::<GetPost, GetPostResponse>))
33 .route("/api/v1/post/list", web::get().to(route_get::<GetPosts, GetPostsResponse>))
34 .route("/api/v1/post/like", web::post().to(route_post::<CreatePostLike, PostResponse>))
35 .route("/api/v1/post/save", web::put().to(route_post::<SavePost, PostResponse>))
37 .route("/api/v1/comment", web::post().to(route_post::<CreateComment, CommentResponse>))
38 .route("/api/v1/comment", web::put().to(route_post::<EditComment, CommentResponse>))
39 .route("/api/v1/comment/like", web::post().to(route_post::<CreateCommentLike, CommentResponse>))
40 .route("/api/v1/comment/save", web::put().to(route_post::<SaveComment, CommentResponse>))
42 .route("/api/v1/user", web::get().to(route_get::<GetUserDetails, GetUserDetailsResponse>))
43 .route("/api/v1/user/mention", web::get().to(route_get::<GetUserMentions, GetUserMentionsResponse>))
44 .route("/api/v1/user/mention", web::put().to(route_post::<EditUserMention, UserMentionResponse>))
45 .route("/api/v1/user/replies", web::get().to(route_get::<GetReplies, GetRepliesResponse>))
46 .route("/api/v1/user/followed_communities", web::get().to(route_get::<GetFollowedCommunities, GetFollowedCommunitiesResponse>))
48 .route("/api/v1/community/transfer", web::post().to(route_post::<TransferCommunity, GetCommunityResponse>))
49 .route("/api/v1/community/ban_user", web::post().to(route_post::<BanFromCommunity, BanFromCommunityResponse>))
50 .route("/api/v1/community/mod", web::post().to(route_post::<AddModToCommunity, AddModToCommunityResponse>))
52 .route("/api/v1/site", web::post().to(route_post::<CreateSite, SiteResponse>))
53 .route("/api/v1/site", web::put().to(route_post::<EditSite, SiteResponse>))
54 .route("/api/v1/site/transfer", web::post().to(route_post::<TransferSite, GetSiteResponse>))
55 .route("/api/v1/admin/add", web::post().to(route_post::<AddAdmin, AddAdminResponse>))
56 .route("/api/v1/user/ban", web::post().to(route_post::<BanUser, BanUserResponse>))
57 // User account actions
58 .route("/api/v1/user/login", web::post().to(route_post::<Login, LoginResponse>))
59 .route("/api/v1/user/register", web::post().to(route_post::<Register, LoginResponse>))
60 .route("/api/v1/user/delete_account", web::post().to(route_post::<DeleteAccount, LoginResponse>))
61 .route("/api/v1/user/password_reset", web::post().to(route_post::<PasswordReset, PasswordResetResponse>))
62 .route("/api/v1/user/password_change", web::post().to(route_post::<PasswordChange, LoginResponse>))
63 .route("/api/v1/user/mark_all_as_read", web::post().to(route_post::<MarkAllAsRead, GetRepliesResponse>))
64 .route("/api/v1/user/save_user_settings", web::put().to(route_post::<SaveUserSettings, LoginResponse>));
67 fn perform<Request, Response>(data: Request, db: DbParam) -> Result<HttpResponse, Error>
70 Oper<Request>: Perform<Response>,
72 let conn = match db.get() {
74 Err(e) => return Err(format_err!("{}", e)),
76 let oper: Oper<Request> = Oper::new(data);
77 let response = oper.perform(&conn);
78 Ok(HttpResponse::Ok().json(response?))
81 async fn route_get<Data, Response>(
82 data: web::Query<Data>,
84 ) -> Result<HttpResponse, Error>
88 Oper<Data>: Perform<Response>,
90 perform::<Data, Response>(data.0, db)
93 async fn route_post<Data, Response>(
94 data: web::Json<Data>,
96 ) -> Result<HttpResponse, Error>
100 Oper<Data>: Perform<Response>,
102 perform::<Data, Response>(data.0, db)