Merge remote-tracking branch 'origin/split-db-workspace' into move_views_to_diesel_split

This commit is contained in:
Dessalines 2020-12-21 09:28:20 -05:00
commit 1a0d1f64f0
109 changed files with 1841 additions and 1637 deletions

18
Cargo.lock generated
View file

@ -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",

View file

@ -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" }

View file

@ -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" }

View file

@ -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};

View file

@ -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},

View file

@ -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},

View file

@ -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};

View file

@ -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},

View file

@ -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,

View file

@ -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},

View file

@ -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"

View file

@ -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};

View file

@ -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};

View file

@ -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};

View file

@ -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;

View file

@ -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};

View file

@ -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};

View file

@ -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};

View file

@ -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},

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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};

View file

@ -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};

View file

@ -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,

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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};

View file

@ -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;

View file

@ -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::{

View file

@ -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::{

View file

@ -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::{

View file

@ -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};

View file

@ -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::{

View file

@ -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"

View file

@ -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)]

View file

@ -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() {

View file

@ -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() {

View file

@ -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() {

View file

@ -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() {

View file

@ -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<i64>, limit: Option<i64>) -> (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)
}

View file

@ -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<chrono::NaiveDateTime>,
pub ap_id: Option<String>,
pub sensitive: Option<bool>,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "activity"]
pub struct ActivityForm {
pub data: Value,
pub local: bool,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: String,
pub sensitive: bool,
}
impl Crud<ActivityForm> for Activity {
fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
use crate::schema::activity::dsl::*;
use lemmy_db_schema::schema::activity::dsl::*;
activity.find(activity_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
use crate::schema::activity::dsl::*;
use lemmy_db_schema::schema::activity::dsl::*;
insert_into(activity)
.values(new_activity)
.get_result::<Self>(conn)
@ -48,25 +26,38 @@ impl Crud<ActivityForm> for Activity {
activity_id: i32,
new_activity: &ActivityForm,
) -> Result<Self, Error> {
use crate::schema::activity::dsl::*;
use lemmy_db_schema::schema::activity::dsl::*;
diesel::update(activity.find(activity_id))
.set(new_activity)
.get_result::<Self>(conn)
}
fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
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<T>(
pub trait Activity_ {
fn insert<T>(
conn: &PgConnection,
ap_id: String,
data: &T,
local: bool,
sensitive: bool,
) -> Result<Self, IoError>
) -> Result<Activity, IoError>
where
T: Serialize + Debug;
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error>;
}
impl Activity_ for Activity {
fn insert<T>(
conn: &PgConnection,
ap_id: String,
data: &T,
local: bool,
sensitive: bool,
) -> Result<Activity, IoError>
where
T: Serialize + Debug,
{
@ -88,23 +79,18 @@ impl Activity {
}
}
pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::activity::dsl::*;
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error> {
use lemmy_db_schema::schema::activity::dsl::*;
activity.filter(ap_id.eq(object_id)).first::<Self>(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;

View file

@ -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<CategoryForm> for Category {
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
@ -40,15 +24,20 @@ impl Crud<CategoryForm> for Category {
}
}
impl Category {
pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
pub trait Category_ {
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error>;
}
impl Category_ for Category {
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error> {
category.load::<Self>(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() {

View file

@ -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<i32>,
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<chrono::NaiveDateTime>,
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<Comment, Error>;
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: i32,
) -> Result<Vec<Comment>, Error>;
fn update_deleted(
conn: &PgConnection,
comment_id: i32,
new_deleted: bool,
) -> Result<Comment, Error>;
fn update_removed(
conn: &PgConnection,
comment_id: i32,
new_removed: bool,
) -> Result<Comment, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Comment>, Error>;
fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Comment, Error>;
fn update_content(
conn: &PgConnection,
comment_id: i32,
new_content: &str,
) -> Result<Comment, Error>;
}
#[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<i32>,
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<chrono::NaiveDateTime>,
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<Self, Error> {
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<i32>,
pub content: String,
pub removed: Option<bool>,
pub read: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub ap_id: Option<String>,
pub local: bool,
}
diesel::update(comment.find(comment_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
impl CommentForm {
pub fn get_ap_id(&self) -> Result<Url, ParseError> {
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<Vec<Self>, 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::<Self>(conn)
}
fn update_deleted(
conn: &PgConnection,
comment_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
fn update_removed(
conn: &PgConnection,
comment_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Self>, 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::<Self>(conn)
}
fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(read.eq(new_read))
.get_result::<Self>(conn)
}
fn update_content(
conn: &PgConnection,
comment_id: i32,
new_content: &str,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((content.eq(new_content), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
}
impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
use lemmy_db_schema::schema::comment::dsl::*;
comment.find(comment_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
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<Self, Error> {
use crate::schema::comment::dsl::*;
use lemmy_db_schema::schema::comment::dsl::*;
insert_into(comment)
.values(comment_form)
.get_result::<Self>(conn)
@ -99,7 +136,7 @@ impl Crud<CommentForm> for Comment {
comment_id: i32,
comment_form: &CommentForm,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(comment_form)
.get_result::<Self>(conn)
@ -108,12 +145,12 @@ impl Crud<CommentForm> for Comment {
impl ApubObject<CommentForm> for Comment {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
use lemmy_db_schema::schema::comment::dsl::*;
comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
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<CommentForm> for Comment {
}
}
impl Comment {
pub fn update_ap_id(
conn: &PgConnection,
comment_id: i32,
apub_id: String,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
pub fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: i32,
) -> Result<Vec<Self>, 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::<Self>(conn)
}
pub fn update_deleted(
conn: &PgConnection,
comment_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed(
conn: &PgConnection,
comment_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Self>, 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::<Self>(conn)
}
pub fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(read.eq(new_read))
.get_result::<Self>(conn)
}
pub fn update_content(
conn: &PgConnection,
comment_id: i32,
new_content: &str,
) -> Result<Self, Error> {
use crate::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set((content.eq(new_content), updated.eq(naive_now())))
.get_result::<Self>(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<CommentLikeForm> for CommentLike {
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
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<CommentLikeForm> for CommentLike {
.get_result::<Self>(conn)
}
fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
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<CommentLikeForm> 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<CommentSavedForm> for CommentSaved {
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
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<CommentSavedForm> for CommentSaved {
.get_result::<Self>(conn)
}
fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
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<CommentSavedForm> 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() {

View file

@ -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<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
}
#[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<CommentReportForm> for CommentReport {
/// creates a comment report and returns it
@ -35,7 +11,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `conn` - the postgres connection
/// * `comment_report_form` - the filled CommentReportForm to insert
fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
use crate::schema::comment_report::dsl::*;
use lemmy_db_schema::schema::comment_report::dsl::*;
insert_into(comment_report)
.values(comment_report_form)
.get_result::<Self>(conn)
@ -47,7 +23,7 @@ impl Reportable<CommentReportForm> 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<usize, Error> {
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<CommentReportForm> 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<usize, Error> {
use crate::schema::comment_report::dsl::*;
use lemmy_db_schema::schema::comment_report::dsl::*;
update(comment_report.find(report_id))
.set((
resolved.eq(false),

View file

@ -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<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub actor_id: String,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub icon: Option<String>,
pub banner: Option<String>,
}
/// 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<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub actor_id: String,
pub local: bool,
pub icon: Option<String>,
pub banner: Option<String>,
}
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<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
pub actor_id: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
pub icon: Option<Option<String>>,
pub banner: Option<Option<String>>,
}
impl Crud<CommunityForm> for Community {
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
use lemmy_db_schema::schema::community::dsl::*;
community.find(community_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
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<Self, Error> {
use crate::schema::community::dsl::*;
use lemmy_db_schema::schema::community::dsl::*;
insert_into(community)
.values(new_community)
.get_result::<Self>(conn)
@ -144,7 +90,7 @@ impl Crud<CommunityForm> for Community {
community_id: i32,
new_community: &CommunityForm,
) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id))
.set(new_community)
.get_result::<Self>(conn)
@ -153,14 +99,14 @@ impl Crud<CommunityForm> for Community {
impl ApubObject<CommunityForm> for Community {
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
use lemmy_db_schema::schema::community::dsl::*;
community
.filter(actor_id.eq(for_actor_id))
.first::<Self>(conn)
}
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
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<CommunityForm> for Community {
}
}
impl Community {
pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
pub trait Community_ {
fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
fn update_deleted(
conn: &PgConnection,
community_id: i32,
new_deleted: bool,
) -> Result<Community, Error>;
fn update_removed(
conn: &PgConnection,
community_id: i32,
new_removed: bool,
) -> Result<Community, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Community>, Error>;
fn update_creator(
conn: &PgConnection,
community_id: i32,
new_creator_id: i32,
) -> Result<Community, Error>;
fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error>;
fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, 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<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community
.filter(local.eq(true))
.filter(name.eq(community_name))
.first::<Self>(conn)
}
pub fn update_deleted(
fn update_deleted(
conn: &PgConnection,
community_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed(
fn update_removed(
conn: &PgConnection,
community_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed_for_creator(
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
use crate::schema::community::dsl::*;
) -> Result<Vec<Community>, 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::<Self>(conn)
}
pub fn update_creator(
fn update_creator(
conn: &PgConnection,
community_id: i32,
new_creator_id: i32,
) -> Result<Self, Error> {
use crate::schema::community::dsl::*;
) -> Result<Community, Error> {
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::<Self>(conn)
@ -234,41 +207,24 @@ impl Community {
Ok(mods_and_admins)
}
pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
use crate::schema::community::dsl::*;
fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community.select(actor_id).distinct().load::<String>(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<CommunityModeratorForm> for CommunityModerator {
fn join(
conn: &PgConnection,
community_user_form: &CommunityModeratorForm,
) -> Result<Self, Error> {
use crate::schema::community_moderator::dsl::*;
use lemmy_db_schema::schema::community_moderator::dsl::*;
insert_into(community_moderator)
.values(community_user_form)
.get_result::<Self>(conn)
@ -278,7 +234,7 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
conn: &PgConnection,
community_user_form: &CommunityModeratorForm,
) -> Result<usize, Error> {
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<CommunityModeratorForm> for CommunityModerator {
}
}
impl CommunityModerator {
pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*;
pub trait CommunityModerator_ {
fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
fn get_user_moderated_communities(
conn: &PgConnection,
for_user_id: i32,
) -> Result<Vec<i32>, Error>;
}
impl CommunityModerator_ for CommunityModerator {
fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
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<Vec<i32>, 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<CommunityUserBanForm> for CommunityUserBan {
fn ban(
conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm,
) -> Result<Self, Error> {
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::<Self>(conn)
@ -338,7 +285,7 @@ impl Bannable<CommunityUserBanForm> for CommunityUserBan {
conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm,
) -> Result<usize, Error> {
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<CommunityUserBanForm> 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<bool>,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "community_follower"]
pub struct CommunityFollowerForm {
pub community_id: i32,
pub user_id: i32,
pub pending: bool,
}
impl Followable<CommunityFollowerForm> for CommunityFollower {
fn follow(
conn: &PgConnection,
community_follower_form: &CommunityFollowerForm,
) -> Result<Self, Error> {
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<CommunityFollowerForm> 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<CommunityFollowerForm> for CommunityFollower {
conn: &PgConnection,
community_follower_form: &CommunityFollowerForm,
) -> Result<usize, Error> {
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<CommunityFollowerForm> 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<bool, Error> {
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<CommunityFollowerForm> 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() {

View file

@ -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<String>,
pub removed: Option<bool>,
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<String>,
pub removed: Option<bool>,
}
use lemmy_db_schema::source::moderator::*;
impl Crud<ModRemovePostForm> for ModRemovePost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*;
use lemmy_db_schema::schema::mod_remove_post::dsl::*;
mod_remove_post.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
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::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
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::<Self>(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<bool>,
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<bool>,
}
impl Crud<ModLockPostForm> for ModLockPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*;
use lemmy_db_schema::schema::mod_lock_post::dsl::*;
mod_lock_post.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
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::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
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::<Self>(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<bool>,
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<bool>,
}
impl Crud<ModStickyPostForm> for ModStickyPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_sticky_post::dsl::*;
use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
mod_sticky_post.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
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::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
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::<Self>(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<String>,
pub removed: Option<bool>,
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<String>,
pub removed: Option<bool>,
}
impl Crud<ModRemoveCommentForm> for ModRemoveComment {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*;
use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
mod_remove_comment.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
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::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
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::<Self>(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<String>,
pub removed: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub removed: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*;
use lemmy_db_schema::schema::mod_remove_community::dsl::*;
mod_remove_community.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
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::<Self>(conn)
@ -215,45 +104,21 @@ impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
from_id: i32,
form: &ModRemoveCommunityForm,
) -> Result<Self, Error> {
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::<Self>(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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
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::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
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::<Self>(conn)
@ -264,126 +129,66 @@ impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
from_id: i32,
form: &ModBanFromCommunityForm,
) -> Result<Self, Error> {
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::<Self>(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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
impl Crud<ModBanForm> for ModBan {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*;
use lemmy_db_schema::schema::mod_ban::dsl::*;
mod_ban.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*;
use lemmy_db_schema::schema::mod_ban::dsl::*;
insert_into(mod_ban).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
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::<Self>(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<bool>,
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<bool>,
}
impl Crud<ModAddCommunityForm> for ModAddCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*;
use lemmy_db_schema::schema::mod_add_community::dsl::*;
mod_add_community.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
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::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
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::<Self>(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<bool>,
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<bool>,
}
impl Crud<ModAddForm> for ModAdd {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*;
use lemmy_db_schema::schema::mod_add::dsl::*;
mod_add.find(from_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*;
use lemmy_db_schema::schema::mod_add::dsl::*;
insert_into(mod_add).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
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::<Self>(conn)
@ -392,12 +197,8 @@ impl Crud<ModAddForm> 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]

View file

@ -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<PasswordResetRequestForm> for PasswordResetRequest {
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
use crate::schema::password_reset_request::dsl::*;
password_reset_request
.find(password_reset_request_id)
.first::<Self>(conn)
@ -44,11 +25,24 @@ impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
}
}
impl PasswordResetRequest {
pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> {
pub trait PasswordResetRequest_ {
fn create_token(
conn: &PgConnection,
from_user_id: i32,
token: &str,
) -> Result<PasswordResetRequest, Error>;
fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error>;
}
impl PasswordResetRequest_ for PasswordResetRequest {
fn create_token(
conn: &PgConnection,
from_user_id: i32,
token: &str,
) -> Result<PasswordResetRequest, Error> {
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<Self, Error> {
fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error> {
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::<Self>(conn)
}
}
fn bytes_to_hex(bytes: Vec<u8>) -> String {
let mut str = String::new();
for byte in bytes {
str = format!("{}{:02x}", str, byte);
}
str
fn bytes_to_hex(bytes: Vec<u8>) -> 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() {

View file

@ -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<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: bool,
pub locked: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub stickied: bool,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: String,
pub local: bool,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "post"]
pub struct PostForm {
pub name: String,
pub url: Option<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: Option<bool>,
pub locked: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
pub stickied: Option<bool>,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: Option<String>,
pub local: bool,
}
impl PostForm {
pub fn get_ap_id(&self) -> Result<Url, ParseError> {
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<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
use lemmy_db_schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
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<Self, Error> {
use crate::schema::post::dsl::*;
use lemmy_db_schema::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(conn)
}
}
impl ApubObject<PostForm> for Post {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
use crate::schema::post::dsl::*;
insert_into(post)
.values(post_form)
.on_conflict(ap_id)
.do_update()
.set(post_form)
.get_result::<Self>(conn)
}
pub trait Post_ {
//fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Post>, Error>;
fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Post, Error>;
fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Post>, Error>;
fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Post, Error>;
fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Post, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Post>, Error>;
fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Post, Error>;
fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Post, Error>;
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<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
impl Post_ for Post {
fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Self>, 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::<Self>(conn)
}
pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
pub fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: i32,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, 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::<Self>(conn)
}
pub fn update_deleted(
conn: &PgConnection,
post_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed(
conn: &PgConnection,
post_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed_for_creator(
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Self>, 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::<Self>(conn)
}
pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(locked.eq(new_locked))
.get_result::<Self>(conn)
}
pub fn update_stickied(
conn: &PgConnection,
post_id: i32,
new_stickied: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied))
.get_result::<Self>(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<PostForm> for Post {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(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<Post, Error> {
use lemmy_db_schema::schema::post::dsl::*;
insert_into(post)
.values(post_form)
.on_conflict(ap_id)
.do_update()
.set(post_form)
.get_result::<Self>(conn)
}
}
impl Likeable<PostLikeForm> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
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<PostLikeForm> for PostLike {
.get_result::<Self>(conn)
}
fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
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<PostLikeForm> 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<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
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<PostSavedForm> for PostSaved {
.get_result::<Self>(conn)
}
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
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<PostSavedForm> 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<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use crate::schema::post_read::dsl::*;
use lemmy_db_schema::schema::post_read::dsl::*;
insert_into(post_read)
.values(post_read_form)
.get_result::<Self>(conn)
}
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
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<PostReadForm> 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]

View file

@ -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<String>,
pub original_post_body: Option<String>,
pub reason: String,
pub resolved: bool,
pub resolver_id: Option<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
}
#[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<String>,
pub original_post_body: Option<String>,
pub reason: String,
}
use lemmy_db_schema::{naive_now, source::post_report::*};
impl Reportable<PostReportForm> for PostReport {
/// creates a post report and returns it
@ -39,7 +8,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `conn` - the postgres connection
/// * `post_report_form` - the filled CommentReportForm to insert
fn report(conn: &PgConnection, post_report_form: &PostReportForm) -> Result<Self, Error> {
use crate::schema::post_report::dsl::*;
use lemmy_db_schema::schema::post_report::dsl::*;
insert_into(post_report)
.values(post_report_form)
.get_result::<Self>(conn)
@ -51,7 +20,7 @@ impl Reportable<PostReportForm> 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<usize, Error> {
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<PostReportForm> 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<usize, Error> {
use crate::schema::post_report::dsl::*;
use lemmy_db_schema::schema::post_report::dsl::*;
update(post_report.find(report_id))
.set((
resolved.eq(false),

View file

@ -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<chrono::NaiveDateTime>,
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<bool>,
pub read: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: Option<String>,
pub local: bool,
}
use lemmy_db_schema::{naive_now, source::private_message::*};
impl Crud<PrivateMessageForm> for PrivateMessage {
fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*;
use lemmy_db_schema::schema::private_message::dsl::*;
private_message.find(private_message_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*;
use lemmy_db_schema::schema::private_message::dsl::*;
insert_into(private_message)
.values(private_message_form)
.get_result::<Self>(conn)
@ -49,7 +20,7 @@ impl Crud<PrivateMessageForm> for PrivateMessage {
private_message_id: i32,
private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> {
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::<Self>(conn)
@ -61,14 +32,14 @@ impl ApubObject<PrivateMessageForm> 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::<Self>(conn)
}
fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
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<PrivateMessageForm> 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<Self, Error> {
use crate::schema::private_message::dsl::*;
) -> Result<PrivateMessage, Error>;
fn update_content(
conn: &PgConnection,
private_message_id: i32,
new_content: &str,
) -> Result<PrivateMessage, Error>;
fn update_deleted(
conn: &PgConnection,
private_message_id: i32,
new_deleted: bool,
) -> Result<PrivateMessage, Error>;
fn update_read(
conn: &PgConnection,
private_message_id: i32,
new_read: bool,
) -> Result<PrivateMessage, Error>;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<PrivateMessage>, Error>;
}
impl PrivateMessage_ for PrivateMessage {
fn update_ap_id(
conn: &PgConnection,
private_message_id: i32,
apub_id: String,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
pub fn update_content(
fn update_content(
conn: &PgConnection,
private_message_id: i32,
new_content: &str,
) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*;
) -> Result<PrivateMessage, Error> {
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::<Self>(conn)
}
pub fn update_deleted(
fn update_deleted(
conn: &PgConnection,
private_message_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*;
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id))
.set(deleted.eq(new_deleted))
.get_result::<Self>(conn)
}
pub fn update_read(
fn update_read(
conn: &PgConnection,
private_message_id: i32,
new_read: bool,
) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*;
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id))
.set(read.eq(new_read))
.get_result::<Self>(conn)
}
pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
use crate::schema::private_message::dsl::*;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<PrivateMessage>, 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() {

View file

@ -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<String>,
pub creator_id: i32,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub enable_downvotes: bool,
pub open_registration: bool,
pub enable_nsfw: bool,
pub icon: Option<String>,
pub banner: Option<String>,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "site"]
pub struct SiteForm {
pub name: String,
pub description: Option<String>,
pub creator_id: i32,
pub updated: Option<chrono::NaiveDateTime>,
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<Option<String>>,
pub banner: Option<Option<String>>,
}
use lemmy_db_schema::{naive_now, source::site::*};
impl Crud<SiteForm> for Site {
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
use lemmy_db_schema::schema::site::dsl::*;
site.first::<Self>(conn)
}
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
use lemmy_db_schema::schema::site::dsl::*;
insert_into(site).values(new_site).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
use lemmy_db_schema::schema::site::dsl::*;
diesel::update(site.find(site_id))
.set(new_site)
.get_result::<Self>(conn)
}
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
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<Self, Error> {
use crate::schema::site::dsl::*;
pub trait Site_ {
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error>;
fn read_simple(conn: &PgConnection) -> Result<Site, Error>;
}
impl Site_ for Site {
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error> {
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::<Self>(conn)
}
pub fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
use lemmy_db_schema::schema::site::dsl::*;
site.first::<Self>(conn)
}
}

View file

@ -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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
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<Option<String>>,
pub password_encrypted: String,
pub admin: bool,
pub banned: Option<bool>,
pub email: Option<Option<String>>,
pub avatar: Option<Option<String>>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
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<Option<String>>,
pub actor_id: Option<String>,
pub bio: Option<Option<String>>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
pub banner: Option<Option<String>>,
}
impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_
@ -338,7 +162,7 @@ impl Crud<UserForm> for User_ {
impl ApubObject<UserForm> for User_ {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
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<UserForm> for User_ {
}
}
impl User_ {
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
pub trait User {
fn register(conn: &PgConnection, form: &UserForm) -> Result<User_, Error>;
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str)
-> Result<User_, Error>;
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<User_, Error>;
fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<User_, Error>;
fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<User_, Error>;
fn find_by_email_or_username(
conn: &PgConnection,
username_or_email: &str,
) -> Result<User_, Error>;
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error>;
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error>;
fn get_profile_url(&self, hostname: &str) -> String;
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
}
impl User for User_ {
fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
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<Self, Error> {
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) -> Result<Self, Error> {
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::<Self>(conn)
}
pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
user_
.filter(local.eq(true))
.filter(deleted.eq(false))
@ -389,19 +227,19 @@ impl User_ {
.first::<Self>(conn)
}
pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
.set(banned.eq(ban))
.get_result::<Self>(conn)
}
pub fn find_by_email_or_username(
fn find_by_email_or_username(
conn: &PgConnection,
username_or_email: &str,
) -> Result<Self, Error> {
@ -412,7 +250,7 @@ impl User_ {
}
}
pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
user_
.filter(deleted.eq(false))
.filter(local.eq(true))
@ -420,7 +258,7 @@ impl User_ {
.first::<User_>(conn)
}
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
user_
.filter(deleted.eq(false))
.filter(local.eq(true))
@ -428,7 +266,7 @@ impl User_ {
.first::<User_>(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<User_, Error> {
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
diesel::update(user_.find(user_id))
.set((last_refreshed_at.eq(naive_now()),))
.get_result::<Self>(conn)
}
pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
diesel::update(user_.find(user_id))
.set((
preferred_username.eq::<Option<String>>(None),

View file

@ -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<bool>,
}
use lemmy_db_schema::source::user_mention::*;
impl Crud<UserMentionForm> for UserMention {
fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*;
use lemmy_db_schema::schema::user_mention::dsl::*;
user_mention.find(user_mention_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
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<UserMentionForm> for UserMention {
user_mention_id: i32,
user_mention_form: &UserMentionForm,
) -> Result<Self, Error> {
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::<Self>(conn)
}
}
impl UserMention {
pub fn update_read(
pub trait UserMention_ {
fn update_read(
conn: &PgConnection,
user_mention_id: i32,
new_read: bool,
) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*;
) -> Result<UserMention, Error>;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<UserMention>, Error>;
}
impl UserMention_ for UserMention {
fn update_read(
conn: &PgConnection,
user_mention_id: i32,
new_read: bool,
) -> Result<UserMention, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
diesel::update(user_mention.find(user_mention_id))
.set(read.eq(new_read))
.get_result::<Self>(conn)
}
pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> {
use crate::schema::user_mention::dsl::*;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<UserMention>, 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]

View file

@ -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)]

View file

@ -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() {

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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() {

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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)]

View file

@ -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"] }

View file

@ -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()
}

View file

@ -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<chrono::NaiveDateTime>,
pub ap_id: Option<String>,
pub sensitive: Option<bool>,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "activity"]
pub struct ActivityForm {
pub data: Value,
pub local: bool,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: String,
pub sensitive: bool,
}

View file

@ -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,
}

View file

@ -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<i32>,
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<chrono::NaiveDateTime>,
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<i32>,
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<chrono::NaiveDateTime>,
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<i32>,
pub content: String,
pub removed: Option<bool>,
pub read: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub ap_id: Option<String>,
pub local: bool,
}
impl CommentForm {
pub fn get_ap_id(&self) -> Result<Url, ParseError> {
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,
}

View file

@ -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<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
}
#[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,
}

View file

@ -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<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub actor_id: String,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub icon: Option<String>,
pub banner: Option<String>,
}
/// 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<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub actor_id: String,
pub local: bool,
pub icon: Option<String>,
pub banner: Option<String>,
}
#[derive(Insertable, AsChangeset, Debug)]
#[table_name = "community"]
pub struct CommunityForm {
pub name: String,
pub title: String,
pub description: Option<String>,
pub category_id: i32,
pub creator_id: i32,
pub removed: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
pub actor_id: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
pub icon: Option<Option<String>>,
pub banner: Option<Option<String>>,
}
#[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<bool>,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "community_follower"]
pub struct CommunityFollowerForm {
pub community_id: i32,
pub user_id: i32,
pub pending: bool,
}

View file

@ -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;

View file

@ -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<String>,
pub removed: Option<bool>,
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<String>,
pub removed: Option<bool>,
}
#[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<bool>,
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<bool>,
}
#[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<bool>,
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<bool>,
}
#[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<String>,
pub removed: Option<bool>,
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<String>,
pub removed: Option<bool>,
}
#[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<String>,
pub removed: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub removed: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
#[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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
#[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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
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<String>,
pub banned: Option<bool>,
pub expires: Option<chrono::NaiveDateTime>,
}
#[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<bool>,
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<bool>,
}
#[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<bool>,
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<bool>,
}

View file

@ -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,
}

View file

@ -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<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: bool,
pub locked: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub stickied: bool,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: String,
pub local: bool,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "post"]
pub struct PostForm {
pub name: String,
pub url: Option<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: Option<bool>,
pub locked: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
pub stickied: Option<bool>,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: Option<String>,
pub local: bool,
}
impl PostForm {
pub fn get_ap_id(&self) -> Result<Url, ParseError> {
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,
}

View file

@ -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<String>,
pub original_post_body: Option<String>,
pub reason: String,
pub resolved: bool,
pub resolver_id: Option<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
}
#[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<String>,
pub original_post_body: Option<String>,
pub reason: String,
}

View file

@ -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<chrono::NaiveDateTime>,
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<bool>,
pub read: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: Option<String>,
pub local: bool,
}

View file

@ -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<String>,
pub creator_id: i32,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub enable_downvotes: bool,
pub open_registration: bool,
pub enable_nsfw: bool,
pub icon: Option<String>,
pub banner: Option<String>,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "site"]
pub struct SiteForm {
pub name: String,
pub description: Option<String>,
pub creator_id: i32,
pub updated: Option<chrono::NaiveDateTime>,
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<Option<String>>,
pub banner: Option<Option<String>>,
}

View file

@ -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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
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<String>,
pub password_encrypted: String,
pub email: Option<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
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<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<String>,
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<String>,
pub avatar: Option<String>,
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub matrix_user_id: Option<String>,
pub actor_id: String,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<String>,
pub deleted: bool,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "user_"]
pub struct UserForm {
pub name: String,
pub preferred_username: Option<Option<String>>,
pub password_encrypted: String,
pub admin: bool,
pub banned: Option<bool>,
pub email: Option<Option<String>>,
pub avatar: Option<Option<String>>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
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<Option<String>>,
pub actor_id: Option<String>,
pub bio: Option<Option<String>>,
pub local: bool,
pub private_key: Option<String>,
pub public_key: Option<String>,
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
pub banner: Option<Option<String>>,
}

View file

@ -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<bool>,
}

Some files were not shown because too many files have changed in this diff Show more