From: Dessalines Date: Mon, 21 Dec 2020 14:28:20 +0000 (-0500) Subject: Merge remote-tracking branch 'origin/split-db-workspace' into move_views_to_diesel_split X-Git-Url: http://these/git/%7B%24%7B%60data:application/%22%7Burl%7D/readmes/static/%7B%7D/README.es.md?a=commitdiff_plain;h=1a0d1f64f0046a2b6af71d1dcb120ef164de581c;p=lemmy.git Merge remote-tracking branch 'origin/split-db-workspace' into move_views_to_diesel_split --- 1a0d1f64f0046a2b6af71d1dcb120ef164de581c diff --cc Cargo.lock index 815cd20f,6cc79fed..060fa891 --- a/Cargo.lock +++ b/Cargo.lock @@@ -1788,8 -1790,8 +1790,9 @@@ dependencies = "bcrypt", "chrono", "diesel", + "diesel_migrations", "lazy_static", + "lemmy_db_schema", "lemmy_utils", "log", "regex", diff --cc lemmy_api/src/comment.rs index e5f079ad,2f0f8a61..bafde6bf --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@@ -27,6 -23,7 +23,7 @@@ use lemmy_db:: Saveable, SortType, }; -use lemmy_db_schema::source::{comment::*, comment_report::*, moderator::*, post::Post, user::*}; ++use lemmy_db_schema::source::{comment::*, comment_report::*, moderator::*}; use lemmy_structs::{blocking, comment::*, send_local_notifs}; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, diff --cc lemmy_api/src/lib.rs index eadb0d1a,fc484c32..4b61539b --- a/lemmy_api/src/lib.rs +++ b/lemmy_api/src/lib.rs @@@ -1,16 -1,16 +1,20 @@@ use crate::claims::Claims; use actix_web::{web, web::Data}; use lemmy_db::{ - source::community::{CommunityModerator_, Community_}, + source::{ - community::{Community, CommunityModerator}, - post::Post, - site::Site, - user::User_, ++ community::{CommunityModerator_, Community_}, ++ site::Site_, + }, views::community::community_user_ban_view::CommunityUserBanView, Crud, DbPool, }; + use lemmy_db_schema::source::{ + community::{Community, CommunityModerator}, + post::Post, ++ site::Site, + user::User_, + }; 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}; diff --cc lemmy_api/src/post.rs index f021b0d7,2dd834be..20b5b8d5 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@@ -11,17 -10,13 +11,12 @@@ use crate:: use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_db::{ - naive_now, - source::{ - moderator::*, - post::*, - post_report::{PostReport, PostReportForm}, - }, + source::post::Post_, views::{ comment_view::CommentQueryBuilder, - community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView}, + community::community_moderator_view::CommunityModeratorView, post_report_view::{PostReportQueryBuilder, PostReportView}, post_view::{PostQueryBuilder, PostView}, - site_view::SiteView, }, Crud, Likeable, diff --cc lemmy_api/src/site.rs index 16c6ecec,05cac0ae..e30e6230 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@@ -10,9 -10,9 +10,8 @@@ use actix_web::web::Data use anyhow::Context; use lemmy_apub::fetcher::search_by_apub_id; use lemmy_db::{ - aggregates::site_aggregates::SiteAggregates, diesel_option_overwrite, - naive_now, - source::{category::*, moderator::*, site::*}, + source::{category::Category_, site::Site_}, views::{ comment_view::CommentQueryBuilder, community::community_view::CommunityQueryBuilder, diff --cc lemmy_api/src/user.rs index cca8dffb,98a186c3..34ef5022 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@@ -16,17 -16,14 +16,15 @@@ use chrono::Duration use lemmy_apub::ApubObjectType; use lemmy_db::{ diesel_option_overwrite, - naive_now, source::{ - comment::*, - community::*, - moderator::*, - password_reset_request::*, - post::*, - private_message::*, - site::*, - user::*, - user_mention::*, + comment::Comment_, + community::Community_, + password_reset_request::PasswordResetRequest_, + post::Post_, + private_message::PrivateMessage_, ++ site::Site_, + user::User, + user_mention::UserMention_, }, views::{ comment_report_view::CommentReportView, diff --cc lemmy_db/Cargo.toml index 5963f32c,0442d014..15dd749a --- a/lemmy_db/Cargo.toml +++ b/lemmy_db/Cargo.toml @@@ -9,8 -9,8 +9,9 @@@ path = "src/lib.rs [dependencies] lemmy_utils = { path = "../lemmy_utils" } + lemmy_db_schema = { path = "../lemmy_db_schema" } diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } +diesel_migrations = "1.4.0" chrono = { version = "0.4.19", features = ["serde"] } serde = { version = "1.0.118", features = ["derive"] } serde_json = { version = "1.0.60", features = ["preserve_order"] } diff --cc lemmy_db/src/aggregates/site_aggregates.rs index df3cad6f,52890d81..70997a66 --- a/lemmy_db/src/aggregates/site_aggregates.rs +++ b/lemmy_db/src/aggregates/site_aggregates.rs @@@ -1,8 -1,8 +1,8 @@@ - use crate::schema::site_aggregates; use diesel::{result::Error, *}; + use lemmy_db_schema::schema::site_aggregates; use serde::Serialize; -#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] +#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[table_name = "site_aggregates"] pub struct SiteAggregates { pub id: i32, diff --cc lemmy_db/src/lib.rs index 4df69ca4,9afb7619..8e352147 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@@ -4,12 -4,7 +4,11 @@@ extern crate diesel extern crate strum_macros; #[macro_use] extern crate lazy_static; +// this is used in tests +#[allow(unused_imports)] +#[macro_use] +extern crate diesel_migrations; - use chrono::NaiveDateTime; use diesel::{result::Error, *}; use regex::Regex; use serde::{Deserialize, Serialize}; diff --cc lemmy_db/src/source/post.rs index 948791ea,81f09e72..c681adbe --- a/lemmy_db/src/source/post.rs +++ b/lemmy_db/src/source/post.rs @@@ -88,29 -38,33 +38,27 @@@ impl Crud for Post } } - impl ApubObject for Post { - fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::post::dsl::*; - post.filter(ap_id.eq(object_id)).first::(conn) - } - - fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { - use crate::schema::post::dsl::*; - insert_into(post) - .values(post_form) - .on_conflict(ap_id) - .do_update() - .set(post_form) - .get_result::(conn) - } + pub trait Post_ { + //fn read(conn: &PgConnection, post_id: i32) -> Result; + fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result, Error>; + fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result; + fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result, Error>; + fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result; + fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result; + fn update_removed_for_creator( + conn: &PgConnection, + for_creator_id: i32, + for_community_id: Option, + new_removed: bool, + ) -> Result, Error>; + fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result; + fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result; + fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool; } - impl Post { - pub fn list_for_community( - conn: &PgConnection, - the_community_id: i32, - ) -> Result, Error> { - use crate::schema::post::dsl::*; + impl Post_ for Post { - // TODO: this is a duplicate? - //fn read(conn: &PgConnection, post_id: i32) -> Result { - // use lemmy_db_schema::schema::post::dsl::*; - // post.filter(id.eq(post_id)).first::(conn) - //} - + fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result, Error> { + use lemmy_db_schema::schema::post::dsl::*; post .filter(community_id.eq(the_community_id)) .then_order_by(published.desc()) diff --cc lemmy_db/src/source/site.rs index 8775191e,2e0e99df..2510f46c --- a/lemmy_db/src/source/site.rs +++ b/lemmy_db/src/source/site.rs @@@ -50,22 -19,17 +19,27 @@@ impl Crud for Site .set(new_site) .get_result::(conn) } + fn delete(conn: &PgConnection, site_id: i32) -> Result { - use crate::schema::site::dsl::*; ++ use lemmy_db_schema::schema::site::dsl::*; + diesel::delete(site.find(site_id)).execute(conn) + } } - impl Site { - pub fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result { - use crate::schema::site::dsl::*; + pub trait Site_ { + fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result; ++ fn read_simple(conn: &PgConnection) -> Result; + } + + impl Site_ for Site { + fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result { + use lemmy_db_schema::schema::site::dsl::*; diesel::update(site.find(1)) .set((creator_id.eq(new_creator_id), updated.eq(naive_now()))) .get_result::(conn) } + - pub fn read_simple(conn: &PgConnection) -> Result { - use crate::schema::site::dsl::*; ++ fn read_simple(conn: &PgConnection) -> Result { ++ use lemmy_db_schema::schema::site::dsl::*; + site.first::(conn) + } } diff --cc lemmy_db/src/views/site_view.rs index 3c605277,a56c8fe3..7772ccdc --- a/lemmy_db/src/views/site_view.rs +++ b/lemmy_db/src/views/site_view.rs @@@ -1,6 -1,7 +1,7 @@@ - use crate::{ - aggregates::site_aggregates::SiteAggregates, -use crate::ToSafe; ++use crate::{aggregates::site_aggregates::SiteAggregates, ToSafe}; + use diesel::{result::Error, *}; + use lemmy_db_schema::{ - schema::{site, user_}, + schema::{site, site_aggregates, user_}, source::{ site::Site, user::{UserSafe, User_}, diff --cc lemmy_db_schema/src/source/comment_report.rs index 00000000,d94fed57..ec53408d mode 000000,100644..100644 --- a/lemmy_db_schema/src/source/comment_report.rs +++ b/lemmy_db_schema/src/source/comment_report.rs @@@ -1,0 -1,26 +1,28 @@@ + 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 { + pub id: i32, + pub creator_id: i32, + pub comment_id: i32, + pub original_comment_text: String, + pub reason: String, + pub resolved: bool, + pub resolver_id: Option, + pub published: chrono::NaiveDateTime, + pub updated: Option, + } + + #[derive(Insertable, AsChangeset, Clone)] + #[table_name = "comment_report"] + pub struct CommentReportForm { + pub creator_id: i32, + pub comment_id: i32, + pub original_comment_text: String, + pub reason: String, + } diff --cc lemmy_db_schema/src/source/post_report.rs index 00000000,608104db..b75fb954 mode 000000,100644..100644 --- a/lemmy_db_schema/src/source/post_report.rs +++ b/lemmy_db_schema/src/source/post_report.rs @@@ -1,0 -1,30 +1,32 @@@ + 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 { + pub id: i32, + pub creator_id: i32, + pub post_id: i32, + pub original_post_name: String, + pub original_post_url: Option, + pub original_post_body: Option, + pub reason: String, + pub resolved: bool, + pub resolver_id: Option, + pub published: chrono::NaiveDateTime, + pub updated: Option, + } + + #[derive(Insertable, AsChangeset, Clone)] + #[table_name = "post_report"] + pub struct PostReportForm { + pub creator_id: i32, + pub post_id: i32, + pub original_post_name: String, + pub original_post_url: Option, + pub original_post_body: Option, + pub reason: String, + } diff --cc lemmy_structs/src/lib.rs index d1912738,d344f452..080cb385 --- a/lemmy_structs/src/lib.rs +++ b/lemmy_structs/src/lib.rs @@@ -3,17 -3,15 +3,14 @@@ pub mod community pub mod post; pub mod site; pub mod user; -pub mod websocket; use diesel::PgConnection; - use lemmy_db::{ - source::{ - comment::Comment, - post::Post, - user::User_, - user_mention::{UserMention, UserMentionForm}, - }, - Crud, - DbPool, + use lemmy_db::{source::user::User, Crud, DbPool}; + use lemmy_db_schema::source::{ + comment::Comment, + post::Post, + user::User_, + user_mention::{UserMention, UserMentionForm}, }; use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError}; use log::error; diff --cc lemmy_structs/src/site.rs index ff6c8a39,5011d84f..f24d9f49 --- a/lemmy_structs/src/site.rs +++ b/lemmy_structs/src/site.rs @@@ -1,24 -1,25 +1,22 @@@ --use lemmy_db::{ - source::{category::*, user::*}, - aggregates::site_aggregates::SiteAggregates, -- views::{ -- comment_view::CommentView, -- community::community_view::CommunityView, -- 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::PostView, -- site_view::SiteView, -- user_view::UserViewSafe, ++use lemmy_db::views::{ ++ comment_view::CommentView, ++ community::community_view::CommunityView, ++ 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::PostView, ++ site_view::SiteView, ++ user_view::UserViewSafe, }; + use lemmy_db_schema::source::{category::*, user::User_}; use serde::{Deserialize, Serialize}; #[derive(Deserialize)]