]> Untitled Git - lemmy.git/blob - crates/api_common/src/websocket/messages.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[lemmy.git] / crates / api_common / src / websocket / messages.rs
1 use crate::{comment::CommentResponse, post::PostResponse, websocket::UserOperation};
2 use actix::{prelude::*, Recipient};
3 use lemmy_db_schema::newtypes::{CommunityId, LocalUserId, PostId};
4 use lemmy_utils::{ConnectionId, IpAddr};
5 use serde::{Deserialize, Serialize};
6
7 /// Chat server sends this messages to session
8 #[derive(Message)]
9 #[rtype(result = "()")]
10 pub struct WsMessage(pub String);
11
12 /// Message for chat server communications
13
14 /// New chat session is created
15 #[derive(Message)]
16 #[rtype(usize)]
17 pub struct Connect {
18   pub addr: Recipient<WsMessage>,
19   pub ip: IpAddr,
20 }
21
22 /// Session is disconnected
23 #[derive(Message)]
24 #[rtype(result = "()")]
25 pub struct Disconnect {
26   pub id: ConnectionId,
27   pub ip: IpAddr,
28 }
29
30 /// The messages sent to websocket clients
31 #[derive(Serialize, Deserialize, Message)]
32 #[rtype(result = "Result<String, std::convert::Infallible>")]
33 pub struct StandardMessage {
34   /// Id of the client session
35   pub id: ConnectionId,
36   /// Peer message
37   pub msg: String,
38 }
39
40 #[derive(Message)]
41 #[rtype(result = "()")]
42 pub struct SendAllMessage<OP: ToString, Response> {
43   pub op: OP,
44   pub response: Response,
45   pub websocket_id: Option<ConnectionId>,
46 }
47
48 #[derive(Message)]
49 #[rtype(result = "()")]
50 pub struct SendUserRoomMessage<OP: ToString, Response> {
51   pub op: OP,
52   pub response: Response,
53   pub local_recipient_id: LocalUserId,
54   pub websocket_id: Option<ConnectionId>,
55 }
56
57 /// Send message to all users viewing the given community.
58 #[derive(Message)]
59 #[rtype(result = "()")]
60 pub struct SendCommunityRoomMessage<OP: ToString, Response> {
61   pub op: OP,
62   pub response: Response,
63   pub community_id: CommunityId,
64   pub websocket_id: Option<ConnectionId>,
65 }
66
67 /// Send message to mods of a given community. Set community_id = 0 to send to site admins.
68 #[derive(Message)]
69 #[rtype(result = "()")]
70 pub struct SendModRoomMessage<Response> {
71   pub op: UserOperation,
72   pub response: Response,
73   pub community_id: CommunityId,
74   pub websocket_id: Option<ConnectionId>,
75 }
76
77 #[derive(Message)]
78 #[rtype(result = "()")]
79 pub(crate) struct SendPost<OP: ToString> {
80   pub op: OP,
81   pub post: PostResponse,
82   pub websocket_id: Option<ConnectionId>,
83 }
84
85 #[derive(Message)]
86 #[rtype(result = "()")]
87 pub(crate) struct SendComment<OP: ToString> {
88   pub op: OP,
89   pub comment: CommentResponse,
90   pub websocket_id: Option<ConnectionId>,
91 }
92
93 #[derive(Message)]
94 #[rtype(result = "()")]
95 pub struct JoinUserRoom {
96   pub local_user_id: LocalUserId,
97   pub id: ConnectionId,
98 }
99
100 #[derive(Message)]
101 #[rtype(result = "()")]
102 pub struct JoinCommunityRoom {
103   pub community_id: CommunityId,
104   pub id: ConnectionId,
105 }
106
107 #[derive(Message)]
108 #[rtype(result = "()")]
109 pub struct JoinModRoom {
110   pub community_id: CommunityId,
111   pub id: ConnectionId,
112 }
113
114 #[derive(Message)]
115 #[rtype(result = "()")]
116 pub struct JoinPostRoom {
117   pub post_id: PostId,
118   pub id: ConnectionId,
119 }
120
121 #[derive(Message)]
122 #[rtype(usize)]
123 pub struct GetUsersOnline;
124
125 #[derive(Message)]
126 #[rtype(usize)]
127 pub struct GetPostUsersOnline {
128   pub post_id: PostId,
129 }
130
131 #[derive(Message)]
132 #[rtype(usize)]
133 pub struct GetCommunityUsersOnline {
134   pub community_id: CommunityId,
135 }
136
137 #[derive(Message, Debug)]
138 #[rtype(result = "()")]
139 pub struct CaptchaItem {
140   pub uuid: String,
141   pub answer: String,
142   pub expires: chrono::NaiveDateTime,
143 }
144
145 #[derive(Message)]
146 #[rtype(bool)]
147 pub struct CheckCaptcha {
148   pub uuid: String,
149   pub answer: String,
150 }