]> Untitled Git - lemmy.git/blob - crates/websocket/src/lib.rs
5a132a18bf212f87f3fccbb72db586ced4d2423a
[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   Search,
134   ResolveObject,
135   MarkAllAsRead,
136   SaveUserSettings,
137   TransferCommunity,
138   TransferSite,
139   PasswordReset,
140   PasswordChange,
141   MarkPrivateMessageAsRead,
142   UserJoin,
143   GetSiteConfig,
144   SaveSiteConfig,
145   PostJoin,
146   CommunityJoin,
147   ModJoin,
148   ChangePassword,
149   GetSiteMetadata,
150   BlockCommunity,
151   BlockPerson,
152 }
153
154 #[derive(EnumString, Display, Debug, Clone)]
155 pub enum UserOperationCrud {
156   // Site
157   CreateSite,
158   GetSite,
159   EditSite,
160   // Community
161   CreateCommunity,
162   ListCommunities,
163   GetCommunity,
164   EditCommunity,
165   DeleteCommunity,
166   RemoveCommunity,
167   // Post
168   CreatePost,
169   GetPost,
170   GetPosts,
171   EditPost,
172   DeletePost,
173   RemovePost,
174   // Comment
175   CreateComment,
176   GetComment,
177   GetComments,
178   EditComment,
179   DeleteComment,
180   RemoveComment,
181   // User
182   Register,
183   GetPersonDetails,
184   DeleteAccount,
185   // Private Message
186   CreatePrivateMessage,
187   GetPrivateMessages,
188   EditPrivateMessage,
189   DeletePrivateMessage,
190 }
191
192 pub trait OperationType {}
193
194 impl OperationType for UserOperationCrud {}
195
196 impl OperationType for UserOperation {}