1 use actix_web::{web, web::Data};
2 use lemmy_api_common::{comment::*, community::*, person::*, post::*, site::*, websocket::*};
3 use lemmy_utils::{ConnectionId, LemmyError};
4 use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
5 use serde::Deserialize;
6 use std::{env, process::Command};
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::GetPersonMentions => {
43 do_websocket_operation::<GetPersonMentions>(context, id, op, data).await
45 UserOperation::MarkPersonMentionAsRead => {
46 do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await
48 UserOperation::MarkAllAsRead => {
49 do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
51 UserOperation::PasswordReset => {
52 do_websocket_operation::<PasswordReset>(context, id, op, data).await
54 UserOperation::PasswordChange => {
55 do_websocket_operation::<PasswordChange>(context, id, op, data).await
57 UserOperation::UserJoin => do_websocket_operation::<UserJoin>(context, id, op, data).await,
58 UserOperation::PostJoin => do_websocket_operation::<PostJoin>(context, id, op, data).await,
59 UserOperation::CommunityJoin => {
60 do_websocket_operation::<CommunityJoin>(context, id, op, data).await
62 UserOperation::ModJoin => do_websocket_operation::<ModJoin>(context, id, op, data).await,
63 UserOperation::SaveUserSettings => {
64 do_websocket_operation::<SaveUserSettings>(context, id, op, data).await
66 UserOperation::ChangePassword => {
67 do_websocket_operation::<ChangePassword>(context, id, op, data).await
69 UserOperation::GetReportCount => {
70 do_websocket_operation::<GetReportCount>(context, id, op, data).await
73 // Private Message ops
74 UserOperation::MarkPrivateMessageAsRead => {
75 do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await
79 UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await,
80 UserOperation::GetSiteConfig => {
81 do_websocket_operation::<GetSiteConfig>(context, id, op, data).await
83 UserOperation::SaveSiteConfig => {
84 do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
86 UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
87 UserOperation::TransferCommunity => {
88 do_websocket_operation::<TransferCommunity>(context, id, op, data).await
90 UserOperation::TransferSite => {
91 do_websocket_operation::<TransferSite>(context, id, op, data).await
95 UserOperation::FollowCommunity => {
96 do_websocket_operation::<FollowCommunity>(context, id, op, data).await
98 UserOperation::GetFollowedCommunities => {
99 do_websocket_operation::<GetFollowedCommunities>(context, id, op, data).await
101 UserOperation::BanFromCommunity => {
102 do_websocket_operation::<BanFromCommunity>(context, id, op, data).await
104 UserOperation::AddModToCommunity => {
105 do_websocket_operation::<AddModToCommunity>(context, id, op, data).await
109 UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await,
110 UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await,
111 UserOperation::CreatePostLike => {
112 do_websocket_operation::<CreatePostLike>(context, id, op, data).await
114 UserOperation::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
115 UserOperation::CreatePostReport => {
116 do_websocket_operation::<CreatePostReport>(context, id, op, data).await
118 UserOperation::ListPostReports => {
119 do_websocket_operation::<ListPostReports>(context, id, op, data).await
121 UserOperation::ResolvePostReport => {
122 do_websocket_operation::<ResolvePostReport>(context, id, op, data).await
126 UserOperation::MarkCommentAsRead => {
127 do_websocket_operation::<MarkCommentAsRead>(context, id, op, data).await
129 UserOperation::SaveComment => {
130 do_websocket_operation::<SaveComment>(context, id, op, data).await
132 UserOperation::CreateCommentLike => {
133 do_websocket_operation::<CreateCommentLike>(context, id, op, data).await
135 UserOperation::CreateCommentReport => {
136 do_websocket_operation::<CreateCommentReport>(context, id, op, data).await
138 UserOperation::ListCommentReports => {
139 do_websocket_operation::<ListCommentReports>(context, id, op, data).await
141 UserOperation::ResolveCommentReport => {
142 do_websocket_operation::<ResolveCommentReport>(context, id, op, data).await
147 async fn do_websocket_operation<'a, 'b, Data>(
148 context: LemmyContext,
152 ) -> Result<String, LemmyError>
154 for<'de> Data: Deserialize<'de> + 'a,
157 let parsed_data: Data = serde_json::from_str(&data)?;
158 let res = parsed_data
159 .perform(&web::Data::new(context), Some(id))
161 serialize_websocket_message(&op, &res)
164 pub(crate) fn captcha_espeak_wav_base64(captcha: &str) -> Result<String, LemmyError> {
165 let mut built_text = String::new();
167 // Building proper speech text for espeak
168 for mut c in captcha.chars() {
169 let new_str = if c.is_alphabetic() {
170 if c.is_lowercase() {
171 c.make_ascii_uppercase();
172 format!("lower case {} ... ", c)
174 c.make_ascii_uppercase();
175 format!("capital {} ... ", c)
181 built_text.push_str(&new_str);
184 espeak_wav_base64(&built_text)
187 pub(crate) fn espeak_wav_base64(text: &str) -> Result<String, LemmyError> {
188 // Make a temp file path
189 let uuid = uuid::Uuid::new_v4().to_string();
190 let file_path = format!(
191 "{}/lemmy_espeak_{}.wav",
192 env::temp_dir().to_string_lossy(),
196 // Write the wav file
197 Command::new("espeak")
203 // Read the wav file bytes
204 let bytes = std::fs::read(&file_path)?;
207 std::fs::remove_file(file_path)?;
210 let base64 = base64::encode(bytes);
217 use crate::captcha_espeak_wav_base64;
218 use lemmy_api_common::check_validator_time;
219 use lemmy_db_queries::{establish_unpooled_connection, source::local_user::LocalUser_, Crud};
220 use lemmy_db_schema::source::{
221 local_user::{LocalUser, LocalUserForm},
222 person::{Person, PersonForm},
224 use lemmy_utils::claims::Claims;
227 fn test_should_not_validate_user_token_after_password_change() {
228 let conn = establish_unpooled_connection();
230 let new_person = PersonForm {
231 name: "Gerry9812".into(),
232 ..PersonForm::default()
235 let inserted_person = Person::create(&conn, &new_person).unwrap();
237 let local_user_form = LocalUserForm {
238 person_id: inserted_person.id,
239 password_encrypted: "123456".to_string(),
240 ..LocalUserForm::default()
243 let inserted_local_user = LocalUser::create(&conn, &local_user_form).unwrap();
245 let jwt = Claims::jwt(inserted_local_user.id.0).unwrap();
246 let claims = Claims::decode(&jwt).unwrap().claims;
247 let check = check_validator_time(&inserted_local_user.validator_time, &claims);
248 assert!(check.is_ok());
250 // The check should fail, since the validator time is now newer than the jwt issue time
251 let updated_local_user =
252 LocalUser::update_password(&conn, inserted_local_user.id, &"password111").unwrap();
253 let check_after = check_validator_time(&updated_local_user.validator_time, &claims);
254 assert!(check_after.is_err());
256 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
257 assert_eq!(1, num_deleted);
262 assert!(captcha_espeak_wav_base64("WxRt2l").is_ok())