From d28e5245d2ae1e496993ef902e8dde7eb8c6ef70 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 12 Aug 2020 07:13:44 -0400 Subject: [PATCH 1/4] Don't allow preferred usernames to start with @. Fixes #1058 (#1076) * Don't allow preferred usernames to start with @. Fixes #1058 * Trim the preferred username. --- server/lemmy_utils/src/lib.rs | 12 ++++++++++++ server/src/api/user.rs | 8 +++++++- ui/src/components/user-details.tsx | 2 +- ui/src/components/user.tsx | 3 ++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/server/lemmy_utils/src/lib.rs b/server/lemmy_utils/src/lib.rs index fc50e199b..1a89beaa7 100644 --- a/server/lemmy_utils/src/lib.rs +++ b/server/lemmy_utils/src/lib.rs @@ -162,6 +162,11 @@ pub fn is_valid_username(name: &str) -> bool { VALID_USERNAME_REGEX.is_match(name) } +// Can't do a regex here, reverse lookarounds not supported +pub fn is_valid_preferred_username(preferred_username: &str) -> bool { + !preferred_username.starts_with("@") && preferred_username.len() >=3 && preferred_username.len() <= 20 +} + pub fn is_valid_community_name(name: &str) -> bool { VALID_COMMUNITY_NAME_REGEX.is_match(name) } @@ -176,6 +181,7 @@ mod tests { is_valid_community_name, is_valid_post_title, is_valid_username, + is_valid_preferred_username, remove_slurs, scrape_text_for_mentions, slur_check, @@ -201,6 +207,12 @@ mod tests { assert!(!is_valid_username("")); } + #[test] + fn test_valid_preferred_username() { + assert!(is_valid_preferred_username("hello @there")); + assert!(!is_valid_preferred_username("@hello there")); + } + #[test] fn test_valid_community_name() { assert!(is_valid_community_name("example")); diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 2d5895170..83d8470cd 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -51,6 +51,7 @@ use lemmy_db::{ use lemmy_utils::{ generate_actor_keypair, generate_random_string, + is_valid_preferred_username, is_valid_username, make_apub_endpoint, naive_from_unix, @@ -576,7 +577,12 @@ impl Perform for Oper { // The DB constraint should stop too many characters let preferred_username = match &data.preferred_username { - Some(preferred_username) => Some(preferred_username.to_owned()), + Some(preferred_username) => { + if !is_valid_preferred_username(preferred_username.trim()) { + return Err(APIError::err("invalid_username").into()); + } + Some(preferred_username.trim().to_string()) + } None => read_user.preferred_username, }; diff --git a/ui/src/components/user-details.tsx b/ui/src/components/user-details.tsx index 5e9a58d22..b3ce294f3 100644 --- a/ui/src/components/user-details.tsx +++ b/ui/src/components/user-details.tsx @@ -79,6 +79,7 @@ export class UserDetails extends Component { componentDidMount() { this.fetchUserData(); + setupTippy(); } componentDidUpdate(lastProps: UserDetailsProps) { @@ -88,7 +89,6 @@ export class UserDetails extends Component { break; } } - setupTippy(); } fetchUserData() { diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index d7db0ae2a..13cc90aca 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -180,6 +180,7 @@ export class User extends Component { ); WebSocketService.Instance.getSite(); + setupTippy(); } get isCurrentUser() { @@ -226,7 +227,6 @@ export class User extends Component { // Couldnt get a refresh working. This does for now. location.reload(); } - setupTippy(); } get documentTitle(): string { @@ -565,6 +565,7 @@ export class User extends Component { this, this.handleUserSettingsPreferredUsernameChange )} + pattern="^(?!@)(.+)$" minLength={3} maxLength={20} /> From 8daeba450c504dd1c7d5a5f4c8707d88ec4e17a7 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 12 Aug 2020 07:31:45 -0400 Subject: [PATCH 2/4] Removing unnecessary oper struct. Fixes #660 (#1077) --- server/src/api/comment.rs | 70 ++++++++++----------- server/src/api/community.rs | 74 ++++++++++++---------- server/src/api/mod.rs | 12 +--- server/src/api/post.rs | 84 +++++++++++++------------ server/src/api/site.rs | 57 +++++++++-------- server/src/api/user.rs | 108 +++++++++++++++++++-------------- server/src/routes/api.rs | 16 ++--- server/src/websocket/server.rs | 6 +- 8 files changed, 227 insertions(+), 200 deletions(-) diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index e3189d434..cb366e3eb 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -5,7 +5,6 @@ use crate::{ get_user_from_jwt_opt, is_mod_or_admin, APIError, - Oper, Perform, }, apub::{ApubLikeableType, ApubObjectType}, @@ -18,6 +17,7 @@ use crate::{ DbPool, LemmyError, }; +use actix_web::client::Client; use lemmy_db::{ comment::*, comment_view::*, @@ -121,15 +121,16 @@ pub struct GetCommentsResponse { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreateComment { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &CreateComment = &self.data; + let data: &CreateComment = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -180,9 +181,7 @@ impl Perform for Oper { Err(_e) => return Err(APIError::err("couldnt_create_comment").into()), }; - updated_comment - .send_create(&user, &self.client, pool) - .await?; + updated_comment.send_create(&user, &client, pool).await?; // Scan the comment for user mentions, add those rows let mentions = scrape_text_for_mentions(&comment_form.content); @@ -202,7 +201,7 @@ impl Perform for Oper { return Err(APIError::err("couldnt_like_comment").into()); } - updated_comment.send_like(&user, &self.client, pool).await?; + updated_comment.send_like(&user, &client, pool).await?; let user_id = user.id; let comment_view = blocking(pool, move |conn| { @@ -233,15 +232,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for EditComment { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &EditComment = &self.data; + let data: &EditComment = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -268,9 +268,7 @@ impl Perform for Oper { }; // Send the apub update - updated_comment - .send_update(&user, &self.client, pool) - .await?; + updated_comment.send_update(&user, &client, pool).await?; // Do the mentions / recipients let post_id = orig_comment.post_id; @@ -311,15 +309,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for DeleteComment { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &DeleteComment = &self.data; + let data: &DeleteComment = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -346,12 +345,10 @@ impl Perform for Oper { // Send the apub message if deleted { - updated_comment - .send_delete(&user, &self.client, pool) - .await?; + updated_comment.send_delete(&user, &client, pool).await?; } else { updated_comment - .send_undo_delete(&user, &self.client, pool) + .send_undo_delete(&user, &client, pool) .await?; } @@ -393,15 +390,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for RemoveComment { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &RemoveComment = &self.data; + let data: &RemoveComment = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -435,12 +433,10 @@ impl Perform for Oper { // Send the apub message if removed { - updated_comment - .send_remove(&user, &self.client, pool) - .await?; + updated_comment.send_remove(&user, &client, pool).await?; } else { updated_comment - .send_undo_remove(&user, &self.client, pool) + .send_undo_remove(&user, &client, pool) .await?; } @@ -482,15 +478,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for MarkCommentAsRead { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &MarkCommentAsRead = &self.data; + let data: &MarkCommentAsRead = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -545,15 +542,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for SaveComment { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &SaveComment = &self.data; + let data: &SaveComment = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let comment_saved_form = CommentSavedForm { @@ -589,15 +587,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreateCommentLike { type Response = CommentResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &CreateCommentLike = &self.data; + let data: &CreateCommentLike = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let mut recipient_ids = Vec::new(); @@ -659,12 +658,12 @@ impl Perform for Oper { } if like_form.score == 1 { - comment.send_like(&user, &self.client, pool).await?; + comment.send_like(&user, &client, pool).await?; } else if like_form.score == -1 { - comment.send_dislike(&user, &self.client, pool).await?; + comment.send_dislike(&user, &client, pool).await?; } } else { - comment.send_undo_like(&user, &self.client, pool).await?; + comment.send_undo_like(&user, &client, pool).await?; } // Have to refetch the comment to get the current state @@ -698,15 +697,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetComments { type Response = GetCommentsResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetComments = &self.data; + let data: &GetComments = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user_id = user.map(|u| u.id); diff --git a/server/src/api/community.rs b/server/src/api/community.rs index fb153895f..3aebb9ee9 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -1,6 +1,6 @@ use super::*; use crate::{ - api::{is_admin, is_mod_or_admin, APIError, Oper, Perform}, + api::{is_admin, is_mod_or_admin, APIError, Perform}, apub::ActorType, blocking, websocket::{ @@ -10,6 +10,7 @@ use crate::{ }, DbPool, }; +use actix_web::client::Client; use lemmy_db::{ diesel_option_overwrite, naive_now, @@ -155,15 +156,16 @@ pub struct TransferCommunity { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetCommunity { type Response = GetCommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetCommunity = &self.data; + let data: &GetCommunity = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user_id = user.map(|u| u.id); @@ -226,15 +228,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreateCommunity { type Response = CommunityResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &CreateCommunity = &self.data; + let data: &CreateCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; check_slurs(&data.name)?; @@ -318,15 +321,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for EditCommunity { type Response = CommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &EditCommunity = &self.data; + let data: &EditCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; check_slurs(&data.title)?; @@ -400,15 +404,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for DeleteCommunity { type Response = CommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &DeleteCommunity = &self.data; + let data: &DeleteCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Verify its the creator (only a creator can delete the community) @@ -432,12 +437,10 @@ impl Perform for Oper { // Send apub messages if deleted { - updated_community - .send_delete(&user, &self.client, pool) - .await?; + updated_community.send_delete(&user, &client, pool).await?; } else { updated_community - .send_undo_delete(&user, &self.client, pool) + .send_undo_delete(&user, &client, pool) .await?; } @@ -459,15 +462,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for RemoveCommunity { type Response = CommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &RemoveCommunity = &self.data; + let data: &RemoveCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Verify its an admin (only an admin can remove a community) @@ -501,12 +505,10 @@ impl Perform for Oper { // Apub messages if removed { - updated_community - .send_remove(&user, &self.client, pool) - .await?; + updated_community.send_remove(&user, &client, pool).await?; } else { updated_community - .send_undo_remove(&user, &self.client, pool) + .send_undo_remove(&user, &client, pool) .await?; } @@ -528,15 +530,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for ListCommunities { type Response = ListCommunitiesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &ListCommunities = &self.data; + let data: &ListCommunities = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user_id = match &user { @@ -570,15 +573,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for FollowCommunity { type Response = CommunityResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + client: Client, ) -> Result { - let data: &FollowCommunity = &self.data; + let data: &FollowCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let community_id = data.community_id; @@ -605,11 +609,11 @@ impl Perform for Oper { // Dont actually add to the community followers here, because you need // to wait for the accept user - .send_follow(&community.actor_id()?, &self.client, pool) + .send_follow(&community.actor_id()?, &client, pool) .await?; } else { user - .send_unfollow(&community.actor_id()?, &self.client, pool) + .send_unfollow(&community.actor_id()?, &client, pool) .await?; let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form); if blocking(pool, unfollow).await?.is_err() { @@ -632,15 +636,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetFollowedCommunities { type Response = GetFollowedCommunitiesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetFollowedCommunities = &self.data; + let data: &GetFollowedCommunities = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let user_id = user.id; @@ -659,15 +664,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for BanFromCommunity { type Response = BanFromCommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &BanFromCommunity = &self.data; + let data: &BanFromCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let community_id = data.community_id; @@ -731,15 +737,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for AddModToCommunity { type Response = AddModToCommunityResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &AddModToCommunity = &self.data; + let data: &AddModToCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let community_moderator_form = CommunityModeratorForm { @@ -795,15 +802,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for TransferCommunity { type Response = GetCommunityResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &TransferCommunity = &self.data; + let data: &TransferCommunity = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let community_id = data.community_id; diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 8124cd4a1..01884d384 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -33,17 +33,6 @@ impl APIError { } } -pub struct Oper { - data: T, - client: Client, -} - -impl Oper { - pub fn new(data: Data, client: Client) -> Oper { - Oper { data, client } - } -} - #[async_trait::async_trait(?Send)] pub trait Perform { type Response: serde::ser::Serialize + Send; @@ -52,6 +41,7 @@ pub trait Perform { &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result; } diff --git a/server/src/api/post.rs b/server/src/api/post.rs index b43e4e55c..4d089bff7 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -7,7 +7,6 @@ use crate::{ get_user_from_jwt_opt, is_mod_or_admin, APIError, - Oper, Perform, }, apub::{ApubLikeableType, ApubObjectType}, @@ -21,6 +20,7 @@ use crate::{ DbPool, LemmyError, }; +use actix_web::client::Client; use lemmy_db::{ comment_view::*, community_view::*, @@ -140,15 +140,16 @@ pub struct SavePost { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreatePost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &CreatePost = &self.data; + let data: &CreatePost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; check_slurs(&data.name)?; @@ -169,7 +170,7 @@ impl Perform for Oper { // Fetch Iframely and pictrs cached image let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = - fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).await; + fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await; let post_form = PostForm { name: data.name.trim().to_owned(), @@ -217,7 +218,7 @@ impl Perform for Oper { Err(_e) => return Err(APIError::err("couldnt_create_post").into()), }; - updated_post.send_create(&user, &self.client, pool).await?; + updated_post.send_create(&user, &client, pool).await?; // They like their own post by default let like_form = PostLikeForm { @@ -231,7 +232,7 @@ impl Perform for Oper { return Err(APIError::err("couldnt_like_post").into()); } - updated_post.send_like(&user, &self.client, pool).await?; + updated_post.send_like(&user, &client, pool).await?; // Refetch the view let inserted_post_id = inserted_post.id; @@ -259,15 +260,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetPost { type Response = GetPostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetPost = &self.data; + let data: &GetPost = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user_id = user.map(|u| u.id); @@ -329,15 +331,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetPosts { type Response = GetPostsResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetPosts = &self.data; + let data: &GetPosts = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let user_id = match &user { @@ -394,15 +397,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreatePostLike { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &CreatePostLike = &self.data; + let data: &CreatePostLike = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Don't do a downvote if site has downvotes disabled @@ -439,12 +443,12 @@ impl Perform for Oper { } if like_form.score == 1 { - post.send_like(&user, &self.client, pool).await?; + post.send_like(&user, &client, pool).await?; } else if like_form.score == -1 { - post.send_dislike(&user, &self.client, pool).await?; + post.send_dislike(&user, &client, pool).await?; } } else { - post.send_undo_like(&user, &self.client, pool).await?; + post.send_undo_like(&user, &client, pool).await?; } let post_id = data.post_id; @@ -473,15 +477,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for EditPost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &EditPost = &self.data; + let data: &EditPost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; check_slurs(&data.name)?; @@ -503,7 +508,7 @@ impl Perform for Oper { // Fetch Iframely and Pictrs cached image let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) = - fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).await; + fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await; let post_form = PostForm { name: data.name.trim().to_owned(), @@ -542,7 +547,7 @@ impl Perform for Oper { }; // Send apub update - updated_post.send_update(&user, &self.client, pool).await?; + updated_post.send_update(&user, &client, pool).await?; let edit_id = data.edit_id; let post_view = blocking(pool, move |conn| { @@ -565,15 +570,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for DeletePost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &DeletePost = &self.data; + let data: &DeletePost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -596,11 +602,9 @@ impl Perform for Oper { // apub updates if deleted { - updated_post.send_delete(&user, &self.client, pool).await?; + updated_post.send_delete(&user, &client, pool).await?; } else { - updated_post - .send_undo_delete(&user, &self.client, pool) - .await?; + updated_post.send_undo_delete(&user, &client, pool).await?; } // Refetch the post @@ -625,15 +629,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for RemovePost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &RemovePost = &self.data; + let data: &RemovePost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -663,11 +668,9 @@ impl Perform for Oper { // apub updates if removed { - updated_post.send_remove(&user, &self.client, pool).await?; + updated_post.send_remove(&user, &client, pool).await?; } else { - updated_post - .send_undo_remove(&user, &self.client, pool) - .await?; + updated_post.send_undo_remove(&user, &client, pool).await?; } // Refetch the post @@ -693,15 +696,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for LockPost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &LockPost = &self.data; + let data: &LockPost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -727,7 +731,7 @@ impl Perform for Oper { blocking(pool, move |conn| ModLockPost::create(conn, &form)).await??; // apub updates - updated_post.send_update(&user, &self.client, pool).await?; + updated_post.send_update(&user, &client, pool).await?; // Refetch the post let edit_id = data.edit_id; @@ -751,15 +755,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for StickyPost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &StickyPost = &self.data; + let data: &StickyPost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let edit_id = data.edit_id; @@ -788,7 +793,7 @@ impl Perform for Oper { // Apub updates // TODO stickied should pry work like locked for ease of use - updated_post.send_update(&user, &self.client, pool).await?; + updated_post.send_update(&user, &client, pool).await?; // Refetch the post let edit_id = data.edit_id; @@ -812,15 +817,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for SavePost { type Response = PostResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &SavePost = &self.data; + let data: &SavePost = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let post_saved_form = PostSavedForm { diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 515c3e5be..7bf14d3a7 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -7,7 +7,6 @@ use crate::{ get_user_from_jwt_opt, is_admin, APIError, - Oper, Perform, }, apub::fetcher::search_by_apub_id, @@ -17,6 +16,7 @@ use crate::{ DbPool, LemmyError, }; +use actix_web::client::Client; use lemmy_db::{ category::*, comment_view::*, @@ -156,15 +156,16 @@ pub struct SaveSiteConfig { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for ListCategories { type Response = ListCategoriesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let _data: &ListCategories = &self.data; + let _data: &ListCategories = &self; let categories = blocking(pool, move |conn| Category::list_all(conn)).await??; @@ -174,15 +175,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetModlog { type Response = GetModlogResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetModlog = &self.data; + let data: &GetModlog = &self; let community_id = data.community_id; let mod_user_id = data.mod_user_id; @@ -248,15 +250,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreateSite { type Response = SiteResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &CreateSite = &self.data; + let data: &CreateSite = &self; let user = get_user_from_jwt(&data.auth, pool).await?; @@ -290,14 +293,15 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for EditSite { type Response = SiteResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &EditSite = &self.data; + let data: &EditSite = &self; let user = get_user_from_jwt(&data.auth, pool).await?; check_slurs(&data.name)?; @@ -345,15 +349,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetSite { type Response = GetSiteResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &GetSite = &self.data; + let data: &GetSite = &self; // TODO refactor this a little let res = blocking(pool, move |conn| Site::read(conn, 1)).await?; @@ -370,8 +375,8 @@ impl Perform for Oper { captcha_uuid: None, captcha_answer: None, }; - let login_response = Oper::new(register, self.client.clone()) - .perform(pool, websocket_info.clone()) + let login_response = register + .perform(pool, websocket_info.clone(), client.clone()) .await?; info!("Admin {} created", setup.admin_username); @@ -385,8 +390,8 @@ impl Perform for Oper { enable_nsfw: true, auth: login_response.jwt, }; - Oper::new(create_site, self.client.clone()) - .perform(pool, websocket_info.clone()) + create_site + .perform(pool, websocket_info.clone(), client.clone()) .await?; info!("Site {} created", setup.site_name); Some(blocking(pool, move |conn| SiteView::read(conn)).await??) @@ -440,19 +445,20 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for Search { type Response = SearchResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + client: Client, ) -> Result { - let data: &Search = &self.data; + let data: &Search = &self; dbg!(&data); - match search_by_apub_id(&data.q, &self.client, pool).await { + match search_by_apub_id(&data.q, &client, pool).await { Ok(r) => return Ok(r), Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e), } @@ -604,15 +610,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for TransferSite { type Response = GetSiteResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &TransferSite = &self.data; + let data: &TransferSite = &self; let mut user = get_user_from_jwt(&data.auth, pool).await?; // TODO add a User_::read_safe() for this. @@ -667,15 +674,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetSiteConfig { type Response = GetSiteConfigResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetSiteConfig = &self.data; + let data: &GetSiteConfig = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Only let admins read this @@ -688,15 +696,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for SaveSiteConfig { type Response = GetSiteConfigResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &SaveSiteConfig = &self.data; + let data: &SaveSiteConfig = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Only let admins read this diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 83d8470cd..9bb03ba1a 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -6,7 +6,6 @@ use crate::{ get_user_from_jwt_opt, is_admin, APIError, - Oper, Perform, }, apub::ApubObjectType, @@ -20,6 +19,7 @@ use crate::{ DbPool, LemmyError, }; +use actix_web::client::Client; use bcrypt::verify; use captcha::{gen, Difficulty}; use chrono::Duration; @@ -294,15 +294,16 @@ pub struct UserJoinResponse { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for Login { type Response = LoginResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &Login = &self.data; + let data: &Login = &self; // Fetch that username / email let username_or_email = data.username_or_email.clone(); @@ -329,15 +330,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for Register { type Response = LoginResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &Register = &self.data; + let data: &Register = &self; // Make sure site has open registration if let Ok(site) = blocking(pool, move |conn| SiteView::read(conn)).await? { @@ -498,13 +500,14 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetCaptcha { type Response = GetCaptchaResponse; async fn perform( &self, _pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { let captcha_settings = Settings::get().captcha; @@ -546,15 +549,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for SaveUserSettings { type Response = LoginResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &SaveUserSettings = &self.data; + let data: &SaveUserSettings = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let user_id = user.id; @@ -669,15 +673,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetUserDetails { type Response = GetUserDetailsResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetUserDetails = &self.data; + let data: &GetUserDetails = &self; let user = get_user_from_jwt_opt(&data.auth, pool).await?; let show_nsfw = match &user { @@ -764,15 +769,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for AddAdmin { type Response = AddAdminResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &AddAdmin = &self.data; + let data: &AddAdmin = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Make sure user is an admin @@ -817,15 +823,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for BanUser { type Response = BanUserResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &BanUser = &self.data; + let data: &BanUser = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Make sure user is an admin @@ -875,15 +882,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetReplies { type Response = GetRepliesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetReplies = &self.data; + let data: &GetReplies = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let sort = SortType::from_str(&data.sort)?; @@ -907,15 +915,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetUserMentions { type Response = GetUserMentionsResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetUserMentions = &self.data; + let data: &GetUserMentions = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let sort = SortType::from_str(&data.sort)?; @@ -939,15 +948,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for MarkUserMentionAsRead { type Response = UserMentionResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &MarkUserMentionAsRead = &self.data; + let data: &MarkUserMentionAsRead = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let user_mention_id = data.user_mention_id; @@ -979,15 +989,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for MarkAllAsRead { type Response = GetRepliesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &MarkAllAsRead = &self.data; + let data: &MarkAllAsRead = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let user_id = user.id; @@ -1028,15 +1039,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for DeleteAccount { type Response = LoginResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &DeleteAccount = &self.data; + let data: &DeleteAccount = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Verify the password @@ -1090,15 +1102,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for PasswordReset { type Response = PasswordResetResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &PasswordReset = &self.data; + let data: &PasswordReset = &self; // Fetch that email let email = data.email.clone(); @@ -1134,15 +1147,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for PasswordChange { type Response = LoginResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &PasswordChange = &self.data; + let data: &PasswordChange = &self; // Fetch the user_id from the token let token = data.token.clone(); @@ -1175,15 +1189,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for CreatePrivateMessage { type Response = PrivateMessageResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &CreatePrivateMessage = &self.data; + let data: &CreatePrivateMessage = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let hostname = &format!("https://{}", Settings::get().hostname); @@ -1229,7 +1244,7 @@ impl Perform for Oper { }; updated_private_message - .send_create(&user, &self.client, pool) + .send_create(&user, &client, pool) .await?; // Send notifications to the recipient @@ -1274,15 +1289,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for EditPrivateMessage { type Response = PrivateMessageResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &EditPrivateMessage = &self.data; + let data: &EditPrivateMessage = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Checking permissions @@ -1307,7 +1323,7 @@ impl Perform for Oper { // Send the apub update updated_private_message - .send_update(&user, &self.client, pool) + .send_update(&user, &client, pool) .await?; let edit_id = data.edit_id; @@ -1330,15 +1346,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for DeletePrivateMessage { type Response = PrivateMessageResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + client: Client, ) -> Result { - let data: &DeletePrivateMessage = &self.data; + let data: &DeletePrivateMessage = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Checking permissions @@ -1364,11 +1381,11 @@ impl Perform for Oper { // Send the apub update if data.deleted { updated_private_message - .send_delete(&user, &self.client, pool) + .send_delete(&user, &client, pool) .await?; } else { updated_private_message - .send_undo_delete(&user, &self.client, pool) + .send_undo_delete(&user, &client, pool) .await?; } @@ -1392,15 +1409,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for MarkPrivateMessageAsRead { type Response = PrivateMessageResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &MarkPrivateMessageAsRead = &self.data; + let data: &MarkPrivateMessageAsRead = &self; let user = get_user_from_jwt(&data.auth, pool).await?; // Checking permissions @@ -1445,15 +1463,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for GetPrivateMessages { type Response = PrivateMessagesResponse; async fn perform( &self, pool: &DbPool, _websocket_info: Option, + _client: Client, ) -> Result { - let data: &GetPrivateMessages = &self.data; + let data: &GetPrivateMessages = &self; let user = get_user_from_jwt(&data.auth, pool).await?; let user_id = user.id; @@ -1474,15 +1493,16 @@ impl Perform for Oper { } #[async_trait::async_trait(?Send)] -impl Perform for Oper { +impl Perform for UserJoin { type Response = UserJoinResponse; async fn perform( &self, pool: &DbPool, websocket_info: Option, + _client: Client, ) -> Result { - let data: &UserJoin = &self.data; + let data: &UserJoin = &self; let user = get_user_from_jwt(&data.auth, pool).await?; if let Some(ws) = websocket_info { diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index f524cf411..17e7e591c 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -1,5 +1,5 @@ use crate::{ - api::{comment::*, community::*, post::*, site::*, user::*, Oper, Perform}, + api::{comment::*, community::*, post::*, site::*, user::*, Perform}, rate_limit::RateLimit, routes::{ChatServerParam, DbPoolParam}, websocket::WebsocketInfo, @@ -179,7 +179,7 @@ async fn perform( chat_server: ChatServerParam, ) -> Result where - Oper: Perform, + Request: Perform, Request: Send + 'static, { let ws_info = WebsocketInfo { @@ -187,10 +187,8 @@ where id: None, }; - let oper: Oper = Oper::new(data, client.clone()); - - let res = oper - .perform(&db, Some(ws_info)) + let res = data + .perform(&db, Some(ws_info), client.clone()) .await .map(|json| HttpResponse::Ok().json(json)) .map_err(ErrorBadRequest)?; @@ -204,8 +202,7 @@ async fn route_get( chat_server: ChatServerParam, ) -> Result where - Data: Serialize + Send + 'static, - Oper: Perform, + Data: Serialize + Send + 'static + Perform, { perform::(data.0, &client, db, chat_server).await } @@ -217,8 +214,7 @@ async fn route_post( chat_server: ChatServerParam, ) -> Result where - Data: Serialize + Send + 'static, - Oper: Perform, + Data: Serialize + Send + 'static + Perform, { perform::(data.0, &client, db, chat_server).await } diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 6c3eed4be..a6169fab9 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -556,7 +556,7 @@ struct Args<'a> { async fn do_user_operation<'a, 'b, Data>(args: Args<'b>) -> Result where for<'de> Data: Deserialize<'de> + 'a, - Oper: Perform, + Data: Perform, { let Args { client, @@ -581,9 +581,7 @@ where let fut = async move { let pool = pool.clone(); let parsed_data: Data = serde_json::from_str(&data)?; - let res = Oper::new(parsed_data, client) - .perform(&pool, Some(ws_info)) - .await?; + let res = parsed_data.perform(&pool, Some(ws_info), client).await?; to_json_string(&op, &res) }; From aace1bd71e616d0ce9c76cb7872a2b9d1460a38e Mon Sep 17 00:00:00 2001 From: nutomic Date: Wed, 12 Aug 2020 12:30:52 +0000 Subject: [PATCH 3/4] Remove dead code (#81) Add post_read back in, do some cleanup Add some delete functions back in Merge branch 'main' into remove-dead-code Replace body of unused db functions with unimplemented!() Remove dead code Remove remaining usages of unwrap() from activitypub code Remove most usage of Option::unwrap() from activitypub code Co-authored-by: Felix Ableitner Reviewed-on: https://yerbamate.dev/LemmyNet/lemmy/pulls/81 --- server/lemmy_db/src/activity.rs | 5 --- server/lemmy_db/src/category.rs | 4 -- server/lemmy_db/src/comment.rs | 16 ------- server/lemmy_db/src/community.rs | 5 --- server/lemmy_db/src/community_view.rs | 14 ------ server/lemmy_db/src/lib.rs | 10 ++--- server/lemmy_db/src/moderator.rs | 45 ------------------- server/lemmy_db/src/password_reset_request.rs | 3 -- server/lemmy_db/src/post.rs | 34 ++++++-------- server/lemmy_db/src/post_view.rs | 5 --- server/lemmy_db/src/private_message.rs | 5 --- server/lemmy_db/src/site.rs | 5 --- server/lemmy_db/src/user_mention.rs | 5 --- server/lemmy_db/src/user_view.rs | 5 --- server/lemmy_utils/src/lib.rs | 6 +-- server/lemmy_utils/src/settings.rs | 4 -- server/src/api/claims.rs | 8 +--- server/src/apub/fetcher.rs | 38 ---------------- server/src/main.rs | 6 +-- server/src/routes/mod.rs | 4 +- 20 files changed, 22 insertions(+), 205 deletions(-) diff --git a/server/lemmy_db/src/activity.rs b/server/lemmy_db/src/activity.rs index 3f7fd1d5c..a83233426 100644 --- a/server/lemmy_db/src/activity.rs +++ b/server/lemmy_db/src/activity.rs @@ -34,11 +34,6 @@ impl Crud for Activity { activity.find(activity_id).first::(conn) } - fn delete(conn: &PgConnection, activity_id: i32) -> Result { - use crate::schema::activity::dsl::*; - diesel::delete(activity.find(activity_id)).execute(conn) - } - fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result { use crate::schema::activity::dsl::*; insert_into(activity) diff --git a/server/lemmy_db/src/category.rs b/server/lemmy_db/src/category.rs index ec2efc7b7..ff4e757bf 100644 --- a/server/lemmy_db/src/category.rs +++ b/server/lemmy_db/src/category.rs @@ -23,10 +23,6 @@ impl Crud for Category { category.find(category_id).first::(conn) } - fn delete(conn: &PgConnection, category_id: i32) -> Result { - diesel::delete(category.find(category_id)).execute(conn) - } - fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result { insert_into(category) .values(new_category) diff --git a/server/lemmy_db/src/comment.rs b/server/lemmy_db/src/comment.rs index 354922e85..6802ad62c 100644 --- a/server/lemmy_db/src/comment.rs +++ b/server/lemmy_db/src/comment.rs @@ -172,13 +172,6 @@ pub struct CommentLikeForm { } impl Likeable for CommentLike { - fn read(conn: &PgConnection, comment_id_from: i32) -> Result, Error> { - use crate::schema::comment_like::dsl::*; - comment_like - .filter(comment_id.eq(comment_id_from)) - .load::(conn) - } - fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result { use crate::schema::comment_like::dsl::*; insert_into(comment_like) @@ -196,15 +189,6 @@ impl Likeable for CommentLike { } } -impl CommentLike { - pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result, Error> { - use crate::schema::comment_like::dsl::*; - comment_like - .filter(post_id.eq(post_id_from)) - .load::(conn) - } -} - #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[belongs_to(Comment)] #[table_name = "comment_saved"] diff --git a/server/lemmy_db/src/community.rs b/server/lemmy_db/src/community.rs index b4200b2d9..7490f3664 100644 --- a/server/lemmy_db/src/community.rs +++ b/server/lemmy_db/src/community.rs @@ -99,11 +99,6 @@ impl Community { .first::(conn) } - pub fn list_local(conn: &PgConnection) -> Result, Error> { - use crate::schema::community::dsl::*; - community.filter(local.eq(true)).load::(conn) - } - pub fn update_deleted( conn: &PgConnection, community_id: i32, diff --git a/server/lemmy_db/src/community_view.rs b/server/lemmy_db/src/community_view.rs index 540841f22..e68dbd5e2 100644 --- a/server/lemmy_db/src/community_view.rs +++ b/server/lemmy_db/src/community_view.rs @@ -386,20 +386,6 @@ pub struct CommunityUserBanView { } impl CommunityUserBanView { - pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result, Error> { - use super::community_view::community_user_ban_view::dsl::*; - community_user_ban_view - .filter(community_id.eq(from_community_id)) - .load::(conn) - } - - pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result, Error> { - use super::community_view::community_user_ban_view::dsl::*; - community_user_ban_view - .filter(user_id.eq(from_user_id)) - .load::(conn) - } - pub fn get( conn: &PgConnection, from_user_id: i32, diff --git a/server/lemmy_db/src/lib.rs b/server/lemmy_db/src/lib.rs index edfc26468..3c22d1776 100644 --- a/server/lemmy_db/src/lib.rs +++ b/server/lemmy_db/src/lib.rs @@ -50,9 +50,12 @@ pub trait Crud { fn update(conn: &PgConnection, id: i32, form: &T) -> Result where Self: Sized; - fn delete(conn: &PgConnection, id: i32) -> Result + fn delete(_conn: &PgConnection, _id: i32) -> Result where - Self: Sized; + Self: Sized, + { + unimplemented!() + } } pub trait Followable { @@ -74,9 +77,6 @@ pub trait Joinable { } pub trait Likeable { - fn read(conn: &PgConnection, id: i32) -> Result, Error> - where - Self: Sized; fn like(conn: &PgConnection, form: &T) -> Result where Self: Sized; diff --git a/server/lemmy_db/src/moderator.rs b/server/lemmy_db/src/moderator.rs index 33e1063c5..70c253fe3 100644 --- a/server/lemmy_db/src/moderator.rs +++ b/server/lemmy_db/src/moderator.rs @@ -41,11 +41,6 @@ impl Crud for ModRemovePost { mod_remove_post.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_post::dsl::*; - diesel::delete(mod_remove_post.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result { use crate::schema::mod_remove_post::dsl::*; insert_into(mod_remove_post) @@ -85,11 +80,6 @@ impl Crud for ModLockPost { mod_lock_post.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_lock_post::dsl::*; - diesel::delete(mod_lock_post.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result { use crate::schema::mod_lock_post::dsl::*; insert_into(mod_lock_post) @@ -129,11 +119,6 @@ impl Crud for ModStickyPost { mod_sticky_post.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_sticky_post::dsl::*; - diesel::delete(mod_sticky_post.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result { use crate::schema::mod_sticky_post::dsl::*; insert_into(mod_sticky_post) @@ -175,11 +160,6 @@ impl Crud for ModRemoveComment { mod_remove_comment.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_comment::dsl::*; - diesel::delete(mod_remove_comment.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result { use crate::schema::mod_remove_comment::dsl::*; insert_into(mod_remove_comment) @@ -223,11 +203,6 @@ impl Crud for ModRemoveCommunity { mod_remove_community.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_community::dsl::*; - diesel::delete(mod_remove_community.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result { use crate::schema::mod_remove_community::dsl::*; insert_into(mod_remove_community) @@ -277,11 +252,6 @@ impl Crud for ModBanFromCommunity { mod_ban_from_community.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_ban_from_community::dsl::*; - diesel::delete(mod_ban_from_community.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result { use crate::schema::mod_ban_from_community::dsl::*; insert_into(mod_ban_from_community) @@ -329,11 +299,6 @@ impl Crud for ModBan { mod_ban.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_ban::dsl::*; - diesel::delete(mod_ban.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModBanForm) -> Result { use crate::schema::mod_ban::dsl::*; insert_into(mod_ban).values(form).get_result::(conn) @@ -373,11 +338,6 @@ impl Crud for ModAddCommunity { mod_add_community.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_add_community::dsl::*; - diesel::delete(mod_add_community.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result { use crate::schema::mod_add_community::dsl::*; insert_into(mod_add_community) @@ -417,11 +377,6 @@ impl Crud for ModAdd { mod_add.find(from_id).first::(conn) } - fn delete(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_add::dsl::*; - diesel::delete(mod_add.find(from_id)).execute(conn) - } - fn create(conn: &PgConnection, form: &ModAddForm) -> Result { use crate::schema::mod_add::dsl::*; insert_into(mod_add).values(form).get_result::(conn) diff --git a/server/lemmy_db/src/password_reset_request.rs b/server/lemmy_db/src/password_reset_request.rs index 038450bcb..06615187e 100644 --- a/server/lemmy_db/src/password_reset_request.rs +++ b/server/lemmy_db/src/password_reset_request.rs @@ -28,9 +28,6 @@ impl Crud for PasswordResetRequest { .find(password_reset_request_id) .first::(conn) } - fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result { - diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn) - } fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result { insert_into(password_reset_request) .values(form) diff --git a/server/lemmy_db/src/post.rs b/server/lemmy_db/src/post.rs index 591b4cbb0..33b78cd6e 100644 --- a/server/lemmy_db/src/post.rs +++ b/server/lemmy_db/src/post.rs @@ -1,11 +1,4 @@ -use crate::{ - naive_now, - schema::{post, post_like, post_read, post_saved}, - Crud, - Likeable, - Readable, - Saveable, -}; +use crate::{naive_now, schema::{post, post_like, post_read, post_saved}, Crud, Likeable, Saveable, Readable}; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; use url::{ParseError, Url}; @@ -201,12 +194,6 @@ pub struct PostLikeForm { } impl Likeable for PostLike { - fn read(conn: &PgConnection, post_id_from: i32) -> Result, Error> { - use crate::schema::post_like::dsl::*; - post_like - .filter(post_id.eq(post_id_from)) - .load::(conn) - } fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result { use crate::schema::post_like::dsl::*; insert_into(post_like) @@ -264,8 +251,11 @@ impl Saveable for PostSaved { #[table_name = "post_read"] pub struct PostRead { pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, } @@ -273,6 +263,7 @@ pub struct PostRead { #[table_name = "post_read"] pub struct PostReadForm { pub post_id: i32, + pub user_id: i32, } @@ -283,6 +274,7 @@ impl Readable for PostRead { .values(post_read_form) .get_result::(conn) } + fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { use crate::schema::post_read::dsl::*; diesel::delete( @@ -438,17 +430,17 @@ mod tests { // Post Read let post_read_form = PostReadForm { - post_id: inserted_post.id, - user_id: inserted_user.id, - }; + post_id: inserted_post.id, + user_id: inserted_user.id, + }; let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap(); let expected_post_read = PostRead { - id: inserted_post_read.id, - post_id: inserted_post.id, - user_id: inserted_user.id, - published: inserted_post_read.published, + id: inserted_post_read.id, + post_id: inserted_post.id, + user_id: inserted_user.id, + published: inserted_post_read.published, }; let read_post = Post::read(&conn, inserted_post.id).unwrap(); diff --git a/server/lemmy_db/src/post_view.rs b/server/lemmy_db/src/post_view.rs index 9878807a4..c10cdf1e7 100644 --- a/server/lemmy_db/src/post_view.rs +++ b/server/lemmy_db/src/post_view.rs @@ -252,11 +252,6 @@ impl<'a> PostQueryBuilder<'a> { self } - pub fn unread_only(mut self, unread_only: bool) -> Self { - self.unread_only = unread_only; - self - } - pub fn page>(mut self, page: T) -> Self { self.page = page.get_optional(); self diff --git a/server/lemmy_db/src/private_message.rs b/server/lemmy_db/src/private_message.rs index d9dc047b2..16c6f9bf8 100644 --- a/server/lemmy_db/src/private_message.rs +++ b/server/lemmy_db/src/private_message.rs @@ -37,11 +37,6 @@ impl Crud for PrivateMessage { private_message.find(private_message_id).first::(conn) } - fn delete(conn: &PgConnection, private_message_id: i32) -> Result { - use crate::schema::private_message::dsl::*; - diesel::delete(private_message.find(private_message_id)).execute(conn) - } - fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result { use crate::schema::private_message::dsl::*; insert_into(private_message) diff --git a/server/lemmy_db/src/site.rs b/server/lemmy_db/src/site.rs index 51699d657..36b3e833d 100644 --- a/server/lemmy_db/src/site.rs +++ b/server/lemmy_db/src/site.rs @@ -39,11 +39,6 @@ impl Crud for Site { site.first::(conn) } - fn delete(conn: &PgConnection, site_id: i32) -> Result { - use crate::schema::site::dsl::*; - diesel::delete(site.find(site_id)).execute(conn) - } - fn create(conn: &PgConnection, new_site: &SiteForm) -> Result { use crate::schema::site::dsl::*; insert_into(site).values(new_site).get_result::(conn) diff --git a/server/lemmy_db/src/user_mention.rs b/server/lemmy_db/src/user_mention.rs index e8bfa73d6..a8ec88d21 100644 --- a/server/lemmy_db/src/user_mention.rs +++ b/server/lemmy_db/src/user_mention.rs @@ -28,11 +28,6 @@ impl Crud for UserMention { user_mention.find(user_mention_id).first::(conn) } - fn delete(conn: &PgConnection, user_mention_id: i32) -> Result { - use crate::schema::user_mention::dsl::*; - diesel::delete(user_mention.find(user_mention_id)).execute(conn) - } - fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result { use crate::schema::user_mention::dsl::*; insert_into(user_mention) diff --git a/server/lemmy_db/src/user_view.rs b/server/lemmy_db/src/user_view.rs index f304b1769..08f4c79cf 100644 --- a/server/lemmy_db/src/user_view.rs +++ b/server/lemmy_db/src/user_view.rs @@ -159,11 +159,6 @@ impl<'a> UserQueryBuilder<'a> { } impl UserView { - pub fn read(conn: &PgConnection, from_user_id: i32) -> Result { - use super::user_view::user_fast::dsl::*; - user_fast.find(from_user_id).first::(conn) - } - pub fn admins(conn: &PgConnection) -> Result, Error> { use super::user_view::user_fast::dsl::*; use diesel::sql_types::{Nullable, Text}; diff --git a/server/lemmy_utils/src/lib.rs b/server/lemmy_utils/src/lib.rs index fc50e199b..16d954ff1 100644 --- a/server/lemmy_utils/src/lib.rs +++ b/server/lemmy_utils/src/lib.rs @@ -12,7 +12,7 @@ pub extern crate url; pub mod settings; use crate::settings::Settings; -use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, Utc}; +use chrono::{DateTime, FixedOffset, Local, NaiveDateTime}; use itertools::Itertools; use lettre::{ smtp::{ @@ -43,10 +43,6 @@ macro_rules! location_info { }; } -pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime { - DateTime::::from_utc(ndt, Utc) -} - pub fn naive_from_unix(time: i64) -> NaiveDateTime { NaiveDateTime::from_timestamp(time, 0) } diff --git a/server/lemmy_utils/src/settings.rs b/server/lemmy_utils/src/settings.rs index 55b6cbb32..16fd424cb 100644 --- a/server/lemmy_utils/src/settings.rs +++ b/server/lemmy_utils/src/settings.rs @@ -121,10 +121,6 @@ impl Settings { ) } - pub fn api_endpoint(&self) -> String { - format!("{}/api/v1", self.hostname) - } - pub fn get_config_defaults_location() -> String { env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string()) } diff --git a/server/src/api/claims.rs b/server/src/api/claims.rs index 477ff1d9d..0f0d05de8 100644 --- a/server/src/api/claims.rs +++ b/server/src/api/claims.rs @@ -1,6 +1,5 @@ -use diesel::{result::Error, PgConnection}; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation}; -use lemmy_db::{user::User_, Crud}; +use lemmy_db::user::User_; use lemmy_utils::settings::Settings; use serde::{Deserialize, Serialize}; @@ -37,9 +36,4 @@ impl Claims { ) .unwrap() } - - pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result { - let claims: Claims = Claims::decode(&jwt).expect("Invalid token").claims; - User_::read(&conn, claims.id) - } } diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 55e0991b6..f6573a1f4 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -11,7 +11,6 @@ use crate::{ }, blocking, request::{retry, RecvError}, - routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}, DbPool, LemmyError, }; @@ -43,20 +42,6 @@ use url::Url; static ACTOR_REFETCH_INTERVAL_SECONDS: i64 = 24 * 60 * 60; static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 10; -// Fetch nodeinfo metadata from a remote instance. -async fn _fetch_node_info(client: &Client, domain: &str) -> Result { - let well_known_uri = Url::parse(&format!( - "{}://{}/.well-known/nodeinfo", - get_apub_protocol_string(), - domain - ))?; - - let well_known = fetch_remote_object::(client, &well_known_uri).await?; - let nodeinfo = fetch_remote_object::(client, &well_known.links.href).await?; - - Ok(nodeinfo) -} - /// Fetch any type of ActivityPub object, handling things like HTTP headers, deserialisation, /// timeouts etc. pub async fn fetch_remote_object( @@ -447,26 +432,3 @@ pub async fn get_or_fetch_and_insert_comment( Err(e) => Err(e.into()), } } - -// TODO It should not be fetching data from a community outbox. -// All posts, comments, comment likes, etc should be posts to our community_inbox -// The only data we should be periodically fetching (if it hasn't been fetched in the last day -// maybe), is community and user actors -// and user actors -// Fetch all posts in the outbox of the given user, and insert them into the database. -// fn fetch_community_outbox(community: &Community, conn: &PgConnection) -> Result, LemmyError> { -// let outbox_url = Url::parse(&community.get_outbox_url())?; -// let outbox = fetch_remote_object::(&outbox_url)?; -// let items = outbox.collection_props.get_many_items_base_boxes(); - -// Ok( -// items -// .context(location_info!())? -// .map(|obox: &BaseBox| -> Result { -// let page = obox.clone().to_concrete::()?; -// PostForm::from_page(&page, conn) -// }) -// .map(|pf| upsert_post(&pf?, conn)) -// .collect::, LemmyError>>()?, -// ) -// } diff --git a/server/src/main.rs b/server/src/main.rs index b27ddb9cb..daa41447a 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,17 +1,13 @@ -extern crate lemmy_server; #[macro_use] extern crate diesel_migrations; #[macro_use] pub extern crate lazy_static; -pub type DbPool = Pool>; - -use crate::lemmy_server::actix_web::dev::Service; use actix::prelude::*; use actix_web::{ body::Body, client::Client, - dev::{ServiceRequest, ServiceResponse}, + dev::{Service, ServiceRequest, ServiceResponse}, http::{ header::{CACHE_CONTROL, CONTENT_TYPE}, HeaderValue, diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs index 4a7d30993..c3987145b 100644 --- a/server/src/routes/mod.rs +++ b/server/src/routes/mod.rs @@ -7,15 +7,13 @@ pub mod nodeinfo; pub mod webfinger; pub mod websocket; -use crate::{rate_limit::rate_limiter::RateLimiter, websocket::server::ChatServer}; +use crate::websocket::server::ChatServer; use actix::prelude::*; use actix_web::*; use diesel::{ r2d2::{ConnectionManager, Pool}, PgConnection, }; -use std::sync::{Arc, Mutex}; pub type DbPoolParam = web::Data>>; -pub type RateLimitParam = web::Data>>; pub type ChatServerParam = web::Data>; From 8ee6f852cb5d5bffb86460cc1933a70782d56424 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 12 Aug 2020 08:31:05 -0400 Subject: [PATCH 4/4] Some formatting of post.rs --- server/lemmy_db/src/post.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/server/lemmy_db/src/post.rs b/server/lemmy_db/src/post.rs index 33b78cd6e..3c697e33d 100644 --- a/server/lemmy_db/src/post.rs +++ b/server/lemmy_db/src/post.rs @@ -1,4 +1,11 @@ -use crate::{naive_now, schema::{post, post_like, post_read, post_saved}, Crud, Likeable, Saveable, Readable}; +use crate::{ + naive_now, + schema::{post, post_like, post_read, post_saved}, + Crud, + Likeable, + Readable, + Saveable, +}; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; use url::{ParseError, Url}; @@ -430,17 +437,17 @@ mod tests { // Post Read let post_read_form = PostReadForm { - post_id: inserted_post.id, - user_id: inserted_user.id, - }; + post_id: inserted_post.id, + user_id: inserted_user.id, + }; let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap(); let expected_post_read = PostRead { - id: inserted_post_read.id, - post_id: inserted_post.id, - user_id: inserted_user.id, - published: inserted_post_read.published, + id: inserted_post_read.id, + post_id: inserted_post.id, + user_id: inserted_user.id, + published: inserted_post_read.published, }; let read_post = Post::read(&conn, inserted_post.id).unwrap();