lemmy_client/
form.rs

1use lemmy_api_common::{
2    comment::*, community::*, custom_emoji::*, person::*, post::*, private_message::*, site::*,
3};
4use serde::Serialize;
5use std::fmt;
6
7use crate::utils::impl_marker_trait;
8
9pub trait LemmyForm: Serialize + Clone + fmt::Debug {}
10
11#[derive(Debug, Clone)]
12/// A request to send to lemmy. If you don't want to set the JWT for each request, you can set the Authorization header with [`LemmyClient::headers_mut`](lemmy_client::LemmyClient.headers_mut).
13pub struct LemmyRequest<Body>
14where
15    Body: LemmyForm,
16{
17    /// The body to send with the request. Uses [`unit`] for when a body is not required.
18    pub body: Body,
19    /// The JWT that is used when authorization is required.
20    pub jwt: Option<String>,
21}
22
23impl LemmyRequest<()> {
24    /// Returns a request with no body or JWT.
25    pub fn empty() -> Self {
26        Self {
27            body: (),
28            jwt: None,
29        }
30    }
31
32    /// Returns a request with no body and JWT if [`Some`].
33    pub fn from_jwt(jwt: Option<String>) -> Self {
34        Self { body: (), jwt }
35    }
36}
37
38impl<Form> From<Form> for LemmyRequest<Form>
39where
40    Form: LemmyForm,
41{
42    fn from(body: Form) -> Self {
43        Self { body, jwt: None }
44    }
45}
46
47impl_marker_trait!(
48    LemmyForm,
49    [
50        (),
51        String,
52        // Comments
53        CreateComment,
54        CreateCommentLike,
55        CreateCommentReport,
56        DeleteComment,
57        DistinguishComment,
58        EditComment,
59        GetComment,
60        GetComments,
61        ListCommentLikes,
62        ListCommentReports,
63        RemoveComment,
64        ResolveCommentReport,
65        SaveComment,
66        // Communities
67        AddModToCommunity,
68        BanFromCommunity,
69        BlockCommunity,
70        CreateCommunity,
71        DeleteCommunity,
72        EditCommunity,
73        FollowCommunity,
74        GetCommunity,
75        HideCommunity,
76        ListCommunities,
77        RemoveCommunity,
78        TransferCommunity,
79        // Emojis
80        CreateCustomEmoji,
81        DeleteCustomEmoji,
82        EditCustomEmoji,
83        // Person
84        AddAdmin,
85        BanPerson,
86        BlockPerson,
87        ChangePassword,
88        DeleteAccount,
89        GetPersonDetails,
90        GetPersonMentions,
91        GetReplies,
92        GetReportCount,
93        Login,
94        MarkCommentReplyAsRead,
95        MarkPersonMentionAsRead,
96        PasswordChangeAfterReset,
97        PasswordReset,
98        PersonMentionResponse,
99        Register,
100        SaveUserSettings,
101        UpdateTotp,
102        VerifyEmail,
103        // Posts
104        CreatePost,
105        CreatePostLike,
106        CreatePostReport,
107        DeletePost,
108        EditPost,
109        FeaturePost,
110        GetPost,
111        GetPosts,
112        GetSiteMetadata,
113        ListPostLikes,
114        ListPostReports,
115        LockPost,
116        MarkPostAsRead,
117        RemovePost,
118        ResolvePostReport,
119        SavePost,
120        HidePost,
121        // Private Messages
122        CreatePrivateMessage,
123        CreatePrivateMessageReport,
124        DeletePrivateMessage,
125        EditPrivateMessage,
126        GetPrivateMessages,
127        ListPrivateMessageReports,
128        MarkPrivateMessageAsRead,
129        ResolvePrivateMessageReport,
130        // Site
131        ApproveRegistrationApplication,
132        GetRegistrationApplication,
133        BlockInstance,
134        CreateSite,
135        EditSite,
136        FederatedInstances,
137        GetModlog,
138        InstanceWithFederationState,
139        ListRegistrationApplications,
140        PurgeComment,
141        PurgeCommunity,
142        PurgePerson,
143        PurgePost,
144        ResolveObject,
145        Search,
146        // Media
147        ListMedia
148    ]
149);