]> Untitled Git - lemmy.git/blob - crates/websocket/src/lib.rs
Adding a banned endpoint for admins. Removing it from GetSite. Fixes #1806
[lemmy.git] / crates / websocket / src / lib.rs
1 #[macro_use]
2 extern crate strum_macros;
3
4 use crate::chat_server::ChatServer;
5 use actix::Addr;
6 use background_jobs::QueueHandle;
7 use lemmy_db_schema::{source::secret::Secret, DbPool};
8 use lemmy_utils::{settings::structs::Settings, LemmyError};
9 use reqwest_middleware::ClientWithMiddleware;
10 use serde::Serialize;
11
12 pub mod chat_server;
13 pub mod handlers;
14 pub mod messages;
15 pub mod routes;
16 pub mod send;
17
18 pub struct LemmyContext {
19   pool: DbPool,
20   chat_server: Addr<ChatServer>,
21   client: ClientWithMiddleware,
22   activity_queue: QueueHandle,
23   settings: Settings,
24   secret: Secret,
25 }
26
27 impl LemmyContext {
28   pub fn create(
29     pool: DbPool,
30     chat_server: Addr<ChatServer>,
31     client: ClientWithMiddleware,
32     activity_queue: QueueHandle,
33     settings: Settings,
34     secret: Secret,
35   ) -> LemmyContext {
36     LemmyContext {
37       pool,
38       chat_server,
39       client,
40       activity_queue,
41       settings,
42       secret,
43     }
44   }
45   pub fn pool(&self) -> &DbPool {
46     &self.pool
47   }
48   pub fn chat_server(&self) -> &Addr<ChatServer> {
49     &self.chat_server
50   }
51   pub fn client(&self) -> &ClientWithMiddleware {
52     &self.client
53   }
54   pub fn activity_queue(&self) -> &QueueHandle {
55     &self.activity_queue
56   }
57   pub fn settings(&self) -> Settings {
58     // TODO hacky solution to be able to hotload the settings.
59     Settings::get()
60   }
61   pub fn secret(&self) -> &Secret {
62     &self.secret
63   }
64 }
65
66 impl Clone for LemmyContext {
67   fn clone(&self) -> Self {
68     LemmyContext {
69       pool: self.pool.clone(),
70       chat_server: self.chat_server.clone(),
71       client: self.client.clone(),
72       activity_queue: self.activity_queue.clone(),
73       settings: self.settings.clone(),
74       secret: self.secret.clone(),
75     }
76   }
77 }
78
79 #[derive(Serialize)]
80 struct WebsocketResponse<T> {
81   op: String,
82   data: T,
83 }
84
85 pub fn serialize_websocket_message<OP, Response>(
86   op: &OP,
87   data: &Response,
88 ) -> Result<String, LemmyError>
89 where
90   Response: Serialize,
91   OP: ToString,
92 {
93   let response = WebsocketResponse {
94     op: op.to_string(),
95     data,
96   };
97   Ok(serde_json::to_string(&response)?)
98 }
99
100 #[derive(EnumString, Display, Debug, Clone)]
101 pub enum UserOperation {
102   Login,
103   GetCaptcha,
104   MarkCommentAsRead,
105   SaveComment,
106   CreateCommentLike,
107   CreateCommentReport,
108   ResolveCommentReport,
109   ListCommentReports,
110   CreatePostLike,
111   LockPost,
112   StickyPost,
113   MarkPostAsRead,
114   SavePost,
115   CreatePostReport,
116   ResolvePostReport,
117   ListPostReports,
118   GetReportCount,
119   GetUnreadCount,
120   VerifyEmail,
121   FollowCommunity,
122   GetReplies,
123   GetPersonMentions,
124   MarkPersonMentionAsRead,
125   GetModlog,
126   BanFromCommunity,
127   AddModToCommunity,
128   AddAdmin,
129   GetUnreadRegistrationApplicationCount,
130   ListRegistrationApplications,
131   ApproveRegistrationApplication,
132   BanPerson,
133   GetBannedPersons,
134   Search,
135   ResolveObject,
136   MarkAllAsRead,
137   SaveUserSettings,
138   TransferCommunity,
139   TransferSite,
140   PasswordReset,
141   PasswordChange,
142   MarkPrivateMessageAsRead,
143   UserJoin,
144   GetSiteConfig,
145   SaveSiteConfig,
146   PostJoin,
147   CommunityJoin,
148   ModJoin,
149   ChangePassword,
150   GetSiteMetadata,
151   BlockCommunity,
152   BlockPerson,
153 }
154
155 #[derive(EnumString, Display, Debug, Clone)]
156 pub enum UserOperationCrud {
157   // Site
158   CreateSite,
159   GetSite,
160   EditSite,
161   // Community
162   CreateCommunity,
163   ListCommunities,
164   GetCommunity,
165   EditCommunity,
166   DeleteCommunity,
167   RemoveCommunity,
168   // Post
169   CreatePost,
170   GetPost,
171   GetPosts,
172   EditPost,
173   DeletePost,
174   RemovePost,
175   // Comment
176   CreateComment,
177   GetComment,
178   GetComments,
179   EditComment,
180   DeleteComment,
181   RemoveComment,
182   // User
183   Register,
184   GetPersonDetails,
185   DeleteAccount,
186   // Private Message
187   CreatePrivateMessage,
188   GetPrivateMessages,
189   EditPrivateMessage,
190   DeletePrivateMessage,
191 }
192
193 pub trait OperationType {}
194
195 impl OperationType for UserOperationCrud {}
196
197 impl OperationType for UserOperation {}