]> Untitled Git - lemmy.git/blob - crates/websocket/src/lib.rs
Fix API and clippy warnings
[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_queries::DbPool;
8 use lemmy_utils::LemmyError;
9 use reqwest::Client;
10 use serde::Serialize;
11
12 pub mod chat_server;
13 pub mod handlers;
14 pub mod messages;
15 pub mod routes;
16
17 pub struct LemmyContext {
18   pub pool: DbPool,
19   pub chat_server: Addr<ChatServer>,
20   pub client: Client,
21   pub activity_queue: QueueHandle,
22 }
23
24 impl LemmyContext {
25   pub fn create(
26     pool: DbPool,
27     chat_server: Addr<ChatServer>,
28     client: Client,
29     activity_queue: QueueHandle,
30   ) -> LemmyContext {
31     LemmyContext {
32       pool,
33       chat_server,
34       client,
35       activity_queue,
36     }
37   }
38   pub fn pool(&self) -> &DbPool {
39     &self.pool
40   }
41   pub fn chat_server(&self) -> &Addr<ChatServer> {
42     &self.chat_server
43   }
44   pub fn client(&self) -> &Client {
45     &self.client
46   }
47   pub fn activity_queue(&self) -> &QueueHandle {
48     &self.activity_queue
49   }
50 }
51
52 impl Clone for LemmyContext {
53   fn clone(&self) -> Self {
54     LemmyContext {
55       pool: self.pool.clone(),
56       chat_server: self.chat_server.clone(),
57       client: self.client.clone(),
58       activity_queue: self.activity_queue.clone(),
59     }
60   }
61 }
62
63 #[derive(Serialize)]
64 struct WebsocketResponse<T> {
65   op: String,
66   data: T,
67 }
68
69 pub fn serialize_websocket_message<OP, Response>(
70   op: &OP,
71   data: &Response,
72 ) -> Result<String, LemmyError>
73 where
74   Response: Serialize,
75   OP: ToString,
76 {
77   let response = WebsocketResponse {
78     op: op.to_string(),
79     data,
80   };
81   Ok(serde_json::to_string(&response)?)
82 }
83
84 #[derive(EnumString, ToString, Debug, Clone)]
85 pub enum UserOperation {
86   Login,
87   GetCaptcha,
88   MarkCommentAsRead,
89   SaveComment,
90   CreateCommentLike,
91   CreateCommentReport,
92   ResolveCommentReport,
93   ListCommentReports,
94   CreatePostLike,
95   LockPost,
96   StickyPost,
97   SavePost,
98   CreatePostReport,
99   ResolvePostReport,
100   ListPostReports,
101   GetReportCount,
102   FollowCommunity,
103   GetFollowedCommunities,
104   GetReplies,
105   GetPersonMentions,
106   MarkPersonMentionAsRead,
107   GetModlog,
108   BanFromCommunity,
109   AddModToCommunity,
110   AddAdmin,
111   BanPerson,
112   Search,
113   MarkAllAsRead,
114   SaveUserSettings,
115   TransferCommunity,
116   TransferSite,
117   PasswordReset,
118   PasswordChange,
119   MarkPrivateMessageAsRead,
120   UserJoin,
121   GetSiteConfig,
122   SaveSiteConfig,
123   PostJoin,
124   CommunityJoin,
125   ModJoin,
126 }
127
128 #[derive(EnumString, ToString, Debug, Clone)]
129 pub enum UserOperationCrud {
130   // Site
131   CreateSite,
132   GetSite,
133   EditSite,
134   // Community
135   CreateCommunity,
136   ListCommunities,
137   GetCommunity,
138   EditCommunity,
139   DeleteCommunity,
140   RemoveCommunity,
141   // Post
142   CreatePost,
143   GetPost,
144   GetPosts,
145   EditPost,
146   DeletePost,
147   RemovePost,
148   // Comment
149   CreateComment,
150   GetComments,
151   EditComment,
152   DeleteComment,
153   RemoveComment,
154   // User
155   Register,
156   GetPersonDetails,
157   DeleteAccount,
158   // Private Message
159   CreatePrivateMessage,
160   GetPrivateMessages,
161   EditPrivateMessage,
162   DeletePrivateMessage,
163 }
164
165 pub trait OperationType {}
166
167 impl OperationType for UserOperationCrud {}
168
169 impl OperationType for UserOperation {}