From a5707328cff0133373491ef8e8908faeefacfa30 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Thu, 25 May 2023 16:50:07 +0200 Subject: [PATCH] Dont return error in case optional auth is invalid (#2879) * Dont return error in case optional auth is invalid * fixes --- api_tests/src/user.spec.ts | 18 +++++ crates/api/src/comment/distinguish.rs | 5 +- crates/api/src/comment/like.rs | 5 +- crates/api/src/comment/save.rs | 5 +- crates/api/src/comment_report/create.rs | 5 +- crates/api/src/comment_report/list.rs | 5 +- crates/api/src/comment_report/resolve.rs | 5 +- crates/api/src/community/add_mod.rs | 5 +- crates/api/src/community/ban.rs | 5 +- crates/api/src/community/block.rs | 5 +- crates/api/src/community/follow.rs | 5 +- crates/api/src/community/hide.rs | 5 +- crates/api/src/community/transfer.rs | 5 +- crates/api/src/local_user/add_admin.rs | 5 +- crates/api/src/local_user/ban_person.rs | 5 +- crates/api/src/local_user/block.rs | 5 +- crates/api/src/local_user/change_password.rs | 5 +- crates/api/src/local_user/list_banned.rs | 5 +- .../local_user/notifications/list_mentions.rs | 5 +- .../local_user/notifications/list_replies.rs | 5 +- .../local_user/notifications/mark_all_read.rs | 5 +- .../notifications/mark_mention_read.rs | 5 +- .../notifications/mark_reply_read.rs | 5 +- .../local_user/notifications/unread_count.rs | 5 +- crates/api/src/local_user/report_count.rs | 5 +- crates/api/src/local_user/save_settings.rs | 5 +- crates/api/src/post/feature.rs | 5 +- crates/api/src/post/like.rs | 5 +- crates/api/src/post/lock.rs | 5 +- crates/api/src/post/mark_read.rs | 5 +- crates/api/src/post/save.rs | 5 +- crates/api/src/post_report/create.rs | 5 +- crates/api/src/post_report/list.rs | 5 +- crates/api/src/post_report/resolve.rs | 5 +- crates/api/src/private_message/mark_read.rs | 5 +- .../api/src/private_message_report/create.rs | 5 +- crates/api/src/private_message_report/list.rs | 5 +- .../api/src/private_message_report/resolve.rs | 5 +- crates/api/src/site/leave_admin.rs | 5 +- crates/api/src/site/mod_log.rs | 6 +- crates/api/src/site/purge/comment.rs | 5 +- crates/api/src/site/purge/community.rs | 5 +- crates/api/src/site/purge/person.rs | 5 +- crates/api/src/site/purge/post.rs | 5 +- .../site/registration_applications/approve.rs | 5 +- .../site/registration_applications/list.rs | 5 +- .../registration_applications/unread_count.rs | 5 +- crates/api/src/websocket.rs | 5 +- crates/api_common/src/utils.rs | 73 ++++++------------- crates/api_crud/src/comment/create.rs | 5 +- crates/api_crud/src/comment/delete.rs | 5 +- crates/api_crud/src/comment/read.rs | 6 +- crates/api_crud/src/comment/remove.rs | 5 +- crates/api_crud/src/comment/update.rs | 5 +- crates/api_crud/src/community/create.rs | 5 +- crates/api_crud/src/community/delete.rs | 5 +- crates/api_crud/src/community/list.rs | 6 +- crates/api_crud/src/community/remove.rs | 5 +- crates/api_crud/src/community/update.rs | 5 +- crates/api_crud/src/custom_emoji/create.rs | 5 +- crates/api_crud/src/custom_emoji/delete.rs | 5 +- crates/api_crud/src/custom_emoji/update.rs | 5 +- crates/api_crud/src/post/create.rs | 5 +- crates/api_crud/src/post/delete.rs | 5 +- crates/api_crud/src/post/read.rs | 6 +- crates/api_crud/src/post/remove.rs | 5 +- crates/api_crud/src/post/update.rs | 5 +- crates/api_crud/src/private_message/create.rs | 5 +- crates/api_crud/src/private_message/delete.rs | 5 +- crates/api_crud/src/private_message/read.rs | 5 +- crates/api_crud/src/private_message/update.rs | 5 +- crates/api_crud/src/site/create.rs | 5 +- crates/api_crud/src/site/read.rs | 55 ++++++++++---- crates/api_crud/src/site/update.rs | 5 +- crates/api_crud/src/user/delete.rs | 5 +- crates/apub/src/activities/block/mod.rs | 8 +- .../activities/community/collection_add.rs | 8 +- .../src/activities/community/lock_page.rs | 5 +- .../apub/src/activities/community/report.rs | 8 +- .../apub/src/activities/community/update.rs | 8 +- .../src/activities/deletion/delete_user.rs | 5 +- crates/apub/src/activities/deletion/mod.rs | 20 ++--- .../apub/src/activities/following/follow.rs | 5 +- crates/apub/src/activities/following/mod.rs | 5 +- crates/apub/src/activities/voting/mod.rs | 4 +- crates/apub/src/api/list_comments.rs | 6 +- crates/apub/src/api/list_posts.rs | 6 +- crates/apub/src/api/read_community.rs | 6 +- crates/apub/src/api/read_person.rs | 6 +- crates/apub/src/api/resolve_object.rs | 5 +- crates/apub/src/api/search.rs | 6 +- crates/routes/src/images.rs | 4 +- 92 files changed, 271 insertions(+), 354 deletions(-) diff --git a/api_tests/src/user.spec.ts b/api_tests/src/user.spec.ts index 204dc727f..afe21d1a0 100644 --- a/api_tests/src/user.spec.ts +++ b/api_tests/src/user.spec.ts @@ -18,6 +18,8 @@ import { saveUserSettingsFederated, setupLogins, } from "./shared"; +import { LemmyHttp } from "lemmy-js-client"; +import { GetPosts } from "lemmy-js-client/dist/types/GetPosts"; beforeAll(async () => { await setupLogins(); @@ -95,3 +97,19 @@ test("Delete user", async () => { expect((await resolvePost(alpha, remotePost)).post).toBeUndefined(); expect((await resolveComment(alpha, remoteComment)).comment).toBeUndefined(); }); + +test("Requests with invalid auth should be treated as unauthenticated", async () => { + let invalid_auth: API = { + client: new LemmyHttp("http://127.0.0.1:8541"), + auth: "invalid", + }; + let site = await getSite(invalid_auth); + expect(site.my_user).toBeUndefined(); + expect(site.site_view).toBeDefined(); + + let form: GetPosts = { + auth: "invalid", + }; + let posts = invalid_auth.client.getPosts(form); + expect((await posts).posts).toBeDefined(); +}); diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index cf51107f4..73e1469e3 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, DistinguishComment}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::comment::{Comment, CommentUpdateForm}, @@ -23,8 +23,7 @@ impl Perform for DistinguishComment { _websocket_id: Option, ) -> Result { let data: &DistinguishComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; diff --git a/crates/api/src/comment/like.rs b/crates/api/src/comment/like.rs index 6946b60c8..b6e7e5043 100644 --- a/crates/api/src/comment/like.rs +++ b/crates/api/src/comment/like.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, CreateCommentLike}, context::LemmyContext, - utils::{check_community_ban, check_downvotes_enabled, get_local_user_view_from_jwt}, + utils::{check_community_ban, check_downvotes_enabled, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -30,8 +30,7 @@ impl Perform for CreateCommentLike { ) -> Result { let data: &CreateCommentLike = self; let local_site = LocalSite::read(context.pool()).await?; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let mut recipient_ids = Vec::::new(); diff --git a/crates/api/src/comment/save.rs b/crates/api/src/comment/save.rs index 03051f6cc..d52b3322d 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, SaveComment}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::comment::{CommentSaved, CommentSavedForm}, @@ -23,8 +23,7 @@ impl Perform for SaveComment { _websocket_id: Option, ) -> Result { let data: &SaveComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_saved_form = CommentSavedForm { comment_id: data.comment_id, diff --git a/crates/api/src/comment_report/create.rs b/crates/api/src/comment_report/create.rs index fe2471ede..8d016bf53 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentReportResponse, CreateCommentReport}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt, send_new_report_email_to_admins}, + utils::{check_community_ban, local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -28,8 +28,7 @@ impl Perform for CreateCommentReport { websocket_id: Option, ) -> Result { let data: &CreateCommentReport = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let reason = self.reason.trim(); diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index 8aa3fcbcd..2cd9ca1bd 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{ListCommentReports, ListCommentReportsResponse}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views::comment_report_view::CommentReportQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -21,8 +21,7 @@ impl Perform for ListCommentReports { _websocket_id: Option, ) -> Result { let data: &ListCommentReports = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; let admin = local_user_view.person.admin; diff --git a/crates/api/src/comment_report/resolve.rs b/crates/api/src/comment_report/resolve.rs index f5fd6b124..4ba45481f 100644 --- a/crates/api/src/comment_report/resolve.rs +++ b/crates/api/src/comment_report/resolve.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentReportResponse, ResolveCommentReport}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{is_mod_or_admin, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{source::comment_report::CommentReport, traits::Reportable}; @@ -22,8 +22,7 @@ impl Perform for ResolveCommentReport { websocket_id: Option, ) -> Result { let data: &ResolveCommentReport = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let report_id = data.report_id; let person_id = local_user_view.person.id; diff --git a/crates/api/src/community/add_mod.rs b/crates/api/src/community/add_mod.rs index 7052f7cdf..80c470de9 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{AddModToCommunity, AddModToCommunityResponse}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{is_mod_or_admin, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -27,8 +27,7 @@ impl Perform for AddModToCommunity { websocket_id: Option, ) -> Result { let data: &AddModToCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index c19df91ff..47cb6798f 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{BanFromCommunity, BanFromCommunityResponse}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_mod_or_admin, remove_user_data_in_community}, + utils::{is_mod_or_admin, local_user_view_from_jwt, remove_user_data_in_community}, websocket::{ handlers::messages::SendCommunityRoomMessage, serialize_websocket_message, @@ -40,8 +40,7 @@ impl Perform for BanFromCommunity { websocket_id: Option, ) -> Result { let data: &BanFromCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; let banned_person_id = data.person_id; diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index 58dabf1c0..21112971f 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{BlockCommunity, BlockCommunityResponse}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for BlockCommunity { _websocket_id: Option, ) -> Result { let data: &BlockCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; let person_id = local_user_view.person.id; diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index b519d214b..9e4efb33f 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{CommunityResponse, FollowCommunity}, context::LemmyContext, - utils::{check_community_ban, check_community_deleted_or_removed, get_local_user_view_from_jwt}, + utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for FollowCommunity { _websocket_id: Option, ) -> Result { let data: &FollowCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let community_id = data.community_id; let community = Community::read(context.pool(), community_id).await?; diff --git a/crates/api/src/community/hide.rs b/crates/api/src/community/hide.rs index 8e1811578..e33bf32ca 100644 --- a/crates/api/src/community/hide.rs +++ b/crates/api/src/community/hide.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{CommunityResponse, HideCommunity}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -28,8 +28,7 @@ impl Perform for HideCommunity { let data: &HideCommunity = self; // Verify its a admin (only admin can hide or unhide it) - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; is_admin(&local_user_view)?; let community_form = CommunityUpdateForm::builder() diff --git a/crates/api/src/community/transfer.rs b/crates/api/src/community/transfer.rs index d96ba8488..def3df0fa 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -4,7 +4,7 @@ use anyhow::Context; use lemmy_api_common::{ community::{GetCommunityResponse, TransferCommunity}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_admin, is_top_mod}, + utils::{is_admin, is_top_mod, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::{ @@ -29,8 +29,7 @@ impl Perform for TransferCommunity { _websocket_id: Option, ) -> Result { let data: &TransferCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Fetch the community mods let community_id = data.community_id; diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index ad8b67e12..5de584650 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{AddAdmin, AddAdminResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -27,8 +27,7 @@ impl Perform for AddAdmin { websocket_id: Option, ) -> Result { let data: &AddAdmin = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index d2b780433..014f98b8d 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{BanPerson, BanPersonResponse}, - utils::{get_local_user_view_from_jwt, is_admin, remove_user_data}, + utils::{is_admin, local_user_view_from_jwt, remove_user_data}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -31,8 +31,7 @@ impl Perform for BanPerson { websocket_id: Option, ) -> Result { let data: &BanPerson = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; diff --git a/crates/api/src/local_user/block.rs b/crates/api/src/local_user/block.rs index 6dbf7cffa..5a4c9dca0 100644 --- a/crates/api/src/local_user/block.rs +++ b/crates/api/src/local_user/block.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{BlockPerson, BlockPersonResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::person_block::{PersonBlock, PersonBlockForm}, @@ -23,8 +23,7 @@ impl Perform for BlockPerson { _websocket_id: Option, ) -> Result { let data: &BlockPerson = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let target_id = data.person_id; let person_id = local_user_view.person.id; diff --git a/crates/api/src/local_user/change_password.rs b/crates/api/src/local_user/change_password.rs index 2d0fd30e5..2b486a5ed 100644 --- a/crates/api/src/local_user/change_password.rs +++ b/crates/api/src/local_user/change_password.rs @@ -4,7 +4,7 @@ use bcrypt::verify; use lemmy_api_common::{ context::LemmyContext, person::{ChangePassword, LoginResponse}, - utils::{get_local_user_view_from_jwt, password_length_check}, + utils::{local_user_view_from_jwt, password_length_check}, }; use lemmy_db_schema::source::local_user::LocalUser; use lemmy_utils::{claims::Claims, error::LemmyError, ConnectionId}; @@ -20,8 +20,7 @@ impl Perform for ChangePassword { _websocket_id: Option, ) -> Result { let data: &ChangePassword = self; - let local_user_view = - get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?; password_length_check(&data.new_password)?; diff --git a/crates/api/src/local_user/list_banned.rs b/crates/api/src/local_user/list_banned.rs index 3fd19f558..e1d0f6e75 100644 --- a/crates/api/src/local_user/list_banned.rs +++ b/crates/api/src/local_user/list_banned.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{BannedPersonsResponse, GetBannedPersons}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -18,8 +18,7 @@ impl Perform for GetBannedPersons { _websocket_id: Option, ) -> Result { let data: &GetBannedPersons = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; diff --git a/crates/api/src/local_user/notifications/list_mentions.rs b/crates/api/src/local_user/notifications/list_mentions.rs index c03e9d167..fbafdb52a 100644 --- a/crates/api/src/local_user/notifications/list_mentions.rs +++ b/crates/api/src/local_user/notifications/list_mentions.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetPersonMentions, GetPersonMentionsResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views_actor::person_mention_view::PersonMentionQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl Perform for GetPersonMentions { _websocket_id: Option, ) -> Result { let data: &GetPersonMentions = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let sort = data.sort; let page = data.page; diff --git a/crates/api/src/local_user/notifications/list_replies.rs b/crates/api/src/local_user/notifications/list_replies.rs index 585db5e95..797b6f8d4 100644 --- a/crates/api/src/local_user/notifications/list_replies.rs +++ b/crates/api/src/local_user/notifications/list_replies.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetReplies, GetRepliesResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views_actor::comment_reply_view::CommentReplyQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl Perform for GetReplies { _websocket_id: Option, ) -> Result { let data: &GetReplies = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let sort = data.sort; let page = data.page; diff --git a/crates/api/src/local_user/notifications/mark_all_read.rs b/crates/api/src/local_user/notifications/mark_all_read.rs index 2515715bd..dfda897a9 100644 --- a/crates/api/src/local_user/notifications/mark_all_read.rs +++ b/crates/api/src/local_user/notifications/mark_all_read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetRepliesResponse, MarkAllAsRead}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::source::{ comment_reply::CommentReply, @@ -23,8 +23,7 @@ impl Perform for MarkAllAsRead { _websocket_id: Option, ) -> Result { let data: &MarkAllAsRead = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; // Mark all comment_replies as read diff --git a/crates/api/src/local_user/notifications/mark_mention_read.rs b/crates/api/src/local_user/notifications/mark_mention_read.rs index a3a75d929..80781e72a 100644 --- a/crates/api/src/local_user/notifications/mark_mention_read.rs +++ b/crates/api/src/local_user/notifications/mark_mention_read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{MarkPersonMentionAsRead, PersonMentionResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::person_mention::{PersonMention, PersonMentionUpdateForm}, @@ -23,8 +23,7 @@ impl Perform for MarkPersonMentionAsRead { _websocket_id: Option, ) -> Result { let data: &MarkPersonMentionAsRead = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_mention_id = data.person_mention_id; let read_person_mention = PersonMention::read(context.pool(), person_mention_id).await?; diff --git a/crates/api/src/local_user/notifications/mark_reply_read.rs b/crates/api/src/local_user/notifications/mark_reply_read.rs index 3921e769d..1e3fad19d 100644 --- a/crates/api/src/local_user/notifications/mark_reply_read.rs +++ b/crates/api/src/local_user/notifications/mark_reply_read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{CommentReplyResponse, MarkCommentReplyAsRead}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::comment_reply::{CommentReply, CommentReplyUpdateForm}, @@ -23,8 +23,7 @@ impl Perform for MarkCommentReplyAsRead { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_reply_id = data.comment_reply_id; let read_comment_reply = CommentReply::read(context.pool(), comment_reply_id).await?; diff --git a/crates/api/src/local_user/notifications/unread_count.rs b/crates/api/src/local_user/notifications/unread_count.rs index 715ccd286..523e9a73f 100644 --- a/crates/api/src/local_user/notifications/unread_count.rs +++ b/crates/api/src/local_user/notifications/unread_count.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetUnreadCount, GetUnreadCountResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views::structs::PrivateMessageView; use lemmy_db_views_actor::structs::{CommentReplyView, PersonMentionView}; @@ -20,8 +20,7 @@ impl Perform for GetUnreadCount { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; diff --git a/crates/api/src/local_user/report_count.rs b/crates/api/src/local_user/report_count.rs index b59a06490..b2f4912cc 100644 --- a/crates/api/src/local_user/report_count.rs +++ b/crates/api/src/local_user/report_count.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetReportCount, GetReportCountResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views::structs::{CommentReportView, PostReportView, PrivateMessageReportView}; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl Perform for GetReportCount { _websocket_id: Option, ) -> Result { let data: &GetReportCount = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; let admin = local_user_view.person.admin; diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 0221d0ce2..a04086125 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, person::{LoginResponse, SaveUserSettings}, - utils::{get_local_user_view_from_jwt, send_verification_email}, + utils::{local_user_view_from_jwt, send_verification_email}, }; use lemmy_db_schema::{ source::{ @@ -39,8 +39,7 @@ impl Perform for SaveUserSettings { _websocket_id: Option, ) -> Result { let data: &SaveUserSettings = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let site_view = SiteView::read_local(context.pool()).await?; let avatar = diesel_option_overwrite_to_url(&data.avatar)?; diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index e1dd1e3d8..6d9f05f07 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -6,9 +6,9 @@ use lemmy_api_common::{ utils::{ check_community_ban, check_community_deleted_or_removed, - get_local_user_view_from_jwt, is_admin, is_mod_or_admin, + local_user_view_from_jwt, }, websocket::UserOperation, }; @@ -33,8 +33,7 @@ impl Perform for FeaturePost { websocket_id: Option, ) -> Result { let data: &FeaturePost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; let orig_post = Post::read(context.pool(), post_id).await?; diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index 9fa480a5b..2413a983c 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -7,7 +7,7 @@ use lemmy_api_common::{ check_community_ban, check_community_deleted_or_removed, check_downvotes_enabled, - get_local_user_view_from_jwt, + local_user_view_from_jwt, mark_post_as_read, }, websocket::UserOperation, @@ -32,8 +32,7 @@ impl Perform for CreatePostLike { websocket_id: Option, ) -> Result { let data: &CreatePostLike = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Don't do a downvote if site has downvotes disabled diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index cd7db9084..426436400 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -6,8 +6,8 @@ use lemmy_api_common::{ utils::{ check_community_ban, check_community_deleted_or_removed, - get_local_user_view_from_jwt, is_mod_or_admin, + local_user_view_from_jwt, }, websocket::UserOperation, }; @@ -31,8 +31,7 @@ impl Perform for LockPost { websocket_id: Option, ) -> Result { let data: &LockPost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; let orig_post = Post::read(context.pool(), post_id).await?; diff --git a/crates/api/src/post/mark_read.rs b/crates/api/src/post/mark_read.rs index 858d71d22..e53d7a78f 100644 --- a/crates/api/src/post/mark_read.rs +++ b/crates/api/src/post/mark_read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{MarkPostAsRead, PostResponse}, - utils::{get_local_user_view_from_jwt, mark_post_as_read, mark_post_as_unread}, + utils::{local_user_view_from_jwt, mark_post_as_read, mark_post_as_unread}, }; use lemmy_db_views::structs::PostView; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl Perform for MarkPostAsRead { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; let person_id = local_user_view.person.id; diff --git a/crates/api/src/post/save.rs b/crates/api/src/post/save.rs index e0c7c8168..f3ef6319c 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{PostResponse, SavePost}, - utils::{get_local_user_view_from_jwt, mark_post_as_read}, + utils::{local_user_view_from_jwt, mark_post_as_read}, }; use lemmy_db_schema::{ source::post::{PostSaved, PostSavedForm}, @@ -23,8 +23,7 @@ impl Perform for SavePost { _websocket_id: Option, ) -> Result { let data: &SavePost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_saved_form = PostSavedForm { post_id: data.post_id, diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index 423f8b814..092f1cd8d 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{CreatePostReport, PostReportResponse}, - utils::{check_community_ban, get_local_user_view_from_jwt, send_new_report_email_to_admins}, + utils::{check_community_ban, local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -28,8 +28,7 @@ impl Perform for CreatePostReport { websocket_id: Option, ) -> Result { let data: &CreatePostReport = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let reason = self.reason.trim(); diff --git a/crates/api/src/post_report/list.rs b/crates/api/src/post_report/list.rs index 2feed7ed2..f59270424 100644 --- a/crates/api/src/post_report/list.rs +++ b/crates/api/src/post_report/list.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{ListPostReports, ListPostReportsResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views::post_report_view::PostReportQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -21,8 +21,7 @@ impl Perform for ListPostReports { _websocket_id: Option, ) -> Result { let data: &ListPostReports = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let person_id = local_user_view.person.id; let admin = local_user_view.person.admin; diff --git a/crates/api/src/post_report/resolve.rs b/crates/api/src/post_report/resolve.rs index 0426262df..80290b2a3 100644 --- a/crates/api/src/post_report/resolve.rs +++ b/crates/api/src/post_report/resolve.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{PostReportResponse, ResolvePostReport}, - utils::{get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{is_mod_or_admin, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{source::post_report::PostReport, traits::Reportable}; @@ -22,8 +22,7 @@ impl Perform for ResolvePostReport { websocket_id: Option, ) -> Result { let data: &ResolvePostReport = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let report_id = data.report_id; let person_id = local_user_view.person.id; diff --git a/crates/api/src/private_message/mark_read.rs b/crates/api/src/private_message/mark_read.rs index ba5cf8eaa..bf373e99c 100644 --- a/crates/api/src/private_message/mark_read.rs +++ b/crates/api/src/private_message/mark_read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -23,8 +23,7 @@ impl Perform for MarkPrivateMessageAsRead { websocket_id: Option, ) -> Result { let data: &MarkPrivateMessageAsRead = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Checking permissions let private_message_id = data.private_message_id; diff --git a/crates/api/src/private_message_report/create.rs b/crates/api/src/private_message_report/create.rs index 2a132b6f4..a9ca0291d 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse}, - utils::{get_local_user_view_from_jwt, send_new_report_email_to_admins}, + utils::{local_user_view_from_jwt, send_new_report_email_to_admins}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -28,8 +28,7 @@ impl Perform for CreatePrivateMessageReport { context: &Data, websocket_id: Option, ) -> Result { - let local_user_view = - get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let reason = self.reason.trim(); diff --git a/crates/api/src/private_message_report/list.rs b/crates/api/src/private_message_report/list.rs index 98c2d9269..69b3dbf94 100644 --- a/crates/api/src/private_message_report/list.rs +++ b/crates/api/src/private_message_report/list.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{ListPrivateMessageReports, ListPrivateMessageReportsResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_views::private_message_report_view::PrivateMessageReportQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -18,8 +18,7 @@ impl Perform for ListPrivateMessageReports { context: &Data, _websocket_id: Option, ) -> Result { - let local_user_view = - get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; is_admin(&local_user_view)?; diff --git a/crates/api/src/private_message_report/resolve.rs b/crates/api/src/private_message_report/resolve.rs index 9fe014c0f..09e3b251a 100644 --- a/crates/api/src/private_message_report/resolve.rs +++ b/crates/api/src/private_message_report/resolve.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{PrivateMessageReportResponse, ResolvePrivateMessageReport}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -24,8 +24,7 @@ impl Perform for ResolvePrivateMessageReport { context: &Data, websocket_id: Option, ) -> Result { - let local_user_view = - get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; is_admin(&local_user_view)?; diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 4cec5fc4a..a7c0941d1 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{GetSiteResponse, LeaveAdmin}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::{ @@ -30,8 +30,7 @@ impl Perform for LeaveAdmin { _websocket_id: Option, ) -> Result { let data: &LeaveAdmin = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; is_admin(&local_user_view)?; diff --git a/crates/api/src/site/mod_log.rs b/crates/api/src/site/mod_log.rs index ec3ee0f70..dcb5f5edf 100644 --- a/crates/api/src/site/mod_log.rs +++ b/crates/api/src/site/mod_log.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{GetModlog, GetModlogResponse}, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin, is_mod_or_admin}, + utils::{check_private_instance, is_admin, is_mod_or_admin, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, @@ -43,9 +43,7 @@ impl Perform for GetModlog { ) -> Result { let data: &GetModlog = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 9664a6288..f4ba45097 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{PurgeComment, PurgeItemResponse}, - utils::{get_local_user_view_from_jwt, is_top_admin}, + utils::{is_top_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::{ @@ -25,8 +25,7 @@ impl Perform for PurgeComment { _websocket_id: Option, ) -> Result { let data: &Self = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Only let the top admin purge an item is_top_admin(context.pool(), local_user_view.person.id).await?; diff --git a/crates/api/src/site/purge/community.rs b/crates/api/src/site/purge/community.rs index abc4ff328..5bdf62e75 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ context::LemmyContext, request::purge_image_from_pictrs, site::{PurgeCommunity, PurgeItemResponse}, - utils::{get_local_user_view_from_jwt, is_top_admin, purge_image_posts_for_community}, + utils::{is_top_admin, local_user_view_from_jwt, purge_image_posts_for_community}, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for PurgeCommunity { _websocket_id: Option, ) -> Result { let data: &Self = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Only let the top admin purge an item is_top_admin(context.pool(), local_user_view.person.id).await?; diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index 94c80a928..896dc783c 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ context::LemmyContext, request::purge_image_from_pictrs, site::{PurgeItemResponse, PurgePerson}, - utils::{get_local_user_view_from_jwt, is_top_admin, purge_image_posts_for_person}, + utils::{is_top_admin, local_user_view_from_jwt, purge_image_posts_for_person}, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for PurgePerson { _websocket_id: Option, ) -> Result { let data: &Self = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Only let the top admin purge an item is_top_admin(context.pool(), local_user_view.person.id).await?; diff --git a/crates/api/src/site/purge/post.rs b/crates/api/src/site/purge/post.rs index 27f908188..c04fc5601 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ context::LemmyContext, request::purge_image_from_pictrs, site::{PurgeItemResponse, PurgePost}, - utils::{get_local_user_view_from_jwt, is_top_admin}, + utils::{is_top_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for PurgePost { _websocket_id: Option, ) -> Result { let data: &Self = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Only let the top admin purge an item is_top_admin(context.pool(), local_user_view.person.id).await?; diff --git a/crates/api/src/site/registration_applications/approve.rs b/crates/api/src/site/registration_applications/approve.rs index 61a686899..edba7f2b8 100644 --- a/crates/api/src/site/registration_applications/approve.rs +++ b/crates/api/src/site/registration_applications/approve.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{ApproveRegistrationApplication, RegistrationApplicationResponse}, - utils::{get_local_user_view_from_jwt, is_admin, send_application_approved_email}, + utils::{is_admin, local_user_view_from_jwt, send_application_approved_email}, }; use lemmy_db_schema::{ source::{ @@ -26,8 +26,7 @@ impl Perform for ApproveRegistrationApplication { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let app_id = data.id; diff --git a/crates/api/src/site/registration_applications/list.rs b/crates/api/src/site/registration_applications/list.rs index 7be2e2e8e..b5a366e16 100644 --- a/crates/api/src/site/registration_applications/list.rs +++ b/crates/api/src/site/registration_applications/list.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{ListRegistrationApplications, ListRegistrationApplicationsResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_views::registration_application_view::RegistrationApplicationQuery; @@ -20,8 +20,7 @@ impl Perform for ListRegistrationApplications { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Make sure user is an admin diff --git a/crates/api/src/site/registration_applications/unread_count.rs b/crates/api/src/site/registration_applications/unread_count.rs index fe33d17c8..72490e480 100644 --- a/crates/api/src/site/registration_applications/unread_count.rs +++ b/crates/api/src/site/registration_applications/unread_count.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, site::{GetUnreadRegistrationApplicationCount, GetUnreadRegistrationApplicationCountResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_views::structs::RegistrationApplicationView; @@ -19,8 +19,7 @@ impl Perform for GetUnreadRegistrationApplicationCount { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Only let admins do this diff --git a/crates/api/src/websocket.rs b/crates/api/src/websocket.rs index 522991ae9..0552c1906 100644 --- a/crates/api/src/websocket.rs +++ b/crates/api/src/websocket.rs @@ -2,7 +2,7 @@ use crate::Perform; use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::{ handlers::join_rooms::{JoinCommunityRoom, JoinModRoom, JoinPostRoom, JoinUserRoom}, structs::{ @@ -30,8 +30,7 @@ impl Perform for UserJoin { websocket_id: Option, ) -> Result { let data: &UserJoin = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; if let Some(id) = websocket_id { context.chat_server().do_send(JoinUserRoom { diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 75f878fc4..522a35647 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -1,6 +1,12 @@ -use crate::{request::purge_image_from_pictrs, sensitive::Sensitive, site::FederatedInstances}; +use crate::{ + context::LemmyContext, + request::purge_image_from_pictrs, + sensitive::Sensitive, + site::FederatedInstances, +}; use anyhow::Context; use chrono::NaiveDateTime; +use futures::try_join; use lemmy_db_schema::{ impls::person::is_banned, newtypes::{CommunityId, DbUrl, LocalUserId, PersonId, PostId}, @@ -16,7 +22,6 @@ use lemmy_db_schema::{ person_block::PersonBlock, post::{Post, PostRead, PostReadForm}, registration_application::RegistrationApplication, - secret::Secret, }, traits::{Crud, Readable}, utils::DbPool, @@ -141,18 +146,16 @@ pub async fn mark_post_as_unread( .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read")) } -// TODO: this should simply take LemmyContext as param #[tracing::instrument(skip_all)] -pub async fn get_local_user_view_from_jwt( +pub async fn local_user_view_from_jwt( jwt: &str, - pool: &DbPool, - secret: &Secret, + context: &LemmyContext, ) -> Result { - let claims = Claims::decode(jwt, &secret.jwt_secret) + let claims = Claims::decode(jwt, &context.secret().jwt_secret) .map_err(|e| e.with_message("not_logged_in"))? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(pool, local_user_id).await?; + let local_user_view = LocalUserView::read(context.pool(), local_user_id).await?; check_user_valid( local_user_view.person.banned, local_user_view.person.ban_expires, @@ -164,6 +167,14 @@ pub async fn get_local_user_view_from_jwt( Ok(local_user_view) } +#[tracing::instrument(skip_all)] +pub async fn local_user_view_from_jwt_opt( + jwt: Option<&Sensitive>, + context: &LemmyContext, +) -> Option { + local_user_view_from_jwt(jwt?, context).await.ok() +} + /// Checks if user's token was issued before user's password reset. pub fn check_validator_time( validator_time: &NaiveDateTime, @@ -177,44 +188,6 @@ pub fn check_validator_time( } } -#[tracing::instrument(skip_all)] -pub async fn get_local_user_view_from_jwt_opt( - jwt: Option<&Sensitive>, - pool: &DbPool, - secret: &Secret, -) -> Result, LemmyError> { - match jwt { - Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool, secret).await?)), - None => Ok(None), - } -} - -#[tracing::instrument(skip_all)] -pub async fn get_local_user_settings_view_from_jwt_opt( - jwt: Option<&Sensitive>, - pool: &DbPool, - secret: &Secret, -) -> Result, LemmyError> { - match jwt { - Some(jwt) => { - let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret) - .map_err(|e| e.with_message("not_logged_in"))? - .claims; - let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserView::read(pool, local_user_id).await?; - check_user_valid( - local_user_view.person.banned, - local_user_view.person.ban_expires, - local_user_view.person.deleted, - )?; - - check_validator_time(&local_user_view.local_user.validator_time, &claims)?; - - Ok(Some(local_user_view)) - } - None => Ok(None), - } -} pub fn check_user_valid( banned: bool, ban_expires: Option, @@ -314,9 +287,11 @@ pub async fn build_federated_instances( ) -> Result, LemmyError> { if local_site.federation_enabled { // TODO I hate that this requires 3 queries - let linked = Instance::linked(pool).await?; - let allowed = Instance::allowlist(pool).await?; - let blocked = Instance::blocklist(pool).await?; + let (linked, allowed, blocked) = try_join!( + Instance::linked(pool), + Instance::allowlist(pool), + Instance::blocklist(pool) + )?; Ok(Some(FederatedInstances { linked, diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 103c71ce6..8909444d2 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -8,9 +8,9 @@ use lemmy_api_common::{ check_community_deleted_or_removed, check_post_deleted_or_removed, generate_local_apub_endpoint, - get_local_user_view_from_jwt, get_post, local_site_to_slur_regex, + local_user_view_from_jwt, EndpointType, }, websocket::UserOperationCrud, @@ -46,8 +46,7 @@ impl PerformCrud for CreateComment { websocket_id: Option, ) -> Result { let data: &CreateComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let content_slurs_removed = remove_slurs( diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index a96b4cc7a..cde4ab629 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, DeleteComment}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt}, + utils::{check_community_ban, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -27,8 +27,7 @@ impl PerformCrud for DeleteComment { websocket_id: Option, ) -> Result { let data: &DeleteComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index c0136f06c..bd37eb77c 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, GetComment}, context::LemmyContext, - utils::{check_private_instance, get_local_user_view_from_jwt_opt}, + utils::{check_private_instance, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_views::structs::CommentView; @@ -20,9 +20,7 @@ impl PerformCrud for GetComment { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index add01aa30..b8834df8d 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, RemoveComment}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -28,8 +28,7 @@ impl PerformCrud for RemoveComment { websocket_id: Option, ) -> Result { let data: &RemoveComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let comment_id = data.comment_id; let orig_comment = CommentView::read(context.pool(), comment_id, None).await?; diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index 1d6d0869d..e74d5c4ed 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ comment::{CommentResponse, EditComment}, context::LemmyContext, - utils::{check_community_ban, get_local_user_view_from_jwt, local_site_to_slur_regex}, + utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -37,8 +37,7 @@ impl PerformCrud for EditComment { websocket_id: Option, ) -> Result { let data: &EditComment = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let comment_id = data.comment_id; diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index e2e80b849..fbc557b72 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -9,9 +9,9 @@ use lemmy_api_common::{ generate_inbox_url, generate_local_apub_endpoint, generate_shared_inbox_url, - get_local_user_view_from_jwt, is_admin, local_site_to_slur_regex, + local_user_view_from_jwt, EndpointType, }, }; @@ -52,8 +52,7 @@ impl PerformCrud for CreateCommunity { _websocket_id: Option, ) -> Result { let data: &CreateCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let site_view = SiteView::read_local(context.pool()).await?; let local_site = site_view.local_site; diff --git a/crates/api_crud/src/community/delete.rs b/crates/api_crud/src/community/delete.rs index 7ce685d8b..9909f5cdd 100644 --- a/crates/api_crud/src/community/delete.rs +++ b/crates/api_crud/src/community/delete.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{CommunityResponse, DeleteCommunity}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_top_mod}, + utils::{is_top_mod, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -24,8 +24,7 @@ impl PerformCrud for DeleteCommunity { websocket_id: Option, ) -> Result { let data: &DeleteCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Fetch the community mods let community_id = data.community_id; diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index ef4c46d81..e620e1606 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{ListCommunities, ListCommunitiesResponse}, context::LemmyContext, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin}, + utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_views_actor::community_view::CommunityQuery; @@ -20,9 +20,7 @@ impl PerformCrud for ListCommunities { _websocket_id: Option, ) -> Result { let data: &ListCommunities = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); diff --git a/crates/api_crud/src/community/remove.rs b/crates/api_crud/src/community/remove.rs index f79147622..9a905b874 100644 --- a/crates/api_crud/src/community/remove.rs +++ b/crates/api_crud/src/community/remove.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{CommunityResponse, RemoveCommunity}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -26,8 +26,7 @@ impl PerformCrud for RemoveCommunity { websocket_id: Option, ) -> Result { let data: &RemoveCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Verify its an admin (only an admin can remove a community) is_admin(&local_user_view)?; diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index 200c5f2c8..9d56ce8a1 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ community::{CommunityResponse, EditCommunity}, context::LemmyContext, - utils::{get_local_user_view_from_jwt, local_site_to_slur_regex}, + utils::{local_site_to_slur_regex, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -34,8 +34,7 @@ impl PerformCrud for EditCommunity { websocket_id: Option, ) -> Result { let data: &EditCommunity = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let icon = diesel_option_overwrite_to_url(&data.icon)?; diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index 42b6c5d91..333379451 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, custom_emoji::{CreateCustomEmoji, CustomEmojiResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::source::{ custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, @@ -24,8 +24,7 @@ impl PerformCrud for CreateCustomEmoji { _websocket_id: Option, ) -> Result { let data: &CreateCustomEmoji = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Make sure user is an admin diff --git a/crates/api_crud/src/custom_emoji/delete.rs b/crates/api_crud/src/custom_emoji/delete.rs index db8c973ec..dcca88392 100644 --- a/crates/api_crud/src/custom_emoji/delete.rs +++ b/crates/api_crud/src/custom_emoji/delete.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, custom_emoji::{DeleteCustomEmoji, DeleteCustomEmojiResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::source::custom_emoji::CustomEmoji; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl PerformCrud for DeleteCustomEmoji { _websocket_id: Option, ) -> Result { let data: &DeleteCustomEmoji = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Make sure user is an admin is_admin(&local_user_view)?; diff --git a/crates/api_crud/src/custom_emoji/update.rs b/crates/api_crud/src/custom_emoji/update.rs index 6233cbe3c..67a2bed9f 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, custom_emoji::{CustomEmojiResponse, EditCustomEmoji}, - utils::{get_local_user_view_from_jwt, is_admin}, + utils::{is_admin, local_user_view_from_jwt}, }; use lemmy_db_schema::source::{ custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, @@ -24,8 +24,7 @@ impl PerformCrud for EditCustomEmoji { _websocket_id: Option, ) -> Result { let data: &EditCustomEmoji = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Make sure user is an admin diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 1a8851847..e0c1a5d13 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -8,9 +8,9 @@ use lemmy_api_common::{ check_community_ban, check_community_deleted_or_removed, generate_local_apub_endpoint, - get_local_user_view_from_jwt, honeypot_check, local_site_to_slur_regex, + local_user_view_from_jwt, mark_post_as_read, EndpointType, }, @@ -50,8 +50,7 @@ impl PerformCrud for CreatePost { websocket_id: Option, ) -> Result { let data: &CreatePost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let slur_regex = local_site_to_slur_regex(&local_site); diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index fae5b0096..82efeaa17 100644 --- a/crates/api_crud/src/post/delete.rs +++ b/crates/api_crud/src/post/delete.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{DeletePost, PostResponse}, - utils::{check_community_ban, check_community_deleted_or_removed, get_local_user_view_from_jwt}, + utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -23,8 +23,7 @@ impl PerformCrud for DeletePost { websocket_id: Option, ) -> Result { let data: &DeletePost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; let orig_post = Post::read(context.pool(), post_id).await?; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index eadb6d11a..cda2f65b6 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -5,8 +5,8 @@ use lemmy_api_common::{ post::{GetPost, GetPostResponse}, utils::{ check_private_instance, - get_local_user_view_from_jwt_opt, is_mod_or_admin_opt, + local_user_view_from_jwt_opt, mark_post_as_read, }, websocket::handlers::online_users::GetPostUsersOnline, @@ -31,9 +31,7 @@ impl PerformCrud for GetPost { _websocket_id: Option, ) -> Result { let data: &GetPost = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs index b53a468e9..9081d911e 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, post::{PostResponse, RemovePost}, - utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin}, + utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -26,8 +26,7 @@ impl PerformCrud for RemovePost { websocket_id: Option, ) -> Result { let data: &RemovePost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let post_id = data.post_id; let orig_post = Post::read(context.pool(), post_id).await?; diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 8f5c442fc..2bd101c09 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ context::LemmyContext, post::{EditPost, PostResponse}, request::fetch_site_data, - utils::{check_community_ban, get_local_user_view_from_jwt, local_site_to_slur_regex}, + utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -36,8 +36,7 @@ impl PerformCrud for EditPost { websocket_id: Option, ) -> Result { let data: &EditPost = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let data_url = data.url.as_ref(); diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 48fa84023..6fd369008 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -7,8 +7,8 @@ use lemmy_api_common::{ check_person_block, generate_local_apub_endpoint, get_interface_language, - get_local_user_view_from_jwt, local_site_to_slur_regex, + local_user_view_from_jwt, send_email_to_user, EndpointType, }, @@ -39,8 +39,7 @@ impl PerformCrud for CreatePrivateMessage { websocket_id: Option, ) -> Result { let data: &CreatePrivateMessage = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let content_slurs_removed = remove_slurs( diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index e6a643a7f..f494edffa 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{DeletePrivateMessage, PrivateMessageResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -23,8 +23,7 @@ impl PerformCrud for DeletePrivateMessage { websocket_id: Option, ) -> Result { let data: &DeletePrivateMessage = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; // Checking permissions let private_message_id = data.private_message_id; diff --git a/crates/api_crud/src/private_message/read.rs b/crates/api_crud/src/private_message/read.rs index c9a377533..da2bc690b 100644 --- a/crates/api_crud/src/private_message/read.rs +++ b/crates/api_crud/src/private_message/read.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{GetPrivateMessages, PrivateMessagesResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_views::private_message_view::PrivateMessageQuery; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl PerformCrud for GetPrivateMessages { _websocket_id: Option, ) -> Result { let data: &GetPrivateMessages = self; - let local_user_view = - get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?; let person_id = local_user_view.person.id; let page = data.page; diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index 6a6bfa218..a485a5093 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -3,7 +3,7 @@ use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, private_message::{EditPrivateMessage, PrivateMessageResponse}, - utils::{get_local_user_view_from_jwt, local_site_to_slur_regex}, + utils::{local_site_to_slur_regex, local_user_view_from_jwt}, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -31,8 +31,7 @@ impl PerformCrud for EditPrivateMessage { websocket_id: Option, ) -> Result { let data: &EditPrivateMessage = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; // Checking permissions diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index d45caaa8a..0f3e8c56a 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -6,10 +6,10 @@ use lemmy_api_common::{ site::{CreateSite, SiteResponse}, utils::{ generate_site_inbox_url, - get_local_user_view_from_jwt, is_admin, local_site_rate_limit_to_rate_limit_config, local_site_to_slur_regex, + local_user_view_from_jwt, site_description_length_check, }, }; @@ -52,8 +52,7 @@ impl PerformCrud for CreateSite { return Err(LemmyError::from_message("site_already_exists")); }; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let sidebar = diesel_option_overwrite(&data.sidebar); let description = diesel_option_overwrite(&data.description); diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index fc74ecaeb..1e40cabdd 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -2,16 +2,20 @@ use crate::PerformCrud; use actix_web::web::Data; use lemmy_api_common::{ context::LemmyContext, + sensitive::Sensitive, site::{GetSite, GetSiteResponse, MyUserInfo}, - utils::get_local_user_settings_view_from_jwt_opt, + utils::{check_user_valid, check_validator_time}, websocket::handlers::online_users::GetUsersOnline, }; -use lemmy_db_schema::source::{ - actor_language::{LocalUserLanguage, SiteLanguage}, - language::Language, - tagline::Tagline, +use lemmy_db_schema::{ + newtypes::LocalUserId, + source::{ + actor_language::{LocalUserLanguage, SiteLanguage}, + language::Language, + tagline::Tagline, + }, }; -use lemmy_db_views::structs::{CustomEmojiView, SiteView}; +use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; use lemmy_db_views_actor::structs::{ CommunityBlockView, CommunityFollowerView, @@ -19,7 +23,7 @@ use lemmy_db_views_actor::structs::{ PersonBlockView, PersonView, }; -use lemmy_utils::{error::LemmyError, version, ConnectionId}; +use lemmy_utils::{claims::Claims, error::LemmyError, version, ConnectionId}; #[async_trait::async_trait(?Send)] impl PerformCrud for GetSite { @@ -40,12 +44,8 @@ impl PerformCrud for GetSite { let online = context.chat_server().send(GetUsersOnline).await?; // Build the local user - let my_user = if let Some(local_user_view) = get_local_user_settings_view_from_jwt_opt( - data.auth.as_ref(), - context.pool(), - context.secret(), - ) - .await? + let my_user = if let Some(local_user_view) = + local_user_settings_view_from_jwt_opt(data.auth.as_ref(), context).await { let person_id = local_user_view.person.id; let local_user_id = local_user_view.local_user.id; @@ -102,3 +102,32 @@ impl PerformCrud for GetSite { }) } } + +#[tracing::instrument(skip_all)] +async fn local_user_settings_view_from_jwt_opt( + jwt: Option<&Sensitive>, + context: &LemmyContext, +) -> Option { + match jwt { + Some(jwt) => { + let claims = Claims::decode(jwt.as_ref(), &context.secret().jwt_secret) + .ok()? + .claims; + let local_user_id = LocalUserId(claims.sub); + let local_user_view = LocalUserView::read(context.pool(), local_user_id) + .await + .ok()?; + check_user_valid( + local_user_view.person.banned, + local_user_view.person.ban_expires, + local_user_view.person.deleted, + ) + .ok()?; + + check_validator_time(&local_user_view.local_user.validator_time, &claims).ok()?; + + Some(local_user_view) + } + None => None, + } +} diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 03ff59013..693be6904 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -4,10 +4,10 @@ use lemmy_api_common::{ context::LemmyContext, site::{EditSite, SiteResponse}, utils::{ - get_local_user_view_from_jwt, is_admin, local_site_rate_limit_to_rate_limit_config, local_site_to_slur_regex, + local_user_view_from_jwt, site_description_length_check, }, websocket::UserOperationCrud, @@ -46,8 +46,7 @@ impl PerformCrud for EditSite { websocket_id: Option, ) -> Result { let data: &EditSite = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let site_view = SiteView::read_local(context.pool()).await?; let local_site = site_view.local_site; let site = site_view.site; diff --git a/crates/api_crud/src/user/delete.rs b/crates/api_crud/src/user/delete.rs index b49695eac..731f90721 100644 --- a/crates/api_crud/src/user/delete.rs +++ b/crates/api_crud/src/user/delete.rs @@ -4,7 +4,7 @@ use bcrypt::verify; use lemmy_api_common::{ context::LemmyContext, person::{DeleteAccount, DeleteAccountResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_utils::{error::LemmyError, ConnectionId}; @@ -19,8 +19,7 @@ impl PerformCrud for DeleteAccount { _websocket_id: Option, ) -> Result { let data = self; - let local_user_view = - get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?; // Verify the password let valid: bool = verify( diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index a1315bb78..a791ea40e 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -16,7 +16,7 @@ use lemmy_api_common::{ community::{BanFromCommunity, BanFromCommunityResponse}, context::LemmyContext, person::{BanPerson, BanPersonResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::{community::Community, person::Person, site::Site}, @@ -138,8 +138,7 @@ impl SendActivity for BanPerson { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let person = Person::read(context.pool(), request.person_id).await?; let site = SiteOrCommunity::Site(SiteView::read_local(context.pool()).await?.site.into()); let expires = request.expires.map(naive_from_unix); @@ -182,8 +181,7 @@ impl SendActivity for BanFromCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community: ApubCommunity = Community::read(context.pool(), request.community_id) .await? .into(); diff --git a/crates/apub/src/activities/community/collection_add.rs b/crates/apub/src/activities/community/collection_add.rs index dfab0baf6..afe8387f2 100644 --- a/crates/apub/src/activities/community/collection_add.rs +++ b/crates/apub/src/activities/community/collection_add.rs @@ -29,7 +29,7 @@ use lemmy_api_common::{ community::{AddModToCommunity, AddModToCommunityResponse}, context::LemmyContext, post::{FeaturePost, PostResponse}, - utils::{generate_featured_url, generate_moderators_url, get_local_user_view_from_jwt}, + utils::{generate_featured_url, generate_moderators_url, local_user_view_from_jwt}, }; use lemmy_db_schema::{ impls::community::CollectionType, @@ -177,8 +177,7 @@ impl SendActivity for AddModToCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community: ApubCommunity = Community::read(context.pool(), request.community_id) .await? .into(); @@ -214,8 +213,7 @@ impl SendActivity for FeaturePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; // Deprecated, for backwards compatibility with 0.17 CreateOrUpdatePage::send( &response.post_view.post, diff --git a/crates/apub/src/activities/community/lock_page.rs b/crates/apub/src/activities/community/lock_page.rs index 498c3a324..26326efcb 100644 --- a/crates/apub/src/activities/community/lock_page.rs +++ b/crates/apub/src/activities/community/lock_page.rs @@ -27,7 +27,7 @@ use activitypub_federation::{ use lemmy_api_common::{ context::LemmyContext, post::{LockPost, PostResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::{ @@ -115,8 +115,7 @@ impl SendActivity for LockPost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; // For backwards compat with 0.17 CreateOrUpdatePage::send( &response.post_view.post, diff --git a/crates/apub/src/activities/community/report.rs b/crates/apub/src/activities/community/report.rs index 9830be13f..aaa1e5108 100644 --- a/crates/apub/src/activities/community/report.rs +++ b/crates/apub/src/activities/community/report.rs @@ -16,7 +16,7 @@ use lemmy_api_common::{ comment::{CommentReportResponse, CreateCommentReport}, context::LemmyContext, post::{CreatePostReport, PostReportResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -39,8 +39,7 @@ impl SendActivity for CreatePostReport { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; Report::send( ObjectId::from(response.post_report_view.post.ap_id.clone()), &local_user_view.person.into(), @@ -61,8 +60,7 @@ impl SendActivity for CreateCommentReport { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; Report::send( ObjectId::from(response.comment_report_view.comment.ap_id.clone()), &local_user_view.person.into(), diff --git a/crates/apub/src/activities/community/update.rs b/crates/apub/src/activities/community/update.rs index 85581e2a1..5cf1881a3 100644 --- a/crates/apub/src/activities/community/update.rs +++ b/crates/apub/src/activities/community/update.rs @@ -20,7 +20,7 @@ use activitypub_federation::{ use lemmy_api_common::{ community::{CommunityResponse, EditCommunity, HideCommunity}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperationCrud, }; use lemmy_db_schema::{source::community::Community, traits::Crud}; @@ -36,8 +36,7 @@ impl SendActivity for EditCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } @@ -123,8 +122,7 @@ impl SendActivity for HideCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), request.community_id).await?; UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await } diff --git a/crates/apub/src/activities/deletion/delete_user.rs b/crates/apub/src/activities/deletion/delete_user.rs index e5c1021cc..11be132e8 100644 --- a/crates/apub/src/activities/deletion/delete_user.rs +++ b/crates/apub/src/activities/deletion/delete_user.rs @@ -14,7 +14,7 @@ use activitypub_federation::{ use lemmy_api_common::{ context::LemmyContext, person::{DeleteAccount, DeleteAccountResponse}, - utils::{delete_user_account, get_local_user_view_from_jwt}, + utils::{delete_user_account, local_user_view_from_jwt}, }; use lemmy_utils::error::LemmyError; use url::Url; @@ -28,8 +28,7 @@ impl SendActivity for DeleteAccount { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let actor: ApubPerson = local_user_view.person.into(); delete_user_account( actor.id, diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index 690b1d3a3..ea6f4a67d 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -34,7 +34,7 @@ use lemmy_api_common::{ context::LemmyContext, post::{DeletePost, PostResponse, RemovePost}, private_message::{DeletePrivateMessage, PrivateMessageResponse}, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperationCrud, }; use lemmy_db_schema::{ @@ -64,8 +64,7 @@ impl SendActivity for DeletePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), response.post_view.community.id).await?; let deletable = DeletableObjects::Post(response.post_view.post.clone().into()); send_apub_delete_in_community( @@ -89,8 +88,7 @@ impl SendActivity for RemovePost { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), response.post_view.community.id).await?; let deletable = DeletableObjects::Post(response.post_view.post.clone().into()); send_apub_delete_in_community( @@ -132,8 +130,7 @@ impl SendActivity for RemoveComment { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let comment = Comment::read(context.pool(), request.comment_id).await?; let community = Community::read(context.pool(), response.comment_view.community.id).await?; let deletable = DeletableObjects::Comment(comment.into()); @@ -158,8 +155,7 @@ impl SendActivity for DeletePrivateMessage { response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; send_apub_delete_private_message( &local_user_view.person.into(), response.private_message_view.private_message.clone(), @@ -179,8 +175,7 @@ impl SendActivity for DeleteCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( @@ -204,8 +199,7 @@ impl SendActivity for RemoveCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), request.community_id).await?; let deletable = DeletableObjects::Community(community.clone().into()); send_apub_delete_in_community( diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index 4e2acd518..8a785da05 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -24,7 +24,7 @@ use activitypub_federation::{ use lemmy_api_common::{ community::{BlockCommunity, BlockCommunityResponse}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{ source::{ @@ -138,8 +138,7 @@ impl SendActivity for BlockCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let community = Community::read(context.pool(), request.community_id).await?; UndoFollow::send(&local_user_view.person.into(), &community.into(), context).await } diff --git a/crates/apub/src/activities/following/mod.rs b/crates/apub/src/activities/following/mod.rs index 5b9d98b42..a4c2fd673 100644 --- a/crates/apub/src/activities/following/mod.rs +++ b/crates/apub/src/activities/following/mod.rs @@ -7,7 +7,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ community::{CommunityResponse, FollowCommunity}, context::LemmyContext, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, }; use lemmy_db_schema::{source::community::Community, traits::Crud}; use lemmy_utils::error::LemmyError; @@ -25,8 +25,7 @@ impl SendActivity for FollowCommunity { _response: &Self::Response, context: &Data, ) -> Result<(), LemmyError> { - let local_user_view = - get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&request.auth, context).await?; let person = local_user_view.person.clone().into(); let community: ApubCommunity = Community::read(context.pool(), request.community_id) .await? diff --git a/crates/apub/src/activities/voting/mod.rs b/crates/apub/src/activities/voting/mod.rs index 89832148f..60015b137 100644 --- a/crates/apub/src/activities/voting/mod.rs +++ b/crates/apub/src/activities/voting/mod.rs @@ -15,7 +15,7 @@ use lemmy_api_common::{ context::LemmyContext, post::{CreatePostLike, PostResponse}, sensitive::Sensitive, - utils::get_local_user_view_from_jwt, + utils::local_user_view_from_jwt, websocket::UserOperation, }; use lemmy_db_schema::{ @@ -85,7 +85,7 @@ async fn send_activity( context: &Data, ) -> Result<(), LemmyError> { let community = Community::read(context.pool(), community_id).await?.into(); - let local_user_view = get_local_user_view_from_jwt(jwt, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(jwt, context).await?; let actor = Person::read(context.pool(), local_user_view.person.id) .await? .into(); diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index bc0010ab5..f697a7358 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -7,7 +7,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ comment::{GetComments, GetCommentsResponse}, context::LemmyContext, - utils::{check_private_instance, get_local_user_view_from_jwt_opt}, + utils::{check_private_instance, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::{ source::{comment::Comment, community::Community, local_site::LocalSite}, @@ -27,9 +27,7 @@ impl PerformApub for GetComments { _websocket_id: Option, ) -> Result { let data: &GetComments = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 7859f7547..58c31b2e6 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -7,7 +7,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ context::LemmyContext, post::{GetPosts, GetPostsResponse}, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_mod_or_admin_opt}, + utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::source::{community::Community, local_site::LocalSite}; use lemmy_db_views::post_view::PostQuery; @@ -24,9 +24,7 @@ impl PerformApub for GetPosts { _websocket_id: Option, ) -> Result { let data: &GetPosts = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index e01c7bdaf..53469cf84 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -7,7 +7,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ community::{GetCommunity, GetCommunityResponse}, context::LemmyContext, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_mod_or_admin_opt}, + utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt}, websocket::handlers::online_users::GetCommunityUsersOnline, }; use lemmy_db_schema::source::{ @@ -30,9 +30,7 @@ impl PerformApub for GetCommunity { _websocket_id: Option, ) -> Result { let data: &GetCommunity = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; if data.name.is_none() && data.id.is_none() { diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 95d9ecc08..9d453acee 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -3,7 +3,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ context::LemmyContext, person::{GetPersonDetails, GetPersonDetailsResponse}, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin}, + utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::{ source::{local_site::LocalSite, person::Person}, @@ -30,9 +30,7 @@ impl PerformApub for GetPersonDetails { return Err(LemmyError::from_message("no_id_given")); } - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok()); diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index f0cb15e36..193494ddb 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -7,7 +7,7 @@ use diesel::NotFound; use lemmy_api_common::{ context::LemmyContext, site::{ResolveObject, ResolveObjectResponse}, - utils::{check_private_instance, get_local_user_view_from_jwt}, + utils::{check_private_instance, local_user_view_from_jwt}, }; use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool}; use lemmy_db_views::structs::{CommentView, PostView}; @@ -24,8 +24,7 @@ impl PerformApub for ResolveObject { context: &Data, _websocket_id: Option, ) -> Result { - let local_user_view = - get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?; + let local_user_view = local_user_view_from_jwt(&self.auth, context).await?; let local_site = LocalSite::read(context.pool()).await?; let person_id = local_user_view.person.id; check_private_instance(&Some(local_user_view), &local_site)?; diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index 212e037fd..6cbd6b1e5 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -7,7 +7,7 @@ use activitypub_federation::config::Data; use lemmy_api_common::{ context::LemmyContext, site::{Search, SearchResponse}, - utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin}, + utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt}, }; use lemmy_db_schema::{ source::{community::Community, local_site::LocalSite}, @@ -30,9 +30,7 @@ impl PerformApub for Search { ) -> Result { let data: &Search = self; - let local_user_view = - get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) - .await?; + let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await; let local_site = LocalSite::read(context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; diff --git a/crates/routes/src/images.rs b/crates/routes/src/images.rs index cd6555d7d..9b7801f2a 100644 --- a/crates/routes/src/images.rs +++ b/crates/routes/src/images.rs @@ -11,7 +11,7 @@ use actix_web::{ HttpResponse, }; use futures::stream::{Stream, StreamExt}; -use lemmy_api_common::{context::LemmyContext, utils::get_local_user_view_from_jwt}; +use lemmy_api_common::{context::LemmyContext, utils::local_user_view_from_jwt}; use lemmy_db_schema::source::local_site::LocalSite; use lemmy_utils::{claims::Claims, rate_limit::RateLimitCell, REQWEST_TIMEOUT}; use reqwest::Body; @@ -136,7 +136,7 @@ async fn full_res( let jwt = req .cookie("jwt") .expect("No auth header for picture access"); - if get_local_user_view_from_jwt(jwt.value(), context.pool(), context.secret()) + if local_user_view_from_jwt(jwt.value(), &context) .await .is_err() {