]> Untitled Git - lemmy.git/commitdiff
api fixes
authorFelix <me@nutomic.com>
Thu, 23 Jan 2020 14:22:17 +0000 (15:22 +0100)
committerFelix <me@nutomic.com>
Thu, 23 Jan 2020 14:22:17 +0000 (15:22 +0100)
server/src/routes/api.rs

index 6ba627661cd00afd91305974f998769577fb0657..5121d2401f060913e5580e1d73d46a28f72ee97f 100644 (file)
@@ -16,52 +16,52 @@ type DbParam = web::Data<Pool<ConnectionManager<PgConnection>>>;
 pub fn config(cfg: &mut web::ServiceConfig) {
   cfg
     // Site
-    .route("/api/v1/site", web::get().to(route::<GetSite, GetSiteResponse>))
-    .route("/api/v1/categories", web::get().to(route::<ListCategories, ListCategoriesResponse>))
-    .route("/api/v1/modlog", web::get().to(route::<GetModlog, GetModlogResponse>))
-    .route("/api/v1/search", web::post().to(route::<Search, SearchResponse>))
+    .route("/api/v1/site", web::get().to(route_get::<GetSite, GetSiteResponse>))
+    .route("/api/v1/categories", web::get().to(route_get::<ListCategories, ListCategoriesResponse>))
+    .route("/api/v1/modlog", web::get().to(route_get::<GetModlog, GetModlogResponse>))
+    .route("/api/v1/search", web::get().to(route_get::<Search, SearchResponse>))
     // Community
-    .route("/api/v1/community", web::post().to(route::<CreateCommunity, CommunityResponse>))
-    .route("/api/v1/community", web::get().to(route::<GetCommunity, GetCommunityResponse>))
-    .route("/api/v1/community", web::put().to(route::<EditCommunity, CommunityResponse>))
-    .route("/api/v1/community/list", web::get().to(route::<ListCommunities, ListCommunitiesResponse>))
-    .route("/api/v1/community/follow", web::post().to(route::<FollowCommunity, CommunityResponse>))
+    .route("/api/v1/community", web::post().to(route_post::<CreateCommunity, CommunityResponse>))
+    .route("/api/v1/community", web::get().to(route_get::<GetCommunity, GetCommunityResponse>))
+    .route("/api/v1/community", web::put().to(route_post::<EditCommunity, CommunityResponse>))
+    .route("/api/v1/community/list", web::get().to(route_get::<ListCommunities, ListCommunitiesResponse>))
+    .route("/api/v1/community/follow", web::post().to(route_post::<FollowCommunity, CommunityResponse>))
     // Post
-    .route("/api/v1/post", web::post().to(route::<CreatePost, PostResponse>))
-    .route("/api/v1/post", web::put().to(route::<EditPost, PostResponse>))
-    .route("/api/v1/post", web::get().to(route::<GetPost, GetPostResponse>))
-    .route("/api/v1/post/list", web::get().to(route::<GetPosts, GetPostsResponse>))
-    .route("/api/v1/post/like", web::post().to(route::<CreatePostLike, CreatePostLikeResponse>))
-    .route("/api/v1/post/save", web::post().to(route::<SavePost, PostResponse>))
-    .route("/api/v1/post/replies", web::get().to(route::<GetReplies, GetRepliesResponse>))
+    .route("/api/v1/post", web::post().to(route_post::<CreatePost, PostResponse>))
+    .route("/api/v1/post", web::put().to(route_post::<EditPost, PostResponse>))
+    .route("/api/v1/post", web::get().to(route_get::<GetPost, GetPostResponse>))
+    .route("/api/v1/post/list", web::get().to(route_get::<GetPosts, GetPostsResponse>))
+    .route("/api/v1/post/like", web::post().to(route_post::<CreatePostLike, CreatePostLikeResponse>))
+    .route("/api/v1/post/save", web::put().to(route_post::<SavePost, PostResponse>))
     // Comment
-    .route("/api/v1/comment", web::post().to(route::<CreateComment, CommentResponse>))
-    .route("/api/v1/comment", web::put().to(route::<EditComment, CommentResponse>))
-    .route("/api/v1/comment/like", web::post().to(route::<CreateCommentLike, CommentResponse>))
-    .route("/api/v1/comment/save", web::post().to(route::<SaveComment, CommentResponse>))
+    .route("/api/v1/comment", web::post().to(route_post::<CreateComment, CommentResponse>))
+    .route("/api/v1/comment", web::put().to(route_post::<EditComment, CommentResponse>))
+    .route("/api/v1/comment/like", web::post().to(route_post::<CreateCommentLike, CommentResponse>))
+    .route("/api/v1/comment/save", web::put().to(route_post::<SaveComment, CommentResponse>))
     // User
-    .route("/api/v1/user", web::get().to(route::<GetUserDetails, GetUserDetailsResponse>))
-    .route("/api/v1/user/mentions", web::get().to(route::<GetUserMentions, GetUserMentionsResponse>))
-    .route("/api/v1/user/mentions", web::put().to(route::<EditUserMention, UserMentionResponse>))
-    .route("/api/v1/user/followed-communities", web::get().to(route::<GetFollowedCommunities, GetFollowedCommunitiesResponse>))
+    .route("/api/v1/user", web::get().to(route_get::<GetUserDetails, GetUserDetailsResponse>))
+    .route("/api/v1/user/mention", web::get().to(route_get::<GetUserMentions, GetUserMentionsResponse>))
+    .route("/api/v1/user/mention", web::put().to(route_post::<EditUserMention, UserMentionResponse>))
+    .route("/api/v1/user/replies", web::get().to(route_get::<GetReplies, GetRepliesResponse>))
+    .route("/api/v1/user/followed_communities", web::get().to(route_get::<GetFollowedCommunities, GetFollowedCommunitiesResponse>))
     // Mod actions
-    .route("/api/v1/community/transfer", web::post().to(route::<TransferCommunity, GetCommunityResponse>))
-    .route("/api/v1/community/ban-user", web::post().to(route::<BanFromCommunity, BanFromCommunityResponse>))
-    .route("/api/v1/community/mod", web::post().to(route::<AddModToCommunity, AddModToCommunityResponse>))
+    .route("/api/v1/community/transfer", web::post().to(route_post::<TransferCommunity, GetCommunityResponse>))
+    .route("/api/v1/community/ban_user", web::post().to(route_post::<BanFromCommunity, BanFromCommunityResponse>))
+    .route("/api/v1/community/mod", web::post().to(route_post::<AddModToCommunity, AddModToCommunityResponse>))
     // Admin actions
-    .route("/api/v1/site", web::post().to(route::<CreateSite, SiteResponse>))
-    .route("/api/v1/site", web::put().to(route::<EditSite, SiteResponse>))
-    .route("/api/v1/site/transfer", web::post().to(route::<TransferSite, GetSiteResponse>))
-    .route("/api/v1/admin/add", web::post().to(route::<AddAdmin, AddAdminResponse>))
-    .route("/api/v1/user/ban", web::post().to(route::<BanUser, BanUserResponse>))
+    .route("/api/v1/site", web::post().to(route_post::<CreateSite, SiteResponse>))
+    .route("/api/v1/site", web::put().to(route_post::<EditSite, SiteResponse>))
+    .route("/api/v1/site/transfer", web::post().to(route_post::<TransferSite, GetSiteResponse>))
+    .route("/api/v1/admin/add", web::post().to(route_post::<AddAdmin, AddAdminResponse>))
+    .route("/api/v1/user/ban", web::post().to(route_post::<BanUser, BanUserResponse>))
     // User account actions
-    .route("/api/v1/user/login", web::post().to(route::<Login, LoginResponse>))
-    .route("/api/v1/user/register", web::post().to(route::<Register, LoginResponse>))
-    .route("/api/v1/user/delete_account", web::post().to(route::<DeleteAccount, LoginResponse>))
-    .route("/api/v1/user/password_reset", web::post().to(route::<PasswordReset, PasswordResetResponse>))
-    .route("/api/v1/user/password_change", web::post().to(route::<PasswordChange, LoginResponse>))
-    .route("/api/v1/user/mark_all_as_read", web::post().to(route::<MarkAllAsRead, GetRepliesResponse>))
-    .route("/api/v1/user/save_user_settings", web::post().to(route::<SaveUserSettings, LoginResponse>));
+    .route("/api/v1/user/login", web::post().to(route_post::<Login, LoginResponse>))
+    .route("/api/v1/user/register", web::post().to(route_post::<Register, LoginResponse>))
+    .route("/api/v1/user/delete_account", web::post().to(route_post::<DeleteAccount, LoginResponse>))
+    .route("/api/v1/user/password_reset", web::post().to(route_post::<PasswordReset, PasswordResetResponse>))
+    .route("/api/v1/user/password_change", web::post().to(route_post::<PasswordChange, LoginResponse>))
+    .route("/api/v1/user/mark_all_as_read", web::post().to(route_post::<MarkAllAsRead, GetRepliesResponse>))
+    .route("/api/v1/user/save_user_settings", web::put().to(route_post::<SaveUserSettings, LoginResponse>));
 }
 
 fn perform<Request, Response>(data: Request, db: DbParam) -> Result<HttpResponse, Error>
@@ -78,7 +78,22 @@ where
   Ok(HttpResponse::Ok().json(response?))
 }
 
-async fn route<Data, Response>(data: web::Query<Data>, db: DbParam) -> Result<HttpResponse, Error>
+async fn route_get<Data, Response>(
+  data: web::Query<Data>,
+  db: DbParam,
+) -> Result<HttpResponse, Error>
+where
+  Data: Serialize,
+  Response: Serialize,
+  Oper<Data>: Perform<Response>,
+{
+  perform::<Data, Response>(data.0, db)
+}
+
+async fn route_post<Data, Response>(
+  data: web::Json<Data>,
+  db: DbParam,
+) -> Result<HttpResponse, Error>
 where
   Data: Serialize,
   Response: Serialize,