]> Untitled Git - lemmy.git/blob - crates/websocket/src/lib.rs
User / community blocking. Fixes #426 (#1604)
[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 pub mod send;
17
18 pub struct LemmyContext {
19   pub pool: DbPool,
20   pub chat_server: Addr<ChatServer>,
21   pub client: Client,
22   pub activity_queue: QueueHandle,
23 }
24
25 impl LemmyContext {
26   pub fn create(
27     pool: DbPool,
28     chat_server: Addr<ChatServer>,
29     client: Client,
30     activity_queue: QueueHandle,
31   ) -> LemmyContext {
32     LemmyContext {
33       pool,
34       chat_server,
35       client,
36       activity_queue,
37     }
38   }
39   pub fn pool(&self) -> &DbPool {
40     &self.pool
41   }
42   pub fn chat_server(&self) -> &Addr<ChatServer> {
43     &self.chat_server
44   }
45   pub fn client(&self) -> &Client {
46     &self.client
47   }
48   pub fn activity_queue(&self) -> &QueueHandle {
49     &self.activity_queue
50   }
51 }
52
53 impl Clone for LemmyContext {
54   fn clone(&self) -> Self {
55     LemmyContext {
56       pool: self.pool.clone(),
57       chat_server: self.chat_server.clone(),
58       client: self.client.clone(),
59       activity_queue: self.activity_queue.clone(),
60     }
61   }
62 }
63
64 #[derive(Serialize)]
65 struct WebsocketResponse<T> {
66   op: String,
67   data: T,
68 }
69
70 pub fn serialize_websocket_message<OP, Response>(
71   op: &OP,
72   data: &Response,
73 ) -> Result<String, LemmyError>
74 where
75   Response: Serialize,
76   OP: ToString,
77 {
78   let response = WebsocketResponse {
79     op: op.to_string(),
80     data,
81   };
82   Ok(serde_json::to_string(&response)?)
83 }
84
85 #[derive(EnumString, ToString, Debug, Clone)]
86 pub enum UserOperation {
87   Login,
88   GetCaptcha,
89   MarkCommentAsRead,
90   SaveComment,
91   CreateCommentLike,
92   CreateCommentReport,
93   ResolveCommentReport,
94   ListCommentReports,
95   CreatePostLike,
96   LockPost,
97   StickyPost,
98   SavePost,
99   CreatePostReport,
100   ResolvePostReport,
101   ListPostReports,
102   GetReportCount,
103   FollowCommunity,
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   ChangePassword,
127   GetSiteMetadata,
128   BlockCommunity,
129   BlockPerson,
130 }
131
132 #[derive(EnumString, ToString, Debug, Clone)]
133 pub enum UserOperationCrud {
134   // Site
135   CreateSite,
136   GetSite,
137   EditSite,
138   // Community
139   CreateCommunity,
140   ListCommunities,
141   GetCommunity,
142   EditCommunity,
143   DeleteCommunity,
144   RemoveCommunity,
145   // Post
146   CreatePost,
147   GetPost,
148   GetPosts,
149   EditPost,
150   DeletePost,
151   RemovePost,
152   // Comment
153   CreateComment,
154   GetComments,
155   EditComment,
156   DeleteComment,
157   RemoveComment,
158   // User
159   Register,
160   GetPersonDetails,
161   DeleteAccount,
162   // Private Message
163   CreatePrivateMessage,
164   GetPrivateMessages,
165   EditPrivateMessage,
166   DeletePrivateMessage,
167 }
168
169 pub trait OperationType {}
170
171 impl OperationType for UserOperationCrud {}
172
173 impl OperationType for UserOperation {}