From: Felix Ableitner Date: Fri, 18 Dec 2020 17:27:25 +0000 (+0100) Subject: Move comment, post definitions into lemmy_db_schema X-Git-Url: http://these/git/%22%7Burl%7D/%7BrepoUrl%7D?a=commitdiff_plain;h=114f3cbfb5fc7b752fe52fa47143518c2a1172fb;p=lemmy.git Move comment, post definitions into lemmy_db_schema --- diff --git a/Cargo.lock b/Cargo.lock index 850a19e5..6cc79fed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1718,6 +1718,7 @@ dependencies = [ "lazy_static", "lemmy_apub", "lemmy_db", + "lemmy_db_schema", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", @@ -1762,6 +1763,7 @@ dependencies = [ "itertools", "lazy_static", "lemmy_db", + "lemmy_db_schema", "lemmy_structs", "lemmy_utils", "lemmy_websocket", @@ -1805,7 +1807,12 @@ dependencies = [ name = "lemmy_db_schema" version = "0.1.0" dependencies = [ + "chrono", "diesel", + "log", + "serde 1.0.118", + "serde_json", + "url", ] [[package]] @@ -1868,6 +1875,7 @@ dependencies = [ "chrono", "diesel", "lemmy_db", + "lemmy_db_schema", "lemmy_utils", "log", "serde 1.0.118", @@ -1909,6 +1917,7 @@ dependencies = [ "chrono", "diesel", "lemmy_db", + "lemmy_db_schema", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", diff --git a/lemmy_api/Cargo.toml b/lemmy_api/Cargo.toml index 94fd2d50..f4a10924 100644 --- a/lemmy_api/Cargo.toml +++ b/lemmy_api/Cargo.toml @@ -12,6 +12,7 @@ path = "src/lib.rs" lemmy_apub = { path = "../lemmy_apub" } lemmy_utils = { path = "../lemmy_utils" } lemmy_db = { path = "../lemmy_db" } +lemmy_db_schema = { path = "../lemmy_db_schema" } 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 689fe4b8..acff6004 100644 --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@ -11,10 +11,8 @@ use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_db::{ source::{ - comment::*, comment_report::{CommentReport, CommentReportForm}, moderator::*, - post::*, user::*, }, views::{ @@ -29,6 +27,10 @@ use lemmy_db::{ Saveable, SortType, }; +use lemmy_db_schema::source::{ + comment::{Comment, CommentForm, CommentLike, CommentLikeForm, CommentSaved, CommentSavedForm}, + post::Post, +}; 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 6e20a30b..35aafc88 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -11,8 +11,7 @@ use anyhow::Context; use lemmy_apub::ActorType; use lemmy_db::{ diesel_option_overwrite, - naive_now, - source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, + source::{community::*, moderator::*, site::*}, views::{ comment_view::CommentQueryBuilder, community::{ @@ -29,6 +28,10 @@ use lemmy_db::{ Joinable, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{comment::Comment, post::Post}, +}; 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 ad7355e1..927846c0 100644 --- a/lemmy_api/src/lib.rs +++ b/lemmy_api/src/lib.rs @@ -3,13 +3,13 @@ use actix_web::{web, web::Data}; use lemmy_db::{ source::{ community::{Community, CommunityModerator}, - post::Post, user::User_, }, views::community::community_user_ban_view::CommunityUserBanView, Crud, DbPool, }; +use lemmy_db_schema::source::post::Post; 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 --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index 22f95877..ee09059a 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -10,10 +10,8 @@ use crate::{ use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_db::{ - naive_now, source::{ moderator::*, - post::*, post_report::{PostReport, PostReportForm}, }, views::{ @@ -30,6 +28,10 @@ use lemmy_db::{ Saveable, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::post::{Post, PostForm, PostLike, PostLikeForm, PostSaved, PostSavedForm}, +}; 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 138cc875..8eda284f 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -12,7 +12,6 @@ use lemmy_apub::fetcher::search_by_apub_id; use lemmy_db::{ aggregates::site_aggregates::SiteAggregates, diesel_option_overwrite, - naive_now, source::{category::*, moderator::*, site::*}, views::{ comment_view::CommentQueryBuilder, @@ -36,6 +35,7 @@ use lemmy_db::{ SearchType, SortType, }; +use lemmy_db_schema::naive_now; 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 f31e42e5..680910b8 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -16,13 +16,10 @@ 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::*, @@ -48,6 +45,10 @@ use lemmy_db::{ ListingType, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{comment::Comment, post::Post}, +}; 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 2dd9a64a..e62ae9f0 100644 --- a/lemmy_apub/Cargo.toml +++ b/lemmy_apub/Cargo.toml @@ -11,6 +11,7 @@ path = "src/lib.rs" [dependencies] lemmy_utils = { path = "../lemmy_utils" } lemmy_db = { path = "../lemmy_db" } +lemmy_db_schema = { path = "../lemmy_db_schema" } 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 700a2653..76337c4f 100644 --- a/lemmy_apub/src/activities/receive/comment.rs +++ b/lemmy_apub/src/activities/receive/comment.rs @@ -4,13 +4,10 @@ use activitystreams::{ base::ExtendsExt, }; use anyhow::Context; -use lemmy_db::{ - source::{ - comment::{Comment, CommentLike, CommentLikeForm}, - post::Post, - }, - views::comment_view::CommentView, - Likeable, +use lemmy_db::{views::comment_view::CommentView, Likeable}; +use lemmy_db_schema::source::{ + comment::{Comment, CommentLike, CommentLikeForm}, + post::Post, }; use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs}; use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; diff --git a/lemmy_apub/src/activities/receive/comment_undo.rs b/lemmy_apub/src/activities/receive/comment_undo.rs index 85dcc143..f446b286 100644 --- a/lemmy_apub/src/activities/receive/comment_undo.rs +++ b/lemmy_apub/src/activities/receive/comment_undo.rs @@ -1,10 +1,7 @@ use crate::activities::receive::get_actor_as_user; use activitystreams::activity::{Dislike, Like}; -use lemmy_db::{ - source::comment::{Comment, CommentLike}, - views::comment_view::CommentView, - Likeable, -}; +use lemmy_db::{views::comment_view::CommentView, Likeable}; +use lemmy_db_schema::source::comment::{Comment, CommentLike}; 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/post.rs b/lemmy_apub/src/activities/receive/post.rs index f0907112..4a3cc13f 100644 --- a/lemmy_apub/src/activities/receive/post.rs +++ b/lemmy_apub/src/activities/receive/post.rs @@ -4,11 +4,8 @@ use activitystreams::{ prelude::*, }; use anyhow::Context; -use lemmy_db::{ - source::post::{Post, PostLike, PostLikeForm}, - views::post_view::PostView, - Likeable, -}; +use lemmy_db::{views::post_view::PostView, Likeable}; +use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm}; 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 6827ded0..817a74e4 100644 --- a/lemmy_apub/src/activities/receive/post_undo.rs +++ b/lemmy_apub/src/activities/receive/post_undo.rs @@ -1,10 +1,7 @@ use crate::activities::receive::get_actor_as_user; use activitystreams::activity::{Dislike, Like}; -use lemmy_db::{ - source::post::{Post, PostLike}, - views::post_view::PostView, - Likeable, -}; +use lemmy_db::{views::post_view::PostView, Likeable}; +use lemmy_db_schema::source::post::{Post, PostLike}; use lemmy_structs::{blocking, post::PostResponse}; use lemmy_utils::LemmyError; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/send/comment.rs b/lemmy_apub/src/activities/send/comment.rs index 6b417c25..358e5020 100644 --- a/lemmy_apub/src/activities/send/comment.rs +++ b/lemmy_apub/src/activities/send/comment.rs @@ -27,10 +27,11 @@ use activitystreams::{ use anyhow::anyhow; use itertools::Itertools; use lemmy_db::{ - source::{comment::Comment, community::Community, post::Post, user::User_}, + source::{community::Community, user::User_}, Crud, DbPool, }; +use lemmy_db_schema::source::{comment::Comment, post::Post}; use lemmy_structs::{blocking, WebFingerResponse}; use lemmy_utils::{ request::{retry, RecvError}, diff --git a/lemmy_apub/src/activities/send/post.rs b/lemmy_apub/src/activities/send/post.rs index ec1ca67d..fd7f7c12 100644 --- a/lemmy_apub/src/activities/send/post.rs +++ b/lemmy_apub/src/activities/send/post.rs @@ -22,9 +22,10 @@ use activitystreams::{ public, }; use lemmy_db::{ - source::{community::Community, post::Post, user::User_}, + source::{community::Community, user::User_}, Crud, }; +use lemmy_db_schema::source::post::Post; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/fetcher.rs b/lemmy_apub/src/fetcher.rs index 61cdbd47..d06b221e 100644 --- a/lemmy_apub/src/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -13,11 +13,8 @@ use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; use diesel::result::Error::NotFound; use lemmy_db::{ - naive_now, source::{ - comment::Comment, community::{Community, CommunityModerator, CommunityModeratorForm}, - post::Post, user::User_, }, views::{ @@ -30,6 +27,10 @@ use lemmy_db::{ Joinable, SearchType, }; +use lemmy_db_schema::{ + naive_now, + source::{comment::Comment, post::Post}, +}; use lemmy_structs::{blocking, site::SearchResponse}; use lemmy_utils::{ location_info, diff --git a/lemmy_apub/src/http/comment.rs b/lemmy_apub/src/http/comment.rs index 16f41190..f71d542b 100644 --- a/lemmy_apub/src/http/comment.rs +++ b/lemmy_apub/src/http/comment.rs @@ -4,7 +4,8 @@ use crate::{ }; use actix_web::{body::Body, web, web::Path, HttpResponse}; use diesel::result::Error::NotFound; -use lemmy_db::{source::comment::Comment, Crud}; +use lemmy_db::Crud; +use lemmy_db_schema::source::comment::Comment; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/http/community.rs b/lemmy_apub/src/http/community.rs index a17e2abf..a1a7870f 100644 --- a/lemmy_apub/src/http/community.rs +++ b/lemmy_apub/src/http/community.rs @@ -10,9 +10,10 @@ use activitystreams::{ }; use actix_web::{body::Body, web, HttpResponse}; use lemmy_db::{ - source::{community::Community, post::Post}, + source::community::Community, views::community::community_follower_view::CommunityFollowerView, }; +use lemmy_db_schema::source::post::Post; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/http/post.rs b/lemmy_apub/src/http/post.rs index 563af845..ad846407 100644 --- a/lemmy_apub/src/http/post.rs +++ b/lemmy_apub/src/http/post.rs @@ -4,7 +4,7 @@ use crate::{ }; use actix_web::{body::Body, web, HttpResponse}; use diesel::result::Error::NotFound; -use lemmy_db::source::post::Post; +use lemmy_db_schema::source::post::Post; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/receive_for_community.rs b/lemmy_apub/src/inbox/receive_for_community.rs index 73deb971..995a1399 100644 --- a/lemmy_apub/src/inbox/receive_for_community.rs +++ b/lemmy_apub/src/inbox/receive_for_community.rs @@ -41,11 +41,8 @@ use activitystreams::{ }; use anyhow::Context; use diesel::result::Error::NotFound; -use lemmy_db::{ - source::{comment::Comment, post::Post, site::Site}, - ApubObject, - Crud, -}; +use lemmy_db::{source::site::Site, ApubObject, Crud}; +use lemmy_db_schema::source::{comment::Comment, post::Post}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/objects/comment.rs b/lemmy_apub/src/objects/comment.rs index 957966d7..9dd035c5 100644 --- a/lemmy_apub/src/objects/comment.rs +++ b/lemmy_apub/src/objects/comment.rs @@ -24,15 +24,14 @@ use activitystreams::{ }; use anyhow::{anyhow, Context}; use lemmy_db::{ - source::{ - comment::{Comment, CommentForm}, - community::Community, - post::Post, - user::User_, - }, + source::{community::Community, user::User_}, Crud, DbPool, }; +use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + post::Post, +}; use lemmy_structs::blocking; use lemmy_utils::{ location_info, diff --git a/lemmy_apub/src/objects/community.rs b/lemmy_apub/src/objects/community.rs index 9d8210a6..f5fa2c31 100644 --- a/lemmy_apub/src/objects/community.rs +++ b/lemmy_apub/src/objects/community.rs @@ -23,11 +23,11 @@ use activitystreams::{ use activitystreams_ext::Ext2; use anyhow::Context; use lemmy_db::{ - naive_now, source::community::{Community, CommunityForm}, views::community::community_moderator_view::CommunityModeratorView, DbPool, }; +use lemmy_db_schema::naive_now; use lemmy_structs::blocking; use lemmy_utils::{ location_info, diff --git a/lemmy_apub/src/objects/post.rs b/lemmy_apub/src/objects/post.rs index 9c9df5b1..7090cd16 100644 --- a/lemmy_apub/src/objects/post.rs +++ b/lemmy_apub/src/objects/post.rs @@ -21,14 +21,11 @@ use activitystreams::{ use activitystreams_ext::Ext1; use anyhow::Context; use lemmy_db::{ - source::{ - community::Community, - post::{Post, PostForm}, - user::User_, - }, + source::{community::Community, user::User_}, Crud, DbPool, }; +use lemmy_db_schema::source::post::{Post, PostForm}; use lemmy_structs::blocking; use lemmy_utils::{ location_info, diff --git a/lemmy_apub/src/objects/user.rs b/lemmy_apub/src/objects/user.rs index 8c3312d1..6862867a 100644 --- a/lemmy_apub/src/objects/user.rs +++ b/lemmy_apub/src/objects/user.rs @@ -19,11 +19,11 @@ use activitystreams::{ use activitystreams_ext::Ext1; use anyhow::Context; use lemmy_db::{ - naive_now, source::user::{UserForm, User_}, ApubObject, DbPool, }; +use lemmy_db_schema::naive_now; use lemmy_structs::blocking; use lemmy_utils::{ location_info, diff --git a/lemmy_db/src/aggregates/community_aggregates.rs b/lemmy_db/src/aggregates/community_aggregates.rs index 47c40f7b..cb94e632 100644 --- a/lemmy_db/src/aggregates/community_aggregates.rs +++ b/lemmy_db/src/aggregates/community_aggregates.rs @@ -25,9 +25,7 @@ mod tests { use crate::{ aggregates::community_aggregates::CommunityAggregates, source::{ - comment::{Comment, CommentForm}, community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm}, - post::{Post, PostForm}, user::{UserForm, User_}, }, tests::establish_unpooled_connection, @@ -36,6 +34,10 @@ mod tests { ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + post::{Post, PostForm}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/post_aggregates.rs b/lemmy_db/src/aggregates/post_aggregates.rs index 6cb3a740..0b9bfa5f 100644 --- a/lemmy_db/src/aggregates/post_aggregates.rs +++ b/lemmy_db/src/aggregates/post_aggregates.rs @@ -27,9 +27,7 @@ mod tests { use crate::{ aggregates::post_aggregates::PostAggregates, source::{ - comment::{Comment, CommentForm}, community::{Community, CommunityForm}, - post::{Post, PostForm, PostLike, PostLikeForm}, user::{UserForm, User_}, }, tests::establish_unpooled_connection, @@ -38,6 +36,10 @@ mod tests { ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + post::{Post, PostForm, PostLike, PostLikeForm}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/site_aggregates.rs b/lemmy_db/src/aggregates/site_aggregates.rs index a3bd199c..ea58b216 100644 --- a/lemmy_db/src/aggregates/site_aggregates.rs +++ b/lemmy_db/src/aggregates/site_aggregates.rs @@ -23,9 +23,7 @@ mod tests { use crate::{ aggregates::site_aggregates::SiteAggregates, source::{ - comment::{Comment, CommentForm}, community::{Community, CommunityForm}, - post::{Post, PostForm}, user::{UserForm, User_}, }, tests::establish_unpooled_connection, @@ -33,6 +31,10 @@ mod tests { ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + post::{Post, PostForm}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/user_aggregates.rs b/lemmy_db/src/aggregates/user_aggregates.rs index f6eab467..91fa7460 100644 --- a/lemmy_db/src/aggregates/user_aggregates.rs +++ b/lemmy_db/src/aggregates/user_aggregates.rs @@ -26,9 +26,7 @@ mod tests { use crate::{ aggregates::user_aggregates::UserAggregates, source::{ - comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, community::{Community, CommunityForm}, - post::{Post, PostForm, PostLike, PostLikeForm}, user::{UserForm, User_}, }, tests::establish_unpooled_connection, @@ -37,6 +35,10 @@ mod tests { ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, + post::{Post, PostForm, PostLike, PostLikeForm}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index 52180fb7..9afb7619 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -5,7 +5,6 @@ extern crate strum_macros; #[macro_use] extern crate lazy_static; -use chrono::NaiveDateTime; use diesel::{result::Error, *}; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -181,10 +180,6 @@ pub fn limit_and_offset(page: Option, limit: Option) -> (i64, i64) { (limit, offset) } -pub fn naive_now() -> NaiveDateTime { - chrono::prelude::Utc::now().naive_utc() -} - pub fn is_email_regex(test: &str) -> bool { EMAIL_REGEX.is_match(test) } diff --git a/lemmy_db/src/source/comment.rs b/lemmy_db/src/source/comment.rs index 76cd9133..17e9d637 100644 --- a/lemmy_db/src/source/comment.rs +++ b/lemmy_db/src/source/comment.rs @@ -1,74 +1,13 @@ -use super::post::Post; -use crate::{naive_now, ApubObject, Crud, Likeable, Saveable}; +use crate::{ApubObject, Crud, Likeable, Saveable}; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::{comment, comment_alias_1, comment_like, comment_saved}; -use serde::Serialize; -use url::{ParseError, Url}; - -// WITH RECURSIVE MyTree AS ( -// SELECT * FROM comment WHERE parent_id IS NULL -// UNION ALL -// SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id -// ) -// SELECT * FROM MyTree; - -#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] -#[belongs_to(Post)] -#[table_name = "comment"] -pub struct Comment { - pub id: i32, - pub creator_id: i32, - pub post_id: i32, - pub parent_id: Option, - pub content: String, - pub removed: bool, - pub read: bool, // Whether the recipient has read the comment or not - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub ap_id: String, - pub local: bool, -} - -#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] -#[belongs_to(Post)] -#[table_name = "comment_alias_1"] -pub struct CommentAlias1 { - pub id: i32, - pub creator_id: i32, - pub post_id: i32, - pub parent_id: Option, - pub content: String, - pub removed: bool, - pub read: bool, // Whether the recipient has read the comment or not - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub ap_id: String, - pub local: bool, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "comment"] -pub struct CommentForm { - pub creator_id: i32, - pub post_id: i32, - pub parent_id: Option, - pub content: String, - pub removed: Option, - pub read: Option, - pub published: Option, - pub updated: Option, - pub deleted: Option, - pub ap_id: Option, - pub local: bool, -} - -impl CommentForm { - pub fn get_ap_id(&self) -> Result { - Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) - } -} +use lemmy_db_schema::source::comment::{ + Comment, + CommentForm, + CommentLike, + CommentLikeForm, + CommentSaved, + CommentSavedForm, +}; impl Crud for Comment { fn read(conn: &PgConnection, comment_id: i32) -> Result { @@ -117,106 +56,6 @@ impl ApubObject for Comment { } } -impl Comment { - pub fn update_ap_id( - conn: &PgConnection, - comment_id: i32, - apub_id: String, - ) -> Result { - use lemmy_db_schema::schema::comment::dsl::*; - - diesel::update(comment.find(comment_id)) - .set(ap_id.eq(apub_id)) - .get_result::(conn) - } - - pub fn permadelete_for_creator( - conn: &PgConnection, - for_creator_id: i32, - ) -> Result, Error> { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.filter(creator_id.eq(for_creator_id))) - .set(( - content.eq("*Permananently Deleted*"), - deleted.eq(true), - updated.eq(naive_now()), - )) - .get_results::(conn) - } - - pub fn update_deleted( - conn: &PgConnection, - comment_id: i32, - new_deleted: bool, - ) -> Result { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.find(comment_id)) - .set((deleted.eq(new_deleted), updated.eq(naive_now()))) - .get_result::(conn) - } - - pub fn update_removed( - conn: &PgConnection, - comment_id: i32, - new_removed: bool, - ) -> Result { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.find(comment_id)) - .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_result::(conn) - } - - pub fn update_removed_for_creator( - conn: &PgConnection, - for_creator_id: i32, - new_removed: bool, - ) -> Result, Error> { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.filter(creator_id.eq(for_creator_id))) - .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_results::(conn) - } - - pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.find(comment_id)) - .set(read.eq(new_read)) - .get_result::(conn) - } - - pub fn update_content( - conn: &PgConnection, - comment_id: i32, - new_content: &str, - ) -> Result { - use lemmy_db_schema::schema::comment::dsl::*; - diesel::update(comment.find(comment_id)) - .set((content.eq(new_content), updated.eq(naive_now()))) - .get_result::(conn) - } -} - -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] -#[belongs_to(Comment)] -#[table_name = "comment_like"] -pub struct CommentLike { - pub id: i32, - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, // TODO this is redundant - pub score: i16, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "comment_like"] -pub struct CommentLikeForm { - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, // TODO this is redundant - pub score: i16, -} - impl Likeable for CommentLike { fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result { use lemmy_db_schema::schema::comment_like::dsl::*; @@ -238,23 +77,6 @@ impl Likeable for CommentLike { } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Comment)] -#[table_name = "comment_saved"] -pub struct CommentSaved { - pub id: i32, - pub comment_id: i32, - pub user_id: i32, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "comment_saved"] -pub struct CommentSavedForm { - pub comment_id: i32, - pub user_id: i32, -} - impl Saveable for CommentSaved { fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result { use lemmy_db_schema::schema::comment_saved::dsl::*; @@ -279,12 +101,15 @@ impl Saveable for CommentSaved { #[cfg(test)] mod tests { use crate::{ - source::{comment::*, community::*, post::*, user::*}, + source::{community::*, user::*}, tests::establish_unpooled_connection, Crud, + Likeable, ListingType, + Saveable, SortType, }; + use lemmy_db_schema::source::{comment::*, post::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/comment_report.rs b/lemmy_db/src/source/comment_report.rs index 2937e631..d05d9e03 100644 --- a/lemmy_db/src/source/comment_report.rs +++ b/lemmy_db/src/source/comment_report.rs @@ -1,6 +1,6 @@ -use crate::{naive_now, source::comment::Comment, Reportable}; +use crate::Reportable; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::comment_report; +use lemmy_db_schema::{naive_now, schema::comment_report, source::comment::Comment}; use serde::{Deserialize, Serialize}; #[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)] diff --git a/lemmy_db/src/source/community.rs b/lemmy_db/src/source/community.rs index 05bc3c5c..13045cca 100644 --- a/lemmy_db/src/source/community.rs +++ b/lemmy_db/src/source/community.rs @@ -1,5 +1,4 @@ use crate::{ - naive_now, views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}, ApubObject, Bannable, @@ -8,11 +7,9 @@ use crate::{ Joinable, }; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::{ - community, - community_follower, - community_moderator, - community_user_ban, +use lemmy_db_schema::{ + naive_now, + schema::{community, community_follower, community_moderator, community_user_ban}, }; use serde::Serialize; diff --git a/lemmy_db/src/source/moderator.rs b/lemmy_db/src/source/moderator.rs index 0766f49c..eb6c2d56 100644 --- a/lemmy_db/src/source/moderator.rs +++ b/lemmy_db/src/source/moderator.rs @@ -391,11 +391,12 @@ impl Crud for ModAdd { #[cfg(test)] mod tests { use crate::{ - source::{comment::*, community::*, moderator::*, post::*, user::*}, + source::{community::*, moderator::*, user::*}, tests::establish_unpooled_connection, ListingType, SortType, }; + use lemmy_db_schema::source::{comment::*, post::*}; // use Crud; #[test] diff --git a/lemmy_db/src/source/post.rs b/lemmy_db/src/source/post.rs index 181157a2..5e8dc1cc 100644 --- a/lemmy_db/src/source/post.rs +++ b/lemmy_db/src/source/post.rs @@ -1,61 +1,15 @@ -use crate::{naive_now, ApubObject, Crud, Likeable, Readable, Saveable}; +use crate::{ApubObject, Crud, Likeable, Readable, Saveable}; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::{post, post_like, post_read, post_saved}; -use serde::Serialize; -use url::{ParseError, Url}; - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "post"] -pub struct Post { - pub id: i32, - pub name: String, - pub url: Option, - pub body: Option, - pub creator_id: i32, - pub community_id: i32, - pub removed: bool, - pub locked: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub nsfw: bool, - pub stickied: bool, - pub embed_title: Option, - pub embed_description: Option, - pub embed_html: Option, - pub thumbnail_url: Option, - pub ap_id: String, - pub local: bool, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "post"] -pub struct PostForm { - pub name: String, - pub url: Option, - pub body: Option, - pub creator_id: i32, - pub community_id: i32, - pub removed: Option, - pub locked: Option, - pub published: Option, - pub updated: Option, - pub deleted: Option, - pub nsfw: bool, - pub stickied: Option, - pub embed_title: Option, - pub embed_description: Option, - pub embed_html: Option, - pub thumbnail_url: Option, - pub ap_id: Option, - pub local: bool, -} - -impl PostForm { - pub fn get_ap_id(&self) -> Result { - Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) - } -} +use lemmy_db_schema::source::post::{ + Post, + PostForm, + PostLike, + PostLikeForm, + PostRead, + PostReadForm, + PostSaved, + PostSavedForm, +}; impl Crud for Post { fn read(conn: &PgConnection, post_id: i32) -> Result { @@ -98,137 +52,6 @@ impl ApubObject for Post { } } -impl Post { - pub fn read(conn: &PgConnection, post_id: i32) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - post.filter(id.eq(post_id)).first::(conn) - } - - pub 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()) - .then_order_by(stickied.desc()) - .limit(20) - .load::(conn) - } - - pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - - diesel::update(post.find(post_id)) - .set(ap_id.eq(apub_id)) - .get_result::(conn) - } - - pub fn permadelete_for_creator( - conn: &PgConnection, - for_creator_id: i32, - ) -> Result, Error> { - use lemmy_db_schema::schema::post::dsl::*; - - let perma_deleted = "*Permananently Deleted*"; - let perma_deleted_url = "https://deleted.com"; - - diesel::update(post.filter(creator_id.eq(for_creator_id))) - .set(( - name.eq(perma_deleted), - url.eq(perma_deleted_url), - body.eq(perma_deleted), - deleted.eq(true), - updated.eq(naive_now()), - )) - .get_results::(conn) - } - - pub fn update_deleted( - conn: &PgConnection, - post_id: i32, - new_deleted: bool, - ) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - diesel::update(post.find(post_id)) - .set((deleted.eq(new_deleted), updated.eq(naive_now()))) - .get_result::(conn) - } - - pub fn update_removed( - conn: &PgConnection, - post_id: i32, - new_removed: bool, - ) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - diesel::update(post.find(post_id)) - .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_result::(conn) - } - - pub fn update_removed_for_creator( - conn: &PgConnection, - for_creator_id: i32, - for_community_id: Option, - new_removed: bool, - ) -> Result, Error> { - use lemmy_db_schema::schema::post::dsl::*; - - let mut update = diesel::update(post).into_boxed(); - update = update.filter(creator_id.eq(for_creator_id)); - - if let Some(for_community_id) = for_community_id { - update = update.filter(community_id.eq(for_community_id)); - } - - update - .set((removed.eq(new_removed), updated.eq(naive_now()))) - .get_results::(conn) - } - - pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - diesel::update(post.find(post_id)) - .set(locked.eq(new_locked)) - .get_result::(conn) - } - - pub fn update_stickied( - conn: &PgConnection, - post_id: i32, - new_stickied: bool, - ) -> Result { - use lemmy_db_schema::schema::post::dsl::*; - diesel::update(post.find(post_id)) - .set(stickied.eq(new_stickied)) - .get_result::(conn) - } - - pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool { - user_id == post_creator_id - } -} - -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Post)] -#[table_name = "post_like"] -pub struct PostLike { - pub id: i32, - pub post_id: i32, - pub user_id: i32, - pub score: i16, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "post_like"] -pub struct PostLikeForm { - pub post_id: i32, - pub user_id: i32, - pub score: i16, -} - impl Likeable for PostLike { fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result { use lemmy_db_schema::schema::post_like::dsl::*; @@ -250,23 +73,6 @@ impl Likeable for PostLike { } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Post)] -#[table_name = "post_saved"] -pub struct PostSaved { - pub id: i32, - pub post_id: i32, - pub user_id: i32, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "post_saved"] -pub struct PostSavedForm { - pub post_id: i32, - pub user_id: i32, -} - impl Saveable for PostSaved { fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result { use lemmy_db_schema::schema::post_saved::dsl::*; @@ -288,27 +94,6 @@ impl Saveable for PostSaved { } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Post)] -#[table_name = "post_read"] -pub struct PostRead { - pub id: i32, - - pub post_id: i32, - - pub user_id: i32, - - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "post_read"] -pub struct PostReadForm { - pub post_id: i32, - - pub user_id: i32, -} - impl Readable for PostRead { fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { use lemmy_db_schema::schema::post_read::dsl::*; diff --git a/lemmy_db/src/source/post_report.rs b/lemmy_db/src/source/post_report.rs index af45aa3d..e72e32a8 100644 --- a/lemmy_db/src/source/post_report.rs +++ b/lemmy_db/src/source/post_report.rs @@ -1,6 +1,6 @@ -use crate::{naive_now, source::post::Post, Reportable}; +use crate::Reportable; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::post_report; +use lemmy_db_schema::{naive_now, schema::post_report, source::post::Post}; use serde::{Deserialize, Serialize}; #[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)] diff --git a/lemmy_db/src/source/private_message.rs b/lemmy_db/src/source/private_message.rs index e2b55c87..fd73a864 100644 --- a/lemmy_db/src/source/private_message.rs +++ b/lemmy_db/src/source/private_message.rs @@ -1,6 +1,6 @@ -use crate::{naive_now, ApubObject, Crud}; +use crate::{ApubObject, Crud}; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::private_message; +use lemmy_db_schema::{naive_now, schema::private_message}; use serde::Serialize; #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] diff --git a/lemmy_db/src/source/site.rs b/lemmy_db/src/source/site.rs index ad6f9ea6..a7db2cee 100644 --- a/lemmy_db/src/source/site.rs +++ b/lemmy_db/src/source/site.rs @@ -1,6 +1,6 @@ -use crate::{naive_now, Crud}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::site; +use lemmy_db_schema::{naive_now, schema::site}; use serde::Serialize; #[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)] diff --git a/lemmy_db/src/source/user.rs b/lemmy_db/src/source/user.rs index 809a579a..6bca769e 100644 --- a/lemmy_db/src/source/user.rs +++ b/lemmy_db/src/source/user.rs @@ -1,7 +1,10 @@ -use crate::{is_email_regex, naive_now, ApubObject, Crud}; +use crate::{is_email_regex, ApubObject, Crud}; use bcrypt::{hash, DEFAULT_COST}; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::{user_, user_::dsl::*, user_alias_1, user_alias_2}; +use lemmy_db_schema::{ + naive_now, + schema::{user_, user_::dsl::*, user_alias_1, user_alias_2}, +}; use lemmy_utils::settings::Settings; use serde::Serialize; diff --git a/lemmy_db/src/source/user_mention.rs b/lemmy_db/src/source/user_mention.rs index a61d08d2..5df17286 100644 --- a/lemmy_db/src/source/user_mention.rs +++ b/lemmy_db/src/source/user_mention.rs @@ -1,7 +1,6 @@ -use super::comment::Comment; use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use lemmy_db_schema::schema::user_mention; +use lemmy_db_schema::{schema::user_mention, source::comment::Comment}; use serde::Serialize; #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] @@ -80,11 +79,12 @@ impl UserMention { #[cfg(test)] mod tests { use crate::{ - source::{comment::*, community::*, post::*, user::*, user_mention::*}, + source::{community::*, user::*, user_mention::*}, tests::establish_unpooled_connection, ListingType, SortType, }; + use lemmy_db_schema::source::{comment::*, post::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/views/comment_report_view.rs b/lemmy_db/src/views/comment_report_view.rs index 0f55b432..b067a9ec 100644 --- a/lemmy_db/src/views/comment_report_view.rs +++ b/lemmy_db/src/views/comment_report_view.rs @@ -1,10 +1,8 @@ use crate::{ limit_and_offset, source::{ - comment::Comment, comment_report::CommentReport, community::{Community, CommunitySafe}, - post::Post, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, }, views::ViewToVec, @@ -12,14 +10,9 @@ use crate::{ ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{ - comment, - comment_report, - community, - post, - user_, - user_alias_1, - user_alias_2, +use lemmy_db_schema::{ + schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2}, + source::{comment::Comment, post::Post}, }; use serde::Serialize; diff --git a/lemmy_db/src/views/comment_view.rs b/lemmy_db/src/views/comment_view.rs index 7f78c20f..d4680a34 100644 --- a/lemmy_db/src/views/comment_view.rs +++ b/lemmy_db/src/views/comment_view.rs @@ -4,9 +4,7 @@ use crate::{ fuzzy_search, limit_and_offset, source::{ - comment::{Comment, CommentAlias1, CommentSaved}, community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan}, - post::Post, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, views::ViewToVec, @@ -16,18 +14,24 @@ use crate::{ ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{ - comment, - comment_aggregates, - comment_alias_1, - comment_like, - comment_saved, - community, - community_follower, - community_user_ban, - post, - user_, - user_alias_1, +use lemmy_db_schema::{ + schema::{ + comment, + comment_aggregates, + comment_alias_1, + comment_like, + comment_saved, + community, + community_follower, + community_user_ban, + post, + user_, + user_alias_1, + }, + source::{ + comment::{Comment, CommentAlias1, CommentSaved}, + post::Post, + }, }; use serde::Serialize; @@ -408,13 +412,14 @@ impl ViewToVec for CommentView { #[cfg(test)] mod tests { use crate::{ - source::{comment::*, community::*, post::*, user::*}, + source::{community::*, user::*}, tests::establish_unpooled_connection, views::comment_view::*, Crud, Likeable, *, }; + use lemmy_db_schema::source::{comment::*, post::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/views/moderator/mod_lock_post_view.rs b/lemmy_db/src/views/moderator/mod_lock_post_view.rs index 2d71a881..685d83bb 100644 --- a/lemmy_db/src/views/moderator/mod_lock_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_lock_post_view.rs @@ -3,14 +3,16 @@ use crate::{ source::{ community::{Community, CommunitySafe}, moderator::ModLockPost, - post::Post, user::{UserSafe, User_}, }, views::ViewToVec, ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{community, mod_lock_post, post, user_}; +use lemmy_db_schema::{ + schema::{community, mod_lock_post, post, user_}, + source::post::Post, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs index 4fa82eed..0c6519de 100644 --- a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs @@ -1,17 +1,18 @@ use crate::{ limit_and_offset, source::{ - comment::Comment, community::{Community, CommunitySafe}, moderator::ModRemoveComment, - post::Post, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, views::ViewToVec, ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}; +use lemmy_db_schema::{ + schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}, + source::{comment::Comment, post::Post}, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_remove_post_view.rs b/lemmy_db/src/views/moderator/mod_remove_post_view.rs index d21e985c..98aefd21 100644 --- a/lemmy_db/src/views/moderator/mod_remove_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_post_view.rs @@ -3,14 +3,16 @@ use crate::{ source::{ community::{Community, CommunitySafe}, moderator::ModRemovePost, - post::Post, user::{UserSafe, User_}, }, views::ViewToVec, ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{community, mod_remove_post, post, user_}; +use lemmy_db_schema::{ + schema::{community, mod_remove_post, post, user_}, + source::post::Post, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs index ec277159..40672f8b 100644 --- a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs @@ -3,14 +3,16 @@ use crate::{ source::{ community::{Community, CommunitySafe}, moderator::ModStickyPost, - post::Post, user::{UserSafe, User_}, }, views::ViewToVec, ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{community, mod_sticky_post, post, user_}; +use lemmy_db_schema::{ + schema::{community, mod_sticky_post, post, user_}, + source::post::Post, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/post_report_view.rs b/lemmy_db/src/views/post_report_view.rs index 75bfac95..5e186239 100644 --- a/lemmy_db/src/views/post_report_view.rs +++ b/lemmy_db/src/views/post_report_view.rs @@ -2,7 +2,6 @@ use crate::{ limit_and_offset, source::{ community::{Community, CommunitySafe}, - post::Post, post_report::PostReport, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, }, @@ -11,7 +10,10 @@ use crate::{ ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{community, post, post_report, user_, user_alias_1, user_alias_2}; +use lemmy_db_schema::{ + schema::{community, post, post_report, user_, user_alias_1, user_alias_2}, + source::post::Post, +}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] diff --git a/lemmy_db/src/views/post_view.rs b/lemmy_db/src/views/post_view.rs index 25d86aa1..fb3fbbf3 100644 --- a/lemmy_db/src/views/post_view.rs +++ b/lemmy_db/src/views/post_view.rs @@ -5,7 +5,6 @@ use crate::{ limit_and_offset, source::{ community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan}, - post::{Post, PostRead, PostSaved}, user::{UserSafe, User_}, }, views::ViewToVec, @@ -15,16 +14,19 @@ use crate::{ ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{ - community, - community_follower, - community_user_ban, - post, - post_aggregates, - post_like, - post_read, - post_saved, - user_, +use lemmy_db_schema::{ + schema::{ + community, + community_follower, + community_user_ban, + post, + post_aggregates, + post_like, + post_read, + post_saved, + user_, + }, + source::post::{Post, PostRead, PostSaved}, }; use serde::Serialize; @@ -406,13 +408,14 @@ impl ViewToVec for PostView { mod tests { use crate::{ aggregates::post_aggregates::PostAggregates, - source::{community::*, post::*, user::*}, + source::{community::*, user::*}, tests::establish_unpooled_connection, views::post_view::{PostQueryBuilder, PostView}, Crud, Likeable, *, }; + use lemmy_db_schema::source::post::*; #[test] fn test_crud() { diff --git a/lemmy_db/src/views/user_mention_view.rs b/lemmy_db/src/views/user_mention_view.rs index bb5fa009..61fb5626 100644 --- a/lemmy_db/src/views/user_mention_view.rs +++ b/lemmy_db/src/views/user_mention_view.rs @@ -3,9 +3,7 @@ use crate::{ functions::hot_rank, limit_and_offset, source::{ - comment::{Comment, CommentSaved}, community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan}, - post::Post, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user_mention::UserMention, }, @@ -15,18 +13,24 @@ use crate::{ ToSafe, }; use diesel::{result::Error, *}; -use lemmy_db_schema::schema::{ - comment, - comment_aggregates, - comment_like, - comment_saved, - community, - community_follower, - community_user_ban, - post, - user_, - user_alias_1, - user_mention, +use lemmy_db_schema::{ + schema::{ + comment, + comment_aggregates, + comment_like, + comment_saved, + community, + community_follower, + community_user_ban, + post, + user_, + user_alias_1, + user_mention, + }, + source::{ + comment::{Comment, CommentSaved}, + post::Post, + }, }; use serde::Serialize; diff --git a/lemmy_db_schema/Cargo.toml b/lemmy_db_schema/Cargo.toml index 3ef97550..99b7399b 100644 --- a/lemmy_db_schema/Cargo.toml +++ b/lemmy_db_schema/Cargo.toml @@ -5,3 +5,8 @@ edition = "2018" [dependencies] diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } +chrono = { version = "0.4.19", features = ["serde"] } +serde = { version = "1.0.118", features = ["derive"] } +serde_json = { version = "1.0.60", features = ["preserve_order"] } +log = "0.4.11" +url = { version = "2.2.0", features = ["serde"] } diff --git a/lemmy_db_schema/src/lib.rs b/lemmy_db_schema/src/lib.rs index b37d9461..11451d17 100644 --- a/lemmy_db_schema/src/lib.rs +++ b/lemmy_db_schema/src/lib.rs @@ -1,4 +1,11 @@ #[macro_use] extern crate diesel; +use chrono::NaiveDateTime; + pub mod schema; +pub mod source; + +pub fn naive_now() -> NaiveDateTime { + chrono::prelude::Utc::now().naive_utc() +} diff --git a/lemmy_db_schema/src/source/comment.rs b/lemmy_db_schema/src/source/comment.rs new file mode 100644 index 00000000..34577640 --- /dev/null +++ b/lemmy_db_schema/src/source/comment.rs @@ -0,0 +1,190 @@ +use crate::{ + naive_now, + schema::{comment, comment_alias_1, comment_like, comment_saved}, + source::post::Post, +}; +use diesel::{result::Error, PgConnection, *}; +use serde::Serialize; +use url::{ParseError, Url}; + +// WITH RECURSIVE MyTree AS ( +// SELECT * FROM comment WHERE parent_id IS NULL +// UNION ALL +// SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id +// ) +// SELECT * FROM MyTree; + +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] +#[belongs_to(Post)] +#[table_name = "comment"] +pub struct Comment { + pub id: i32, + pub creator_id: i32, + pub post_id: i32, + pub parent_id: Option, + pub content: String, + pub removed: bool, + pub read: bool, // Whether the recipient has read the comment or not + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub deleted: bool, + pub ap_id: String, + pub local: bool, +} + +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] +#[belongs_to(Post)] +#[table_name = "comment_alias_1"] +pub struct CommentAlias1 { + pub id: i32, + pub creator_id: i32, + pub post_id: i32, + pub parent_id: Option, + pub content: String, + pub removed: bool, + pub read: bool, // Whether the recipient has read the comment or not + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub deleted: bool, + pub ap_id: String, + pub local: bool, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "comment"] +pub struct CommentForm { + pub creator_id: i32, + pub post_id: i32, + pub parent_id: Option, + pub content: String, + pub removed: Option, + pub read: Option, + pub published: Option, + pub updated: Option, + pub deleted: Option, + pub ap_id: Option, + pub local: bool, +} + +impl Comment { + pub fn update_ap_id( + conn: &PgConnection, + comment_id: i32, + apub_id: String, + ) -> Result { + use crate::schema::comment::dsl::*; + + diesel::update(comment.find(comment_id)) + .set(ap_id.eq(apub_id)) + .get_result::(conn) + } + + pub fn permadelete_for_creator( + conn: &PgConnection, + for_creator_id: i32, + ) -> Result, Error> { + use crate::schema::comment::dsl::*; + diesel::update(comment.filter(creator_id.eq(for_creator_id))) + .set(( + content.eq("*Permananently Deleted*"), + deleted.eq(true), + updated.eq(naive_now()), + )) + .get_results::(conn) + } + + pub fn update_deleted( + conn: &PgConnection, + comment_id: i32, + new_deleted: bool, + ) -> Result { + use crate::schema::comment::dsl::*; + diesel::update(comment.find(comment_id)) + .set((deleted.eq(new_deleted), updated.eq(naive_now()))) + .get_result::(conn) + } + + pub fn update_removed( + conn: &PgConnection, + comment_id: i32, + new_removed: bool, + ) -> Result { + use crate::schema::comment::dsl::*; + diesel::update(comment.find(comment_id)) + .set((removed.eq(new_removed), updated.eq(naive_now()))) + .get_result::(conn) + } + + pub fn update_removed_for_creator( + conn: &PgConnection, + for_creator_id: i32, + new_removed: bool, + ) -> Result, Error> { + use crate::schema::comment::dsl::*; + diesel::update(comment.filter(creator_id.eq(for_creator_id))) + .set((removed.eq(new_removed), updated.eq(naive_now()))) + .get_results::(conn) + } + + pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result { + use crate::schema::comment::dsl::*; + diesel::update(comment.find(comment_id)) + .set(read.eq(new_read)) + .get_result::(conn) + } + + pub fn update_content( + conn: &PgConnection, + comment_id: i32, + new_content: &str, + ) -> Result { + use crate::schema::comment::dsl::*; + diesel::update(comment.find(comment_id)) + .set((content.eq(new_content), updated.eq(naive_now()))) + .get_result::(conn) + } +} + +impl CommentForm { + pub fn get_ap_id(&self) -> Result { + Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) + } +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] +#[belongs_to(Comment)] +#[table_name = "comment_like"] +pub struct CommentLike { + pub id: i32, + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, // TODO this is redundant + pub score: i16, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "comment_like"] +pub struct CommentLikeForm { + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, // TODO this is redundant + pub score: i16, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Comment)] +#[table_name = "comment_saved"] +pub struct CommentSaved { + pub id: i32, + pub comment_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "comment_saved"] +pub struct CommentSavedForm { + pub comment_id: i32, + pub user_id: i32, +} diff --git a/lemmy_db_schema/src/source/mod.rs b/lemmy_db_schema/src/source/mod.rs new file mode 100644 index 00000000..38203b5e --- /dev/null +++ b/lemmy_db_schema/src/source/mod.rs @@ -0,0 +1,2 @@ +pub mod comment; +pub mod post; diff --git a/lemmy_db_schema/src/source/post.rs b/lemmy_db_schema/src/source/post.rs new file mode 100644 index 00000000..a0b974e2 --- /dev/null +++ b/lemmy_db_schema/src/source/post.rs @@ -0,0 +1,229 @@ +use crate::{ + naive_now, + schema::{post, post_like, post_read, post_saved}, +}; +use diesel::{result::Error, *}; +use serde::Serialize; +use url::{ParseError, Url}; + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "post"] +pub struct Post { + pub id: i32, + pub name: String, + pub url: Option, + pub body: Option, + pub creator_id: i32, + pub community_id: i32, + pub removed: bool, + pub locked: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub deleted: bool, + pub nsfw: bool, + pub stickied: bool, + pub embed_title: Option, + pub embed_description: Option, + pub embed_html: Option, + pub thumbnail_url: Option, + pub ap_id: String, + pub local: bool, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "post"] +pub struct PostForm { + pub name: String, + pub url: Option, + pub body: Option, + pub creator_id: i32, + pub community_id: i32, + pub removed: Option, + pub locked: Option, + pub published: Option, + pub updated: Option, + pub deleted: Option, + pub nsfw: bool, + pub stickied: Option, + pub embed_title: Option, + pub embed_description: Option, + pub embed_html: Option, + pub thumbnail_url: Option, + pub ap_id: Option, + pub local: bool, +} + +impl Post { + pub fn read(conn: &PgConnection, post_id: i32) -> Result { + use crate::schema::post::dsl::*; + post.filter(id.eq(post_id)).first::(conn) + } + + pub fn list_for_community( + conn: &PgConnection, + the_community_id: i32, + ) -> Result, Error> { + use crate::schema::post::dsl::*; + post + .filter(community_id.eq(the_community_id)) + .then_order_by(published.desc()) + .then_order_by(stickied.desc()) + .limit(20) + .load::(conn) + } + + pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result { + use crate::schema::post::dsl::*; + + diesel::update(post.find(post_id)) + .set(ap_id.eq(apub_id)) + .get_result::(conn) + } + + pub fn permadelete_for_creator( + conn: &PgConnection, + for_creator_id: i32, + ) -> Result, Error> { + use crate::schema::post::dsl::*; + + let perma_deleted = "*Permananently Deleted*"; + let perma_deleted_url = "https://deleted.com"; + + diesel::update(post.filter(creator_id.eq(for_creator_id))) + .set(( + name.eq(perma_deleted), + url.eq(perma_deleted_url), + body.eq(perma_deleted), + deleted.eq(true), + updated.eq(naive_now()), + )) + .get_results::(conn) + } + + pub fn update_deleted( + conn: &PgConnection, + post_id: i32, + new_deleted: bool, + ) -> Result { + use crate::schema::post::dsl::*; + diesel::update(post.find(post_id)) + .set((deleted.eq(new_deleted), updated.eq(naive_now()))) + .get_result::(conn) + } + + pub fn update_removed( + conn: &PgConnection, + post_id: i32, + new_removed: bool, + ) -> Result { + use crate::schema::post::dsl::*; + diesel::update(post.find(post_id)) + .set((removed.eq(new_removed), updated.eq(naive_now()))) + .get_result::(conn) + } + + pub fn update_removed_for_creator( + conn: &PgConnection, + for_creator_id: i32, + for_community_id: Option, + new_removed: bool, + ) -> Result, Error> { + use crate::schema::post::dsl::*; + + let mut update = diesel::update(post).into_boxed(); + update = update.filter(creator_id.eq(for_creator_id)); + + if let Some(for_community_id) = for_community_id { + update = update.filter(community_id.eq(for_community_id)); + } + + update + .set((removed.eq(new_removed), updated.eq(naive_now()))) + .get_results::(conn) + } + + pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result { + use crate::schema::post::dsl::*; + diesel::update(post.find(post_id)) + .set(locked.eq(new_locked)) + .get_result::(conn) + } + + pub fn update_stickied( + conn: &PgConnection, + post_id: i32, + new_stickied: bool, + ) -> Result { + use crate::schema::post::dsl::*; + diesel::update(post.find(post_id)) + .set(stickied.eq(new_stickied)) + .get_result::(conn) + } + + pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool { + user_id == post_creator_id + } +} + +impl PostForm { + pub fn get_ap_id(&self) -> Result { + Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) + } +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_like"] +pub struct PostLike { + pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub score: i16, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "post_like"] +pub struct PostLikeForm { + pub post_id: i32, + pub user_id: i32, + pub score: i16, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_saved"] +pub struct PostSaved { + pub id: i32, + pub post_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "post_saved"] +pub struct PostSavedForm { + pub post_id: i32, + pub user_id: i32, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Post)] +#[table_name = "post_read"] +pub struct PostRead { + pub id: i32, + + pub post_id: i32, + + pub user_id: i32, + + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "post_read"] +pub struct PostReadForm { + pub post_id: i32, + + pub user_id: i32, +} diff --git a/lemmy_structs/Cargo.toml b/lemmy_structs/Cargo.toml index e1462306..329ef413 100644 --- a/lemmy_structs/Cargo.toml +++ b/lemmy_structs/Cargo.toml @@ -10,6 +10,7 @@ path = "src/lib.rs" [dependencies] lemmy_db = { path = "../lemmy_db" } +lemmy_db_schema = { path = "../lemmy_db_schema" } lemmy_utils = { path = "../lemmy_utils" } serde = { version = "1.0.118", features = ["derive"] } log = "0.4.11" diff --git a/lemmy_structs/src/lib.rs b/lemmy_structs/src/lib.rs index dc06a40c..595f6d07 100644 --- a/lemmy_structs/src/lib.rs +++ b/lemmy_structs/src/lib.rs @@ -8,14 +8,13 @@ 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_schema::source::{comment::Comment, post::Post}; use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError}; use log::error; use serde::{Deserialize, Serialize}; diff --git a/lemmy_websocket/Cargo.toml b/lemmy_websocket/Cargo.toml index ed0ba4ce..30dbe1fb 100644 --- a/lemmy_websocket/Cargo.toml +++ b/lemmy_websocket/Cargo.toml @@ -12,6 +12,7 @@ path = "src/lib.rs" lemmy_utils = { path = "../lemmy_utils" } lemmy_structs = { path = "../lemmy_structs" } lemmy_db = { path = "../lemmy_db" } +lemmy_db_schema = { path = "../lemmy_db_schema" } lemmy_rate_limit = { path = "../lemmy_rate_limit" } reqwest = { version = "0.10.10", features = ["json"] } log = "0.4.11" diff --git a/lemmy_websocket/src/handlers.rs b/lemmy_websocket/src/handlers.rs index d95dfd57..0762b948 100644 --- a/lemmy_websocket/src/handlers.rs +++ b/lemmy_websocket/src/handlers.rs @@ -3,7 +3,7 @@ use crate::{ messages::*, }; use actix::{Actor, Context, Handler, ResponseFuture}; -use lemmy_db::naive_now; +use lemmy_db_schema::naive_now; use log::{error, info}; use rand::Rng; use serde::Serialize; diff --git a/src/code_migrations.rs b/src/code_migrations.rs index 6a39d0da..c294be67 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -4,16 +4,17 @@ use diesel::{ *, }; use lemmy_db::{ - naive_now, source::{ - comment::Comment, community::{Community, CommunityForm}, - post::Post, private_message::PrivateMessage, user::{UserForm, User_}, }, Crud, }; +use lemmy_db_schema::{ + naive_now, + source::{comment::Comment, post::Post}, +}; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, settings::Settings,