diff --git a/Cargo.lock b/Cargo.lock index 815cd20f6..060fa8913 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", @@ -1790,6 +1792,7 @@ dependencies = [ "diesel", "diesel_migrations", "lazy_static", + "lemmy_db_schema", "lemmy_utils", "log", "regex", @@ -1801,6 +1804,18 @@ dependencies = [ "url", ] +[[package]] +name = "lemmy_db_schema" +version = "0.1.0" +dependencies = [ + "chrono", + "diesel", + "log", + "serde 1.0.118", + "serde_json", + "url", +] + [[package]] name = "lemmy_rate_limit" version = "0.1.0" @@ -1836,6 +1851,7 @@ dependencies = [ "lemmy_api", "lemmy_apub", "lemmy_db", + "lemmy_db_schema", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", @@ -1860,6 +1876,7 @@ dependencies = [ "chrono", "diesel", "lemmy_db", + "lemmy_db_schema", "lemmy_utils", "log", "serde 1.0.118", @@ -1901,6 +1918,7 @@ dependencies = [ "chrono", "diesel", "lemmy_db", + "lemmy_db_schema", "lemmy_rate_limit", "lemmy_structs", "lemmy_utils", diff --git a/Cargo.toml b/Cargo.toml index 40b286775..7bed59d1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "lemmy_apub", "lemmy_utils", "lemmy_db", + "lemmy_db_schema", "lemmy_structs", "lemmy_rate_limit", "lemmy_websocket", @@ -21,6 +22,7 @@ members = [ 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_structs = { path = "./lemmy_structs" } lemmy_rate_limit = { path = "./lemmy_rate_limit" } diff --git a/lemmy_api/Cargo.toml b/lemmy_api/Cargo.toml index 94fd2d500..f4a10924c 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/claims.rs b/lemmy_api/src/claims.rs index 4a0ab12b8..f99730bdd 100644 --- a/lemmy_api/src/claims.rs +++ b/lemmy_api/src/claims.rs @@ -1,5 +1,5 @@ use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation}; -use lemmy_db::source::user::User_; +use lemmy_db_schema::source::user::User_; use lemmy_utils::settings::Settings; use serde::{Deserialize, Serialize}; diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs index e5f079ad2..bafde6bfb 100644 --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@ -11,11 +11,7 @@ use crate::{ use actix_web::web::Data; use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_db::{ - source::{ - comment::{Comment, CommentForm, CommentLike, CommentLikeForm, CommentSaved, CommentSavedForm}, - comment_report::{CommentReport, CommentReportForm}, - moderator::{ModRemoveComment, ModRemoveCommentForm}, - }, + source::comment::Comment_, views::{ comment_report_view::{CommentReportQueryBuilder, CommentReportView}, comment_view::{CommentQueryBuilder, CommentView}, @@ -27,6 +23,7 @@ use lemmy_db::{ Saveable, SortType, }; +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 --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs index c5ac40829..405491bd2 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -11,8 +11,11 @@ use anyhow::Context; use lemmy_apub::ActorType; use lemmy_db::{ diesel_option_overwrite, - naive_now, - source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, + source::{ + comment::Comment_, + community::{CommunityModerator_, Community_}, + post::Post_, + }, views::{ comment_view::CommentQueryBuilder, community::{ @@ -29,6 +32,10 @@ use lemmy_db::{ Joinable, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, +}; 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 eadb0d1ac..4b61539b4 100644 --- a/lemmy_api/src/lib.rs +++ b/lemmy_api/src/lib.rs @@ -2,15 +2,19 @@ use crate::claims::Claims; use actix_web::{web, web::Data}; use lemmy_db::{ 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 --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index f021b0d7c..20b5b8d55 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -11,12 +11,7 @@ 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, @@ -30,6 +25,14 @@ use lemmy_db::{ Saveable, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{ + moderator::*, + post::*, + post_report::{PostReport, PostReportForm}, + }, +}; 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 16c6ecec9..e30e62303 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -11,8 +11,7 @@ use anyhow::Context; use lemmy_apub::fetcher::search_by_apub_id; use lemmy_db::{ diesel_option_overwrite, - naive_now, - source::{category::*, moderator::*, site::*}, + source::{category::Category_, site::Site_}, views::{ comment_view::CommentQueryBuilder, community::community_view::CommunityQueryBuilder, @@ -35,6 +34,14 @@ use lemmy_db::{ SearchType, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{ + category::Category, + moderator::*, + site::{Site, *}, + }, +}; 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 cca8dffb8..34ef5022e 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -16,17 +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, @@ -47,6 +45,20 @@ use lemmy_db::{ ListingType, SortType, }; +use lemmy_db_schema::{ + naive_now, + source::{ + comment::Comment, + community::*, + moderator::*, + password_reset_request::*, + post::Post, + private_message::*, + site::*, + user::*, + user_mention::*, + }, +}; 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 2dd9a64a4..e62ae9f0c 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 32eb8c0ca..0149e9313 100644 --- a/lemmy_apub/src/activities/receive/comment.rs +++ b/lemmy_apub/src/activities/receive/comment.rs @@ -4,14 +4,10 @@ use activitystreams::{ base::ExtendsExt, }; use anyhow::Context; -use lemmy_db::{ - source::{ - comment::{Comment, CommentLike, CommentLikeForm}, - post::Post, - }, - views::comment_view::CommentView, - Crud, - Likeable, +use lemmy_db::{source::comment::Comment_, views::comment_view::CommentView, Crud, 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 85dcc143d..7e6720457 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::{source::comment::Comment_, 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/community.rs b/lemmy_apub/src/activities/receive/community.rs index 534da5cb7..932917b1e 100644 --- a/lemmy_apub/src/activities/receive/community.rs +++ b/lemmy_apub/src/activities/receive/community.rs @@ -5,10 +5,11 @@ use activitystreams::{ }; use anyhow::Context; use lemmy_db::{ - source::community::Community, + source::community::Community_, views::community::community_view::CommunityView, ApubObject, }; +use lemmy_db_schema::source::community::Community; 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/mod.rs b/lemmy_apub/src/activities/receive/mod.rs index a66ddfb95..f52bbea1a 100644 --- a/lemmy_apub/src/activities/receive/mod.rs +++ b/lemmy_apub/src/activities/receive/mod.rs @@ -5,7 +5,7 @@ use activitystreams::{ error::DomainError, }; use anyhow::{anyhow, Context}; -use lemmy_db::source::user::User_; +use lemmy_db_schema::source::user::User_; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; use log::debug; diff --git a/lemmy_apub/src/activities/receive/post.rs b/lemmy_apub/src/activities/receive/post.rs index f09071129..8e20da75f 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::{source::post::Post_, 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 6827ded05..facaf65f1 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::{source::post::Post_, 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/receive/private_message.rs b/lemmy_apub/src/activities/receive/private_message.rs index 8f7952b6b..09c234cac 100644 --- a/lemmy_apub/src/activities/receive/private_message.rs +++ b/lemmy_apub/src/activities/receive/private_message.rs @@ -14,9 +14,10 @@ use activitystreams::{ }; use anyhow::{anyhow, Context}; use lemmy_db::{ - source::private_message::PrivateMessage, + source::private_message::PrivateMessage_, views::private_message_view::PrivateMessageView, }; +use lemmy_db_schema::source::private_message::PrivateMessage; 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/comment.rs b/lemmy_apub/src/activities/send/comment.rs index 6b417c259..4ddd2d321 100644 --- a/lemmy_apub/src/activities/send/comment.rs +++ b/lemmy_apub/src/activities/send/comment.rs @@ -26,11 +26,8 @@ use activitystreams::{ }; use anyhow::anyhow; use itertools::Itertools; -use lemmy_db::{ - source::{comment::Comment, community::Community, post::Post, user::User_}, - Crud, - DbPool, -}; +use lemmy_db::{Crud, DbPool}; +use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post, user::User_}; use lemmy_structs::{blocking, WebFingerResponse}; use lemmy_utils::{ request::{retry, RecvError}, diff --git a/lemmy_apub/src/activities/send/community.rs b/lemmy_apub/src/activities/send/community.rs index 96152fa0d..035a8dfed 100644 --- a/lemmy_apub/src/activities/send/community.rs +++ b/lemmy_apub/src/activities/send/community.rs @@ -23,11 +23,8 @@ use activitystreams::{ }; use anyhow::Context; use itertools::Itertools; -use lemmy_db::{ - source::community::Community, - views::community::community_follower_view::CommunityFollowerView, - DbPool, -}; +use lemmy_db::{views::community::community_follower_view::CommunityFollowerView, DbPool}; +use lemmy_db_schema::source::community::Community; use lemmy_structs::blocking; use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/activities/send/post.rs b/lemmy_apub/src/activities/send/post.rs index ec1ca67de..732a53c33 100644 --- a/lemmy_apub/src/activities/send/post.rs +++ b/lemmy_apub/src/activities/send/post.rs @@ -21,10 +21,8 @@ use activitystreams::{ prelude::*, public, }; -use lemmy_db::{ - source::{community::Community, post::Post, user::User_}, - Crud, -}; +use lemmy_db::Crud; +use lemmy_db_schema::source::{community::Community, post::Post, user::User_}; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/activities/send/private_message.rs b/lemmy_apub/src/activities/send/private_message.rs index 9abe70d66..bcbb303bc 100644 --- a/lemmy_apub/src/activities/send/private_message.rs +++ b/lemmy_apub/src/activities/send/private_message.rs @@ -16,10 +16,8 @@ use activitystreams::{ }, prelude::*, }; -use lemmy_db::{ - source::{private_message::PrivateMessage, user::User_}, - Crud, -}; +use lemmy_db::Crud; +use lemmy_db_schema::source::{private_message::PrivateMessage, user::User_}; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/activities/send/user.rs b/lemmy_apub/src/activities/send/user.rs index 26c5a5d56..cad20e9d9 100644 --- a/lemmy_apub/src/activities/send/user.rs +++ b/lemmy_apub/src/activities/send/user.rs @@ -13,14 +13,10 @@ use activitystreams::{ base::{AnyBase, BaseExt, ExtendsExt}, object::ObjectExt, }; -use lemmy_db::{ - source::{ - community::{Community, CommunityFollower, CommunityFollowerForm}, - user::User_, - }, - ApubObject, - DbPool, - Followable, +use lemmy_db::{ApubObject, DbPool, Followable}; +use lemmy_db_schema::source::{ + community::{Community, CommunityFollower, CommunityFollowerForm}, + user::User_, }; use lemmy_structs::blocking; use lemmy_utils::LemmyError; diff --git a/lemmy_apub/src/activity_queue.rs b/lemmy_apub/src/activity_queue.rs index d94c7cbf7..fe70d8e8b 100644 --- a/lemmy_apub/src/activity_queue.rs +++ b/lemmy_apub/src/activity_queue.rs @@ -19,10 +19,8 @@ use background_jobs::{ WorkerConfig, }; use itertools::Itertools; -use lemmy_db::{ - source::{community::Community, user::User_}, - DbPool, -}; +use lemmy_db::DbPool; +use lemmy_db_schema::source::{community::Community, user::User_}; use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext; use log::{debug, warn}; diff --git a/lemmy_apub/src/extensions/group_extensions.rs b/lemmy_apub/src/extensions/group_extensions.rs index ab7acf337..2d8cfe799 100644 --- a/lemmy_apub/src/extensions/group_extensions.rs +++ b/lemmy_apub/src/extensions/group_extensions.rs @@ -1,7 +1,8 @@ use activitystreams::unparsed::UnparsedMutExt; use activitystreams_ext::UnparsedExtension; use diesel::PgConnection; -use lemmy_db::{source::category::Category, Crud}; +use lemmy_db::Crud; +use lemmy_db_schema::source::category::Category; use lemmy_utils::LemmyError; use serde::{Deserialize, Serialize}; diff --git a/lemmy_apub/src/fetcher.rs b/lemmy_apub/src/fetcher.rs index 39188f138..608302fab 100644 --- a/lemmy_apub/src/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -13,13 +13,7 @@ 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_, - }, + source::user::User, views::{ comment_view::CommentView, community::community_view::CommunityView, @@ -31,6 +25,15 @@ use lemmy_db::{ Joinable, SearchType, }; +use lemmy_db_schema::{ + naive_now, + source::{ + comment::Comment, + community::{Community, CommunityModerator, CommunityModeratorForm}, + post::Post, + user::User_, + }, +}; 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 16f411902..f71d542b0 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 a17e2abf9..45c576d24 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_, post::Post_}, views::community::community_follower_view::CommunityFollowerView, }; +use lemmy_db_schema::source::{community::Community, post::Post}; use lemmy_structs::blocking; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/http/mod.rs b/lemmy_apub/src/http/mod.rs index fee15b773..5d45a725f 100644 --- a/lemmy_apub/src/http/mod.rs +++ b/lemmy_apub/src/http/mod.rs @@ -1,6 +1,7 @@ use crate::APUB_JSON_CONTENT_TYPE; use actix_web::{body::Body, web, HttpResponse}; -use lemmy_db::source::activity::Activity; +use lemmy_db::source::activity::Activity_; +use lemmy_db_schema::source::activity::Activity; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/http/post.rs b/lemmy_apub/src/http/post.rs index 3d0c6ff8e..8ade35296 100644 --- a/lemmy_apub/src/http/post.rs +++ b/lemmy_apub/src/http/post.rs @@ -4,7 +4,8 @@ use crate::{ }; use actix_web::{body::Body, web, HttpResponse}; use diesel::result::Error::NotFound; -use lemmy_db::{source::post::Post, Crud}; +use lemmy_db::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/http/user.rs b/lemmy_apub/src/http/user.rs index 8dcc8edab..b01347e0d 100644 --- a/lemmy_apub/src/http/user.rs +++ b/lemmy_apub/src/http/user.rs @@ -9,7 +9,8 @@ use activitystreams::{ collection::{CollectionExt, OrderedCollection}, }; use actix_web::{body::Body, web, HttpResponse}; -use lemmy_db::source::user::User_; +use lemmy_db::source::user::User; +use lemmy_db_schema::source::user::User_; 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 ec4d30ae8..f82da236b 100644 --- a/lemmy_apub/src/inbox/community_inbox.rs +++ b/lemmy_apub/src/inbox/community_inbox.rs @@ -27,15 +27,16 @@ use activitystreams::{ use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::{anyhow, Context}; use lemmy_db::{ - source::{ - community::{Community, CommunityFollower, CommunityFollowerForm}, - user::User_, - }, + source::community::Community_, views::community::community_user_ban_view::CommunityUserBanView, ApubObject, DbPool, Followable, }; +use lemmy_db_schema::source::{ + community::{Community, CommunityFollower, CommunityFollowerForm}, + user::User_, +}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/mod.rs b/lemmy_apub/src/inbox/mod.rs index 415851883..9b2009342 100644 --- a/lemmy_apub/src/inbox/mod.rs +++ b/lemmy_apub/src/inbox/mod.rs @@ -12,11 +12,8 @@ use activitystreams::{ }; use actix_web::HttpRequest; use anyhow::{anyhow, Context}; -use lemmy_db::{ - source::{activity::Activity, community::Community, user::User_}, - ApubObject, - DbPool, -}; +use lemmy_db::{source::activity::Activity_, ApubObject, DbPool}; +use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_}; use lemmy_structs::blocking; use lemmy_utils::{location_info, settings::Settings, 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 ff160144f..31c5efba1 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::{ApubObject, Crud}; +use lemmy_db_schema::source::{comment::Comment, post::Post, site::Site}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/shared_inbox.rs b/lemmy_apub/src/inbox/shared_inbox.rs index d94c54f25..d6c08a310 100644 --- a/lemmy_apub/src/inbox/shared_inbox.rs +++ b/lemmy_apub/src/inbox/shared_inbox.rs @@ -15,7 +15,8 @@ use crate::{ use activitystreams::{activity::ActorAndObject, prelude::*}; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::Context; -use lemmy_db::{source::community::Community, ApubObject, DbPool}; +use lemmy_db::{ApubObject, DbPool}; +use lemmy_db_schema::source::community::Community; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/user_inbox.rs b/lemmy_apub/src/inbox/user_inbox.rs index dc387739b..a9ca4b944 100644 --- a/lemmy_apub/src/inbox/user_inbox.rs +++ b/lemmy_apub/src/inbox/user_inbox.rs @@ -48,14 +48,11 @@ use activitystreams::{ use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::{anyhow, Context}; use diesel::NotFound; -use lemmy_db::{ - source::{ - community::{Community, CommunityFollower}, - private_message::PrivateMessage, - user::User_, - }, - ApubObject, - Followable, +use lemmy_db::{source::user::User, ApubObject, Followable}; +use lemmy_db_schema::source::{ + community::{Community, CommunityFollower}, + private_message::PrivateMessage, + user::User_, }; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; diff --git a/lemmy_apub/src/lib.rs b/lemmy_apub/src/lib.rs index 78224c32e..44786d171 100644 --- a/lemmy_apub/src/lib.rs +++ b/lemmy_apub/src/lib.rs @@ -22,10 +22,8 @@ use activitystreams::{ }; use activitystreams_ext::{Ext1, Ext2}; use anyhow::{anyhow, Context}; -use lemmy_db::{ - source::{activity::Activity, user::User_}, - DbPool, -}; +use lemmy_db::{source::activity::Activity_, DbPool}; +use lemmy_db_schema::source::{activity::Activity, user::User_}; use lemmy_structs::blocking; use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/objects/comment.rs b/lemmy_apub/src/objects/comment.rs index 0d1f6db80..e92272485 100644 --- a/lemmy_apub/src/objects/comment.rs +++ b/lemmy_apub/src/objects/comment.rs @@ -23,15 +23,12 @@ use activitystreams::{ prelude::*, }; use anyhow::{anyhow, Context}; -use lemmy_db::{ - source::{ - comment::{Comment, CommentForm}, - community::Community, - post::Post, - user::User_, - }, - Crud, - DbPool, +use lemmy_db::{Crud, DbPool}; +use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + community::Community, + post::Post, + user::User_, }; use lemmy_structs::blocking; use lemmy_utils::{ diff --git a/lemmy_apub/src/objects/community.rs b/lemmy_apub/src/objects/community.rs index 9d8210a6a..b408f7737 100644 --- a/lemmy_apub/src/objects/community.rs +++ b/lemmy_apub/src/objects/community.rs @@ -22,11 +22,10 @@ use activitystreams::{ }; use activitystreams_ext::Ext2; use anyhow::Context; -use lemmy_db::{ +use lemmy_db::{views::community::community_moderator_view::CommunityModeratorView, DbPool}; +use lemmy_db_schema::{ naive_now, source::community::{Community, CommunityForm}, - views::community::community_moderator_view::CommunityModeratorView, - DbPool, }; use lemmy_structs::blocking; use lemmy_utils::{ diff --git a/lemmy_apub/src/objects/post.rs b/lemmy_apub/src/objects/post.rs index 9c9df5b19..499ac8020 100644 --- a/lemmy_apub/src/objects/post.rs +++ b/lemmy_apub/src/objects/post.rs @@ -20,14 +20,11 @@ use activitystreams::{ }; use activitystreams_ext::Ext1; use anyhow::Context; -use lemmy_db::{ - source::{ - community::Community, - post::{Post, PostForm}, - user::User_, - }, - Crud, - DbPool, +use lemmy_db::{Crud, DbPool}; +use lemmy_db_schema::source::{ + community::Community, + post::{Post, PostForm}, + user::User_, }; use lemmy_structs::blocking; use lemmy_utils::{ diff --git a/lemmy_apub/src/objects/private_message.rs b/lemmy_apub/src/objects/private_message.rs index e69c28110..34f37c4cd 100644 --- a/lemmy_apub/src/objects/private_message.rs +++ b/lemmy_apub/src/objects/private_message.rs @@ -19,13 +19,10 @@ use activitystreams::{ prelude::*, }; use anyhow::Context; -use lemmy_db::{ - source::{ - private_message::{PrivateMessage, PrivateMessageForm}, - user::User_, - }, - Crud, - DbPool, +use lemmy_db::{Crud, DbPool}; +use lemmy_db_schema::source::{ + private_message::{PrivateMessage, PrivateMessageForm}, + user::User_, }; use lemmy_structs::blocking; use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; diff --git a/lemmy_apub/src/objects/user.rs b/lemmy_apub/src/objects/user.rs index 8c3312d1f..3ec1548d4 100644 --- a/lemmy_apub/src/objects/user.rs +++ b/lemmy_apub/src/objects/user.rs @@ -18,11 +18,10 @@ use activitystreams::{ }; use activitystreams_ext::Ext1; use anyhow::Context; -use lemmy_db::{ +use lemmy_db::{ApubObject, DbPool}; +use lemmy_db_schema::{ naive_now, source::user::{UserForm, User_}, - ApubObject, - DbPool, }; use lemmy_structs::blocking; use lemmy_utils::{ diff --git a/lemmy_db/Cargo.toml b/lemmy_db/Cargo.toml index 5963f32cc..15dd749ac 100644 --- a/lemmy_db/Cargo.toml +++ b/lemmy_db/Cargo.toml @@ -9,6 +9,7 @@ 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"] } @@ -18,7 +19,7 @@ strum = "0.20.0" strum_macros = "0.20.1" log = "0.4.11" sha2 = "0.9.2" -bcrypt = "0.9.0" url = { version = "2.2.0", features = ["serde"] } lazy_static = "1.4.0" regex = "1.4.2" +bcrypt = "0.9.0" diff --git a/lemmy_db/src/aggregates/comment_aggregates.rs b/lemmy_db/src/aggregates/comment_aggregates.rs index 7304dacc2..c6b726755 100644 --- a/lemmy_db/src/aggregates/comment_aggregates.rs +++ b/lemmy_db/src/aggregates/comment_aggregates.rs @@ -1,5 +1,5 @@ -use crate::schema::comment_aggregates; use diesel::{result::Error, *}; +use lemmy_db_schema::schema::comment_aggregates; use serde::Serialize; #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] diff --git a/lemmy_db/src/aggregates/community_aggregates.rs b/lemmy_db/src/aggregates/community_aggregates.rs index 8c977bf0c..229652f00 100644 --- a/lemmy_db/src/aggregates/community_aggregates.rs +++ b/lemmy_db/src/aggregates/community_aggregates.rs @@ -1,5 +1,5 @@ -use crate::schema::community_aggregates; use diesel::{result::Error, *}; +use lemmy_db_schema::schema::community_aggregates; use serde::Serialize; #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] @@ -24,18 +24,18 @@ impl CommunityAggregates { 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, Crud, Followable, ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm}, + post::{Post, PostForm}, + user::{UserForm, User_}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/post_aggregates.rs b/lemmy_db/src/aggregates/post_aggregates.rs index dff16f9b1..01082ca06 100644 --- a/lemmy_db/src/aggregates/post_aggregates.rs +++ b/lemmy_db/src/aggregates/post_aggregates.rs @@ -1,5 +1,5 @@ -use crate::schema::post_aggregates; use diesel::{result::Error, *}; +use lemmy_db_schema::schema::post_aggregates; use serde::Serialize; #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] @@ -26,18 +26,18 @@ impl PostAggregates { 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, Crud, Likeable, ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + community::{Community, CommunityForm}, + post::{Post, PostForm, PostLike, PostLikeForm}, + user::{UserForm, User_}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/site_aggregates.rs b/lemmy_db/src/aggregates/site_aggregates.rs index df3cad6f1..70997a66a 100644 --- a/lemmy_db/src/aggregates/site_aggregates.rs +++ b/lemmy_db/src/aggregates/site_aggregates.rs @@ -1,5 +1,5 @@ -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, Clone)] @@ -23,18 +23,17 @@ impl SiteAggregates { mod tests { use crate::{ aggregates::site_aggregates::SiteAggregates, - source::{ - comment::{Comment, CommentForm}, - community::{Community, CommunityForm}, - post::{Post, PostForm}, - site::{Site, SiteForm}, - user::{UserForm, User_}, - }, tests::establish_unpooled_connection, Crud, ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm}, + community::{Community, CommunityForm}, + post::{Post, PostForm}, + user::{UserForm, User_}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/aggregates/user_aggregates.rs b/lemmy_db/src/aggregates/user_aggregates.rs index 104bf6f7d..e8981fd62 100644 --- a/lemmy_db/src/aggregates/user_aggregates.rs +++ b/lemmy_db/src/aggregates/user_aggregates.rs @@ -1,5 +1,5 @@ -use crate::schema::user_aggregates; use diesel::{result::Error, *}; +use lemmy_db_schema::schema::user_aggregates; use serde::Serialize; #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] @@ -25,18 +25,18 @@ impl UserAggregates { 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, Crud, Likeable, ListingType, SortType, }; + use lemmy_db_schema::source::{ + comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, + community::{Community, CommunityForm}, + post::{Post, PostForm, PostLike, PostLikeForm}, + user::{UserForm, User_}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index 4df69ca43..8e3521478 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -9,14 +9,12 @@ extern crate lazy_static; #[macro_use] extern crate diesel_migrations; -use chrono::NaiveDateTime; use diesel::{result::Error, *}; use regex::Regex; use serde::{Deserialize, Serialize}; use std::{env, env::VarError}; pub mod aggregates; -pub mod schema; pub mod source; pub mod views; @@ -186,10 +184,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/activity.rs b/lemmy_db/src/source/activity.rs index b4b54c6ed..d63b3b6bf 100644 --- a/lemmy_db/src/source/activity.rs +++ b/lemmy_db/src/source/activity.rs @@ -1,43 +1,21 @@ -use crate::{schema::activity, Crud}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; +use lemmy_db_schema::source::activity::*; use log::debug; use serde::Serialize; -use serde_json::Value; use std::{ fmt::Debug, io::{Error as IoError, ErrorKind}, }; -#[derive(Queryable, Identifiable, PartialEq, Debug)] -#[table_name = "activity"] -pub struct Activity { - pub id: i32, - pub data: Value, - pub local: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub ap_id: Option, - pub sensitive: Option, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "activity"] -pub struct ActivityForm { - pub data: Value, - pub local: bool, - pub updated: Option, - pub ap_id: String, - pub sensitive: bool, -} - impl Crud for Activity { fn read(conn: &PgConnection, activity_id: i32) -> Result { - use crate::schema::activity::dsl::*; + use lemmy_db_schema::schema::activity::dsl::*; activity.find(activity_id).first::(conn) } fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result { - use crate::schema::activity::dsl::*; + use lemmy_db_schema::schema::activity::dsl::*; insert_into(activity) .values(new_activity) .get_result::(conn) @@ -48,25 +26,38 @@ impl Crud for Activity { activity_id: i32, new_activity: &ActivityForm, ) -> Result { - use crate::schema::activity::dsl::*; + use lemmy_db_schema::schema::activity::dsl::*; diesel::update(activity.find(activity_id)) .set(new_activity) .get_result::(conn) } fn delete(conn: &PgConnection, activity_id: i32) -> Result { - use crate::schema::activity::dsl::*; + use lemmy_db_schema::schema::activity::dsl::*; diesel::delete(activity.find(activity_id)).execute(conn) } } -impl Activity { - pub fn insert( +pub trait Activity_ { + fn insert( conn: &PgConnection, ap_id: String, data: &T, local: bool, sensitive: bool, - ) -> Result + ) -> Result + where + T: Serialize + Debug; + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result; +} + +impl Activity_ for Activity { + fn insert( + conn: &PgConnection, + ap_id: String, + data: &T, + local: bool, + sensitive: bool, + ) -> Result where T: Serialize + Debug, { @@ -88,23 +79,18 @@ impl Activity { } } - pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::activity::dsl::*; + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { + use lemmy_db_schema::schema::activity::dsl::*; activity.filter(ap_id.eq(object_id)).first::(conn) } } #[cfg(test)] mod tests { - use crate::{ - source::{ - activity::{Activity, ActivityForm}, - user::{UserForm, User_}, - }, - tests::establish_unpooled_connection, - Crud, - ListingType, - SortType, + use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use lemmy_db_schema::source::{ + activity::{Activity, ActivityForm}, + user::{UserForm, User_}, }; use serde_json::Value; diff --git a/lemmy_db/src/source/category.rs b/lemmy_db/src/source/category.rs index 95b65dc82..c190ddf8f 100644 --- a/lemmy_db/src/source/category.rs +++ b/lemmy_db/src/source/category.rs @@ -1,22 +1,6 @@ -use crate::{ - schema::{category, category::dsl::*}, - Crud, -}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Clone)] -#[table_name = "category"] -pub struct Category { - pub id: i32, - pub name: String, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "category"] -pub struct CategoryForm { - pub name: String, -} +use lemmy_db_schema::{schema::category::dsl::*, source::category::*}; impl Crud for Category { fn read(conn: &PgConnection, category_id: i32) -> Result { @@ -40,15 +24,20 @@ impl Crud for Category { } } -impl Category { - pub fn list_all(conn: &PgConnection) -> Result, Error> { +pub trait Category_ { + fn list_all(conn: &PgConnection) -> Result, Error>; +} + +impl Category_ for Category { + fn list_all(conn: &PgConnection) -> Result, Error> { category.load::(conn) } } #[cfg(test)] mod tests { - use crate::{source::category::Category, tests::establish_unpooled_connection}; + use crate::tests::establish_unpooled_connection; + use lemmy_db_schema::source::category::Category; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/comment.rs b/lemmy_db/src/source/comment.rs index 380bdb3d1..d70d7b699 100644 --- a/lemmy_db/src/source/comment.rs +++ b/lemmy_db/src/source/comment.rs @@ -1,94 +1,131 @@ -use super::post::Post; -use crate::{ - naive_now, - schema::{comment, comment_alias_1, comment_like, comment_saved}, - ApubObject, - Crud, - Likeable, - Saveable, -}; +use crate::{ApubObject, Crud, Likeable, Saveable}; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; -use url::{ParseError, Url}; +use lemmy_db_schema::{ + naive_now, + source::comment::{ + Comment, + CommentForm, + CommentLike, + CommentLikeForm, + CommentSaved, + CommentSavedForm, + }, +}; -// 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, +pub trait Comment_ { + fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result; + fn permadelete_for_creator( + conn: &PgConnection, + for_creator_id: i32, + ) -> Result, Error>; + fn update_deleted( + conn: &PgConnection, + comment_id: i32, + new_deleted: bool, + ) -> Result; + fn update_removed( + conn: &PgConnection, + comment_id: i32, + new_removed: bool, + ) -> Result; + fn update_removed_for_creator( + conn: &PgConnection, + for_creator_id: i32, + new_removed: bool, + ) -> Result, Error>; + fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result; + fn update_content( + conn: &PgConnection, + comment_id: i32, + new_content: &str, + ) -> Result; } -#[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, -} +impl Comment_ for Comment { + fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result { + use lemmy_db_schema::schema::comment::dsl::*; -#[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, -} + diesel::update(comment.find(comment_id)) + .set(ap_id.eq(apub_id)) + .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())) + 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) + } + + 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) + } + + 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) + } + + 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) + } + + 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) + } + + 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) } } impl Crud for Comment { fn read(conn: &PgConnection, comment_id: i32) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; comment.find(comment_id).first::(conn) } fn delete(conn: &PgConnection, comment_id: i32) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; diesel::delete(comment.find(comment_id)).execute(conn) } fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; insert_into(comment) .values(comment_form) .get_result::(conn) @@ -99,7 +136,7 @@ impl Crud for Comment { comment_id: i32, comment_form: &CommentForm, ) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; diesel::update(comment.find(comment_id)) .set(comment_form) .get_result::(conn) @@ -108,12 +145,12 @@ impl Crud for Comment { impl ApubObject for Comment { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; comment.filter(ap_id.eq(object_id)).first::(conn) } fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result { - use crate::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; insert_into(comment) .values(comment_form) .on_conflict(ap_id) @@ -123,109 +160,9 @@ impl ApubObject for Comment { } } -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) - } -} - -#[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 crate::schema::comment_like::dsl::*; + use lemmy_db_schema::schema::comment_like::dsl::*; insert_into(comment_like) .values(comment_like_form) .on_conflict((comment_id, user_id)) @@ -234,7 +171,7 @@ impl Likeable for CommentLike { .get_result::(conn) } fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result { - use crate::schema::comment_like::dsl; + use lemmy_db_schema::schema::comment_like::dsl; diesel::delete( dsl::comment_like .filter(dsl::comment_id.eq(comment_id)) @@ -244,26 +181,9 @@ 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 crate::schema::comment_saved::dsl::*; + use lemmy_db_schema::schema::comment_saved::dsl::*; insert_into(comment_saved) .values(comment_saved_form) .on_conflict((comment_id, user_id)) @@ -272,7 +192,7 @@ impl Saveable for CommentSaved { .get_result::(conn) } fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result { - use crate::schema::comment_saved::dsl::*; + use lemmy_db_schema::schema::comment_saved::dsl::*; diesel::delete( comment_saved .filter(comment_id.eq(comment_saved_form.comment_id)) @@ -285,12 +205,19 @@ impl Saveable for CommentSaved { #[cfg(test)] mod tests { use crate::{ - source::{comment::*, community::*, post::*, user::*}, tests::establish_unpooled_connection, Crud, + Likeable, ListingType, + Saveable, SortType, }; + use lemmy_db_schema::source::{ + comment::*, + community::{Community, CommunityForm}, + post::*, + user::{UserForm, User_}, + }; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/comment_report.rs b/lemmy_db/src/source/comment_report.rs index 7a28089f2..bf42704ea 100644 --- a/lemmy_db/src/source/comment_report.rs +++ b/lemmy_db/src/source/comment_report.rs @@ -1,33 +1,9 @@ +use crate::Reportable; use diesel::{dsl::*, result::Error, *}; -use serde::{Deserialize, Serialize}; - -use crate::{naive_now, schema::comment_report, source::comment::Comment, Reportable}; - -#[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, -} +use lemmy_db_schema::{ + naive_now, + source::comment_report::{CommentReport, CommentReportForm}, +}; impl Reportable for CommentReport { /// creates a comment report and returns it @@ -35,7 +11,7 @@ impl Reportable for CommentReport { /// * `conn` - the postgres connection /// * `comment_report_form` - the filled CommentReportForm to insert fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result { - use crate::schema::comment_report::dsl::*; + use lemmy_db_schema::schema::comment_report::dsl::*; insert_into(comment_report) .values(comment_report_form) .get_result::(conn) @@ -47,7 +23,7 @@ impl Reportable for CommentReport { /// * `report_id` - the id of the report to resolve /// * `by_resolver_id` - the id of the user resolving the report fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { - use crate::schema::comment_report::dsl::*; + use lemmy_db_schema::schema::comment_report::dsl::*; update(comment_report.find(report_id)) .set(( resolved.eq(true), @@ -63,7 +39,7 @@ impl Reportable for CommentReport { /// * `report_id` - the id of the report to unresolve /// * `by_resolver_id` - the id of the user unresolving the report fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { - use crate::schema::comment_report::dsl::*; + use lemmy_db_schema::schema::comment_report::dsl::*; update(comment_report.find(report_id)) .set(( resolved.eq(false), diff --git a/lemmy_db/src/source/community.rs b/lemmy_db/src/source/community.rs index 0ad90da28..9a30ca4cd 100644 --- a/lemmy_db/src/source/community.rs +++ b/lemmy_db/src/source/community.rs @@ -1,6 +1,4 @@ use crate::{ - naive_now, - schema::{community, community_follower, community_moderator, community_user_ban}, views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}, ApubObject, Bannable, @@ -9,54 +7,24 @@ use crate::{ Joinable, }; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "community"] -pub struct Community { - pub id: i32, - pub name: String, - pub title: String, - pub description: Option, - pub category_id: i32, - pub creator_id: i32, - pub removed: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub nsfw: bool, - pub actor_id: String, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: chrono::NaiveDateTime, - pub icon: Option, - pub banner: Option, -} - -/// A safe representation of community, without the sensitive info -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "community"] -pub struct CommunitySafe { - pub id: i32, - pub name: String, - pub title: String, - pub description: Option, - pub category_id: i32, - pub creator_id: i32, - pub removed: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub nsfw: bool, - pub actor_id: String, - pub local: bool, - pub icon: Option, - pub banner: Option, -} +use lemmy_db_schema::{ + naive_now, + source::community::{ + Community, + CommunityFollower, + CommunityFollowerForm, + CommunityForm, + CommunityModerator, + CommunityModeratorForm, + CommunityUserBan, + CommunityUserBanForm, + }, +}; mod safe_type { - use crate::{schema::community::columns::*, source::community::Community, ToSafe}; + use crate::{source::community::Community, ToSafe}; + use lemmy_db_schema::schema::community::*; + type Columns = ( id, name, @@ -99,41 +67,19 @@ mod safe_type { } } -#[derive(Insertable, AsChangeset, Debug)] -#[table_name = "community"] -pub struct CommunityForm { - pub name: String, - pub title: String, - pub description: Option, - pub category_id: i32, - pub creator_id: i32, - pub removed: Option, - pub published: Option, - pub updated: Option, - pub deleted: Option, - pub nsfw: bool, - pub actor_id: Option, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: Option, - pub icon: Option>, - pub banner: Option>, -} - impl Crud for Community { fn read(conn: &PgConnection, community_id: i32) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; community.find(community_id).first::(conn) } fn delete(conn: &PgConnection, community_id: i32) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; diesel::delete(community.find(community_id)).execute(conn) } fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; insert_into(community) .values(new_community) .get_result::(conn) @@ -144,7 +90,7 @@ impl Crud for Community { community_id: i32, new_community: &CommunityForm, ) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; diesel::update(community.find(community_id)) .set(new_community) .get_result::(conn) @@ -153,14 +99,14 @@ impl Crud for Community { impl ApubObject for Community { fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; community .filter(actor_id.eq(for_actor_id)) .first::(conn) } fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result { - use crate::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; insert_into(community) .values(community_form) .on_conflict(actor_id) @@ -170,54 +116,81 @@ impl ApubObject for Community { } } -impl Community { - pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result { - use crate::schema::community::dsl::*; +pub trait Community_ { + fn read_from_name(conn: &PgConnection, community_name: &str) -> Result; + fn update_deleted( + conn: &PgConnection, + community_id: i32, + new_deleted: bool, + ) -> Result; + fn update_removed( + conn: &PgConnection, + community_id: i32, + new_removed: bool, + ) -> Result; + fn update_removed_for_creator( + conn: &PgConnection, + for_creator_id: i32, + new_removed: bool, + ) -> Result, Error>; + fn update_creator( + conn: &PgConnection, + 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 { + fn read_from_name(conn: &PgConnection, community_name: &str) -> Result { + use lemmy_db_schema::schema::community::dsl::*; community .filter(local.eq(true)) .filter(name.eq(community_name)) .first::(conn) } - pub fn update_deleted( + fn update_deleted( conn: &PgConnection, community_id: i32, new_deleted: bool, - ) -> Result { - use crate::schema::community::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::community::dsl::*; diesel::update(community.find(community_id)) .set((deleted.eq(new_deleted), updated.eq(naive_now()))) .get_result::(conn) } - pub fn update_removed( + fn update_removed( conn: &PgConnection, community_id: i32, new_removed: bool, - ) -> Result { - use crate::schema::community::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::community::dsl::*; diesel::update(community.find(community_id)) .set((removed.eq(new_removed), updated.eq(naive_now()))) .get_result::(conn) } - pub fn update_removed_for_creator( + fn update_removed_for_creator( conn: &PgConnection, for_creator_id: i32, new_removed: bool, - ) -> Result, Error> { - use crate::schema::community::dsl::*; + ) -> Result, Error> { + use lemmy_db_schema::schema::community::dsl::*; diesel::update(community.filter(creator_id.eq(for_creator_id))) .set((removed.eq(new_removed), updated.eq(naive_now()))) .get_results::(conn) } - pub fn update_creator( + fn update_creator( conn: &PgConnection, community_id: i32, new_creator_id: i32, - ) -> Result { - use crate::schema::community::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::community::dsl::*; diesel::update(community.find(community_id)) .set((creator_id.eq(new_creator_id), updated.eq(naive_now()))) .get_result::(conn) @@ -234,41 +207,24 @@ impl Community { Ok(mods_and_admins) } - pub fn distinct_federated_communities(conn: &PgConnection) -> Result, Error> { - use crate::schema::community::dsl::*; + fn distinct_federated_communities(conn: &PgConnection) -> Result, Error> { + use lemmy_db_schema::schema::community::dsl::*; community.select(actor_id).distinct().load::(conn) } - pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool { + 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) } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Community)] -#[table_name = "community_moderator"] -pub struct CommunityModerator { - pub id: i32, - pub community_id: i32, - pub user_id: i32, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "community_moderator"] -pub struct CommunityModeratorForm { - pub community_id: i32, - pub user_id: i32, -} - impl Joinable for CommunityModerator { fn join( conn: &PgConnection, community_user_form: &CommunityModeratorForm, ) -> Result { - use crate::schema::community_moderator::dsl::*; + use lemmy_db_schema::schema::community_moderator::dsl::*; insert_into(community_moderator) .values(community_user_form) .get_result::(conn) @@ -278,7 +234,7 @@ impl Joinable for CommunityModerator { conn: &PgConnection, community_user_form: &CommunityModeratorForm, ) -> Result { - use crate::schema::community_moderator::dsl::*; + use lemmy_db_schema::schema::community_moderator::dsl::*; diesel::delete( community_moderator .filter(community_id.eq(community_user_form.community_id)) @@ -288,17 +244,25 @@ impl Joinable for CommunityModerator { } } -impl CommunityModerator { - pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result { - use crate::schema::community_moderator::dsl::*; +pub trait CommunityModerator_ { + fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result; + fn get_user_moderated_communities( + conn: &PgConnection, + for_user_id: i32, + ) -> Result, Error>; +} + +impl CommunityModerator_ for CommunityModerator { + fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result { + use lemmy_db_schema::schema::community_moderator::dsl::*; diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn) } - pub fn get_user_moderated_communities( + fn get_user_moderated_communities( conn: &PgConnection, for_user_id: i32, ) -> Result, Error> { - use crate::schema::community_moderator::dsl::*; + use lemmy_db_schema::schema::community_moderator::dsl::*; community_moderator .filter(user_id.eq(for_user_id)) .select(community_id) @@ -306,29 +270,12 @@ impl CommunityModerator { } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Community)] -#[table_name = "community_user_ban"] -pub struct CommunityUserBan { - pub id: i32, - pub community_id: i32, - pub user_id: i32, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "community_user_ban"] -pub struct CommunityUserBanForm { - pub community_id: i32, - pub user_id: i32, -} - impl Bannable for CommunityUserBan { fn ban( conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm, ) -> Result { - use crate::schema::community_user_ban::dsl::*; + use lemmy_db_schema::schema::community_user_ban::dsl::*; insert_into(community_user_ban) .values(community_user_ban_form) .get_result::(conn) @@ -338,7 +285,7 @@ impl Bannable for CommunityUserBan { conn: &PgConnection, community_user_ban_form: &CommunityUserBanForm, ) -> Result { - use crate::schema::community_user_ban::dsl::*; + use lemmy_db_schema::schema::community_user_ban::dsl::*; diesel::delete( community_user_ban .filter(community_id.eq(community_user_ban_form.community_id)) @@ -348,31 +295,12 @@ impl Bannable for CommunityUserBan { } } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] -#[belongs_to(Community)] -#[table_name = "community_follower"] -pub struct CommunityFollower { - pub id: i32, - pub community_id: i32, - pub user_id: i32, - pub published: chrono::NaiveDateTime, - pub pending: Option, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "community_follower"] -pub struct CommunityFollowerForm { - pub community_id: i32, - pub user_id: i32, - pub pending: bool, -} - impl Followable for CommunityFollower { fn follow( conn: &PgConnection, community_follower_form: &CommunityFollowerForm, ) -> Result { - use crate::schema::community_follower::dsl::*; + use lemmy_db_schema::schema::community_follower::dsl::*; insert_into(community_follower) .values(community_follower_form) .on_conflict((community_id, user_id)) @@ -384,7 +312,7 @@ impl Followable for CommunityFollower { where Self: Sized, { - use crate::schema::community_follower::dsl::*; + use lemmy_db_schema::schema::community_follower::dsl::*; diesel::update( community_follower .filter(community_id.eq(community_id_)) @@ -397,7 +325,7 @@ impl Followable for CommunityFollower { conn: &PgConnection, community_follower_form: &CommunityFollowerForm, ) -> Result { - use crate::schema::community_follower::dsl::*; + use lemmy_db_schema::schema::community_follower::dsl::*; diesel::delete( community_follower .filter(community_id.eq(&community_follower_form.community_id)) @@ -408,7 +336,7 @@ impl Followable for CommunityFollower { // TODO: this function name only makes sense if you call it with a remote community. for a local // community, it will also return true if only remote followers exist fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result { - use crate::schema::community_follower::dsl::*; + use lemmy_db_schema::schema::community_follower::dsl::*; diesel::select(exists( community_follower.filter(community_id.eq(community_id_)), )) @@ -419,11 +347,15 @@ impl Followable for CommunityFollower { #[cfg(test)] mod tests { use crate::{ - source::{community::*, user::*}, tests::establish_unpooled_connection, + Bannable, + Crud, + Followable, + Joinable, ListingType, SortType, }; + use lemmy_db_schema::source::{community::*, user::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/moderator.rs b/lemmy_db/src/source/moderator.rs index 766c17fc5..b0a17d6d5 100644 --- a/lemmy_db/src/source/moderator.rs +++ b/lemmy_db/src/source/moderator.rs @@ -1,210 +1,99 @@ -use crate::{ - schema::{ - mod_add, - mod_add_community, - mod_ban, - mod_ban_from_community, - mod_lock_post, - mod_remove_comment, - mod_remove_community, - mod_remove_post, - mod_sticky_post, - }, - Crud, -}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_remove_post"] -pub struct ModRemovePost { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub reason: Option, - pub removed: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_remove_post"] -pub struct ModRemovePostForm { - pub mod_user_id: i32, - pub post_id: i32, - pub reason: Option, - pub removed: Option, -} +use lemmy_db_schema::source::moderator::*; impl Crud for ModRemovePost { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_post::dsl::*; + use lemmy_db_schema::schema::mod_remove_post::dsl::*; mod_remove_post.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result { - use crate::schema::mod_remove_post::dsl::*; + use lemmy_db_schema::schema::mod_remove_post::dsl::*; insert_into(mod_remove_post) .values(form) .get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result { - use crate::schema::mod_remove_post::dsl::*; + use lemmy_db_schema::schema::mod_remove_post::dsl::*; diesel::update(mod_remove_post.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_lock_post"] -pub struct ModLockPost { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub locked: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_lock_post"] -pub struct ModLockPostForm { - pub mod_user_id: i32, - pub post_id: i32, - pub locked: Option, -} - impl Crud for ModLockPost { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_lock_post::dsl::*; + use lemmy_db_schema::schema::mod_lock_post::dsl::*; mod_lock_post.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result { - use crate::schema::mod_lock_post::dsl::*; + use lemmy_db_schema::schema::mod_lock_post::dsl::*; insert_into(mod_lock_post) .values(form) .get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result { - use crate::schema::mod_lock_post::dsl::*; + use lemmy_db_schema::schema::mod_lock_post::dsl::*; diesel::update(mod_lock_post.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_sticky_post"] -pub struct ModStickyPost { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub stickied: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_sticky_post"] -pub struct ModStickyPostForm { - pub mod_user_id: i32, - pub post_id: i32, - pub stickied: Option, -} - impl Crud for ModStickyPost { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_sticky_post::dsl::*; + use lemmy_db_schema::schema::mod_sticky_post::dsl::*; mod_sticky_post.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result { - use crate::schema::mod_sticky_post::dsl::*; + use lemmy_db_schema::schema::mod_sticky_post::dsl::*; insert_into(mod_sticky_post) .values(form) .get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result { - use crate::schema::mod_sticky_post::dsl::*; + use lemmy_db_schema::schema::mod_sticky_post::dsl::*; diesel::update(mod_sticky_post.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_remove_comment"] -pub struct ModRemoveComment { - pub id: i32, - pub mod_user_id: i32, - pub comment_id: i32, - pub reason: Option, - pub removed: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_remove_comment"] -pub struct ModRemoveCommentForm { - pub mod_user_id: i32, - pub comment_id: i32, - pub reason: Option, - pub removed: Option, -} - impl Crud for ModRemoveComment { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_comment::dsl::*; + use lemmy_db_schema::schema::mod_remove_comment::dsl::*; mod_remove_comment.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result { - use crate::schema::mod_remove_comment::dsl::*; + use lemmy_db_schema::schema::mod_remove_comment::dsl::*; insert_into(mod_remove_comment) .values(form) .get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result { - use crate::schema::mod_remove_comment::dsl::*; + use lemmy_db_schema::schema::mod_remove_comment::dsl::*; diesel::update(mod_remove_comment.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_remove_community"] -pub struct ModRemoveCommunity { - pub id: i32, - pub mod_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub removed: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_remove_community"] -pub struct ModRemoveCommunityForm { - pub mod_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub removed: Option, - pub expires: Option, -} - impl Crud for ModRemoveCommunity { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_remove_community::dsl::*; + use lemmy_db_schema::schema::mod_remove_community::dsl::*; mod_remove_community.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result { - use crate::schema::mod_remove_community::dsl::*; + use lemmy_db_schema::schema::mod_remove_community::dsl::*; insert_into(mod_remove_community) .values(form) .get_result::(conn) @@ -215,45 +104,21 @@ impl Crud for ModRemoveCommunity { from_id: i32, form: &ModRemoveCommunityForm, ) -> Result { - use crate::schema::mod_remove_community::dsl::*; + use lemmy_db_schema::schema::mod_remove_community::dsl::*; diesel::update(mod_remove_community.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_ban_from_community"] -pub struct ModBanFromCommunity { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_ban_from_community"] -pub struct ModBanFromCommunityForm { - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, -} - impl Crud for ModBanFromCommunity { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_ban_from_community::dsl::*; + use lemmy_db_schema::schema::mod_ban_from_community::dsl::*; mod_ban_from_community.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result { - use crate::schema::mod_ban_from_community::dsl::*; + use lemmy_db_schema::schema::mod_ban_from_community::dsl::*; insert_into(mod_ban_from_community) .values(form) .get_result::(conn) @@ -264,126 +129,66 @@ impl Crud for ModBanFromCommunity { from_id: i32, form: &ModBanFromCommunityForm, ) -> Result { - use crate::schema::mod_ban_from_community::dsl::*; + use lemmy_db_schema::schema::mod_ban_from_community::dsl::*; diesel::update(mod_ban_from_community.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_ban"] -pub struct ModBan { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_ban"] -pub struct ModBanForm { - pub mod_user_id: i32, - pub other_user_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, -} - impl Crud for ModBan { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_ban::dsl::*; + use lemmy_db_schema::schema::mod_ban::dsl::*; mod_ban.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModBanForm) -> Result { - use crate::schema::mod_ban::dsl::*; + use lemmy_db_schema::schema::mod_ban::dsl::*; insert_into(mod_ban).values(form).get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result { - use crate::schema::mod_ban::dsl::*; + use lemmy_db_schema::schema::mod_ban::dsl::*; diesel::update(mod_ban.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_add_community"] -pub struct ModAddCommunity { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub removed: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_add_community"] -pub struct ModAddCommunityForm { - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub removed: Option, -} - impl Crud for ModAddCommunity { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_add_community::dsl::*; + use lemmy_db_schema::schema::mod_add_community::dsl::*; mod_add_community.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result { - use crate::schema::mod_add_community::dsl::*; + use lemmy_db_schema::schema::mod_add_community::dsl::*; insert_into(mod_add_community) .values(form) .get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result { - use crate::schema::mod_add_community::dsl::*; + use lemmy_db_schema::schema::mod_add_community::dsl::*; diesel::update(mod_add_community.find(from_id)) .set(form) .get_result::(conn) } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "mod_add"] -pub struct ModAdd { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub removed: Option, - pub when_: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "mod_add"] -pub struct ModAddForm { - pub mod_user_id: i32, - pub other_user_id: i32, - pub removed: Option, -} - impl Crud for ModAdd { fn read(conn: &PgConnection, from_id: i32) -> Result { - use crate::schema::mod_add::dsl::*; + use lemmy_db_schema::schema::mod_add::dsl::*; mod_add.find(from_id).first::(conn) } fn create(conn: &PgConnection, form: &ModAddForm) -> Result { - use crate::schema::mod_add::dsl::*; + use lemmy_db_schema::schema::mod_add::dsl::*; insert_into(mod_add).values(form).get_result::(conn) } fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result { - use crate::schema::mod_add::dsl::*; + use lemmy_db_schema::schema::mod_add::dsl::*; diesel::update(mod_add.find(from_id)) .set(form) .get_result::(conn) @@ -392,12 +197,8 @@ impl Crud for ModAdd { #[cfg(test)] mod tests { - use crate::{ - source::{comment::*, community::*, moderator::*, post::*, user::*}, - tests::establish_unpooled_connection, - ListingType, - SortType, - }; + use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*}; // use Crud; #[test] diff --git a/lemmy_db/src/source/password_reset_request.rs b/lemmy_db/src/source/password_reset_request.rs index 0cf0169f0..9ca8dab68 100644 --- a/lemmy_db/src/source/password_reset_request.rs +++ b/lemmy_db/src/source/password_reset_request.rs @@ -1,29 +1,10 @@ -use crate::{ - schema::{password_reset_request, password_reset_request::dsl::*}, - Crud, -}; +use crate::Crud; use diesel::{dsl::*, result::Error, PgConnection, *}; +use lemmy_db_schema::{schema::password_reset_request::dsl::*, source::password_reset_request::*}; use sha2::{Digest, Sha256}; -#[derive(Queryable, Identifiable, PartialEq, Debug)] -#[table_name = "password_reset_request"] -pub struct PasswordResetRequest { - pub id: i32, - pub user_id: i32, - pub token_encrypted: String, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "password_reset_request"] -pub struct PasswordResetRequestForm { - pub user_id: i32, - pub token_encrypted: String, -} - impl Crud for PasswordResetRequest { fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result { - use crate::schema::password_reset_request::dsl::*; password_reset_request .find(password_reset_request_id) .first::(conn) @@ -44,11 +25,24 @@ impl Crud for PasswordResetRequest { } } -impl PasswordResetRequest { - pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result { +pub trait PasswordResetRequest_ { + fn create_token( + conn: &PgConnection, + from_user_id: i32, + token: &str, + ) -> Result; + fn read_from_token(conn: &PgConnection, token: &str) -> Result; +} + +impl PasswordResetRequest_ for PasswordResetRequest { + fn create_token( + conn: &PgConnection, + from_user_id: i32, + token: &str, + ) -> Result { let mut hasher = Sha256::new(); hasher.update(token); - let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.finalize().to_vec()); + let token_hash: String = bytes_to_hex(hasher.finalize().to_vec()); let form = PasswordResetRequestForm { user_id: from_user_id, @@ -57,35 +51,29 @@ impl PasswordResetRequest { Self::create(&conn, &form) } - pub fn read_from_token(conn: &PgConnection, token: &str) -> Result { + fn read_from_token(conn: &PgConnection, token: &str) -> Result { let mut hasher = Sha256::new(); hasher.update(token); - let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.finalize().to_vec()); + let token_hash: String = bytes_to_hex(hasher.finalize().to_vec()); password_reset_request .filter(token_encrypted.eq(token_hash)) .filter(published.gt(now - 1.days())) .first::(conn) } +} - fn bytes_to_hex(bytes: Vec) -> String { - let mut str = String::new(); - for byte in bytes { - str = format!("{}{:02x}", str, byte); - } - str +fn bytes_to_hex(bytes: Vec) -> String { + let mut str = String::new(); + for byte in bytes { + str = format!("{}{:02x}", str, byte); } + str } #[cfg(test)] mod tests { - use super::super::user::*; - use crate::{ - source::password_reset_request::PasswordResetRequest, - tests::establish_unpooled_connection, - Crud, - ListingType, - SortType, - }; + use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/post.rs b/lemmy_db/src/source/post.rs index 948791ea4..c681adbe4 100644 --- a/lemmy_db/src/source/post.rs +++ b/lemmy_db/src/source/post.rs @@ -1,116 +1,64 @@ -use crate::{ - naive_now, - schema::{post, post_like, post_read, post_saved}, - ApubObject, - Crud, - Likeable, - Readable, - Saveable, -}; +use crate::{ApubObject, Crud, Likeable, Readable, Saveable}; use diesel::{dsl::*, 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 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::{ + naive_now, + source::post::{ + Post, + PostForm, + PostLike, + PostLikeForm, + PostRead, + PostReadForm, + PostSaved, + PostSavedForm, + }, +}; impl Crud for Post { fn read(conn: &PgConnection, post_id: i32) -> Result { - use crate::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; post.find(post_id).first::(conn) } fn delete(conn: &PgConnection, post_id: i32) -> Result { - use crate::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; diesel::delete(post.find(post_id)).execute(conn) } fn create(conn: &PgConnection, new_post: &PostForm) -> Result { - use crate::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; insert_into(post).values(new_post).get_result::(conn) } fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result { - use crate::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; diesel::update(post.find(post_id)) .set(new_post) .get_result::(conn) } } -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 { + 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()) @@ -119,19 +67,16 @@ impl Post { .load::(conn) } - pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result { - use crate::schema::post::dsl::*; + 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 crate::schema::post::dsl::*; + 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"; @@ -147,35 +92,27 @@ impl Post { .get_results::(conn) } - pub fn update_deleted( - conn: &PgConnection, - post_id: i32, - new_deleted: bool, - ) -> Result { - use crate::schema::post::dsl::*; + 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 crate::schema::post::dsl::*; + 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( + 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::*; + use lemmy_db_schema::schema::post::dsl::*; let mut update = diesel::update(post).into_boxed(); update = update.filter(creator_id.eq(for_creator_id)); @@ -189,51 +126,45 @@ impl Post { .get_results::(conn) } - pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result { - use crate::schema::post::dsl::*; + 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 crate::schema::post::dsl::*; + 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 { + 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, -} +impl ApubObject for Post { + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { + use lemmy_db_schema::schema::post::dsl::*; + post.filter(ap_id.eq(object_id)).first::(conn) + } -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "post_like"] -pub struct PostLikeForm { - pub post_id: i32, - pub user_id: i32, - pub score: i16, + fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { + use lemmy_db_schema::schema::post::dsl::*; + insert_into(post) + .values(post_form) + .on_conflict(ap_id) + .do_update() + .set(post_form) + .get_result::(conn) + } } impl Likeable for PostLike { fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result { - use crate::schema::post_like::dsl::*; + use lemmy_db_schema::schema::post_like::dsl::*; insert_into(post_like) .values(post_like_form) .on_conflict((post_id, user_id)) @@ -242,7 +173,7 @@ impl Likeable for PostLike { .get_result::(conn) } fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result { - use crate::schema::post_like::dsl; + use lemmy_db_schema::schema::post_like::dsl; diesel::delete( dsl::post_like .filter(dsl::post_id.eq(post_id)) @@ -252,26 +183,9 @@ 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 crate::schema::post_saved::dsl::*; + use lemmy_db_schema::schema::post_saved::dsl::*; insert_into(post_saved) .values(post_saved_form) .on_conflict((post_id, user_id)) @@ -280,7 +194,7 @@ impl Saveable for PostSaved { .get_result::(conn) } fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result { - use crate::schema::post_saved::dsl::*; + use lemmy_db_schema::schema::post_saved::dsl::*; diesel::delete( post_saved .filter(post_id.eq(post_saved_form.post_id)) @@ -290,37 +204,16 @@ 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 crate::schema::post_read::dsl::*; + use lemmy_db_schema::schema::post_read::dsl::*; insert_into(post_read) .values(post_read_form) .get_result::(conn) } fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { - use crate::schema::post_read::dsl::*; + use lemmy_db_schema::schema::post_read::dsl::*; diesel::delete( post_read .filter(post_id.eq(post_read_form.post_id)) @@ -332,11 +225,10 @@ impl Readable for PostRead { #[cfg(test)] mod tests { - use crate::{ - source::{community::*, post::*, user::*}, - tests::establish_unpooled_connection, - ListingType, - SortType, + use crate::{source::post::*, tests::establish_unpooled_connection, ListingType, SortType}; + use lemmy_db_schema::source::{ + community::{Community, CommunityForm}, + user::*, }; #[test] diff --git a/lemmy_db/src/source/post_report.rs b/lemmy_db/src/source/post_report.rs index 0e597959a..e81dc7c04 100644 --- a/lemmy_db/src/source/post_report.rs +++ b/lemmy_db/src/source/post_report.rs @@ -1,37 +1,6 @@ +use crate::Reportable; use diesel::{dsl::*, result::Error, *}; -use serde::{Deserialize, Serialize}; - -use crate::{naive_now, schema::post_report, source::post::Post, Reportable}; - -#[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, -} +use lemmy_db_schema::{naive_now, source::post_report::*}; impl Reportable for PostReport { /// creates a post report and returns it @@ -39,7 +8,7 @@ impl Reportable for PostReport { /// * `conn` - the postgres connection /// * `post_report_form` - the filled CommentReportForm to insert fn report(conn: &PgConnection, post_report_form: &PostReportForm) -> Result { - use crate::schema::post_report::dsl::*; + use lemmy_db_schema::schema::post_report::dsl::*; insert_into(post_report) .values(post_report_form) .get_result::(conn) @@ -51,7 +20,7 @@ impl Reportable for PostReport { /// * `report_id` - the id of the report to resolve /// * `by_resolver_id` - the id of the user resolving the report fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { - use crate::schema::post_report::dsl::*; + use lemmy_db_schema::schema::post_report::dsl::*; update(post_report.find(report_id)) .set(( resolved.eq(true), @@ -67,7 +36,7 @@ impl Reportable for PostReport { /// * `report_id` - the id of the report to unresolve /// * `by_resolver_id` - the id of the user unresolving the report fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { - use crate::schema::post_report::dsl::*; + use lemmy_db_schema::schema::post_report::dsl::*; update(post_report.find(report_id)) .set(( resolved.eq(false), diff --git a/lemmy_db/src/source/private_message.rs b/lemmy_db/src/source/private_message.rs index f474cf9ac..b49ed8f4f 100644 --- a/lemmy_db/src/source/private_message.rs +++ b/lemmy_db/src/source/private_message.rs @@ -1,44 +1,15 @@ -use crate::{naive_now, schema::private_message, ApubObject, Crud}; +use crate::{ApubObject, Crud}; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "private_message"] -pub struct PrivateMessage { - pub id: i32, - pub creator_id: i32, - pub recipient_id: i32, - pub content: String, - pub deleted: bool, - pub read: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub ap_id: String, - pub local: bool, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "private_message"] -pub struct PrivateMessageForm { - pub creator_id: i32, - pub recipient_id: i32, - pub content: String, - pub deleted: Option, - pub read: Option, - pub published: Option, - pub updated: Option, - pub ap_id: Option, - pub local: bool, -} +use lemmy_db_schema::{naive_now, source::private_message::*}; impl Crud for PrivateMessage { fn read(conn: &PgConnection, private_message_id: i32) -> Result { - use crate::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; private_message.find(private_message_id).first::(conn) } fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result { - use crate::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; insert_into(private_message) .values(private_message_form) .get_result::(conn) @@ -49,7 +20,7 @@ impl Crud for PrivateMessage { private_message_id: i32, private_message_form: &PrivateMessageForm, ) -> Result { - use crate::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update(private_message.find(private_message_id)) .set(private_message_form) .get_result::(conn) @@ -61,14 +32,14 @@ impl ApubObject for PrivateMessage { where Self: Sized, { - use crate::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; private_message .filter(ap_id.eq(object_id)) .first::(conn) } fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result { - use crate::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; insert_into(private_message) .values(private_message_form) .on_conflict(ap_id) @@ -78,54 +49,84 @@ impl ApubObject for PrivateMessage { } } -impl PrivateMessage { - pub fn update_ap_id( +pub trait PrivateMessage_ { + fn update_ap_id( conn: &PgConnection, private_message_id: i32, apub_id: String, - ) -> Result { - use crate::schema::private_message::dsl::*; + ) -> Result; + fn update_content( + conn: &PgConnection, + private_message_id: i32, + new_content: &str, + ) -> Result; + fn update_deleted( + conn: &PgConnection, + private_message_id: i32, + new_deleted: bool, + ) -> Result; + fn update_read( + conn: &PgConnection, + private_message_id: i32, + new_read: bool, + ) -> Result; + fn mark_all_as_read( + conn: &PgConnection, + for_recipient_id: i32, + ) -> Result, Error>; +} + +impl PrivateMessage_ for PrivateMessage { + fn update_ap_id( + conn: &PgConnection, + private_message_id: i32, + apub_id: String, + ) -> Result { + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update(private_message.find(private_message_id)) .set(ap_id.eq(apub_id)) .get_result::(conn) } - pub fn update_content( + fn update_content( conn: &PgConnection, private_message_id: i32, new_content: &str, - ) -> Result { - use crate::schema::private_message::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update(private_message.find(private_message_id)) .set((content.eq(new_content), updated.eq(naive_now()))) .get_result::(conn) } - pub fn update_deleted( + fn update_deleted( conn: &PgConnection, private_message_id: i32, new_deleted: bool, - ) -> Result { - use crate::schema::private_message::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update(private_message.find(private_message_id)) .set(deleted.eq(new_deleted)) .get_result::(conn) } - pub fn update_read( + fn update_read( conn: &PgConnection, private_message_id: i32, new_read: bool, - ) -> Result { - use crate::schema::private_message::dsl::*; + ) -> Result { + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update(private_message.find(private_message_id)) .set(read.eq(new_read)) .get_result::(conn) } - pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result, Error> { - use crate::schema::private_message::dsl::*; + fn mark_all_as_read( + conn: &PgConnection, + for_recipient_id: i32, + ) -> Result, Error> { + use lemmy_db_schema::schema::private_message::dsl::*; diesel::update( private_message .filter(recipient_id.eq(for_recipient_id)) @@ -138,12 +139,8 @@ impl PrivateMessage { #[cfg(test)] mod tests { - use crate::{ - source::{private_message::*, user::*}, - tests::establish_unpooled_connection, - ListingType, - SortType, - }; + use crate::{tests::establish_unpooled_connection, ListingType, SortType}; + use lemmy_db_schema::source::{private_message::*, user::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/source/site.rs b/lemmy_db/src/source/site.rs index 8775191e1..2510f46c9 100644 --- a/lemmy_db/src/source/site.rs +++ b/lemmy_db/src/source/site.rs @@ -1,71 +1,45 @@ -use crate::{naive_now, schema::site, Crud}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)] -#[table_name = "site"] -pub struct Site { - pub id: i32, - pub name: String, - pub description: Option, - pub creator_id: i32, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub enable_downvotes: bool, - pub open_registration: bool, - pub enable_nsfw: bool, - pub icon: Option, - pub banner: Option, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "site"] -pub struct SiteForm { - pub name: String, - pub description: Option, - pub creator_id: i32, - pub updated: Option, - pub enable_downvotes: bool, - pub open_registration: bool, - pub enable_nsfw: bool, - // when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. - pub icon: Option>, - pub banner: Option>, -} +use lemmy_db_schema::{naive_now, source::site::*}; impl Crud for Site { fn read(conn: &PgConnection, _site_id: i32) -> Result { - use crate::schema::site::dsl::*; + use lemmy_db_schema::schema::site::dsl::*; site.first::(conn) } fn create(conn: &PgConnection, new_site: &SiteForm) -> Result { - use crate::schema::site::dsl::*; + use lemmy_db_schema::schema::site::dsl::*; insert_into(site).values(new_site).get_result::(conn) } fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result { - use crate::schema::site::dsl::*; + use lemmy_db_schema::schema::site::dsl::*; diesel::update(site.find(site_id)) .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 --git a/lemmy_db/src/source/user.rs b/lemmy_db/src/source/user.rs index 601e6e8c6..7461f4b45 100644 --- a/lemmy_db/src/source/user.rs +++ b/lemmy_db/src/source/user.rs @@ -1,68 +1,17 @@ -use crate::{ - is_email_regex, - naive_now, - schema::{user_, user_::dsl::*, user_alias_1, user_alias_2}, - ApubObject, - Crud, -}; +use crate::{is_email_regex, ApubObject, Crud}; use bcrypt::{hash, DEFAULT_COST}; use diesel::{dsl::*, result::Error, *}; +use lemmy_db_schema::{ + naive_now, + schema::user_::dsl::*, + source::user::{UserForm, User_}, +}; use lemmy_utils::settings::Settings; -use serde::Serialize; - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_"] -pub struct User_ { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub password_encrypted: String, - pub email: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub show_nsfw: bool, - pub theme: String, - pub default_sort_type: i16, - pub default_listing_type: i16, - pub lang: String, - pub show_avatars: bool, - pub send_notifications_to_email: bool, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: chrono::NaiveDateTime, - pub banner: Option, - pub deleted: bool, -} - -/// A safe representation of user, without the sensitive info -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_"] -pub struct UserSafe { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub banner: Option, - pub deleted: bool, -} mod safe_type { - use crate::{schema::user_::columns::*, source::user::User_, ToSafe}; + use crate::ToSafe; + use lemmy_db_schema::{schema::user_::columns::*, source::user::User_}; + type Columns = ( id, name, @@ -103,58 +52,10 @@ mod safe_type { } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_alias_1"] -pub struct UserAlias1 { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub password_encrypted: String, - pub email: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub show_nsfw: bool, - pub theme: String, - pub default_sort_type: i16, - pub default_listing_type: i16, - pub lang: String, - pub show_avatars: bool, - pub send_notifications_to_email: bool, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: chrono::NaiveDateTime, - pub banner: Option, - pub deleted: bool, -} - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_alias_1"] -pub struct UserSafeAlias1 { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub banner: Option, - pub deleted: bool, -} - mod safe_type_alias_1 { - use crate::{schema::user_alias_1::columns::*, source::user::UserAlias1, ToSafe}; + use crate::ToSafe; + use lemmy_db_schema::{schema::user_alias_1::columns::*, source::user::UserAlias1}; + type Columns = ( id, name, @@ -195,58 +96,10 @@ mod safe_type_alias_1 { } } -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_alias_2"] -pub struct UserAlias2 { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub password_encrypted: String, - pub email: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub show_nsfw: bool, - pub theme: String, - pub default_sort_type: i16, - pub default_listing_type: i16, - pub lang: String, - pub show_avatars: bool, - pub send_notifications_to_email: bool, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: chrono::NaiveDateTime, - pub banner: Option, - pub deleted: bool, -} - -#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] -#[table_name = "user_alias_2"] -pub struct UserSafeAlias2 { - pub id: i32, - pub name: String, - pub preferred_username: Option, - pub avatar: Option, - pub admin: bool, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub matrix_user_id: Option, - pub actor_id: String, - pub bio: Option, - pub local: bool, - pub banner: Option, - pub deleted: bool, -} - mod safe_type_alias_2 { - use crate::{schema::user_alias_2::columns::*, source::user::UserAlias2, ToSafe}; + use crate::ToSafe; + use lemmy_db_schema::{schema::user_alias_2::columns::*, source::user::UserAlias2}; + type Columns = ( id, name, @@ -287,35 +140,6 @@ mod safe_type_alias_2 { } } -#[derive(Insertable, AsChangeset, Clone)] -#[table_name = "user_"] -pub struct UserForm { - pub name: String, - pub preferred_username: Option>, - pub password_encrypted: String, - pub admin: bool, - pub banned: Option, - pub email: Option>, - pub avatar: Option>, - pub published: Option, - pub updated: Option, - pub show_nsfw: bool, - pub theme: String, - pub default_sort_type: i16, - pub default_listing_type: i16, - pub lang: String, - pub show_avatars: bool, - pub send_notifications_to_email: bool, - pub matrix_user_id: Option>, - pub actor_id: Option, - pub bio: Option>, - pub local: bool, - pub private_key: Option, - pub public_key: Option, - pub last_refreshed_at: Option, - pub banner: Option>, -} - impl Crud for User_ { fn read(conn: &PgConnection, user_id: i32) -> Result { user_ @@ -338,7 +162,7 @@ impl Crud for User_ { impl ApubObject for User_ { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::user_::dsl::*; + use lemmy_db_schema::schema::user_::dsl::*; user_ .filter(deleted.eq(false)) .filter(actor_id.eq(object_id)) @@ -355,8 +179,26 @@ impl ApubObject for User_ { } } -impl User_ { - pub fn register(conn: &PgConnection, form: &UserForm) -> Result { +pub trait User { + fn register(conn: &PgConnection, form: &UserForm) -> Result; + fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) + -> Result; + fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result; + fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result; + fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result; + fn find_by_email_or_username( + conn: &PgConnection, + username_or_email: &str, + ) -> Result; + fn find_by_username(conn: &PgConnection, username: &str) -> Result; + fn find_by_email(conn: &PgConnection, from_email: &str) -> Result; + fn get_profile_url(&self, hostname: &str) -> String; + fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result; + fn delete_account(conn: &PgConnection, user_id: i32) -> Result; +} + +impl User for User_ { + fn register(conn: &PgConnection, form: &UserForm) -> Result { let mut edited_user = form.clone(); let password_hash = hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password"); @@ -366,11 +208,7 @@ impl User_ { } // TODO do more individual updates like these - pub fn update_password( - conn: &PgConnection, - user_id: i32, - new_password: &str, - ) -> Result { + fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) -> Result { let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password"); diesel::update(user_.find(user_id)) @@ -381,7 +219,7 @@ impl User_ { .get_result::(conn) } - pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result { + fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result { user_ .filter(local.eq(true)) .filter(deleted.eq(false)) @@ -389,19 +227,19 @@ impl User_ { .first::(conn) } - pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result { + fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result { diesel::update(user_.find(user_id)) .set(admin.eq(added)) .get_result::(conn) } - pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result { + fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result { diesel::update(user_.find(user_id)) .set(banned.eq(ban)) .get_result::(conn) } - pub fn find_by_email_or_username( + fn find_by_email_or_username( conn: &PgConnection, username_or_email: &str, ) -> Result { @@ -412,7 +250,7 @@ impl User_ { } } - pub fn find_by_username(conn: &PgConnection, username: &str) -> Result { + fn find_by_username(conn: &PgConnection, username: &str) -> Result { user_ .filter(deleted.eq(false)) .filter(local.eq(true)) @@ -420,7 +258,7 @@ impl User_ { .first::(conn) } - pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result { + fn find_by_email(conn: &PgConnection, from_email: &str) -> Result { user_ .filter(deleted.eq(false)) .filter(local.eq(true)) @@ -428,7 +266,7 @@ impl User_ { .first::(conn) } - pub fn get_profile_url(&self, hostname: &str) -> String { + fn get_profile_url(&self, hostname: &str) -> String { format!( "{}://{}/u/{}", Settings::get().get_protocol_string(), @@ -437,13 +275,13 @@ impl User_ { ) } - pub fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result { + fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result { diesel::update(user_.find(user_id)) .set((last_refreshed_at.eq(naive_now()),)) .get_result::(conn) } - pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result { + fn delete_account(conn: &PgConnection, user_id: i32) -> Result { diesel::update(user_.find(user_id)) .set(( preferred_username.eq::>(None), diff --git a/lemmy_db/src/source/user_mention.rs b/lemmy_db/src/source/user_mention.rs index bf53cb420..64406f989 100644 --- a/lemmy_db/src/source/user_mention.rs +++ b/lemmy_db/src/source/user_mention.rs @@ -1,35 +1,15 @@ -use super::comment::Comment; -use crate::{schema::user_mention, Crud}; +use crate::Crud; use diesel::{dsl::*, result::Error, *}; -use serde::Serialize; - -#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] -#[belongs_to(Comment)] -#[table_name = "user_mention"] -pub struct UserMention { - pub id: i32, - pub recipient_id: i32, - pub comment_id: i32, - pub read: bool, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset)] -#[table_name = "user_mention"] -pub struct UserMentionForm { - pub recipient_id: i32, - pub comment_id: i32, - pub read: Option, -} +use lemmy_db_schema::source::user_mention::*; impl Crud for UserMention { fn read(conn: &PgConnection, user_mention_id: i32) -> Result { - use crate::schema::user_mention::dsl::*; + use lemmy_db_schema::schema::user_mention::dsl::*; user_mention.find(user_mention_id).first::(conn) } fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result { - use crate::schema::user_mention::dsl::*; + use lemmy_db_schema::schema::user_mention::dsl::*; // since the return here isnt utilized, we dont need to do an update // but get_result doesnt return the existing row here insert_into(user_mention) @@ -45,27 +25,42 @@ impl Crud for UserMention { user_mention_id: i32, user_mention_form: &UserMentionForm, ) -> Result { - use crate::schema::user_mention::dsl::*; + use lemmy_db_schema::schema::user_mention::dsl::*; diesel::update(user_mention.find(user_mention_id)) .set(user_mention_form) .get_result::(conn) } } -impl UserMention { - pub fn update_read( +pub trait UserMention_ { + fn update_read( conn: &PgConnection, user_mention_id: i32, new_read: bool, - ) -> Result { - use crate::schema::user_mention::dsl::*; + ) -> Result; + fn mark_all_as_read( + conn: &PgConnection, + for_recipient_id: i32, + ) -> Result, Error>; +} + +impl UserMention_ for UserMention { + fn update_read( + conn: &PgConnection, + user_mention_id: i32, + new_read: bool, + ) -> Result { + use lemmy_db_schema::schema::user_mention::dsl::*; diesel::update(user_mention.find(user_mention_id)) .set(read.eq(new_read)) .get_result::(conn) } - pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result, Error> { - use crate::schema::user_mention::dsl::*; + fn mark_all_as_read( + conn: &PgConnection, + for_recipient_id: i32, + ) -> Result, Error> { + use lemmy_db_schema::schema::user_mention::dsl::*; diesel::update( user_mention .filter(recipient_id.eq(for_recipient_id)) @@ -78,11 +73,13 @@ impl UserMention { #[cfg(test)] mod tests { - use crate::{ - source::{comment::*, community::*, post::*, user::*, user_mention::*}, - tests::establish_unpooled_connection, - ListingType, - SortType, + use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType}; + use lemmy_db_schema::source::{ + comment::*, + community::{Community, CommunityForm}, + post::*, + user::*, + user_mention::*, }; #[test] diff --git a/lemmy_db/src/views/comment_report_view.rs b/lemmy_db/src/views/comment_report_view.rs index 540bb7569..7a260cd59 100644 --- a/lemmy_db/src/views/comment_report_view.rs +++ b/lemmy_db/src/views/comment_report_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2}, source::{ comment::Comment, @@ -8,11 +9,7 @@ use crate::{ post::Post, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, }, - views::ViewToVec, - MaybeOptional, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] diff --git a/lemmy_db/src/views/comment_view.rs b/lemmy_db/src/views/comment_view.rs index 8c7584471..1b114e190 100644 --- a/lemmy_db/src/views/comment_view.rs +++ b/lemmy_db/src/views/comment_view.rs @@ -3,6 +3,14 @@ use crate::{ functions::hot_rank, fuzzy_search, limit_and_offset, + views::ViewToVec, + ListingType, + MaybeOptional, + SortType, + ToSafe, +}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{ comment, comment_aggregates, @@ -22,13 +30,7 @@ use crate::{ post::Post, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ListingType, - MaybeOptional, - SortType, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] @@ -416,14 +418,8 @@ impl ViewToVec for CommentView { #[cfg(test)] mod tests { - use crate::{ - source::{comment::*, community::*, post::*, user::*}, - tests::establish_unpooled_connection, - views::comment_view::*, - Crud, - Likeable, - *, - }; + use crate::{tests::establish_unpooled_connection, views::comment_view::*, Crud, Likeable, *}; + use lemmy_db_schema::source::{comment::*, community::*, post::*, user::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/views/community/community_follower_view.rs b/lemmy_db/src/views/community/community_follower_view.rs index 7de9cc3a4..e7ba0e4a6 100644 --- a/lemmy_db/src/views/community/community_follower_view.rs +++ b/lemmy_db/src/views/community/community_follower_view.rs @@ -1,13 +1,12 @@ -use crate::{ +use crate::{views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, community_follower, user_}, source::{ community::{Community, CommunitySafe}, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/community/community_moderator_view.rs b/lemmy_db/src/views/community/community_moderator_view.rs index 751e46232..6800853ea 100644 --- a/lemmy_db/src/views/community/community_moderator_view.rs +++ b/lemmy_db/src/views/community/community_moderator_view.rs @@ -1,13 +1,12 @@ -use crate::{ +use crate::{views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, community_moderator, user_}, source::{ community::{Community, CommunitySafe}, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/community/community_user_ban_view.rs b/lemmy_db/src/views/community/community_user_ban_view.rs index 3358f01b2..1c26ebcf1 100644 --- a/lemmy_db/src/views/community/community_user_ban_view.rs +++ b/lemmy_db/src/views/community/community_user_ban_view.rs @@ -1,12 +1,12 @@ -use crate::{ +use crate::ToSafe; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, community_user_ban, user_}, source::{ community::{Community, CommunitySafe}, user::{UserSafe, User_}, }, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/community/community_view.rs b/lemmy_db/src/views/community/community_view.rs index fcc707c06..11962d79a 100644 --- a/lemmy_db/src/views/community/community_view.rs +++ b/lemmy_db/src/views/community/community_view.rs @@ -3,18 +3,20 @@ use crate::{ functions::hot_rank, fuzzy_search, limit_and_offset, - schema::{category, community, community_aggregates, community_follower, user_}, - source::{ - category::Category, - community::{Community, CommunityFollower, CommunitySafe}, - user::{UserSafe, User_}, - }, views::ViewToVec, MaybeOptional, SortType, ToSafe, }; use diesel::{result::Error, *}; +use lemmy_db_schema::{ + schema::{category, community, community_aggregates, community_follower, user_}, + source::{ + category::Category, + community::{Community, CommunityFollower, CommunitySafe}, + user::{UserSafe, User_}, + }, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_add_community_view.rs b/lemmy_db/src/views/moderator/mod_add_community_view.rs index 402c5fe1b..302d37a0b 100644 --- a/lemmy_db/src/views/moderator/mod_add_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_add_community_view.rs @@ -1,15 +1,13 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_add_community, user_, user_alias_1}, source::{ community::{Community, CommunitySafe}, moderator::ModAddCommunity, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_add_view.rs b/lemmy_db/src/views/moderator/mod_add_view.rs index fc1993d45..8f586a6ff 100644 --- a/lemmy_db/src/views/moderator/mod_add_view.rs +++ b/lemmy_db/src/views/moderator/mod_add_view.rs @@ -1,14 +1,12 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{mod_add, user_, user_alias_1}, source::{ moderator::ModAdd, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs b/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs index 6ad232e87..0ed52dd25 100644 --- a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs @@ -1,15 +1,13 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_ban_from_community, user_, user_alias_1}, source::{ community::{Community, CommunitySafe}, moderator::ModBanFromCommunity, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_ban_view.rs b/lemmy_db/src/views/moderator/mod_ban_view.rs index 28214d2da..98cf1969f 100644 --- a/lemmy_db/src/views/moderator/mod_ban_view.rs +++ b/lemmy_db/src/views/moderator/mod_ban_view.rs @@ -1,14 +1,12 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{mod_ban, user_, user_alias_1}, source::{ moderator::ModBan, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] 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 8182b54f7..dbe81c5c3 100644 --- a/lemmy_db/src/views/moderator/mod_lock_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_lock_post_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_lock_post, post, user_}, source::{ community::{Community, CommunitySafe}, @@ -7,10 +8,7 @@ use crate::{ post::Post, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; 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 fb4b77724..04aab30af 100644 --- a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}, source::{ comment::Comment, @@ -8,10 +9,7 @@ use crate::{ post::Post, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/moderator/mod_remove_community_view.rs b/lemmy_db/src/views/moderator/mod_remove_community_view.rs index daaf6d78f..37ffe5402 100644 --- a/lemmy_db/src/views/moderator/mod_remove_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_community_view.rs @@ -1,15 +1,13 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_remove_community, user_}, source::{ community::{Community, CommunitySafe}, moderator::ModRemoveCommunity, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; 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 613a8a541..21bf1b867 100644 --- a/lemmy_db/src/views/moderator/mod_remove_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_post_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_remove_post, post, user_}, source::{ community::{Community, CommunitySafe}, @@ -7,10 +8,7 @@ use crate::{ post::Post, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; 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 9a3d118b9..8512e0793 100644 --- a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, mod_sticky_post, post, user_}, source::{ community::{Community, CommunitySafe}, @@ -7,10 +8,7 @@ use crate::{ post::Post, user::{UserSafe, User_}, }, - views::ViewToVec, - ToSafe, }; -use diesel::{result::Error, *}; 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 d39adfd5c..37d6275cd 100644 --- a/lemmy_db/src/views/post_report_view.rs +++ b/lemmy_db/src/views/post_report_view.rs @@ -1,5 +1,6 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{community, post, post_report, user_, user_alias_1, user_alias_2}, source::{ community::{Community, CommunitySafe}, @@ -7,11 +8,7 @@ use crate::{ post_report::PostReport, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, }, - views::ViewToVec, - MaybeOptional, - ToSafe, }; -use diesel::{result::Error, *}; 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 9a4dbbadb..3cfee1b37 100644 --- a/lemmy_db/src/views/post_view.rs +++ b/lemmy_db/src/views/post_view.rs @@ -3,6 +3,14 @@ use crate::{ functions::hot_rank, fuzzy_search, limit_and_offset, + views::ViewToVec, + ListingType, + MaybeOptional, + SortType, + ToSafe, +}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{ community, community_follower, @@ -19,13 +27,7 @@ use crate::{ post::{Post, PostRead, PostSaved}, user::{UserSafe, User_}, }, - views::ViewToVec, - ListingType, - MaybeOptional, - SortType, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] @@ -406,13 +408,13 @@ impl ViewToVec for PostView { mod tests { use crate::{ aggregates::post_aggregates::PostAggregates, - source::{community::*, post::*, user::*}, tests::establish_unpooled_connection, views::post_view::{PostQueryBuilder, PostView}, Crud, Likeable, *, }; + use lemmy_db_schema::source::{community::*, post::*, user::*}; #[test] fn test_crud() { diff --git a/lemmy_db/src/views/private_message_view.rs b/lemmy_db/src/views/private_message_view.rs index 43d960a8a..f439a75d5 100644 --- a/lemmy_db/src/views/private_message_view.rs +++ b/lemmy_db/src/views/private_message_view.rs @@ -1,15 +1,12 @@ -use crate::{ - limit_and_offset, +use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{private_message, user_, user_alias_1}, source::{ private_message::PrivateMessage, user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, }, - views::ViewToVec, - MaybeOptional, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] diff --git a/lemmy_db/src/views/site_view.rs b/lemmy_db/src/views/site_view.rs index 3c605277e..7772ccdce 100644 --- a/lemmy_db/src/views/site_view.rs +++ b/lemmy_db/src/views/site_view.rs @@ -1,13 +1,12 @@ -use crate::{ - aggregates::site_aggregates::SiteAggregates, +use crate::{aggregates::site_aggregates::SiteAggregates, ToSafe}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{site, site_aggregates, user_}, source::{ site::Site, user::{UserSafe, User_}, }, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db/src/views/user_mention_view.rs b/lemmy_db/src/views/user_mention_view.rs index 67616fbc3..2cd1cd3c8 100644 --- a/lemmy_db/src/views/user_mention_view.rs +++ b/lemmy_db/src/views/user_mention_view.rs @@ -2,6 +2,13 @@ use crate::{ aggregates::comment_aggregates::CommentAggregates, functions::hot_rank, limit_and_offset, + views::ViewToVec, + MaybeOptional, + SortType, + ToSafe, +}; +use diesel::{result::Error, *}; +use lemmy_db_schema::{ schema::{ comment, comment_aggregates, @@ -22,12 +29,7 @@ use crate::{ user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user_mention::UserMention, }, - views::ViewToVec, - MaybeOptional, - SortType, - ToSafe, }; -use diesel::{result::Error, *}; use serde::Serialize; #[derive(Debug, PartialEq, Serialize, Clone)] diff --git a/lemmy_db/src/views/user_view.rs b/lemmy_db/src/views/user_view.rs index 587ebf617..f3109011e 100644 --- a/lemmy_db/src/views/user_view.rs +++ b/lemmy_db/src/views/user_view.rs @@ -2,14 +2,16 @@ use crate::{ aggregates::user_aggregates::UserAggregates, fuzzy_search, limit_and_offset, - schema::{user_, user_aggregates}, - source::user::{UserSafe, User_}, views::ViewToVec, MaybeOptional, SortType, ToSafe, }; use diesel::{dsl::*, result::Error, *}; +use lemmy_db_schema::{ + schema::{user_, user_aggregates}, + source::user::{UserSafe, User_}, +}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] diff --git a/lemmy_db_schema/Cargo.toml b/lemmy_db_schema/Cargo.toml new file mode 100644 index 000000000..99b7399b1 --- /dev/null +++ b/lemmy_db_schema/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "lemmy_db_schema" +version = "0.1.0" +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 new file mode 100644 index 000000000..3868a3b75 --- /dev/null +++ b/lemmy_db_schema/src/lib.rs @@ -0,0 +1,12 @@ +#[macro_use] +extern crate diesel; + +use chrono::NaiveDateTime; + +pub mod schema; +pub mod source; + +// TODO: can probably move this back to lemmy_db +pub fn naive_now() -> NaiveDateTime { + chrono::prelude::Utc::now().naive_utc() +} diff --git a/lemmy_db/src/schema.rs b/lemmy_db_schema/src/schema.rs similarity index 100% rename from lemmy_db/src/schema.rs rename to lemmy_db_schema/src/schema.rs diff --git a/lemmy_db_schema/src/source/activity.rs b/lemmy_db_schema/src/source/activity.rs new file mode 100644 index 000000000..cf81ab8c8 --- /dev/null +++ b/lemmy_db_schema/src/source/activity.rs @@ -0,0 +1,25 @@ +use crate::schema::activity; +use serde_json::Value; +use std::fmt::Debug; + +#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[table_name = "activity"] +pub struct Activity { + pub id: i32, + pub data: Value, + pub local: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub ap_id: Option, + pub sensitive: Option, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "activity"] +pub struct ActivityForm { + pub data: Value, + pub local: bool, + pub updated: Option, + pub ap_id: String, + pub sensitive: bool, +} diff --git a/lemmy_db_schema/src/source/category.rs b/lemmy_db_schema/src/source/category.rs new file mode 100644 index 000000000..ea2ff1238 --- /dev/null +++ b/lemmy_db_schema/src/source/category.rs @@ -0,0 +1,15 @@ +use crate::schema::category; +use serde::Serialize; + +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Clone)] +#[table_name = "category"] +pub struct Category { + pub id: i32, + pub name: String, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "category"] +pub struct CategoryForm { + pub name: String, +} diff --git a/lemmy_db_schema/src/source/comment.rs b/lemmy_db_schema/src/source/comment.rs new file mode 100644 index 000000000..8c553a51a --- /dev/null +++ b/lemmy_db_schema/src/source/comment.rs @@ -0,0 +1,109 @@ +use crate::{ + schema::{comment, comment_alias_1, comment_like, comment_saved}, + source::post::Post, +}; +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())) + } +} + +#[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/comment_report.rs b/lemmy_db_schema/src/source/comment_report.rs new file mode 100644 index 000000000..ec53408d1 --- /dev/null +++ b/lemmy_db_schema/src/source/comment_report.rs @@ -0,0 +1,28 @@ +use crate::{schema::comment_report, source::comment::Comment}; +use serde::{Deserialize, Serialize}; + +#[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 --git a/lemmy_db_schema/src/source/community.rs b/lemmy_db_schema/src/source/community.rs new file mode 100644 index 000000000..af7fce0c9 --- /dev/null +++ b/lemmy_db_schema/src/source/community.rs @@ -0,0 +1,121 @@ +use crate::schema::{community, community_follower, community_moderator, community_user_ban}; +use serde::Serialize; + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "community"] +pub struct Community { + pub id: i32, + pub name: String, + pub title: String, + pub description: Option, + pub category_id: i32, + pub creator_id: i32, + pub removed: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub deleted: bool, + pub nsfw: bool, + pub actor_id: String, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: chrono::NaiveDateTime, + pub icon: Option, + pub banner: Option, +} + +/// A safe representation of community, without the sensitive info +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "community"] +pub struct CommunitySafe { + pub id: i32, + pub name: String, + pub title: String, + pub description: Option, + pub category_id: i32, + pub creator_id: i32, + pub removed: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub deleted: bool, + pub nsfw: bool, + pub actor_id: String, + pub local: bool, + pub icon: Option, + pub banner: Option, +} + +#[derive(Insertable, AsChangeset, Debug)] +#[table_name = "community"] +pub struct CommunityForm { + pub name: String, + pub title: String, + pub description: Option, + pub category_id: i32, + pub creator_id: i32, + pub removed: Option, + pub published: Option, + pub updated: Option, + pub deleted: Option, + pub nsfw: bool, + pub actor_id: Option, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: Option, + pub icon: Option>, + pub banner: Option>, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Community)] +#[table_name = "community_moderator"] +pub struct CommunityModerator { + pub id: i32, + pub community_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "community_moderator"] +pub struct CommunityModeratorForm { + pub community_id: i32, + pub user_id: i32, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Community)] +#[table_name = "community_user_ban"] +pub struct CommunityUserBan { + pub id: i32, + pub community_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "community_user_ban"] +pub struct CommunityUserBanForm { + pub community_id: i32, + pub user_id: i32, +} + +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Community)] +#[table_name = "community_follower"] +pub struct CommunityFollower { + pub id: i32, + pub community_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, + pub pending: Option, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "community_follower"] +pub struct CommunityFollowerForm { + pub community_id: i32, + pub user_id: i32, + pub pending: bool, +} diff --git a/lemmy_db_schema/src/source/mod.rs b/lemmy_db_schema/src/source/mod.rs new file mode 100644 index 000000000..211194a44 --- /dev/null +++ b/lemmy_db_schema/src/source/mod.rs @@ -0,0 +1,13 @@ +pub mod activity; +pub mod category; +pub mod comment; +pub mod comment_report; +pub mod community; +pub mod moderator; +pub mod password_reset_request; +pub mod post; +pub mod post_report; +pub mod private_message; +pub mod site; +pub mod user; +pub mod user_mention; diff --git a/lemmy_db_schema/src/source/moderator.rs b/lemmy_db_schema/src/source/moderator.rs new file mode 100644 index 000000000..d1a5d8308 --- /dev/null +++ b/lemmy_db_schema/src/source/moderator.rs @@ -0,0 +1,194 @@ +use crate::schema::{ + mod_add, + mod_add_community, + mod_ban, + mod_ban_from_community, + mod_lock_post, + mod_remove_comment, + mod_remove_community, + mod_remove_post, + mod_sticky_post, +}; +use serde::Serialize; + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_remove_post"] +pub struct ModRemovePost { + pub id: i32, + pub mod_user_id: i32, + pub post_id: i32, + pub reason: Option, + pub removed: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_remove_post"] +pub struct ModRemovePostForm { + pub mod_user_id: i32, + pub post_id: i32, + pub reason: Option, + pub removed: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_lock_post"] +pub struct ModLockPost { + pub id: i32, + pub mod_user_id: i32, + pub post_id: i32, + pub locked: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_lock_post"] +pub struct ModLockPostForm { + pub mod_user_id: i32, + pub post_id: i32, + pub locked: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_sticky_post"] +pub struct ModStickyPost { + pub id: i32, + pub mod_user_id: i32, + pub post_id: i32, + pub stickied: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_sticky_post"] +pub struct ModStickyPostForm { + pub mod_user_id: i32, + pub post_id: i32, + pub stickied: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_remove_comment"] +pub struct ModRemoveComment { + pub id: i32, + pub mod_user_id: i32, + pub comment_id: i32, + pub reason: Option, + pub removed: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_remove_comment"] +pub struct ModRemoveCommentForm { + pub mod_user_id: i32, + pub comment_id: i32, + pub reason: Option, + pub removed: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_remove_community"] +pub struct ModRemoveCommunity { + pub id: i32, + pub mod_user_id: i32, + pub community_id: i32, + pub reason: Option, + pub removed: Option, + pub expires: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_remove_community"] +pub struct ModRemoveCommunityForm { + pub mod_user_id: i32, + pub community_id: i32, + pub reason: Option, + pub removed: Option, + pub expires: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_ban_from_community"] +pub struct ModBanFromCommunity { + pub id: i32, + pub mod_user_id: i32, + pub other_user_id: i32, + pub community_id: i32, + pub reason: Option, + pub banned: Option, + pub expires: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_ban_from_community"] +pub struct ModBanFromCommunityForm { + pub mod_user_id: i32, + pub other_user_id: i32, + pub community_id: i32, + pub reason: Option, + pub banned: Option, + pub expires: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_ban"] +pub struct ModBan { + pub id: i32, + pub mod_user_id: i32, + pub other_user_id: i32, + pub reason: Option, + pub banned: Option, + pub expires: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_ban"] +pub struct ModBanForm { + pub mod_user_id: i32, + pub other_user_id: i32, + pub reason: Option, + pub banned: Option, + pub expires: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_add_community"] +pub struct ModAddCommunity { + pub id: i32, + pub mod_user_id: i32, + pub other_user_id: i32, + pub community_id: i32, + pub removed: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_add_community"] +pub struct ModAddCommunityForm { + pub mod_user_id: i32, + pub other_user_id: i32, + pub community_id: i32, + pub removed: Option, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "mod_add"] +pub struct ModAdd { + pub id: i32, + pub mod_user_id: i32, + pub other_user_id: i32, + pub removed: Option, + pub when_: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "mod_add"] +pub struct ModAddForm { + pub mod_user_id: i32, + pub other_user_id: i32, + pub removed: Option, +} diff --git a/lemmy_db_schema/src/source/password_reset_request.rs b/lemmy_db_schema/src/source/password_reset_request.rs new file mode 100644 index 000000000..f81f28efe --- /dev/null +++ b/lemmy_db_schema/src/source/password_reset_request.rs @@ -0,0 +1,17 @@ +use crate::schema::password_reset_request; + +#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[table_name = "password_reset_request"] +pub struct PasswordResetRequest { + pub id: i32, + pub user_id: i32, + pub token_encrypted: String, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "password_reset_request"] +pub struct PasswordResetRequestForm { + pub user_id: i32, + pub token_encrypted: String, +} diff --git a/lemmy_db_schema/src/source/post.rs b/lemmy_db_schema/src/source/post.rs new file mode 100644 index 000000000..b0cc78e0e --- /dev/null +++ b/lemmy_db_schema/src/source/post.rs @@ -0,0 +1,113 @@ +use crate::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())) + } +} + +#[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_db_schema/src/source/post_report.rs b/lemmy_db_schema/src/source/post_report.rs new file mode 100644 index 000000000..b75fb954a --- /dev/null +++ b/lemmy_db_schema/src/source/post_report.rs @@ -0,0 +1,32 @@ +use crate::{schema::post_report, source::post::Post}; +use serde::{Deserialize, Serialize}; + +#[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 --git a/lemmy_db_schema/src/source/private_message.rs b/lemmy_db_schema/src/source/private_message.rs new file mode 100644 index 000000000..341040692 --- /dev/null +++ b/lemmy_db_schema/src/source/private_message.rs @@ -0,0 +1,31 @@ +use crate::schema::private_message; +use serde::Serialize; + +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "private_message"] +pub struct PrivateMessage { + pub id: i32, + pub creator_id: i32, + pub recipient_id: i32, + pub content: String, + pub deleted: bool, + pub read: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub ap_id: String, + pub local: bool, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "private_message"] +pub struct PrivateMessageForm { + pub creator_id: i32, + pub recipient_id: i32, + pub content: String, + pub deleted: Option, + pub read: Option, + pub published: Option, + pub updated: Option, + pub ap_id: Option, + pub local: bool, +} diff --git a/lemmy_db_schema/src/source/site.rs b/lemmy_db_schema/src/source/site.rs new file mode 100644 index 000000000..66319548e --- /dev/null +++ b/lemmy_db_schema/src/source/site.rs @@ -0,0 +1,33 @@ +use crate::schema::site; +use serde::Serialize; + +#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)] +#[table_name = "site"] +pub struct Site { + pub id: i32, + pub name: String, + pub description: Option, + pub creator_id: i32, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub enable_downvotes: bool, + pub open_registration: bool, + pub enable_nsfw: bool, + pub icon: Option, + pub banner: Option, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "site"] +pub struct SiteForm { + pub name: String, + pub description: Option, + pub creator_id: i32, + pub updated: Option, + pub enable_downvotes: bool, + pub open_registration: bool, + pub enable_nsfw: bool, + // when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. + pub icon: Option>, + pub banner: Option>, +} diff --git a/lemmy_db_schema/src/source/user.rs b/lemmy_db_schema/src/source/user.rs new file mode 100644 index 000000000..3d9d9e500 --- /dev/null +++ b/lemmy_db_schema/src/source/user.rs @@ -0,0 +1,182 @@ +use crate::schema::{user_, user_alias_1, user_alias_2}; +use serde::Serialize; + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_"] +pub struct User_ { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub password_encrypted: String, + pub email: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub show_nsfw: bool, + pub theme: String, + pub default_sort_type: i16, + pub default_listing_type: i16, + pub lang: String, + pub show_avatars: bool, + pub send_notifications_to_email: bool, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: chrono::NaiveDateTime, + pub banner: Option, + pub deleted: bool, +} + +/// A safe representation of user, without the sensitive info +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_"] +pub struct UserSafe { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub banner: Option, + pub deleted: bool, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_alias_1"] +pub struct UserAlias1 { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub password_encrypted: String, + pub email: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub show_nsfw: bool, + pub theme: String, + pub default_sort_type: i16, + pub default_listing_type: i16, + pub lang: String, + pub show_avatars: bool, + pub send_notifications_to_email: bool, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: chrono::NaiveDateTime, + pub banner: Option, + pub deleted: bool, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_alias_1"] +pub struct UserSafeAlias1 { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub banner: Option, + pub deleted: bool, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_alias_2"] +pub struct UserAlias2 { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub password_encrypted: String, + pub email: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub show_nsfw: bool, + pub theme: String, + pub default_sort_type: i16, + pub default_listing_type: i16, + pub lang: String, + pub show_avatars: bool, + pub send_notifications_to_email: bool, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: chrono::NaiveDateTime, + pub banner: Option, + pub deleted: bool, +} + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_alias_2"] +pub struct UserSafeAlias2 { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub banner: Option, + pub deleted: bool, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name = "user_"] +pub struct UserForm { + pub name: String, + pub preferred_username: Option>, + pub password_encrypted: String, + pub admin: bool, + pub banned: Option, + pub email: Option>, + pub avatar: Option>, + pub published: Option, + pub updated: Option, + pub show_nsfw: bool, + pub theme: String, + pub default_sort_type: i16, + pub default_listing_type: i16, + pub lang: String, + pub show_avatars: bool, + pub send_notifications_to_email: bool, + pub matrix_user_id: Option>, + pub actor_id: Option, + pub bio: Option>, + pub local: bool, + pub private_key: Option, + pub public_key: Option, + pub last_refreshed_at: Option, + pub banner: Option>, +} diff --git a/lemmy_db_schema/src/source/user_mention.rs b/lemmy_db_schema/src/source/user_mention.rs new file mode 100644 index 000000000..64fd56006 --- /dev/null +++ b/lemmy_db_schema/src/source/user_mention.rs @@ -0,0 +1,21 @@ +use crate::{schema::user_mention, source::comment::Comment}; +use serde::Serialize; + +#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] +#[belongs_to(Comment)] +#[table_name = "user_mention"] +pub struct UserMention { + pub id: i32, + pub recipient_id: i32, + pub comment_id: i32, + pub read: bool, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset)] +#[table_name = "user_mention"] +pub struct UserMentionForm { + pub recipient_id: i32, + pub comment_id: i32, + pub read: Option, +} diff --git a/lemmy_structs/Cargo.toml b/lemmy_structs/Cargo.toml index e14623064..329ef4139 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 d19127384..080cb3852 100644 --- a/lemmy_structs/src/lib.rs +++ b/lemmy_structs/src/lib.rs @@ -5,15 +5,12 @@ pub mod site; pub mod user; 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 --git a/lemmy_structs/src/site.rs b/lemmy_structs/src/site.rs index ff6c8a391..f24d9f49e 100644 --- a/lemmy_structs/src/site.rs +++ b/lemmy_structs/src/site.rs @@ -1,24 +1,22 @@ -use lemmy_db::{ - source::{category::*, user::*}, - 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)] diff --git a/lemmy_websocket/Cargo.toml b/lemmy_websocket/Cargo.toml index ed0ba4ce0..30dbe1fbd 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 d95dfd57f..0762b9485 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 af01982fc..73b030cb2 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -4,6 +4,10 @@ use diesel::{ *, }; use lemmy_db::{ + source::{comment::Comment_, post::Post_, private_message::PrivateMessage_}, + Crud, +}; +use lemmy_db_schema::{ naive_now, source::{ comment::Comment, @@ -12,7 +16,6 @@ use lemmy_db::{ private_message::PrivateMessage, user::{UserForm, User_}, }, - Crud, }; use lemmy_utils::{ apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, @@ -33,7 +36,7 @@ pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> { } fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::user_::dsl::*; + use lemmy_db_schema::schema::user_::dsl::*; info!("Running user_updates_2020_04_02"); @@ -82,7 +85,7 @@ fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> { } fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::community::dsl::*; + use lemmy_db_schema::schema::community::dsl::*; info!("Running community_updates_2020_04_02"); @@ -124,7 +127,7 @@ fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> { } fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; info!("Running post_updates_2020_04_03"); @@ -145,7 +148,7 @@ fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> { } fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::comment::dsl::*; + use lemmy_db_schema::schema::comment::dsl::*; info!("Running comment_updates_2020_04_03"); @@ -166,7 +169,7 @@ fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> { } fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::private_message::dsl::*; + use lemmy_db_schema::schema::private_message::dsl::*; info!("Running private_message_updates_2020_05_05"); @@ -187,7 +190,7 @@ fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyEr } fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> { - use lemmy_db::schema::post::dsl::*; + use lemmy_db_schema::schema::post::dsl::*; info!("Running post_thumbnail_url_updates_2020_07_27"); diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index 7a4801f40..fc397fff0 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -4,7 +4,7 @@ use chrono::{DateTime, NaiveDateTime, Utc}; use diesel::PgConnection; use lemmy_api::claims::Claims; use lemmy_db::{ - source::{community::Community, user::User_}, + source::{community::Community_, user::User}, views::{ comment_view::{CommentQueryBuilder, CommentView}, post_view::{PostQueryBuilder, PostView}, @@ -14,6 +14,7 @@ use lemmy_db::{ ListingType, SortType, }; +use lemmy_db_schema::source::{community::Community, user::User_}; use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/src/routes/webfinger.rs b/src/routes/webfinger.rs index d59b4e389..2a03f5e2a 100644 --- a/src/routes/webfinger.rs +++ b/src/routes/webfinger.rs @@ -1,6 +1,7 @@ use actix_web::{error::ErrorBadRequest, web::Query, *}; use anyhow::anyhow; -use lemmy_db::source::{community::Community, user::User_}; +use lemmy_db::source::{community::Community_, user::User}; +use lemmy_db_schema::source::{community::Community, user::User_}; use lemmy_structs::{blocking, WebFingerLink, WebFingerResponse}; use lemmy_utils::{ settings::Settings, diff --git a/tests/integration_test.rs b/tests/integration_test.rs index c89864544..c507af067 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -28,14 +28,10 @@ use lemmy_apub::{ user_inbox::user_inbox, }, }; -use lemmy_db::{ - source::{ - community::{Community, CommunityForm}, - user::{User_, *}, - }, - Crud, - ListingType, - SortType, +use lemmy_db::{Crud, ListingType, SortType}; +use lemmy_db_schema::source::{ + community::{Community, CommunityForm}, + user::{UserForm, User_}, }; use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit}; use lemmy_utils::{apub::generate_actor_keypair, settings::Settings};