1 use crate::claims::Claims;
2 use actix_web::{web, web::Data};
5 community::{Community, CommunityModerator},
9 views::community::community_user_ban_view::CommunityUserBanView,
13 use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*};
14 use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError};
15 use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
16 use serde::Deserialize;
17 use std::process::Command;
28 #[async_trait::async_trait(?Send)]
30 type Response: serde::ser::Serialize + Send;
34 context: &Data<LemmyContext>,
35 websocket_id: Option<ConnectionId>,
36 ) -> Result<Self::Response, LemmyError>;
39 pub(crate) async fn is_mod_or_admin(
43 ) -> Result<(), LemmyError> {
44 let is_mod_or_admin = blocking(pool, move |conn| {
45 Community::is_mod_or_admin(conn, user_id, community_id)
49 return Err(APIError::err("not_a_mod_or_admin").into());
53 pub async fn is_admin(pool: &DbPool, user_id: i32) -> Result<(), LemmyError> {
54 let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
56 return Err(APIError::err("not_an_admin").into());
61 pub(crate) async fn get_post(post_id: i32, pool: &DbPool) -> Result<Post, LemmyError> {
62 match blocking(pool, move |conn| Post::read(conn, post_id)).await? {
64 Err(_e) => Err(APIError::err("couldnt_find_post").into()),
68 pub(crate) async fn get_user_from_jwt(jwt: &str, pool: &DbPool) -> Result<User_, LemmyError> {
69 let claims = match Claims::decode(&jwt) {
70 Ok(claims) => claims.claims,
71 Err(_e) => return Err(APIError::err("not_logged_in").into()),
73 let user_id = claims.id;
74 let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
75 // Check for a site ban
77 return Err(APIError::err("site_ban").into());
82 pub(crate) async fn get_user_from_jwt_opt(
85 ) -> Result<Option<User_>, LemmyError> {
87 Some(jwt) => Ok(Some(get_user_from_jwt(jwt, pool).await?)),
92 pub(crate) async fn check_community_ban(
96 ) -> Result<(), LemmyError> {
97 let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
98 if blocking(pool, is_banned).await? {
99 Err(APIError::err("community_ban").into())
105 /// Returns a list of communities that the user moderates
106 /// or if a community_id is supplied validates the user is a moderator
107 /// of that community and returns the community id in a vec
109 /// * `user_id` - the user id of the moderator
110 /// * `community_id` - optional community id to check for moderator privileges
111 /// * `pool` - the diesel db pool
112 pub(crate) async fn collect_moderated_communities(
114 community_id: Option<i32>,
116 ) -> Result<Vec<i32>, LemmyError> {
117 if let Some(community_id) = community_id {
118 // if the user provides a community_id, just check for mod/admin privileges
119 is_mod_or_admin(pool, user_id, community_id).await?;
120 Ok(vec![community_id])
122 let ids = blocking(pool, move |conn: &'_ _| {
123 CommunityModerator::get_user_moderated_communities(conn, user_id)
130 pub(crate) fn check_optional_url(item: &Option<Option<String>>) -> Result<(), LemmyError> {
131 if let Some(Some(item)) = &item {
132 if Url::parse(item).is_err() {
133 return Err(APIError::err("invalid_url").into());
139 pub(crate) async fn linked_instances(pool: &DbPool) -> Result<Vec<String>, LemmyError> {
140 let mut instances: Vec<String> = Vec::new();
142 if Settings::get().federation.enabled {
143 let distinct_communities = blocking(pool, move |conn| {
144 Community::distinct_federated_communities(conn)
148 instances = distinct_communities
150 .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
151 .collect::<Result<Vec<String>, LemmyError>>()?;
153 instances.append(&mut Settings::get().get_allowed_instances());
154 instances.retain(|a| {
155 !Settings::get().get_blocked_instances().contains(a)
157 && !a.eq(&Settings::get().hostname)
160 // Sort and remove dupes
161 instances.sort_unstable();
168 pub async fn match_websocket_operation(
169 context: LemmyContext,
173 ) -> Result<String, LemmyError> {
176 UserOperation::Login => do_websocket_operation::<Login>(context, id, op, data).await,
177 UserOperation::Register => do_websocket_operation::<Register>(context, id, op, data).await,
178 UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await,
179 UserOperation::GetUserDetails => {
180 do_websocket_operation::<GetUserDetails>(context, id, op, data).await
182 UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await,
183 UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await,
184 UserOperation::BanUser => do_websocket_operation::<BanUser>(context, id, op, data).await,
185 UserOperation::GetUserMentions => {
186 do_websocket_operation::<GetUserMentions>(context, id, op, data).await
188 UserOperation::MarkUserMentionAsRead => {
189 do_websocket_operation::<MarkUserMentionAsRead>(context, id, op, data).await
191 UserOperation::MarkAllAsRead => {
192 do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
194 UserOperation::DeleteAccount => {
195 do_websocket_operation::<DeleteAccount>(context, id, op, data).await
197 UserOperation::PasswordReset => {
198 do_websocket_operation::<PasswordReset>(context, id, op, data).await
200 UserOperation::PasswordChange => {
201 do_websocket_operation::<PasswordChange>(context, id, op, data).await
203 UserOperation::UserJoin => do_websocket_operation::<UserJoin>(context, id, op, data).await,
204 UserOperation::PostJoin => do_websocket_operation::<PostJoin>(context, id, op, data).await,
205 UserOperation::CommunityJoin => {
206 do_websocket_operation::<CommunityJoin>(context, id, op, data).await
208 UserOperation::ModJoin => do_websocket_operation::<ModJoin>(context, id, op, data).await,
209 UserOperation::SaveUserSettings => {
210 do_websocket_operation::<SaveUserSettings>(context, id, op, data).await
212 UserOperation::GetReportCount => {
213 do_websocket_operation::<GetReportCount>(context, id, op, data).await
216 // Private Message ops
217 UserOperation::CreatePrivateMessage => {
218 do_websocket_operation::<CreatePrivateMessage>(context, id, op, data).await
220 UserOperation::EditPrivateMessage => {
221 do_websocket_operation::<EditPrivateMessage>(context, id, op, data).await
223 UserOperation::DeletePrivateMessage => {
224 do_websocket_operation::<DeletePrivateMessage>(context, id, op, data).await
226 UserOperation::MarkPrivateMessageAsRead => {
227 do_websocket_operation::<MarkPrivateMessageAsRead>(context, id, op, data).await
229 UserOperation::GetPrivateMessages => {
230 do_websocket_operation::<GetPrivateMessages>(context, id, op, data).await
234 UserOperation::GetModlog => do_websocket_operation::<GetModlog>(context, id, op, data).await,
235 UserOperation::CreateSite => do_websocket_operation::<CreateSite>(context, id, op, data).await,
236 UserOperation::EditSite => do_websocket_operation::<EditSite>(context, id, op, data).await,
237 UserOperation::GetSite => do_websocket_operation::<GetSite>(context, id, op, data).await,
238 UserOperation::GetSiteConfig => {
239 do_websocket_operation::<GetSiteConfig>(context, id, op, data).await
241 UserOperation::SaveSiteConfig => {
242 do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
244 UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
245 UserOperation::TransferCommunity => {
246 do_websocket_operation::<TransferCommunity>(context, id, op, data).await
248 UserOperation::TransferSite => {
249 do_websocket_operation::<TransferSite>(context, id, op, data).await
251 UserOperation::ListCategories => {
252 do_websocket_operation::<ListCategories>(context, id, op, data).await
256 UserOperation::GetCommunity => {
257 do_websocket_operation::<GetCommunity>(context, id, op, data).await
259 UserOperation::ListCommunities => {
260 do_websocket_operation::<ListCommunities>(context, id, op, data).await
262 UserOperation::CreateCommunity => {
263 do_websocket_operation::<CreateCommunity>(context, id, op, data).await
265 UserOperation::EditCommunity => {
266 do_websocket_operation::<EditCommunity>(context, id, op, data).await
268 UserOperation::DeleteCommunity => {
269 do_websocket_operation::<DeleteCommunity>(context, id, op, data).await
271 UserOperation::RemoveCommunity => {
272 do_websocket_operation::<RemoveCommunity>(context, id, op, data).await
274 UserOperation::FollowCommunity => {
275 do_websocket_operation::<FollowCommunity>(context, id, op, data).await
277 UserOperation::GetFollowedCommunities => {
278 do_websocket_operation::<GetFollowedCommunities>(context, id, op, data).await
280 UserOperation::BanFromCommunity => {
281 do_websocket_operation::<BanFromCommunity>(context, id, op, data).await
283 UserOperation::AddModToCommunity => {
284 do_websocket_operation::<AddModToCommunity>(context, id, op, data).await
288 UserOperation::CreatePost => do_websocket_operation::<CreatePost>(context, id, op, data).await,
289 UserOperation::GetPost => do_websocket_operation::<GetPost>(context, id, op, data).await,
290 UserOperation::GetPosts => do_websocket_operation::<GetPosts>(context, id, op, data).await,
291 UserOperation::EditPost => do_websocket_operation::<EditPost>(context, id, op, data).await,
292 UserOperation::DeletePost => do_websocket_operation::<DeletePost>(context, id, op, data).await,
293 UserOperation::RemovePost => do_websocket_operation::<RemovePost>(context, id, op, data).await,
294 UserOperation::LockPost => do_websocket_operation::<LockPost>(context, id, op, data).await,
295 UserOperation::StickyPost => do_websocket_operation::<StickyPost>(context, id, op, data).await,
296 UserOperation::CreatePostLike => {
297 do_websocket_operation::<CreatePostLike>(context, id, op, data).await
299 UserOperation::SavePost => do_websocket_operation::<SavePost>(context, id, op, data).await,
300 UserOperation::CreatePostReport => {
301 do_websocket_operation::<CreatePostReport>(context, id, op, data).await
303 UserOperation::ListPostReports => {
304 do_websocket_operation::<ListPostReports>(context, id, op, data).await
306 UserOperation::ResolvePostReport => {
307 do_websocket_operation::<ResolvePostReport>(context, id, op, data).await
311 UserOperation::CreateComment => {
312 do_websocket_operation::<CreateComment>(context, id, op, data).await
314 UserOperation::EditComment => {
315 do_websocket_operation::<EditComment>(context, id, op, data).await
317 UserOperation::DeleteComment => {
318 do_websocket_operation::<DeleteComment>(context, id, op, data).await
320 UserOperation::RemoveComment => {
321 do_websocket_operation::<RemoveComment>(context, id, op, data).await
323 UserOperation::MarkCommentAsRead => {
324 do_websocket_operation::<MarkCommentAsRead>(context, id, op, data).await
326 UserOperation::SaveComment => {
327 do_websocket_operation::<SaveComment>(context, id, op, data).await
329 UserOperation::GetComments => {
330 do_websocket_operation::<GetComments>(context, id, op, data).await
332 UserOperation::CreateCommentLike => {
333 do_websocket_operation::<CreateCommentLike>(context, id, op, data).await
335 UserOperation::CreateCommentReport => {
336 do_websocket_operation::<CreateCommentReport>(context, id, op, data).await
338 UserOperation::ListCommentReports => {
339 do_websocket_operation::<ListCommentReports>(context, id, op, data).await
341 UserOperation::ResolveCommentReport => {
342 do_websocket_operation::<ResolveCommentReport>(context, id, op, data).await
347 async fn do_websocket_operation<'a, 'b, Data>(
348 context: LemmyContext,
352 ) -> Result<String, LemmyError>
354 for<'de> Data: Deserialize<'de> + 'a,
357 let parsed_data: Data = serde_json::from_str(&data)?;
358 let res = parsed_data
359 .perform(&web::Data::new(context), Some(id))
361 serialize_websocket_message(&op, &res)
364 pub(crate) fn captcha_espeak_wav_base64(captcha: &str) -> Result<String, LemmyError> {
365 let mut built_text = String::new();
367 // Building proper speech text for espeak
368 for mut c in captcha.chars() {
369 let new_str = if c.is_alphabetic() {
370 if c.is_lowercase() {
371 c.make_ascii_uppercase();
372 format!("lower case {} ... ", c)
374 c.make_ascii_uppercase();
375 format!("capital {} ... ", c)
381 built_text.push_str(&new_str);
384 espeak_wav_base64(&built_text)
387 pub(crate) fn espeak_wav_base64(text: &str) -> Result<String, LemmyError> {
388 // Make a temp file path
389 let uuid = uuid::Uuid::new_v4().to_string();
390 let file_path = format!("/tmp/lemmy_espeak_{}.wav", &uuid);
392 // Write the wav file
393 Command::new("espeak")
399 // Read the wav file bytes
400 let bytes = std::fs::read(&file_path)?;
403 std::fs::remove_file(file_path)?;
406 let base64 = base64::encode(bytes);
413 use crate::captcha_espeak_wav_base64;
417 assert!(captcha_espeak_wav_base64("WxRt2l").is_ok())