From: Felix Ableitner Date: Mon, 21 Dec 2020 16:30:34 +0000 (+0100) Subject: Split lemmy_db into lemmy_db_queries, lemmy_db_aggregates and lemmy_db_views X-Git-Url: http://these/git/%24%7BgetStaticDir%28%29%7D/styles/%7Bsrc%7D?a=commitdiff_plain;h=d5efebbf4759412c4061abefbd619c404ab10b6d;p=lemmy.git Split lemmy_db into lemmy_db_queries, lemmy_db_aggregates and lemmy_db_views --- diff --git a/Cargo.lock b/Cargo.lock index a639b800..dd16aa97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1717,8 +1717,9 @@ dependencies = [ "jsonwebtoken", "lazy_static", "lemmy_apub", - "lemmy_db", + "lemmy_db_queries", "lemmy_db_schema", + "lemmy_db_views", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", @@ -1762,8 +1763,9 @@ dependencies = [ "http-signature-normalization-reqwest", "itertools", "lazy_static", - "lemmy_db", + "lemmy_db_queries", "lemmy_db_schema", + "lemmy_db_views", "lemmy_structs", "lemmy_utils", "lemmy_websocket", @@ -1784,7 +1786,18 @@ dependencies = [ ] [[package]] -name = "lemmy_db" +name = "lemmy_db_aggregates" +version = "0.1.0" +dependencies = [ + "chrono", + "diesel", + "lemmy_db_queries", + "lemmy_db_schema", + "serde 1.0.118", +] + +[[package]] +name = "lemmy_db_queries" version = "0.1.0" dependencies = [ "bcrypt", @@ -1816,6 +1829,17 @@ dependencies = [ "url", ] +[[package]] +name = "lemmy_db_views" +version = "0.1.0" +dependencies = [ + "diesel", + "lemmy_db_aggregates", + "lemmy_db_queries", + "lemmy_db_schema", + "serde 1.0.118", +] + [[package]] name = "lemmy_rate_limit" version = "0.1.0" @@ -1850,8 +1874,10 @@ dependencies = [ "lazy_static", "lemmy_api", "lemmy_apub", - "lemmy_db", + "lemmy_db_aggregates", + "lemmy_db_queries", "lemmy_db_schema", + "lemmy_db_views", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", @@ -1875,8 +1901,9 @@ dependencies = [ "actix-web", "chrono", "diesel", - "lemmy_db", + "lemmy_db_queries", "lemmy_db_schema", + "lemmy_db_views", "lemmy_utils", "log", "serde 1.0.118", @@ -1917,7 +1944,7 @@ dependencies = [ "background-jobs", "chrono", "diesel", - "lemmy_db", + "lemmy_db_queries", "lemmy_db_schema", "lemmy_rate_limit", "lemmy_structs", diff --git a/Cargo.toml b/Cargo.toml index e7714065..f6234416 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ members = [ "lemmy_api", "lemmy_apub", "lemmy_utils", - "lemmy_db", + "lemmy_db_queries", "lemmy_db_schema", "lemmy_structs", "lemmy_rate_limit", @@ -23,7 +23,9 @@ lemmy_api = { path = "./lemmy_api" } lemmy_apub = { path = "./lemmy_apub" } lemmy_utils = { path = "./lemmy_utils" } lemmy_db_schema = { path = "./lemmy_db_schema" } -lemmy_db = { path = "./lemmy_db" } +lemmy_db_queries = { path = "lemmy_db_queries" } +lemmy_db_views = { path = "./lemmy_db_views" } +lemmy_db_aggregates = { path = "./lemmy_db_aggregates" } lemmy_structs = { path = "./lemmy_structs" } lemmy_rate_limit = { path = "./lemmy_rate_limit" } lemmy_websocket = { path = "./lemmy_websocket" } diff --git a/lemmy_api/Cargo.toml b/lemmy_api/Cargo.toml index fc845c11..b9bda599 100644 --- a/lemmy_api/Cargo.toml +++ b/lemmy_api/Cargo.toml @@ -11,8 +11,9 @@ path = "src/lib.rs" [dependencies] lemmy_apub = { path = "../lemmy_apub" } lemmy_utils = { path = "../lemmy_utils" } -lemmy_db = { path = "../lemmy_db" } +lemmy_db_queries = { path = "../lemmy_db_queries" } lemmy_db_schema = { path = "../lemmy_db_schema" } +lemmy_db_views = { path = "../lemmy_db_views" } lemmy_structs = { path = "../lemmy_structs" } lemmy_rate_limit = { path = "../lemmy_rate_limit" } lemmy_websocket = { path = "../lemmy_websocket" } diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs index bafde6bf..eb8d68b7 100644 --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@ -12,10 +12,6 @@ use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_db::{ source::comment::Comment_, - views::{ - comment_report_view::{CommentReportQueryBuilder, CommentReportView}, - comment_view::{CommentQueryBuilder, CommentView}, - }, Crud, Likeable, ListingType, @@ -24,6 +20,10 @@ use lemmy_db::{ SortType, }; use lemmy_db_schema::source::{comment::*, comment_report::*, moderator::*}; +use lemmy_db_views::{ + comment_report_view::{CommentReportQueryBuilder, CommentReportView}, + comment_view::{CommentQueryBuilder, CommentView}, +}; use lemmy_structs::{blocking, comment::*, send_local_notifs}; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, diff --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs index 405491bd..8c2d9ad5 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -16,15 +16,6 @@ use lemmy_db::{ community::{CommunityModerator_, Community_}, post::Post_, }, - views::{ - comment_view::CommentQueryBuilder, - community::{ - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, - community_view::{CommunityQueryBuilder, CommunityView}, - }, - user_view::UserViewSafe, - }, ApubObject, Bannable, Crud, @@ -36,6 +27,15 @@ use lemmy_db_schema::{ naive_now, source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, }; +use lemmy_db_views::{ + comment_view::CommentQueryBuilder, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + community_view::{CommunityQueryBuilder, CommunityView}, + }, + user_view::UserViewSafe, +}; use lemmy_structs::{blocking, community::*}; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, diff --git a/lemmy_api/src/lib.rs b/lemmy_api/src/lib.rs index 4b61539b..72c2316d 100644 --- a/lemmy_api/src/lib.rs +++ b/lemmy_api/src/lib.rs @@ -5,7 +5,6 @@ use lemmy_db::{ community::{CommunityModerator_, Community_}, site::Site_, }, - views::community::community_user_ban_view::CommunityUserBanView, Crud, DbPool, }; @@ -15,6 +14,10 @@ use lemmy_db_schema::source::{ site::Site, user::User_, }; +use lemmy_db_views::community::{ + community_user_ban_view::CommunityUserBanView, + community_view::CommunityView, +}; use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*}; use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError}; use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation}; @@ -47,7 +50,7 @@ pub(crate) async fn is_mod_or_admin( community_id: i32, ) -> Result<(), LemmyError> { let is_mod_or_admin = blocking(pool, move |conn| { - Community::is_mod_or_admin(conn, user_id, community_id) + CommunityView::is_mod_or_admin(conn, user_id, community_id) }) .await?; if !is_mod_or_admin { diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index 20b5b8d5..6f514970 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -10,21 +10,7 @@ use crate::{ }; use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; -use lemmy_db::{ - source::post::Post_, - views::{ - comment_view::CommentQueryBuilder, - community::community_moderator_view::CommunityModeratorView, - post_report_view::{PostReportQueryBuilder, PostReportView}, - post_view::{PostQueryBuilder, PostView}, - }, - Crud, - Likeable, - ListingType, - Reportable, - Saveable, - SortType, -}; +use lemmy_db::{source::post::Post_, Crud, Likeable, ListingType, Reportable, Saveable, SortType}; use lemmy_db_schema::{ naive_now, source::{ @@ -33,6 +19,12 @@ use lemmy_db_schema::{ post_report::{PostReport, PostReportForm}, }, }; +use lemmy_db_views::{ + comment_view::CommentQueryBuilder, + community::community_moderator_view::CommunityModeratorView, + post_report_view::{PostReportQueryBuilder, PostReportView}, + post_view::{PostQueryBuilder, PostView}, +}; use lemmy_structs::{blocking, post::*}; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, diff --git a/lemmy_api/src/site.rs b/lemmy_api/src/site.rs index e30e6230..dcf35c00 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -12,24 +12,6 @@ use lemmy_apub::fetcher::search_by_apub_id; use lemmy_db::{ diesel_option_overwrite, source::{category::Category_, site::Site_}, - views::{ - comment_view::CommentQueryBuilder, - community::community_view::CommunityQueryBuilder, - moderator::{ - mod_add_community_view::ModAddCommunityView, - mod_add_view::ModAddView, - mod_ban_from_community_view::ModBanFromCommunityView, - mod_ban_view::ModBanView, - mod_lock_post_view::ModLockPostView, - mod_remove_comment_view::ModRemoveCommentView, - mod_remove_community_view::ModRemoveCommunityView, - mod_remove_post_view::ModRemovePostView, - mod_sticky_post_view::ModStickyPostView, - }, - post_view::PostQueryBuilder, - site_view::SiteView, - user_view::{UserQueryBuilder, UserViewSafe}, - }, Crud, SearchType, SortType, @@ -42,6 +24,24 @@ use lemmy_db_schema::{ site::{Site, *}, }, }; +use lemmy_db_views::{ + comment_view::CommentQueryBuilder, + community::community_view::CommunityQueryBuilder, + moderator::{ + mod_add_community_view::ModAddCommunityView, + mod_add_view::ModAddView, + mod_ban_from_community_view::ModBanFromCommunityView, + mod_ban_view::ModBanView, + mod_lock_post_view::ModLockPostView, + mod_remove_comment_view::ModRemoveCommentView, + mod_remove_community_view::ModRemoveCommunityView, + mod_remove_post_view::ModRemovePostView, + mod_sticky_post_view::ModStickyPostView, + }, + post_view::PostQueryBuilder, + site_view::SiteView, + user_view::{UserQueryBuilder, UserViewSafe}, +}; use lemmy_structs::{blocking, site::*, user::Register}; use lemmy_utils::{ location_info, diff --git a/lemmy_api/src/user.rs b/lemmy_api/src/user.rs index 34ef5022..90891154 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -26,19 +26,6 @@ use lemmy_db::{ user::User, user_mention::UserMention_, }, - views::{ - comment_report_view::CommentReportView, - comment_view::CommentQueryBuilder, - community::{ - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, - }, - post_report_view::PostReportView, - post_view::PostQueryBuilder, - private_message_view::{PrivateMessageQueryBuilder, PrivateMessageView}, - user_mention_view::{UserMentionQueryBuilder, UserMentionView}, - user_view::{UserViewDangerous, UserViewSafe}, - }, Crud, Followable, Joinable, @@ -59,6 +46,19 @@ use lemmy_db_schema::{ user_mention::*, }, }; +use lemmy_db_views::{ + comment_report_view::CommentReportView, + comment_view::CommentQueryBuilder, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + }, + post_report_view::PostReportView, + post_view::PostQueryBuilder, + private_message_view::{PrivateMessageQueryBuilder, PrivateMessageView}, + user_mention_view::{UserMentionQueryBuilder, UserMentionView}, + user_view::{UserViewDangerous, UserViewSafe}, +}; use lemmy_structs::{blocking, send_email_to_user, user::*}; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, diff --git a/lemmy_apub/Cargo.toml b/lemmy_apub/Cargo.toml index 09dd9be8..a912f448 100644 --- a/lemmy_apub/Cargo.toml +++ b/lemmy_apub/Cargo.toml @@ -10,8 +10,9 @@ path = "src/lib.rs" [dependencies] lemmy_utils = { path = "../lemmy_utils" } -lemmy_db = { path = "../lemmy_db" } +lemmy_db_queries = { path = "../lemmy_db_queries" } lemmy_db_schema = { path = "../lemmy_db_schema" } +lemmy_db_views = { path = "../lemmy_db_views" } lemmy_structs = { path = "../lemmy_structs" } lemmy_websocket = { path = "../lemmy_websocket" } diesel = "1.4.5" diff --git a/lemmy_apub/src/activities/receive/comment.rs b/lemmy_apub/src/activities/receive/comment.rs index 0149e931..483ef54f 100644 --- a/lemmy_apub/src/activities/receive/comment.rs +++ b/lemmy_apub/src/activities/receive/comment.rs @@ -4,11 +4,12 @@ use activitystreams::{ base::ExtendsExt, }; use anyhow::Context; -use lemmy_db::{source::comment::Comment_, views::comment_view::CommentView, Crud, Likeable}; +use lemmy_db::{source::comment::Comment_, Crud, Likeable}; use lemmy_db_schema::source::{ comment::{Comment, CommentLike, CommentLikeForm}, post::Post, }; +use lemmy_db_views::comment_view::CommentView; use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/receive/comment_undo.rs b/lemmy_apub/src/activities/receive/comment_undo.rs index 7e672045..4f845523 100644 --- a/lemmy_apub/src/activities/receive/comment_undo.rs +++ b/lemmy_apub/src/activities/receive/comment_undo.rs @@ -1,7 +1,8 @@ use crate::activities::receive::get_actor_as_user; use activitystreams::activity::{Dislike, Like}; -use lemmy_db::{source::comment::Comment_, views::comment_view::CommentView, Likeable}; +use lemmy_db::{source::comment::Comment_, Likeable}; use lemmy_db_schema::source::comment::{Comment, CommentLike}; +use lemmy_db_views::comment_view::CommentView; use lemmy_structs::{blocking, comment::CommentResponse}; use lemmy_utils::LemmyError; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/receive/community.rs b/lemmy_apub/src/activities/receive/community.rs index 932917b1..f493a563 100644 --- a/lemmy_apub/src/activities/receive/community.rs +++ b/lemmy_apub/src/activities/receive/community.rs @@ -4,12 +4,9 @@ use activitystreams::{ base::{AnyBase, ExtendsExt}, }; use anyhow::Context; -use lemmy_db::{ - source::community::Community_, - views::community::community_view::CommunityView, - ApubObject, -}; +use lemmy_db::{source::community::Community_, ApubObject}; use lemmy_db_schema::source::community::Community; +use lemmy_db_views::community::community_view::CommunityView; use lemmy_structs::{blocking, community::CommunityResponse}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/receive/post.rs b/lemmy_apub/src/activities/receive/post.rs index 8e20da75..2b46cf54 100644 --- a/lemmy_apub/src/activities/receive/post.rs +++ b/lemmy_apub/src/activities/receive/post.rs @@ -4,8 +4,9 @@ use activitystreams::{ prelude::*, }; use anyhow::Context; -use lemmy_db::{source::post::Post_, views::post_view::PostView, Likeable}; +use lemmy_db::{source::post::Post_, Likeable}; use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm}; +use lemmy_db_views::post_view::PostView; use lemmy_structs::{blocking, post::PostResponse}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/receive/post_undo.rs b/lemmy_apub/src/activities/receive/post_undo.rs index facaf65f..bf188a9f 100644 --- a/lemmy_apub/src/activities/receive/post_undo.rs +++ b/lemmy_apub/src/activities/receive/post_undo.rs @@ -1,7 +1,8 @@ use crate::activities::receive::get_actor_as_user; use activitystreams::activity::{Dislike, Like}; -use lemmy_db::{source::post::Post_, views::post_view::PostView, Likeable}; +use lemmy_db::{source::post::Post_, Likeable}; use lemmy_db_schema::source::post::{Post, PostLike}; +use lemmy_db_views::post_view::PostView; use lemmy_structs::{blocking, post::PostResponse}; use lemmy_utils::LemmyError; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/receive/private_message.rs b/lemmy_apub/src/activities/receive/private_message.rs index 09c234ca..d2755094 100644 --- a/lemmy_apub/src/activities/receive/private_message.rs +++ b/lemmy_apub/src/activities/receive/private_message.rs @@ -13,11 +13,9 @@ use activitystreams::{ public, }; use anyhow::{anyhow, Context}; -use lemmy_db::{ - source::private_message::PrivateMessage_, - views::private_message_view::PrivateMessageView, -}; +use lemmy_db::source::private_message::PrivateMessage_; use lemmy_db_schema::source::private_message::PrivateMessage; +use lemmy_db_views::private_message_view::PrivateMessageView; use lemmy_structs::{blocking, user::PrivateMessageResponse}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/send/community.rs b/lemmy_apub/src/activities/send/community.rs index 035a8dfe..1a4a4a57 100644 --- a/lemmy_apub/src/activities/send/community.rs +++ b/lemmy_apub/src/activities/send/community.rs @@ -23,8 +23,9 @@ use activitystreams::{ }; use anyhow::Context; use itertools::Itertools; -use lemmy_db::{views::community::community_follower_view::CommunityFollowerView, DbPool}; +use lemmy_db::DbPool; use lemmy_db_schema::source::community::Community; +use lemmy_db_views::community::community_follower_view::CommunityFollowerView; use lemmy_structs::blocking; use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/fetcher.rs b/lemmy_apub/src/fetcher.rs index 608302fa..e67fbc96 100644 --- a/lemmy_apub/src/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -12,19 +12,7 @@ use activitystreams::{base::BaseExt, collection::OrderedCollection, prelude::*}; use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; use diesel::result::Error::NotFound; -use lemmy_db::{ - source::user::User, - views::{ - comment_view::CommentView, - community::community_view::CommunityView, - post_view::PostView, - user_view::UserViewSafe, - }, - ApubObject, - Crud, - Joinable, - SearchType, -}; +use lemmy_db::{source::user::User, ApubObject, Crud, Joinable, SearchType}; use lemmy_db_schema::{ naive_now, source::{ @@ -34,6 +22,12 @@ use lemmy_db_schema::{ user::User_, }, }; +use lemmy_db_views::{ + comment_view::CommentView, + community::community_view::CommunityView, + post_view::PostView, + user_view::UserViewSafe, +}; use lemmy_structs::{blocking, site::SearchResponse}; use lemmy_utils::{ location_info, diff --git a/lemmy_apub/src/http/community.rs b/lemmy_apub/src/http/community.rs index 45c576d2..011f2d88 100644 --- a/lemmy_apub/src/http/community.rs +++ b/lemmy_apub/src/http/community.rs @@ -9,11 +9,9 @@ use activitystreams::{ collection::{CollectionExt, OrderedCollection, UnorderedCollection}, }; use actix_web::{body::Body, web, HttpResponse}; -use lemmy_db::{ - source::{community::Community_, post::Post_}, - views::community::community_follower_view::CommunityFollowerView, -}; +use lemmy_db::source::{community::Community_, post::Post_}; use lemmy_db_schema::source::{community::Community, post::Post}; +use lemmy_db_views::community::community_follower_view::CommunityFollowerView; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/community_inbox.rs b/lemmy_apub/src/inbox/community_inbox.rs index f82da236..e6d7b29c 100644 --- a/lemmy_apub/src/inbox/community_inbox.rs +++ b/lemmy_apub/src/inbox/community_inbox.rs @@ -26,17 +26,12 @@ use activitystreams::{ }; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::{anyhow, Context}; -use lemmy_db::{ - source::community::Community_, - views::community::community_user_ban_view::CommunityUserBanView, - ApubObject, - DbPool, - Followable, -}; +use lemmy_db::{source::community::Community_, ApubObject, DbPool, Followable}; use lemmy_db_schema::source::{ community::{Community, CommunityFollower, CommunityFollowerForm}, user::User_, }; +use lemmy_db_views::community::community_user_ban_view::CommunityUserBanView; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/objects/community.rs b/lemmy_apub/src/objects/community.rs index b408f773..f4910716 100644 --- a/lemmy_apub/src/objects/community.rs +++ b/lemmy_apub/src/objects/community.rs @@ -22,11 +22,12 @@ use activitystreams::{ }; use activitystreams_ext::Ext2; use anyhow::Context; -use lemmy_db::{views::community::community_moderator_view::CommunityModeratorView, DbPool}; +use lemmy_db::DbPool; use lemmy_db_schema::{ naive_now, source::community::{Community, CommunityForm}, }; +use lemmy_db_views::community::community_moderator_view::CommunityModeratorView; use lemmy_structs::blocking; use lemmy_utils::{ location_info, diff --git a/lemmy_db_aggregates/Cargo.toml b/lemmy_db_aggregates/Cargo.toml new file mode 100644 index 00000000..6fdd77fd --- /dev/null +++ b/lemmy_db_aggregates/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "lemmy_db_aggregates" +version = "0.1.0" +edition = "2018" + +[dependencies] +lemmy_db_schema = { path = "../lemmy_db_schema" } +lemmy_db_queries = { path = "../lemmy_db_queries" } +diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } +serde = { version = "1.0.118", features = ["derive"] } +chrono = { version = "0.4.19", features = ["serde"] } diff --git a/lemmy_db/src/aggregates/comment_aggregates.rs b/lemmy_db_aggregates/src/comment_aggregates.rs similarity index 94% rename from lemmy_db/src/aggregates/comment_aggregates.rs rename to lemmy_db_aggregates/src/comment_aggregates.rs index c6b72675..611ec287 100644 --- a/lemmy_db/src/aggregates/comment_aggregates.rs +++ b/lemmy_db_aggregates/src/comment_aggregates.rs @@ -22,19 +22,13 @@ impl CommentAggregates { #[cfg(test)] mod tests { - use crate::{ - aggregates::comment_aggregates::CommentAggregates, - source::{ - comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, - community::{Community, CommunityForm}, - post::{Post, PostForm}, - user::{UserForm, User_}, - }, - tests::establish_unpooled_connection, - Crud, - Likeable, - ListingType, - SortType, + use crate::comment_aggregates::CommentAggregates; + use lemmy_db::{establish_unpooled_connection, Crud, Likeable, ListingType, SortType}; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, + community::{Community, CommunityForm}, + post::{Post, PostForm}, + user::{UserForm, User_}, }; #[test] diff --git a/lemmy_db/src/aggregates/community_aggregates.rs b/lemmy_db_aggregates/src/community_aggregates.rs similarity index 97% rename from lemmy_db/src/aggregates/community_aggregates.rs rename to lemmy_db_aggregates/src/community_aggregates.rs index 229652f0..d6491546 100644 --- a/lemmy_db/src/aggregates/community_aggregates.rs +++ b/lemmy_db_aggregates/src/community_aggregates.rs @@ -22,14 +22,8 @@ impl CommunityAggregates { #[cfg(test)] mod tests { - use crate::{ - aggregates::community_aggregates::CommunityAggregates, - tests::establish_unpooled_connection, - Crud, - Followable, - ListingType, - SortType, - }; + use crate::community_aggregates::CommunityAggregates; + use lemmy_db::{establish_unpooled_connection, Crud, Followable, ListingType, SortType}; use lemmy_db_schema::source::{ comment::{Comment, CommentForm}, community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm}, diff --git a/lemmy_db/src/aggregates/mod.rs b/lemmy_db_aggregates/src/lib.rs similarity index 100% rename from lemmy_db/src/aggregates/mod.rs rename to lemmy_db_aggregates/src/lib.rs diff --git a/lemmy_db/src/aggregates/post_aggregates.rs b/lemmy_db_aggregates/src/post_aggregates.rs similarity index 97% rename from lemmy_db/src/aggregates/post_aggregates.rs rename to lemmy_db_aggregates/src/post_aggregates.rs index 01082ca0..e2d91408 100644 --- a/lemmy_db/src/aggregates/post_aggregates.rs +++ b/lemmy_db_aggregates/src/post_aggregates.rs @@ -24,14 +24,8 @@ impl PostAggregates { #[cfg(test)] mod tests { - use crate::{ - aggregates::post_aggregates::PostAggregates, - tests::establish_unpooled_connection, - Crud, - Likeable, - ListingType, - SortType, - }; + use crate::post_aggregates::PostAggregates; + use lemmy_db::{establish_unpooled_connection, Crud, Likeable, ListingType, SortType}; use lemmy_db_schema::source::{ comment::{Comment, CommentForm}, community::{Community, CommunityForm}, diff --git a/lemmy_db/src/aggregates/site_aggregates.rs b/lemmy_db_aggregates/src/site_aggregates.rs similarity index 96% rename from lemmy_db/src/aggregates/site_aggregates.rs rename to lemmy_db_aggregates/src/site_aggregates.rs index 70997a66..559c5b53 100644 --- a/lemmy_db/src/aggregates/site_aggregates.rs +++ b/lemmy_db_aggregates/src/site_aggregates.rs @@ -21,17 +21,13 @@ impl SiteAggregates { #[cfg(test)] mod tests { - use crate::{ - aggregates::site_aggregates::SiteAggregates, - tests::establish_unpooled_connection, - Crud, - ListingType, - SortType, - }; + use crate::site_aggregates::SiteAggregates; + use lemmy_db::{establish_unpooled_connection, Crud, ListingType, SortType}; use lemmy_db_schema::source::{ comment::{Comment, CommentForm}, community::{Community, CommunityForm}, post::{Post, PostForm}, + site::{Site, SiteForm}, user::{UserForm, User_}, }; diff --git a/lemmy_db/src/aggregates/user_aggregates.rs b/lemmy_db_aggregates/src/user_aggregates.rs similarity index 97% rename from lemmy_db/src/aggregates/user_aggregates.rs rename to lemmy_db_aggregates/src/user_aggregates.rs index e8981fd6..cf5dfe69 100644 --- a/lemmy_db/src/aggregates/user_aggregates.rs +++ b/lemmy_db_aggregates/src/user_aggregates.rs @@ -23,14 +23,8 @@ impl UserAggregates { #[cfg(test)] mod tests { - use crate::{ - aggregates::user_aggregates::UserAggregates, - tests::establish_unpooled_connection, - Crud, - Likeable, - ListingType, - SortType, - }; + use crate::user_aggregates::UserAggregates; + use lemmy_db::{establish_unpooled_connection, Crud, Likeable, ListingType, SortType}; use lemmy_db_schema::source::{ comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, community::{Community, CommunityForm}, diff --git a/lemmy_db/Cargo.toml b/lemmy_db_queries/Cargo.toml similarity index 96% rename from lemmy_db/Cargo.toml rename to lemmy_db_queries/Cargo.toml index 15dd749a..eb633c7d 100644 --- a/lemmy_db/Cargo.toml +++ b/lemmy_db_queries/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lemmy_db" +name = "lemmy_db_queries" version = "0.1.0" edition = "2018" diff --git a/lemmy_db/src/lib.rs b/lemmy_db_queries/src/lib.rs similarity index 88% rename from lemmy_db/src/lib.rs rename to lemmy_db_queries/src/lib.rs index 8e352147..6f4c62c7 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db_queries/src/lib.rs @@ -14,9 +14,7 @@ use regex::Regex; use serde::{Deserialize, Serialize}; use std::{env, env::VarError}; -pub mod aggregates; pub mod source; -pub mod views; pub type DbPool = diesel::r2d2::Pool>; @@ -133,7 +131,7 @@ impl MaybeOptional for Option { } } -pub(crate) trait ToSafe { +pub trait ToSafe { type SafeColumns; fn safe_columns_tuple() -> Self::SafeColumns; } @@ -202,12 +200,28 @@ pub fn diesel_option_overwrite(opt: &Option) -> Option> { } } +embed_migrations!(); + +pub fn establish_unpooled_connection() -> PgConnection { + let db_url = match get_database_url_from_env() { + Ok(url) => url, + Err(e) => panic!( + "Failed to read database URL from env var LEMMY_DATABASE_URL: {}", + e + ), + }; + let conn = + PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); + embedded_migrations::run(&conn).unwrap(); + conn +} + lazy_static! { static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap(); } -pub(crate) mod functions { +pub mod functions { use diesel::sql_types::*; sql_function! { @@ -218,24 +232,7 @@ pub(crate) mod functions { #[cfg(test)] mod tests { use super::fuzzy_search; - use crate::{get_database_url_from_env, is_email_regex}; - use diesel::{Connection, PgConnection}; - - embed_migrations!(); - - pub fn establish_unpooled_connection() -> PgConnection { - let db_url = match get_database_url_from_env() { - Ok(url) => url, - Err(e) => panic!( - "Failed to read database URL from env var LEMMY_DATABASE_URL: {}", - e - ), - }; - let conn = - PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); - embedded_migrations::run(&conn).unwrap(); - conn - } + use crate::is_email_regex; #[test] fn test_fuzzy_search() { diff --git a/lemmy_db/src/source/activity.rs b/lemmy_db_queries/src/source/activity.rs similarity index 97% rename from lemmy_db/src/source/activity.rs rename to lemmy_db_queries/src/source/activity.rs index d63b3b6b..662db3ae 100644 --- a/lemmy_db/src/source/activity.rs +++ b/lemmy_db_queries/src/source/activity.rs @@ -87,7 +87,13 @@ impl Activity_ for Activity { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use crate::{ + establish_unpooled_connection, + source::activity::Activity_, + Crud, + ListingType, + SortType, + }; use lemmy_db_schema::source::{ activity::{Activity, ActivityForm}, user::{UserForm, User_}, diff --git a/lemmy_db/src/source/category.rs b/lemmy_db_queries/src/source/category.rs similarity index 94% rename from lemmy_db/src/source/category.rs rename to lemmy_db_queries/src/source/category.rs index c190ddf8..2d9eeb37 100644 --- a/lemmy_db/src/source/category.rs +++ b/lemmy_db_queries/src/source/category.rs @@ -36,7 +36,7 @@ impl Category_ for Category { #[cfg(test)] mod tests { - use crate::tests::establish_unpooled_connection; + use crate::{establish_unpooled_connection, source::category::Category_}; use lemmy_db_schema::source::category::Category; #[test] diff --git a/lemmy_db/src/source/comment.rs b/lemmy_db_queries/src/source/comment.rs similarity index 98% rename from lemmy_db/src/source/comment.rs rename to lemmy_db_queries/src/source/comment.rs index d70d7b69..6e99bf36 100644 --- a/lemmy_db/src/source/comment.rs +++ b/lemmy_db_queries/src/source/comment.rs @@ -204,14 +204,7 @@ impl Saveable for CommentSaved { #[cfg(test)] mod tests { - use crate::{ - tests::establish_unpooled_connection, - Crud, - Likeable, - ListingType, - Saveable, - SortType, - }; + use crate::{establish_unpooled_connection, Crud, Likeable, ListingType, Saveable, SortType}; use lemmy_db_schema::source::{ comment::*, community::{Community, CommunityForm}, diff --git a/lemmy_db/src/source/comment_report.rs b/lemmy_db_queries/src/source/comment_report.rs similarity index 100% rename from lemmy_db/src/source/comment_report.rs rename to lemmy_db_queries/src/source/comment_report.rs diff --git a/lemmy_db/src/source/community.rs b/lemmy_db_queries/src/source/community.rs similarity index 92% rename from lemmy_db/src/source/community.rs rename to lemmy_db_queries/src/source/community.rs index 9a30ca4c..28245d8d 100644 --- a/lemmy_db/src/source/community.rs +++ b/lemmy_db_queries/src/source/community.rs @@ -1,11 +1,4 @@ -use crate::{ - views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}, - ApubObject, - Bannable, - Crud, - Followable, - Joinable, -}; +use crate::{ApubObject, Bannable, Crud, Followable, Joinable}; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::{ naive_now, @@ -138,9 +131,7 @@ pub trait Community_ { community_id: i32, new_creator_id: i32, ) -> Result; - fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result, Error>; fn distinct_federated_communities(conn: &PgConnection) -> Result, Error>; - fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool; } impl Community_ for Community { @@ -196,27 +187,10 @@ impl Community_ for Community { .get_result::(conn) } - fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result, Error> { - let mut mods_and_admins: Vec = Vec::new(); - mods_and_admins.append( - &mut CommunityModeratorView::for_community(conn, community_id) - .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?, - ); - mods_and_admins - .append(&mut UserViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.user.id).collect())?); - Ok(mods_and_admins) - } - fn distinct_federated_communities(conn: &PgConnection) -> Result, Error> { use lemmy_db_schema::schema::community::dsl::*; community.select(actor_id).distinct().load::(conn) } - - fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool { - Self::community_mods_and_admins(conn, community_id) - .unwrap_or_default() - .contains(&user_id) - } } impl Joinable for CommunityModerator { @@ -347,7 +321,7 @@ impl Followable for CommunityFollower { #[cfg(test)] mod tests { use crate::{ - tests::establish_unpooled_connection, + establish_unpooled_connection, Bannable, Crud, Followable, diff --git a/lemmy_db/src/source/mod.rs b/lemmy_db_queries/src/source/mod.rs similarity index 100% rename from lemmy_db/src/source/mod.rs rename to lemmy_db_queries/src/source/mod.rs diff --git a/lemmy_db/src/source/moderator.rs b/lemmy_db_queries/src/source/moderator.rs similarity index 99% rename from lemmy_db/src/source/moderator.rs rename to lemmy_db_queries/src/source/moderator.rs index b0a17d6d..93c42416 100644 --- a/lemmy_db/src/source/moderator.rs +++ b/lemmy_db_queries/src/source/moderator.rs @@ -197,7 +197,7 @@ impl Crud for ModAdd { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use crate::{establish_unpooled_connection, Crud, ListingType, SortType}; use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*}; // use Crud; diff --git a/lemmy_db/src/source/password_reset_request.rs b/lemmy_db_queries/src/source/password_reset_request.rs similarity index 96% rename from lemmy_db/src/source/password_reset_request.rs rename to lemmy_db_queries/src/source/password_reset_request.rs index 9ca8dab6..d4ba2f12 100644 --- a/lemmy_db/src/source/password_reset_request.rs +++ b/lemmy_db_queries/src/source/password_reset_request.rs @@ -72,7 +72,13 @@ fn bytes_to_hex(bytes: Vec) -> String { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use crate::{ + establish_unpooled_connection, + source::password_reset_request::PasswordResetRequest_, + Crud, + ListingType, + SortType, + }; use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*}; #[test] diff --git a/lemmy_db/src/source/post.rs b/lemmy_db_queries/src/source/post.rs similarity index 99% rename from lemmy_db/src/source/post.rs rename to lemmy_db_queries/src/source/post.rs index c681adbe..bca848ca 100644 --- a/lemmy_db/src/source/post.rs +++ b/lemmy_db_queries/src/source/post.rs @@ -225,7 +225,7 @@ impl Readable for PostRead { #[cfg(test)] mod tests { - use crate::{source::post::*, tests::establish_unpooled_connection, ListingType, SortType}; + use crate::{establish_unpooled_connection, source::post::*, ListingType, SortType}; use lemmy_db_schema::source::{ community::{Community, CommunityForm}, user::*, diff --git a/lemmy_db/src/source/post_report.rs b/lemmy_db_queries/src/source/post_report.rs similarity index 100% rename from lemmy_db/src/source/post_report.rs rename to lemmy_db_queries/src/source/post_report.rs diff --git a/lemmy_db/src/source/private_message.rs b/lemmy_db_queries/src/source/private_message.rs similarity index 98% rename from lemmy_db/src/source/private_message.rs rename to lemmy_db_queries/src/source/private_message.rs index b49ed8f4..d63f698d 100644 --- a/lemmy_db/src/source/private_message.rs +++ b/lemmy_db_queries/src/source/private_message.rs @@ -139,7 +139,13 @@ impl PrivateMessage_ for PrivateMessage { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, ListingType, SortType}; + use crate::{ + establish_unpooled_connection, + source::private_message::PrivateMessage_, + Crud, + ListingType, + SortType, + }; use lemmy_db_schema::source::{private_message::*, user::*}; #[test] diff --git a/lemmy_db/src/source/site.rs b/lemmy_db_queries/src/source/site.rs similarity index 100% rename from lemmy_db/src/source/site.rs rename to lemmy_db_queries/src/source/site.rs diff --git a/lemmy_db/src/source/user.rs b/lemmy_db_queries/src/source/user.rs similarity index 99% rename from lemmy_db/src/source/user.rs rename to lemmy_db_queries/src/source/user.rs index 7461f4b4..7789ff2b 100644 --- a/lemmy_db/src/source/user.rs +++ b/lemmy_db_queries/src/source/user.rs @@ -297,7 +297,7 @@ impl User for User_ { #[cfg(test)] mod tests { - use crate::{source::user::*, tests::establish_unpooled_connection, ListingType, SortType}; + use crate::{establish_unpooled_connection, source::user::*, ListingType, SortType}; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/user_mention.rs b/lemmy_db_queries/src/source/user_mention.rs similarity index 98% rename from lemmy_db/src/source/user_mention.rs rename to lemmy_db_queries/src/source/user_mention.rs index 64406f98..d9e0cce3 100644 --- a/lemmy_db/src/source/user_mention.rs +++ b/lemmy_db_queries/src/source/user_mention.rs @@ -73,7 +73,7 @@ impl UserMention_ for UserMention { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use crate::{establish_unpooled_connection, Crud, ListingType, SortType}; use lemmy_db_schema::source::{ comment::*, community::{Community, CommunityForm}, diff --git a/lemmy_db_schema/src/lib.rs b/lemmy_db_schema/src/lib.rs index 3868a3b7..981ecba2 100644 --- a/lemmy_db_schema/src/lib.rs +++ b/lemmy_db_schema/src/lib.rs @@ -6,7 +6,7 @@ use chrono::NaiveDateTime; pub mod schema; pub mod source; -// TODO: can probably move this back to lemmy_db +// TODO: can probably move this back to lemmy_db_queries pub fn naive_now() -> NaiveDateTime { chrono::prelude::Utc::now().naive_utc() } diff --git a/lemmy_db_schema/src/source/comment_report.rs b/lemmy_db_schema/src/source/comment_report.rs index ec53408d..d94fed57 100644 --- a/lemmy_db_schema/src/source/comment_report.rs +++ b/lemmy_db_schema/src/source/comment_report.rs @@ -1,9 +1,7 @@ use crate::{schema::comment_report, source::comment::Comment}; use serde::{Deserialize, Serialize}; -#[derive( - Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone, -)] +#[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)] #[belongs_to(Comment)] #[table_name = "comment_report"] pub struct CommentReport { diff --git a/lemmy_db_schema/src/source/post_report.rs b/lemmy_db_schema/src/source/post_report.rs index b75fb954..608104db 100644 --- a/lemmy_db_schema/src/source/post_report.rs +++ b/lemmy_db_schema/src/source/post_report.rs @@ -1,9 +1,7 @@ use crate::{schema::post_report, source::post::Post}; use serde::{Deserialize, Serialize}; -#[derive( - Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone, -)] +#[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)] #[belongs_to(Post)] #[table_name = "post_report"] pub struct PostReport { diff --git a/lemmy_db_views/Cargo.toml b/lemmy_db_views/Cargo.toml new file mode 100644 index 00000000..86c7ada1 --- /dev/null +++ b/lemmy_db_views/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "lemmy_db_views" +version = "0.1.0" +edition = "2018" + +[dependencies] +lemmy_db_queries = { path = "../lemmy_db_queries" } +lemmy_db_schema = { path = "../lemmy_db_schema" } +lemmy_db_aggregates = { path = "../lemmy_db_aggregates" } +diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } +serde = { version = "1.0.118", features = ["derive"] } \ No newline at end of file diff --git a/lemmy_db/src/views/comment_report_view.rs b/lemmy_db_views/src/comment_report_view.rs similarity index 98% rename from lemmy_db/src/views/comment_report_view.rs rename to lemmy_db_views/src/comment_report_view.rs index 7a260cd5..fa795a0b 100644 --- a/lemmy_db/src/views/comment_report_view.rs +++ b/lemmy_db_views/src/comment_report_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, MaybeOptional, ToSafe}; use lemmy_db_schema::{ schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2}, source::{ diff --git a/lemmy_db/src/views/comment_view.rs b/lemmy_db_views/src/comment_view.rs similarity index 98% rename from lemmy_db/src/views/comment_view.rs rename to lemmy_db_views/src/comment_view.rs index 1b114e19..3101fcea 100644 --- a/lemmy_db/src/views/comment_view.rs +++ b/lemmy_db_views/src/comment_view.rs @@ -1,15 +1,15 @@ -use crate::{ - aggregates::comment_aggregates::CommentAggregates, +use crate::ViewToVec; +use diesel::{result::Error, *}; +use lemmy_db::{ functions::hot_rank, fuzzy_search, limit_and_offset, - views::ViewToVec, ListingType, MaybeOptional, SortType, ToSafe, }; -use diesel::{result::Error, *}; +use lemmy_db_aggregates::comment_aggregates::CommentAggregates; use lemmy_db_schema::{ schema::{ comment, @@ -418,7 +418,9 @@ impl ViewToVec for CommentView { #[cfg(test)] mod tests { - use crate::{tests::establish_unpooled_connection, views::comment_view::*, Crud, Likeable, *}; + use crate::comment_view::*; + use lemmy_db::{establish_unpooled_connection, Crud, Likeable, ListingType, SortType}; + use lemmy_db_aggregates::comment_aggregates::CommentAggregates; use lemmy_db_schema::source::{comment::*, community::*, post::*, user::*}; #[test] diff --git a/lemmy_db/src/views/community/community_follower_view.rs b/lemmy_db_views/src/community/community_follower_view.rs similarity index 97% rename from lemmy_db/src/views/community/community_follower_view.rs rename to lemmy_db_views/src/community/community_follower_view.rs index e7ba0e4a..9837624c 100644 --- a/lemmy_db/src/views/community/community_follower_view.rs +++ b/lemmy_db_views/src/community/community_follower_view.rs @@ -1,5 +1,6 @@ -use crate::{views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::ToSafe; use lemmy_db_schema::{ schema::{community, community_follower, user_}, source::{ diff --git a/lemmy_db/src/views/community/community_moderator_view.rs b/lemmy_db_views/src/community/community_moderator_view.rs similarity index 97% rename from lemmy_db/src/views/community/community_moderator_view.rs rename to lemmy_db_views/src/community/community_moderator_view.rs index 6800853e..490105d8 100644 --- a/lemmy_db/src/views/community/community_moderator_view.rs +++ b/lemmy_db_views/src/community/community_moderator_view.rs @@ -1,5 +1,6 @@ -use crate::{views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::ToSafe; use lemmy_db_schema::{ schema::{community, community_moderator, user_}, source::{ diff --git a/lemmy_db/src/views/community/community_user_ban_view.rs b/lemmy_db_views/src/community/community_user_ban_view.rs similarity index 97% rename from lemmy_db/src/views/community/community_user_ban_view.rs rename to lemmy_db_views/src/community/community_user_ban_view.rs index 1c26ebcf..ddc90276 100644 --- a/lemmy_db/src/views/community/community_user_ban_view.rs +++ b/lemmy_db_views/src/community/community_user_ban_view.rs @@ -1,5 +1,5 @@ -use crate::ToSafe; use diesel::{result::Error, *}; +use lemmy_db::ToSafe; use lemmy_db_schema::{ schema::{community, community_user_ban, user_}, source::{ diff --git a/lemmy_db/src/views/community/community_view.rs b/lemmy_db_views/src/community/community_view.rs similarity index 85% rename from lemmy_db/src/views/community/community_view.rs rename to lemmy_db_views/src/community/community_view.rs index 11962d79..fe87e2c7 100644 --- a/lemmy_db/src/views/community/community_view.rs +++ b/lemmy_db_views/src/community/community_view.rs @@ -1,14 +1,18 @@ use crate::{ - aggregates::community_aggregates::CommunityAggregates, + community::community_moderator_view::CommunityModeratorView, + user_view::UserViewSafe, + ViewToVec, +}; +use diesel::{result::Error, *}; +use lemmy_db::{ functions::hot_rank, fuzzy_search, limit_and_offset, - views::ViewToVec, MaybeOptional, SortType, ToSafe, }; -use diesel::{result::Error, *}; +use lemmy_db_aggregates::community_aggregates::CommunityAggregates; use lemmy_db_schema::{ schema::{category, community, community_aggregates, community_follower, user_}, source::{ @@ -74,6 +78,24 @@ impl CommunityView { counts, }) } + + // TODO: this function is only used by is_mod_or_admin() below, can probably be merged + fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result, Error> { + let mut mods_and_admins: Vec = Vec::new(); + mods_and_admins.append( + &mut CommunityModeratorView::for_community(conn, community_id) + .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?, + ); + mods_and_admins + .append(&mut UserViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.user.id).collect())?); + Ok(mods_and_admins) + } + + pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool { + Self::community_mods_and_admins(conn, community_id) + .unwrap_or_default() + .contains(&user_id) + } } pub struct CommunityQueryBuilder<'a> { diff --git a/lemmy_db/src/views/community/mod.rs b/lemmy_db_views/src/community/mod.rs similarity index 100% rename from lemmy_db/src/views/community/mod.rs rename to lemmy_db_views/src/community/mod.rs diff --git a/lemmy_db/src/views/mod.rs b/lemmy_db_views/src/lib.rs similarity index 100% rename from lemmy_db/src/views/mod.rs rename to lemmy_db_views/src/lib.rs diff --git a/lemmy_db/src/views/moderator/mod.rs b/lemmy_db_views/src/moderator/mod.rs similarity index 100% rename from lemmy_db/src/views/moderator/mod.rs rename to lemmy_db_views/src/moderator/mod.rs diff --git a/lemmy_db/src/views/moderator/mod_add_community_view.rs b/lemmy_db_views/src/moderator/mod_add_community_view.rs similarity index 97% rename from lemmy_db/src/views/moderator/mod_add_community_view.rs rename to lemmy_db_views/src/moderator/mod_add_community_view.rs index 302d37a0..606ee51e 100644 --- a/lemmy_db/src/views/moderator/mod_add_community_view.rs +++ b/lemmy_db_views/src/moderator/mod_add_community_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_add_community, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_add_view.rs b/lemmy_db_views/src/moderator/mod_add_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_add_view.rs rename to lemmy_db_views/src/moderator/mod_add_view.rs index 8f586a6f..efe33fd3 100644 --- a/lemmy_db/src/views/moderator/mod_add_view.rs +++ b/lemmy_db_views/src/moderator/mod_add_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{mod_add, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs b/lemmy_db_views/src/moderator/mod_ban_from_community_view.rs similarity index 97% rename from lemmy_db/src/views/moderator/mod_ban_from_community_view.rs rename to lemmy_db_views/src/moderator/mod_ban_from_community_view.rs index 0ed52dd2..430259a4 100644 --- a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs +++ b/lemmy_db_views/src/moderator/mod_ban_from_community_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_ban_from_community, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_ban_view.rs b/lemmy_db_views/src/moderator/mod_ban_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_ban_view.rs rename to lemmy_db_views/src/moderator/mod_ban_view.rs index 98cf1969..f61a0421 100644 --- a/lemmy_db/src/views/moderator/mod_ban_view.rs +++ b/lemmy_db_views/src/moderator/mod_ban_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{mod_ban, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_lock_post_view.rs b/lemmy_db_views/src/moderator/mod_lock_post_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_lock_post_view.rs rename to lemmy_db_views/src/moderator/mod_lock_post_view.rs index dbe81c5c..11521329 100644 --- a/lemmy_db/src/views/moderator/mod_lock_post_view.rs +++ b/lemmy_db_views/src/moderator/mod_lock_post_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_lock_post, post, user_}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs b/lemmy_db_views/src/moderator/mod_remove_comment_view.rs similarity index 97% rename from lemmy_db/src/views/moderator/mod_remove_comment_view.rs rename to lemmy_db_views/src/moderator/mod_remove_comment_view.rs index 04aab30a..0c52be2f 100644 --- a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs +++ b/lemmy_db_views/src/moderator/mod_remove_comment_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_remove_community_view.rs b/lemmy_db_views/src/moderator/mod_remove_community_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_remove_community_view.rs rename to lemmy_db_views/src/moderator/mod_remove_community_view.rs index 37ffe540..86fcf195 100644 --- a/lemmy_db/src/views/moderator/mod_remove_community_view.rs +++ b/lemmy_db_views/src/moderator/mod_remove_community_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_remove_community, user_}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_remove_post_view.rs b/lemmy_db_views/src/moderator/mod_remove_post_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_remove_post_view.rs rename to lemmy_db_views/src/moderator/mod_remove_post_view.rs index 21bf1b86..d4332918 100644 --- a/lemmy_db/src/views/moderator/mod_remove_post_view.rs +++ b/lemmy_db_views/src/moderator/mod_remove_post_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_remove_post, post, user_}, source::{ diff --git a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs b/lemmy_db_views/src/moderator/mod_sticky_post_view.rs similarity index 96% rename from lemmy_db/src/views/moderator/mod_sticky_post_view.rs rename to lemmy_db_views/src/moderator/mod_sticky_post_view.rs index 8512e079..ec785ce1 100644 --- a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs +++ b/lemmy_db_views/src/moderator/mod_sticky_post_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, ToSafe}; use lemmy_db_schema::{ schema::{community, mod_sticky_post, post, user_}, source::{ diff --git a/lemmy_db/src/views/post_report_view.rs b/lemmy_db_views/src/post_report_view.rs similarity index 98% rename from lemmy_db/src/views/post_report_view.rs rename to lemmy_db_views/src/post_report_view.rs index 37d6275c..3e0ef3d6 100644 --- a/lemmy_db/src/views/post_report_view.rs +++ b/lemmy_db_views/src/post_report_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, MaybeOptional, ToSafe}; use lemmy_db_schema::{ schema::{community, post, post_report, user_, user_alias_1, user_alias_2}, source::{ diff --git a/lemmy_db/src/views/post_view.rs b/lemmy_db_views/src/post_view.rs similarity index 98% rename from lemmy_db/src/views/post_view.rs rename to lemmy_db_views/src/post_view.rs index 3cfee1b3..fe115927 100644 --- a/lemmy_db/src/views/post_view.rs +++ b/lemmy_db_views/src/post_view.rs @@ -1,15 +1,15 @@ -use crate::{ - aggregates::post_aggregates::PostAggregates, +use crate::ViewToVec; +use diesel::{result::Error, *}; +use lemmy_db::{ functions::hot_rank, fuzzy_search, limit_and_offset, - views::ViewToVec, ListingType, MaybeOptional, SortType, ToSafe, }; -use diesel::{result::Error, *}; +use lemmy_db_aggregates::post_aggregates::PostAggregates; use lemmy_db_schema::{ schema::{ community, @@ -406,14 +406,9 @@ impl ViewToVec for PostView { #[cfg(test)] mod tests { - use crate::{ - aggregates::post_aggregates::PostAggregates, - tests::establish_unpooled_connection, - views::post_view::{PostQueryBuilder, PostView}, - Crud, - Likeable, - *, - }; + use crate::post_view::{PostQueryBuilder, PostView}; + use lemmy_db::{establish_unpooled_connection, Crud, Likeable, ListingType, SortType}; + use lemmy_db_aggregates::post_aggregates::PostAggregates; use lemmy_db_schema::source::{community::*, post::*, user::*}; #[test] diff --git a/lemmy_db/src/views/private_message_view.rs b/lemmy_db_views/src/private_message_view.rs similarity index 97% rename from lemmy_db/src/views/private_message_view.rs rename to lemmy_db_views/src/private_message_view.rs index f439a75d..62cb7898 100644 --- a/lemmy_db/src/views/private_message_view.rs +++ b/lemmy_db_views/src/private_message_view.rs @@ -1,5 +1,6 @@ -use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{limit_and_offset, MaybeOptional, ToSafe}; use lemmy_db_schema::{ schema::{private_message, user_, user_alias_1}, source::{ diff --git a/lemmy_db/src/views/site_view.rs b/lemmy_db_views/src/site_view.rs similarity index 90% rename from lemmy_db/src/views/site_view.rs rename to lemmy_db_views/src/site_view.rs index 7772ccdc..a4c533e0 100644 --- a/lemmy_db/src/views/site_view.rs +++ b/lemmy_db_views/src/site_view.rs @@ -1,5 +1,6 @@ -use crate::{aggregates::site_aggregates::SiteAggregates, ToSafe}; use diesel::{result::Error, *}; +use lemmy_db::ToSafe; +use lemmy_db_aggregates::site_aggregates::SiteAggregates; use lemmy_db_schema::{ schema::{site, site_aggregates, user_}, source::{ diff --git a/lemmy_db/src/views/user_mention_view.rs b/lemmy_db_views/src/user_mention_view.rs similarity index 98% rename from lemmy_db/src/views/user_mention_view.rs rename to lemmy_db_views/src/user_mention_view.rs index 2cd1cd3c..f6afd298 100644 --- a/lemmy_db/src/views/user_mention_view.rs +++ b/lemmy_db_views/src/user_mention_view.rs @@ -1,13 +1,7 @@ -use crate::{ - aggregates::comment_aggregates::CommentAggregates, - functions::hot_rank, - limit_and_offset, - views::ViewToVec, - MaybeOptional, - SortType, - ToSafe, -}; +use crate::ViewToVec; use diesel::{result::Error, *}; +use lemmy_db::{functions::hot_rank, limit_and_offset, MaybeOptional, SortType, ToSafe}; +use lemmy_db_aggregates::comment_aggregates::CommentAggregates; use lemmy_db_schema::{ schema::{ comment, diff --git a/lemmy_db/src/views/user_view.rs b/lemmy_db_views/src/user_view.rs similarity index 96% rename from lemmy_db/src/views/user_view.rs rename to lemmy_db_views/src/user_view.rs index f3109011..5e5e7f2c 100644 --- a/lemmy_db/src/views/user_view.rs +++ b/lemmy_db_views/src/user_view.rs @@ -1,13 +1,7 @@ -use crate::{ - aggregates::user_aggregates::UserAggregates, - fuzzy_search, - limit_and_offset, - views::ViewToVec, - MaybeOptional, - SortType, - ToSafe, -}; +use crate::ViewToVec; use diesel::{dsl::*, result::Error, *}; +use lemmy_db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType, ToSafe}; +use lemmy_db_aggregates::user_aggregates::UserAggregates; use lemmy_db_schema::{ schema::{user_, user_aggregates}, source::user::{UserSafe, User_}, diff --git a/lemmy_structs/Cargo.toml b/lemmy_structs/Cargo.toml index 329ef413..d4f013b9 100644 --- a/lemmy_structs/Cargo.toml +++ b/lemmy_structs/Cargo.toml @@ -9,7 +9,8 @@ name = "lemmy_structs" path = "src/lib.rs" [dependencies] -lemmy_db = { path = "../lemmy_db" } +lemmy_db_queries = { path = "../lemmy_db_queries" } +lemmy_db_views = { path = "../lemmy_db_views" } lemmy_db_schema = { path = "../lemmy_db_schema" } lemmy_utils = { path = "../lemmy_utils" } serde = { version = "1.0.118", features = ["derive"] } diff --git a/lemmy_structs/src/comment.rs b/lemmy_structs/src/comment.rs index fe65738d..1e113b36 100644 --- a/lemmy_structs/src/comment.rs +++ b/lemmy_structs/src/comment.rs @@ -1,4 +1,4 @@ -use lemmy_db::views::{comment_report_view::CommentReportView, comment_view::CommentView}; +use lemmy_db_views::{comment_report_view::CommentReportView, comment_view::CommentView}; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] diff --git a/lemmy_structs/src/community.rs b/lemmy_structs/src/community.rs index 65ea0aaa..74f7a4e4 100644 --- a/lemmy_structs/src/community.rs +++ b/lemmy_structs/src/community.rs @@ -1,4 +1,4 @@ -use lemmy_db::views::{ +use lemmy_db_views::{ community::{ community_follower_view::CommunityFollowerView, community_moderator_view::CommunityModeratorView, diff --git a/lemmy_structs/src/post.rs b/lemmy_structs/src/post.rs index ac29d8f7..5a35fa37 100644 --- a/lemmy_structs/src/post.rs +++ b/lemmy_structs/src/post.rs @@ -1,4 +1,4 @@ -use lemmy_db::views::{ +use lemmy_db_views::{ comment_view::CommentView, community::community_moderator_view::CommunityModeratorView, post_report_view::PostReportView, diff --git a/lemmy_structs/src/site.rs b/lemmy_structs/src/site.rs index f24d9f49..fbb295d5 100644 --- a/lemmy_structs/src/site.rs +++ b/lemmy_structs/src/site.rs @@ -1,4 +1,5 @@ -use lemmy_db::views::{ +use lemmy_db_schema::source::{category::*, user::User_}; +use lemmy_db_views::{ comment_view::CommentView, community::community_view::CommunityView, moderator::{ @@ -16,7 +17,6 @@ use lemmy_db::views::{ site_view::SiteView, user_view::UserViewSafe, }; -use lemmy_db_schema::source::{category::*, user::User_}; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] diff --git a/lemmy_structs/src/user.rs b/lemmy_structs/src/user.rs index 52871696..4b4d6a88 100644 --- a/lemmy_structs/src/user.rs +++ b/lemmy_structs/src/user.rs @@ -1,4 +1,4 @@ -use lemmy_db::views::{ +use lemmy_db_views::{ comment_view::CommentView, community::{ community_follower_view::CommunityFollowerView, diff --git a/lemmy_utils/src/settings.rs b/lemmy_utils/src/settings.rs index 4edcbd13..4ca87f28 100644 --- a/lemmy_utils/src/settings.rs +++ b/lemmy_utils/src/settings.rs @@ -89,7 +89,7 @@ impl Settings { /// added to the config. /// /// Note: The env var `LEMMY_DATABASE_URL` is parsed in - /// `lemmy_db/src/lib.rs::get_database_url_from_env()` + /// `lemmy_db_queries/src/lib.rs::get_database_url_from_env()` fn init() -> Result { let mut s = Config::new(); diff --git a/lemmy_websocket/Cargo.toml b/lemmy_websocket/Cargo.toml index 90cf0e17..4f036ae5 100644 --- a/lemmy_websocket/Cargo.toml +++ b/lemmy_websocket/Cargo.toml @@ -11,7 +11,7 @@ path = "src/lib.rs" [dependencies] lemmy_utils = { path = "../lemmy_utils" } lemmy_structs = { path = "../lemmy_structs" } -lemmy_db = { path = "../lemmy_db" } +lemmy_db_queries = { path = "../lemmy_db_queries" } lemmy_db_schema = { path = "../lemmy_db_schema" } lemmy_rate_limit = { path = "../lemmy_rate_limit" } reqwest = { version = "0.10.10", features = ["json"] } diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index fc397fff..d7862dcd 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -5,16 +5,16 @@ use diesel::PgConnection; use lemmy_api::claims::Claims; use lemmy_db::{ source::{community::Community_, user::User}, - views::{ - comment_view::{CommentQueryBuilder, CommentView}, - post_view::{PostQueryBuilder, PostView}, - site_view::SiteView, - user_mention_view::{UserMentionQueryBuilder, UserMentionView}, - }, ListingType, SortType, }; use lemmy_db_schema::source::{community::Community, user::User_}; +use lemmy_db_views::{ + comment_view::{CommentQueryBuilder, CommentView}, + post_view::{PostQueryBuilder, PostView}, + site_view::SiteView, + user_mention_view::{UserMentionQueryBuilder, UserMentionView}, +}; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/src/routes/nodeinfo.rs b/src/routes/nodeinfo.rs index 9f63a523..e6c5e6c4 100644 --- a/src/routes/nodeinfo.rs +++ b/src/routes/nodeinfo.rs @@ -1,7 +1,7 @@ use actix_web::{body::Body, error::ErrorBadRequest, *}; use anyhow::anyhow; use lemmy_api::version; -use lemmy_db::views::site_view::SiteView; +use lemmy_db_views::site_view::SiteView; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext;