From: nutomic Date: Wed, 12 Aug 2020 12:30:52 +0000 (+0000) Subject: Remove dead code (#81) X-Git-Url: http://these/git/%22%7Burl%7D/static/%7B%7D/readmes/README.es.md?a=commitdiff_plain;h=aace1bd71e616d0ce9c76cb7872a2b9d1460a38e;p=lemmy.git 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 --- diff --git a/server/lemmy_db/src/activity.rs b/server/lemmy_db/src/activity.rs index 3f7fd1d5..a8323342 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 ec2efc7b..ff4e757b 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 354922e8..6802ad62 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 b4200b2d..7490f366 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 540841f2..e68dbd5e 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 edfc2646..3c22d177 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 33e1063c..70c253fe 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 038450bc..06615187 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 591b4cbb..33b78cd6 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 9878807a..c10cdf1e 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 d9dc047b..16c6f9bf 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 51699d65..36b3e833 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 e8bfa73d..a8ec88d2 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 f304b176..08f4c79c 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 fc50e199..16d954ff 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 55b6cbb3..16fd424c 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 477ff1d9..0f0d05de 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 55e0991b..f6573a1f 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 b27ddb9c..daa41447 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 4a7d3099..c3987145 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>;