1#![warn(missing_docs)]
2#![doc(
3 html_favicon_url = "https://raw.githubusercontent.com/LemmyNet/lemmy-ui/main/src/assets/icons/favicon.svg"
4)]
5#![doc(
6 html_logo_url = "https://raw.githubusercontent.com/LemmyNet/lemmy-ui/main/src/assets/icons/favicon.svg"
7)]
8use std::collections::HashMap;
30
31use crate::{lemmy_client_trait::LemmyClientInternal, response::LemmyResult};
32use cfg_if::cfg_if;
33use lemmy_api_common::{
34 comment::*, community::*, custom_emoji::*, lemmy_db_schema::source::login_token::LoginToken,
35 person::*, post::*, private_message::*, site::*, SuccessResponse,
36};
37#[cfg(not(target_family = "wasm"))]
38use lemmy_client_internal::ClientWrapper;
39#[cfg(target_family = "wasm")]
40use lemmy_client_internal::Fetch;
41
42mod form;
43mod lemmy_client_internal;
44mod lemmy_client_trait;
45mod response;
46mod utils;
47
48pub use form::LemmyRequest;
49pub use lemmy_api_common;
50pub use utils::ClientOptions;
51
52pub struct LemmyClient {
54 headers: HashMap<String, String>,
55 #[cfg(target_family = "wasm")]
56 client: Fetch,
57 #[cfg(not(target_family = "wasm"))]
58 client: ClientWrapper,
59}
60
61impl LemmyClient {
62 pub fn options(&self) -> &ClientOptions {
64 cfg_if! {
65 if #[cfg(target_family = "wasm")] {
66 &self.client.0
67 } else {
68 &self.client.options
69 }
70 }
71 }
72}
73
74macro_rules! expose_wrapped_fn {
75 ($name:ident, $form:ty, $response:ty, $doc:expr) => {
76 #[doc = $doc]
77 pub async fn $name<Request>(&self, request: Request) -> LemmyResult<$response>
78 where
79 Request: Into<LemmyRequest<$form>>,
80 {
81 self.client.$name(request.into(), &self.headers).await
82 }
83 };
84}
85
86impl LemmyClient {
87 pub fn new(options: ClientOptions) -> Self {
97 cfg_if! {
98 if #[cfg(target_family = "wasm")] {
99 Self {
100 client: Fetch::new(options),
101 headers: HashMap::new()
102 }
103 } else {
104 Self {
105 client: ClientWrapper::new(options),
106 headers: HashMap::new()
107 }
108 }
109 }
110 }
111
112 pub fn headers(&self) -> &HashMap<String, String> {
114 &self.headers
115 }
116
117 pub fn headers_mut(&mut self) -> &mut HashMap<String, String> {
119 &mut self.headers
120 }
121
122 expose_wrapped_fn!(
123 get_site,
124 (),
125 GetSiteResponse,
126 r#"Gets the site and, if you pass an authorized JWT, information about the logged in user.
127
128HTTP GET /site"#
129 );
130 expose_wrapped_fn!(
131 create_site,
132 CreateSite,
133 SiteResponse,
134 r#"Creates site during initial setup.
135
136HTTP POST /site"#
137 );
138 expose_wrapped_fn!(
139 edit_site,
140 EditSite,
141 SiteResponse,
142 r#"Edits your site.
143
144HTTP PUT /site"#
145 );
146 expose_wrapped_fn!(
147 block_instance,
148 BlockInstance,
149 BlockInstanceResponse,
150 r#"Blocks an instance.
151
152HTTP POST /site/block"#
153 );
154 expose_wrapped_fn!(
155 get_modlog,
156 GetModlog,
157 GetModlogResponse,
158 r#"Gets the modlog.
159
160HTTP GET /modlog"#
161 );
162 expose_wrapped_fn!(
163 search,
164 Search,
165 SearchResponse,
166 r#"Searches for content.
167
168HTTP GET /search"#
169 );
170 expose_wrapped_fn!(
171 resolve_object,
172 ResolveObject,
173 ResolveObjectResponse,
174 r#"Fetches an object from a non-local instance.
175
176HTTP GET /resolve_object"#
177 );
178 expose_wrapped_fn!(
179 get_community,
180 GetCommunity,
181 GetCommunityResponse,
182 r#"Gets a community.
183
184HTTP GET /community"#
185 );
186 expose_wrapped_fn!(
187 create_community,
188 CreateCommunity,
189 CommunityResponse,
190 r#"Creates a new community.
191
192HTTP POST /community"#
193 );
194 expose_wrapped_fn!(
195 edit_community,
196 EditCommunity,
197 CommunityResponse,
198 r#"Edits a community.
199
200HTTP PUT /community"#
201 );
202 expose_wrapped_fn!(
203 hide_community,
204 HideCommunity,
205 SuccessResponse,
206 r#"Hides a community from public view.
207
208HTTP PUT /community_hide"#
209 );
210 expose_wrapped_fn!(
211 list_communities,
212 ListCommunities,
213 ListCommunitiesResponse,
214 r#"Lists communities.
215
216HTTP GET /community/list"#
217 );
218 expose_wrapped_fn!(
219 follow_community,
220 FollowCommunity,
221 CommunityResponse,
222 r#"Subscribes to a community.
223
224HTTP POST /community/follow"#
225 );
226 expose_wrapped_fn!(
227 block_community,
228 BlockCommunity,
229 BlockCommunityResponse,
230 r#"Blocks a community.
231
232HTTP POST /community/block"#
233 );
234 expose_wrapped_fn!(
235 delete_community,
236 DeleteCommunity,
237 CommunityResponse,
238 r#"Deletes a community.
239
240HTTP POST /community/delete"#
241 );
242 expose_wrapped_fn!(
243 remove_community,
244 RemoveCommunity,
245 CommunityResponse,
246 r#"Removes a community (moderation action).
247
248HTTP POST /community/remove"#
249 );
250 expose_wrapped_fn!(
251 transfer_community,
252 TransferCommunity,
253 GetCommunityResponse,
254 r#"Transfers a community you own to another user on that community's moderation team.
255
256HTTP POST community/transfer"#
257 );
258 expose_wrapped_fn!(
259 ban_from_community,
260 BanFromCommunity,
261 BanFromCommunityResponse,
262 r#"Bans a user from a community.
263
264HTTP POST /community/ban_user"#
265 );
266 expose_wrapped_fn!(
267 add_mod_to_community,
268 AddModToCommunity,
269 AddModToCommunityResponse,
270 r#"Adds a moderator to your community.
271
272HTTP POST /community/mod"#
273 );
274 expose_wrapped_fn!(
275 get_federated_instances,
276 FederatedInstances,
277 GetFederatedInstancesResponse,
278 r#"Gets the instances that are federated with your instance.
279
280HTTP GET /federated_instances"#
281 );
282 expose_wrapped_fn!(
283 get_post,
284 GetPost,
285 GetPostResponse,
286 r#"Gets post.
287
288HTTP GET /post"#
289 );
290 expose_wrapped_fn!(
291 create_post,
292 CreatePost,
293 PostResponse,
294 r#"Creates a post.
295
296HTTP POST /post"#
297 );
298 expose_wrapped_fn!(
299 edit_post,
300 EditPost,
301 PostResponse,
302 r#"Edits a post you have already created.
303
304HTTP PUT /post"#
305 );
306 expose_wrapped_fn!(
307 delete_post,
308 DeletePost,
309 PostResponse,
310 r#"Deletes a post you have made.
311
312HTTP POST /post/delete"#
313 );
314 expose_wrapped_fn!(
315 remove_post,
316 RemovePost,
317 PostResponse,
318 r#"Removes a post (moderator action).
319
320HTTP POST /post/remove"#
321 );
322 expose_wrapped_fn!(
323 mark_post_as_read,
324 MarkPostAsRead,
325 SuccessResponse,
326 r#"Marks a post as read.
327
328HTTP POST /post/mark_as_read"#
329 );
330 expose_wrapped_fn!(
331 lock_post,
332 LockPost,
333 PostResponse,
334 r#"Prevents users from commenting on the post (moderator action).
335
336HTTP POST /post/lock"#
337 );
338 expose_wrapped_fn!(
339 feature_post,
340 FeaturePost,
341 PostResponse,
342 r#"Pins a post to the top of the community page (moderator action).
343
344HTTP POST /post/feature"#
345 );
346 expose_wrapped_fn!(
347 list_posts,
348 GetPosts,
349 GetPostsResponse,
350 r#"Gets posts with a variety of filters.
351
352HTTP GET /post/list"#
353 );
354 expose_wrapped_fn!(
355 like_post,
356 CreatePostLike,
357 PostResponse,
358 r#"Votes on a post.
359
360HTTP POST /post/like"#
361 );
362 expose_wrapped_fn!(
363 list_post_likes,
364 ListPostLikes,
365 ListPostLikesResponse,
366 r#"Lists the likes for a post.
367
368HTTP GET /post/like/list"#
369 );
370 expose_wrapped_fn!(
371 save_post,
372 SavePost,
373 PostResponse,
374 r#"Saves a post to your favorites list.
375
376HTTP PUT /post/save"#
377 );
378 expose_wrapped_fn!(
379 report_post,
380 CreatePostReport,
381 PostReportResponse,
382 r#"Reports a post to the moderator team of the community the post is in, the admin team of your instance, and the admin team of the poster's instance.
383
384HTTP POST /post/report"#
385 );
386 expose_wrapped_fn!(
387 resolve_post_report,
388 ResolvePostReport,
389 PostReportResponse,
390 r#"Resolves a post report (moderator action).
391
392HTTP PUT /post/report/resolve"#
393 );
394 expose_wrapped_fn!(
395 list_post_reports,
396 ListPostReports,
397 ListPostReportsResponse,
398 r#"Gets reports of posts that you are able to moderate.
399
400HTTP GET /post/report/list"#
401 );
402 expose_wrapped_fn!(
403 get_post_url_metadata,
404 GetSiteMetadata,
405 GetSiteMetadataResponse,
406 r#"Gets the metadata of a given site.
407
408HTTP POST /post/site_metadata"#
409 );
410 expose_wrapped_fn!(
411 hide_post,
412 HidePost,
413 SuccessResponse,
414 r#"Hide a post from list views.
415
416HTTP POST /post/hide"#
417 );
418 expose_wrapped_fn!(
419 get_comment,
420 GetComment,
421 CommentResponse,
422 r#"Gets a comment.
423
424HTTP GET /comment"#
425 );
426 expose_wrapped_fn!(
427 create_comment,
428 CreateComment,
429 CommentResponse,
430 r#"Creates a new comment.
431
432HTTP POST /comment"#
433 );
434 expose_wrapped_fn!(
435 edit_comment,
436 EditComment,
437 CommentResponse,
438 r#"Edits one of your already-created comments.
439
440HTTP PUT /comment"#
441 );
442 expose_wrapped_fn!(
443 delete_comment,
444 DeleteComment,
445 CommentResponse,
446 r#"Deletes one of your already-existing comments.
447
448HTTP POST /comment/delete"#
449 );
450 expose_wrapped_fn!(
451 remove_comment,
452 RemoveComment,
453 CommentResponse,
454 r#"Removes a post (moderator action).
455
456HTTP POST /comment/remove"#
457 );
458 expose_wrapped_fn!(
459 mark_reply_as_read,
460 MarkCommentReplyAsRead,
461 CommentReplyResponse,
462 r#"Marks a reply to one of your posts or comments as read.
463
464HTTP POST /comment/mark_as_read"#
465 );
466 expose_wrapped_fn!(
467 distinguish_comment,
468 DistinguishComment,
469 CommentResponse,
470 r#"Pins a comment to the top of a post's comment section (speak as moderator).
471
472HTTP POST /comment/distinguish"#
473 );
474 expose_wrapped_fn!(
475 like_comment,
476 CreateCommentLike,
477 CommentResponse,
478 r#"Votes on a comment.
479
480HTTP POST /comment/like"#
481 );
482 expose_wrapped_fn!(
483 list_comment_likes,
484 ListCommentLikes,
485 ListCommentLikesResponse,
486 r#"Gets the votes for a comment.
487
488HTTP GET /comment/like/list"#
489 );
490 expose_wrapped_fn!(
491 save_comment,
492 SaveComment,
493 CommentResponse,
494 r#"Saves a comment to your favorites list.
495
496HTTP PUT /comment/save"#
497 );
498 expose_wrapped_fn!(
499 list_comments,
500 GetComments,
501 GetCommentsResponse,
502 r#"Gets comments with various filters.
503
504HTTP GET /comment/list"#
505 );
506 expose_wrapped_fn!(
507 create_comment_report,
508 CreateCommentReport,
509 CommentResponse,
510 r#"Reports a comment to the moderator team of the community the comment is in, your instance's admin team, and the commentor's instance's admin team.
511
512HTTP POST /comment/report"#
513 );
514 expose_wrapped_fn!(
515 resolve_comment_report,
516 ResolveCommentReport,
517 CommentReportResponse,
518 r#"Resolves a report on a comment made in a community you moderate or instance you administrate.
519
520HTTP PUT /comment/report/resolve"#
521 );
522 expose_wrapped_fn!(
523 list_comment_reports,
524 ListCommentReports,
525 ListCommentReportsResponse,
526 r#"Lists reports for comments in communities you moderate or instances you adminstrate.
527
528HTTP GET /comment/report/list"#
529 );
530 expose_wrapped_fn!(
531 create_private_message,
532 CreatePrivateMessage,
533 PrivateMessageResponse,
534 r#"Creates and send a private message to another user.
535
536HTTP POST /private_message"#
537 );
538 expose_wrapped_fn!(
539 edit_private_message,
540 EditPrivateMessage,
541 PrivateMessageResponse,
542 r#"Edits a private message you have already sent.
543
544HTTP PUT /private_message"#
545 );
546 expose_wrapped_fn!(
547 list_private_messages,
548 GetPrivateMessages,
549 PrivateMessagesResponse,
550 r#"Lists private messages that have been sent to you.
551
552HTTP GET /private_message/list"#
553 );
554 expose_wrapped_fn!(
555 delete_private_message,
556 DeletePrivateMessage,
557 PrivateMessageResponse,
558 r#"Deletes a private that you have already sent.
559
560HTTP POST /private_message/delete"#
561 );
562 expose_wrapped_fn!(
563 mark_private_message_as_read,
564 MarkPrivateMessageAsRead,
565 PrivateMessageResponse,
566 r#"Marks a private message that was sent to you as read.
567
568HTTP POST /private_message/mark_as_read"#
569 );
570 expose_wrapped_fn!(
571 create_private_message_report,
572 CreatePrivateMessageReport,
573 PrivateMessageReportResponse,
574 r#"Reports a private message that was sent to you to your instance's admin team and the sender's instance's admin team.
575
576HTTP POST /private_message/report"#
577 );
578 expose_wrapped_fn!(
579 resolve_private_message_report,
580 ResolvePrivateMessageReport,
581 PrivateMessageReportResponse,
582 r#"Resolves a report of a private message sent to a user on the instance you administrate.
583
584HTTP PUT /private_message/report/resolve"#
585 );
586 expose_wrapped_fn!(
587 list_private_message_reports,
588 ListPrivateMessageReports,
589 ListPrivateMessageReportsResponse,
590 r#"Lists reports of private messages received on the isntance you administrate.
591
592HTTP GET /private_message/report/list"#
593 );
594 expose_wrapped_fn!(
595 get_person,
596 GetPersonDetails,
597 GetPersonDetailsResponse,
598 r#"Gets the publicly viewable details of a user's account.
599
600HTTP GET /user"#
601 );
602 expose_wrapped_fn!(
603 register_account,
604 Register,
605 RegistrationApplicationResponse,
606 r#"Registers a new account on an instance.
607
608HTTP POST /user/register"#
609 );
610 expose_wrapped_fn!(
611 get_captcha,
612 (),
613 GetCaptchaResponse,
614 r#"Gets a captcha.
615
616HTTP GET /user/get_captcha"#
617 );
618 expose_wrapped_fn!(
619 export_settings,
620 (),
621 String,
622 r#"Exports a backup of your user settings - including your saved content, followed communities, and blocks - as JSON.
623
624HTTP GET /user/export_settings"#
625 );
626 expose_wrapped_fn!(
627 import_settings,
628 String,
629 SuccessResponse,
630 r#"Imports a backup of your user settings.
631
632HTTP POST /user/import_settings"#
633 );
634 expose_wrapped_fn!(
635 list_mentions,
636 GetPersonMentions,
637 GetPersonMentionsResponse,
638 r#"Gets mentions of the authenticated user.
639
640HTTP GET /user/mention"#
641 );
642 expose_wrapped_fn!(
643 mark_mention_as_read,
644 MarkPersonMentionAsRead,
645 PersonMentionResponse,
646 r#"Marks a mention as read.
647
648HTTP POST /user/mention/mark_as_read"#
649 );
650 expose_wrapped_fn!(
651 list_replies,
652 GetReplies,
653 GetRepliesResponse,
654 r#"Gets replies to your posts and comments.
655
656HTTP GET /user/replies"#
657 );
658 expose_wrapped_fn!(
659 ban_from_site,
660 BanPerson,
661 BanPersonResponse,
662 r#"Bans a person from your instance.
663
664HTTP POST /user/ban"#
665 );
666 expose_wrapped_fn!(
667 list_banned_users,
668 (),
669 BannedPersonsResponse,
670 r#"Gets users banned who are banned from your isntance.
671
672HTTP GET /user/banned"#
673 );
674 expose_wrapped_fn!(
675 block_person,
676 BlockPerson,
677 BlockPersonResponse,
678 r#"Blocks a person.
679
680HTTP POST /user/block"#
681 );
682 expose_wrapped_fn!(
683 login,
684 Login,
685 LoginResponse,
686 r#"Logs into the instance, giving you a JWT to use to make authorized requests.
687
688HTTP POST /user/login"#
689 );
690 expose_wrapped_fn!(
691 logout,
692 (),
693 SuccessResponse,
694 r#"Deletes the active session associated with the JWT.
695
696HTTP POST /user/logout"#
697 );
698 expose_wrapped_fn!(
699 delete_account,
700 DeleteAccount,
701 SuccessResponse,
702 r#"Deletes your account.
703
704HTTP POST /user/delete_account"#
705 );
706 expose_wrapped_fn!(
707 reset_password,
708 PasswordReset,
709 SuccessResponse,
710 r#"Sends an email to your account (if you have one) with a one time link to change your password. Use this if you forgot your password.
711
712HTTP POST /user/password_reset"#
713 );
714 expose_wrapped_fn!(
715 change_password_after_reset,
716 PasswordChangeAfterReset,
717 SuccessResponse,
718 r#"Follows through with one time link password reset request.
719
720HTTP POST /user/password_change"#
721 );
722 expose_wrapped_fn!(
723 mark_all_notifications_as_read,
724 (),
725 GetRepliesResponse,
726 r#"Marks all notifications (replies, mentions, private messages) as read.
727
728HTTP POST /user/mark_all_as_read"#
729 );
730 expose_wrapped_fn!(
731 save_user_settings,
732 SaveUserSettings,
733 SuccessResponse,
734 r#"Saves your account settings.
735
736HTTP PUT /user/save_user_settings"#
737 );
738 expose_wrapped_fn!(
739 change_password,
740 ChangePassword,
741 LoginResponse,
742 r#"Changes your password if you are already logged in.
743
744HTTP PUT /user/change_password"#
745 );
746 expose_wrapped_fn!(
747 report_count,
748 GetReportCount,
749 GetReportCountResponse,
750 r#"Gets number of reports you can resolve.
751
752HTTP GET /user/report_count"#
753 );
754 expose_wrapped_fn!(
755 unread_count,
756 (),
757 GetUnreadCountResponse,
758 r#"Gets the number of unread notifications.
759
760HTTP GET /user/unread_count"#
761 );
762 expose_wrapped_fn!(
763 verify_email,
764 VerifyEmail,
765 SuccessResponse,
766 r#"Verifies your email. Used when the instance you are registering an account on requires email verification.
767
768HTTP POST /user/verify_email"#
769 );
770 expose_wrapped_fn!(
771 leave_admin,
772 (),
773 GetSiteResponse,
774 r#"Leave your instance's admin team.
775
776HTTP POST /user/leave_admin"#
777 );
778 expose_wrapped_fn!(
779 generate_totp_secret,
780 (),
781 GenerateTotpSecretResponse,
782 r#"Generates a secret to enable time-based one time passwords for two-factor authentication.
783
784After this, you will need to call /user/totp/update with a vaild token to enable it.
785
786HTTP POST /user/totp/generate"#
787 );
788 expose_wrapped_fn!(
789 update_totp,
790 UpdateTotp,
791 UpdateTotpResponse,
792 r#"Enables/disables two-factor authentivation.
793
794To enable, you must first call /user/totp/generate to generate a token to pass to this.
795
796You can only disable this if it is already enabled. Again, you must pass a valid TOTP.
797
798HTTP POST /user/totp/update"#
799 );
800 expose_wrapped_fn!(
801 list_logins,
802 (),
803 Vec<LoginToken>,
804 r#"Lists login tokens for your user's active sessions.
805
806HTTP GET /user/list_logins"#
807 );
808 expose_wrapped_fn!(
809 validate_auth,
810 (),
811 SuccessResponse,
812 r#"Returns an error message if your auth token is invalid.
813
814HTTP GET /user/validate_auth"#
815 );
816 expose_wrapped_fn!(
817 add_admin,
818 AddAdmin,
819 AddAdminResponse,
820 r#"Adds a user to your instance's admin team.
821
822HTTP POST admin/add"#
823 );
824 expose_wrapped_fn!(
825 unread_registration_application_count,
826 (),
827 GetUnreadRegistrationApplicationCountResponse,
828 r#"Gets the number of unread registration applications for the instance you administrate.
829
830HTTP GET /admin/registration_application/count"#
831 );
832 expose_wrapped_fn!(
833 get_registration_aplication,
834 GetRegistrationApplication,
835 RegistrationApplicationResponse,
836 r#"Get the application a user submitted when they first registered their account
837
838HTTP GET /admin/registration_application"#
839 );
840 expose_wrapped_fn!(
841 list_registration_applications,
842 ListRegistrationApplications,
843 ListRegistrationApplicationsResponse,
844 r#"Gets applications to register an account on the instance you administer.
845
846HTTP GET /admin/registration_application/list"#
847 );
848 expose_wrapped_fn!(
849 approve_registration_application,
850 ApproveRegistrationApplication,
851 RegistrationApplicationResponse,
852 r#"Approves a pending registration application.
853
854HTTP PUT /admin/registration_application/approve"#
855 );
856 expose_wrapped_fn!(
857 purge_person,
858 PurgePerson,
859 SuccessResponse,
860 r#"Purges a user from the database.
861
862HTTP POST /admin/purge/person"#
863 );
864 expose_wrapped_fn!(
865 purge_community,
866 PurgeCommunity,
867 SuccessResponse,
868 r#"Purges a community from the database.
869
870HTTP POST /admin/purge/community"#
871 );
872 expose_wrapped_fn!(
873 purge_post,
874 PurgePost,
875 SuccessResponse,
876 r#"Purges a post from the datbase.
877
878HTTP POST /admin/purge/post"#
879 );
880 expose_wrapped_fn!(
881 purge_comment,
882 PurgeComment,
883 SuccessResponse,
884 r#"Purges a comment from the database.
885
886HTTP POST /admin/purge/comment"#
887 );
888 expose_wrapped_fn!(
889 create_custom_emoji,
890 CreateCustomEmoji,
891 CustomEmojiResponse,
892 r#"Creates a custom emoji.
893
894HTTP POST /custom_emoji"#
895 );
896 expose_wrapped_fn!(
897 edit_custom_emoji,
898 EditCustomEmoji,
899 CustomEmojiResponse,
900 r#"Edits an existing custom emoji.
901
902HTTP PUT /custom_emoji"#
903 );
904 expose_wrapped_fn!(
905 delete_custom_emoji,
906 DeleteCustomEmoji,
907 CustomEmojiResponse,
908 r#"Deletes an existing custom emoji.
909
910HTTP POST /custom_emoji/delete"#
911 );
912 expose_wrapped_fn!(
913 list_media,
914 ListMedia,
915 ListMediaResponse,
916 r#"Gets all media posted by the logged in user.
917
918HTTP GET /account/list_media"#
919 );
920 expose_wrapped_fn!(
921 list_all_media,
922 ListMedia,
923 ListMediaResponse,
924 r#"Gets all media posted on an instance. Only usable by the instance's admins.
925
926HTTP GET /admin/list_all_media"#
927 );
928}