1 use actix_web::{web, web::Data};
3 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
4 use lemmy_utils::{ConnectionId, LemmyError};
5 use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
6 use serde::Deserialize;
18 #[async_trait::async_trait(?Send)]
20 type Response: serde::ser::Serialize + Send;
24 context: &Data<LemmyContext>,
25 websocket_id: Option<ConnectionId>,
26 ) -> Result<Self::Response, LemmyError>;
29 pub async fn match_websocket_operation(
30 context: LemmyContext,
34 ) -> Result<String, LemmyError> {
37 UserOperation::Login => do_websocket_operation::<Login>(context, id, op, data).await,
38 UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await,
39 UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await,
40 UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await,
41 UserOperation::BanPerson => do_websocket_operation::<BanPerson>(context, id, op, data).await,
42 UserOperation::BlockPerson => {
43 do_websocket_operation::<BlockPerson>(context, id, op, data).await
45 UserOperation::GetPersonMentions => {
46 do_websocket_operation::<GetPersonMentions>(context, id, op, data).await
48 UserOperation::MarkPersonMentionAsRead => {
49 do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await
51 UserOperation::MarkAllAsRead => {
52 do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
54 UserOperation::PasswordReset => {
55 do_websocket_operation::<PasswordReset>(context, id, op, data).await
57 UserOperation::PasswordChange => {
58 do_websocket_operation::<PasswordChange>(context, id, op, data).await
60 UserOperation::UserJoin => do_websocket_operation::<UserJoin>(context, id, op, data).await,
61 UserOperation::PostJoin => do_websocket_operation::<PostJoin>(context, id, op, data).await,
62 UserOperation::CommunityJoin => {
63 do_websocket_operation::<CommunityJoin>(context, id, op, data).await
65 UserOperation::ModJoin => do_websocket_operation::<ModJoin>(context, id, op, data).await,
66 UserOperation::SaveUserSettings => {
67 do_websocket_operation::<SaveUserSettings>(context, id, op, data).await
69 UserOperation::ChangePassword => {
70 do_websocket_operation::<ChangePassword>(context, id, op, data).await
72 UserOperation::GetReportCount => {
73 do_websocket_operation::<GetReportCount>(context, id, op, data).await
76 // Private Message ops
77 UserOperation::MarkPrivateMessageAsRead => {
78 do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await
82 UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await,
83 UserOperation::GetSiteConfig => {
84 do_websocket_operation::<GetSiteConfig>(context, id, op, data).await
86 UserOperation::SaveSiteConfig => {
87 do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
89 UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
90 UserOperation::ResolveObject => {
91 do_websocket_operation::<ResolveObject>(context, id, op, data).await
93 UserOperation::TransferCommunity => {
94 do_websocket_operation::<TransferCommunity>(context, id, op, data).await
96 UserOperation::TransferSite => {
97 do_websocket_operation::<TransferSite>(context, id, op, data).await
101 UserOperation::FollowCommunity => {
102 do_websocket_operation::<FollowCommunity>(context, id, op, data).await
104 UserOperation::BlockCommunity => {
105 do_websocket_operation::<BlockCommunity>(context, id, op, data).await
107 UserOperation::BanFromCommunity => {
108 do_websocket_operation::<BanFromCommunity>(context, id, op, data).await
110 UserOperation::AddModToCommunity => {
111 do_websocket_operation::<AddModToCommunity>(context, id, op, data).await
115 UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await,
116 UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await,
117 UserOperation::CreatePostLike => {
118 do_websocket_operation::<CreatePostLike>(context, id, op, data).await
120 UserOperation::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
121 UserOperation::CreatePostReport => {
122 do_websocket_operation::<CreatePostReport>(context, id, op, data).await
124 UserOperation::ListPostReports => {
125 do_websocket_operation::<ListPostReports>(context, id, op, data).await
127 UserOperation::ResolvePostReport => {
128 do_websocket_operation::<ResolvePostReport>(context, id, op, data).await
130 UserOperation::GetSiteMetadata => {
131 do_websocket_operation::<GetSiteMetadata>(context, id, op, data).await
135 UserOperation::MarkCommentAsRead => {
136 do_websocket_operation::<MarkCommentAsRead>(context, id, op, data).await
138 UserOperation::SaveComment => {
139 do_websocket_operation::<SaveComment>(context, id, op, data).await
141 UserOperation::CreateCommentLike => {
142 do_websocket_operation::<CreateCommentLike>(context, id, op, data).await
144 UserOperation::CreateCommentReport => {
145 do_websocket_operation::<CreateCommentReport>(context, id, op, data).await
147 UserOperation::ListCommentReports => {
148 do_websocket_operation::<ListCommentReports>(context, id, op, data).await
150 UserOperation::ResolveCommentReport => {
151 do_websocket_operation::<ResolveCommentReport>(context, id, op, data).await
156 async fn do_websocket_operation<'a, 'b, Data>(
157 context: LemmyContext,
161 ) -> Result<String, LemmyError>
163 for<'de> Data: Deserialize<'de> + 'a,
166 let parsed_data: Data = serde_json::from_str(data)?;
167 let res = parsed_data
168 .perform(&web::Data::new(context), Some(id))
170 serialize_websocket_message(&op, &res)
173 /// Converts the captcha to a base64 encoded wav audio file
174 pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> String {
175 let letters = captcha.as_wav();
177 let mut concat_letters: Vec<u8> = Vec::new();
179 for letter in letters {
180 let bytes = letter.unwrap_or_default();
181 concat_letters.extend(bytes);
185 base64::encode(concat_letters)
190 use lemmy_api_common::check_validator_time;
191 use lemmy_db_queries::{
192 establish_unpooled_connection,
193 source::{local_user::LocalUser_, secret::SecretSingleton},
196 use lemmy_db_schema::source::{
197 local_user::{LocalUser, LocalUserForm},
198 person::{Person, PersonForm},
201 use lemmy_utils::claims::Claims;
204 fn test_should_not_validate_user_token_after_password_change() {
205 let conn = establish_unpooled_connection();
207 let new_person = PersonForm {
208 name: "Gerry9812".into(),
209 ..PersonForm::default()
212 let inserted_person = Person::create(&conn, &new_person).unwrap();
214 let local_user_form = LocalUserForm {
215 person_id: inserted_person.id,
216 password_encrypted: "123456".to_string(),
217 ..LocalUserForm::default()
220 let inserted_local_user = LocalUser::create(&conn, &local_user_form).unwrap();
222 let jwt_secret = Secret::get().jwt_secret;
223 let jwt = Claims::jwt(inserted_local_user.id.0, &jwt_secret).unwrap();
224 let claims = Claims::decode(&jwt, jwt_secret.as_ref()).unwrap().claims;
225 let check = check_validator_time(&inserted_local_user.validator_time, &claims);
226 assert!(check.is_ok());
228 // The check should fail, since the validator time is now newer than the jwt issue time
229 let updated_local_user =
230 LocalUser::update_password(&conn, inserted_local_user.id, "password111").unwrap();
231 let check_after = check_validator_time(&updated_local_user.validator_time, &claims);
232 assert!(check_after.is_err());
234 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
235 assert_eq!(1, num_deleted);