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", "lazy_static",
"lemmy_apub", "lemmy_apub",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_structs", "lemmy_structs",
"lemmy_utils", "lemmy_utils",
@ -1762,6 +1763,7 @@ dependencies = [
"itertools", "itertools",
"lazy_static", "lazy_static",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_structs", "lemmy_structs",
"lemmy_utils", "lemmy_utils",
"lemmy_websocket", "lemmy_websocket",
@ -1790,6 +1792,7 @@ dependencies = [
"diesel", "diesel",
"diesel_migrations", "diesel_migrations",
"lazy_static", "lazy_static",
"lemmy_db_schema",
"lemmy_utils", "lemmy_utils",
"log", "log",
"regex", "regex",
@ -1801,6 +1804,18 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "lemmy_db_schema"
version = "0.1.0"
dependencies = [
"chrono",
"diesel",
"log",
"serde 1.0.118",
"serde_json",
"url",
]
[[package]] [[package]]
name = "lemmy_rate_limit" name = "lemmy_rate_limit"
version = "0.1.0" version = "0.1.0"
@ -1836,6 +1851,7 @@ dependencies = [
"lemmy_api", "lemmy_api",
"lemmy_apub", "lemmy_apub",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_structs", "lemmy_structs",
"lemmy_utils", "lemmy_utils",
@ -1860,6 +1876,7 @@ dependencies = [
"chrono", "chrono",
"diesel", "diesel",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_utils", "lemmy_utils",
"log", "log",
"serde 1.0.118", "serde 1.0.118",
@ -1901,6 +1918,7 @@ dependencies = [
"chrono", "chrono",
"diesel", "diesel",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_structs", "lemmy_structs",
"lemmy_utils", "lemmy_utils",

View file

@ -12,6 +12,7 @@ members = [
"lemmy_apub", "lemmy_apub",
"lemmy_utils", "lemmy_utils",
"lemmy_db", "lemmy_db",
"lemmy_db_schema",
"lemmy_structs", "lemmy_structs",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_websocket", "lemmy_websocket",
@ -21,6 +22,7 @@ members = [
lemmy_api = { path = "./lemmy_api" } lemmy_api = { path = "./lemmy_api" }
lemmy_apub = { path = "./lemmy_apub" } lemmy_apub = { path = "./lemmy_apub" }
lemmy_utils = { path = "./lemmy_utils" } lemmy_utils = { path = "./lemmy_utils" }
lemmy_db_schema = { path = "./lemmy_db_schema" }
lemmy_db = { path = "./lemmy_db" } lemmy_db = { path = "./lemmy_db" }
lemmy_structs = { path = "./lemmy_structs" } lemmy_structs = { path = "./lemmy_structs" }
lemmy_rate_limit = { path = "./lemmy_rate_limit" } lemmy_rate_limit = { path = "./lemmy_rate_limit" }

View file

@ -12,6 +12,7 @@ path = "src/lib.rs"
lemmy_apub = { path = "../lemmy_apub" } lemmy_apub = { path = "../lemmy_apub" }
lemmy_utils = { path = "../lemmy_utils" } lemmy_utils = { path = "../lemmy_utils" }
lemmy_db = { path = "../lemmy_db" } lemmy_db = { path = "../lemmy_db" }
lemmy_db_schema = { path = "../lemmy_db_schema" }
lemmy_structs = { path = "../lemmy_structs" } lemmy_structs = { path = "../lemmy_structs" }
lemmy_rate_limit = { path = "../lemmy_rate_limit" } lemmy_rate_limit = { path = "../lemmy_rate_limit" }
lemmy_websocket = { path = "../lemmy_websocket" } lemmy_websocket = { path = "../lemmy_websocket" }

View file

@ -1,5 +1,5 @@
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation}; 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 lemmy_utils::settings::Settings;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View file

@ -11,11 +11,7 @@ use crate::{
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{ use lemmy_db::{
source::{ source::comment::Comment_,
comment::{Comment, CommentForm, CommentLike, CommentLikeForm, CommentSaved, CommentSavedForm},
comment_report::{CommentReport, CommentReportForm},
moderator::{ModRemoveComment, ModRemoveCommentForm},
},
views::{ views::{
comment_report_view::{CommentReportQueryBuilder, CommentReportView}, comment_report_view::{CommentReportQueryBuilder, CommentReportView},
comment_view::{CommentQueryBuilder, CommentView}, comment_view::{CommentQueryBuilder, CommentView},
@ -27,6 +23,7 @@ use lemmy_db::{
Saveable, Saveable,
SortType, SortType,
}; };
use lemmy_db_schema::source::{comment::*, comment_report::*, moderator::*};
use lemmy_structs::{blocking, comment::*, send_local_notifs}; use lemmy_structs::{blocking, comment::*, send_local_notifs};
use lemmy_utils::{ use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType}, apub::{make_apub_endpoint, EndpointType},

View file

@ -11,8 +11,11 @@ use anyhow::Context;
use lemmy_apub::ActorType; use lemmy_apub::ActorType;
use lemmy_db::{ use lemmy_db::{
diesel_option_overwrite, diesel_option_overwrite,
naive_now, source::{
source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, comment::Comment_,
community::{CommunityModerator_, Community_},
post::Post_,
},
views::{ views::{
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,
community::{ community::{
@ -29,6 +32,10 @@ use lemmy_db::{
Joinable, Joinable,
SortType, SortType,
}; };
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
};
use lemmy_structs::{blocking, community::*}; use lemmy_structs::{blocking, community::*};
use lemmy_utils::{ use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, 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 actix_web::{web, web::Data};
use lemmy_db::{ use lemmy_db::{
source::{ source::{
community::{Community, CommunityModerator}, community::{CommunityModerator_, Community_},
post::Post, site::Site_,
site::Site,
user::User_,
}, },
views::community::community_user_ban_view::CommunityUserBanView, views::community::community_user_ban_view::CommunityUserBanView,
Crud, Crud,
DbPool, 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_structs::{blocking, comment::*, community::*, post::*, site::*, user::*};
use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError}; use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError};
use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation}; use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};

View file

@ -11,12 +11,7 @@ use crate::{
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{ use lemmy_db::{
naive_now, source::post::Post_,
source::{
moderator::*,
post::*,
post_report::{PostReport, PostReportForm},
},
views::{ views::{
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,
community::community_moderator_view::CommunityModeratorView, community::community_moderator_view::CommunityModeratorView,
@ -30,6 +25,14 @@ use lemmy_db::{
Saveable, Saveable,
SortType, SortType,
}; };
use lemmy_db_schema::{
naive_now,
source::{
moderator::*,
post::*,
post_report::{PostReport, PostReportForm},
},
};
use lemmy_structs::{blocking, post::*}; use lemmy_structs::{blocking, post::*};
use lemmy_utils::{ use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType}, apub::{make_apub_endpoint, EndpointType},

View file

@ -11,8 +11,7 @@ use anyhow::Context;
use lemmy_apub::fetcher::search_by_apub_id; use lemmy_apub::fetcher::search_by_apub_id;
use lemmy_db::{ use lemmy_db::{
diesel_option_overwrite, diesel_option_overwrite,
naive_now, source::{category::Category_, site::Site_},
source::{category::*, moderator::*, site::*},
views::{ views::{
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,
community::community_view::CommunityQueryBuilder, community::community_view::CommunityQueryBuilder,
@ -35,6 +34,14 @@ use lemmy_db::{
SearchType, SearchType,
SortType, SortType,
}; };
use lemmy_db_schema::{
naive_now,
source::{
category::Category,
moderator::*,
site::{Site, *},
},
};
use lemmy_structs::{blocking, site::*, user::Register}; use lemmy_structs::{blocking, site::*, user::Register};
use lemmy_utils::{ use lemmy_utils::{
location_info, location_info,

View file

@ -16,17 +16,15 @@ use chrono::Duration;
use lemmy_apub::ApubObjectType; use lemmy_apub::ApubObjectType;
use lemmy_db::{ use lemmy_db::{
diesel_option_overwrite, diesel_option_overwrite,
naive_now,
source::{ source::{
comment::*, comment::Comment_,
community::*, community::Community_,
moderator::*, password_reset_request::PasswordResetRequest_,
password_reset_request::*, post::Post_,
post::*, private_message::PrivateMessage_,
private_message::*, site::Site_,
site::*, user::User,
user::*, user_mention::UserMention_,
user_mention::*,
}, },
views::{ views::{
comment_report_view::CommentReportView, comment_report_view::CommentReportView,
@ -47,6 +45,20 @@ use lemmy_db::{
ListingType, ListingType,
SortType, 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_structs::{blocking, send_email_to_user, user::*};
use lemmy_utils::{ use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType}, apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},

View file

@ -11,6 +11,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
lemmy_utils = { path = "../lemmy_utils" } lemmy_utils = { path = "../lemmy_utils" }
lemmy_db = { path = "../lemmy_db" } lemmy_db = { path = "../lemmy_db" }
lemmy_db_schema = { path = "../lemmy_db_schema" }
lemmy_structs = { path = "../lemmy_structs" } lemmy_structs = { path = "../lemmy_structs" }
lemmy_websocket = { path = "../lemmy_websocket" } lemmy_websocket = { path = "../lemmy_websocket" }
diesel = "1.4.5" diesel = "1.4.5"

View file

@ -4,14 +4,10 @@ use activitystreams::{
base::ExtendsExt, base::ExtendsExt,
}; };
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{source::comment::Comment_, views::comment_view::CommentView, Crud, Likeable};
source::{ use lemmy_db_schema::source::{
comment::{Comment, CommentLike, CommentLikeForm}, comment::{Comment, CommentLike, CommentLikeForm},
post::Post, post::Post,
},
views::comment_view::CommentView,
Crud,
Likeable,
}; };
use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs}; use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError}; 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 crate::activities::receive::get_actor_as_user;
use activitystreams::activity::{Dislike, Like}; use activitystreams::activity::{Dislike, Like};
use lemmy_db::{ use lemmy_db::{source::comment::Comment_, views::comment_view::CommentView, Likeable};
source::comment::{Comment, CommentLike}, use lemmy_db_schema::source::comment::{Comment, CommentLike};
views::comment_view::CommentView,
Likeable,
};
use lemmy_structs::{blocking, comment::CommentResponse}; use lemmy_structs::{blocking, comment::CommentResponse};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};

View file

@ -5,10 +5,11 @@ use activitystreams::{
}; };
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{
source::community::Community, source::community::Community_,
views::community::community_view::CommunityView, views::community::community_view::CommunityView,
ApubObject, ApubObject,
}; };
use lemmy_db_schema::source::community::Community;
use lemmy_structs::{blocking, community::CommunityResponse}; use lemmy_structs::{blocking, community::CommunityResponse};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};

View file

@ -5,7 +5,7 @@ use activitystreams::{
error::DomainError, error::DomainError,
}; };
use anyhow::{anyhow, Context}; 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_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;

View file

@ -4,11 +4,8 @@ use activitystreams::{
prelude::*, prelude::*,
}; };
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{source::post::Post_, views::post_view::PostView, Likeable};
source::post::{Post, PostLike, PostLikeForm}, use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm};
views::post_view::PostView,
Likeable,
};
use lemmy_structs::{blocking, post::PostResponse}; use lemmy_structs::{blocking, post::PostResponse};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};

View file

@ -1,10 +1,7 @@
use crate::activities::receive::get_actor_as_user; use crate::activities::receive::get_actor_as_user;
use activitystreams::activity::{Dislike, Like}; use activitystreams::activity::{Dislike, Like};
use lemmy_db::{ use lemmy_db::{source::post::Post_, views::post_view::PostView, Likeable};
source::post::{Post, PostLike}, use lemmy_db_schema::source::post::{Post, PostLike};
views::post_view::PostView,
Likeable,
};
use lemmy_structs::{blocking, post::PostResponse}; use lemmy_structs::{blocking, post::PostResponse};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};

View file

@ -14,9 +14,10 @@ use activitystreams::{
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{
source::private_message::PrivateMessage, source::private_message::PrivateMessage_,
views::private_message_view::PrivateMessageView, views::private_message_view::PrivateMessageView,
}; };
use lemmy_db_schema::source::private_message::PrivateMessage;
use lemmy_structs::{blocking, user::PrivateMessageResponse}; use lemmy_structs::{blocking, user::PrivateMessageResponse};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};

View file

@ -26,11 +26,8 @@ use activitystreams::{
}; };
use anyhow::anyhow; use anyhow::anyhow;
use itertools::Itertools; use itertools::Itertools;
use lemmy_db::{ use lemmy_db::{Crud, DbPool};
source::{comment::Comment, community::Community, post::Post, user::User_}, use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post, user::User_};
Crud,
DbPool,
};
use lemmy_structs::{blocking, WebFingerResponse}; use lemmy_structs::{blocking, WebFingerResponse};
use lemmy_utils::{ use lemmy_utils::{
request::{retry, RecvError}, request::{retry, RecvError},

View file

@ -23,11 +23,8 @@ use activitystreams::{
}; };
use anyhow::Context; use anyhow::Context;
use itertools::Itertools; use itertools::Itertools;
use lemmy_db::{ use lemmy_db::{views::community::community_follower_view::CommunityFollowerView, DbPool};
source::community::Community, use lemmy_db_schema::source::community::Community;
views::community::community_follower_view::CommunityFollowerView,
DbPool,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -21,10 +21,8 @@ use activitystreams::{
prelude::*, prelude::*,
public, public,
}; };
use lemmy_db::{ use lemmy_db::Crud;
source::{community::Community, post::Post, user::User_}, use lemmy_db_schema::source::{community::Community, post::Post, user::User_};
Crud,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -16,10 +16,8 @@ use activitystreams::{
}, },
prelude::*, prelude::*,
}; };
use lemmy_db::{ use lemmy_db::Crud;
source::{private_message::PrivateMessage, user::User_}, use lemmy_db_schema::source::{private_message::PrivateMessage, user::User_};
Crud,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -13,14 +13,10 @@ use activitystreams::{
base::{AnyBase, BaseExt, ExtendsExt}, base::{AnyBase, BaseExt, ExtendsExt},
object::ObjectExt, object::ObjectExt,
}; };
use lemmy_db::{ use lemmy_db::{ApubObject, DbPool, Followable};
source::{ use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm}, community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_, user::User_,
},
ApubObject,
DbPool,
Followable,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;

View file

@ -19,10 +19,8 @@ use background_jobs::{
WorkerConfig, WorkerConfig,
}; };
use itertools::Itertools; use itertools::Itertools;
use lemmy_db::{ use lemmy_db::DbPool;
source::{community::Community, user::User_}, use lemmy_db_schema::source::{community::Community, user::User_};
DbPool,
};
use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::{debug, warn}; use log::{debug, warn};

View file

@ -1,7 +1,8 @@
use activitystreams::unparsed::UnparsedMutExt; use activitystreams::unparsed::UnparsedMutExt;
use activitystreams_ext::UnparsedExtension; use activitystreams_ext::UnparsedExtension;
use diesel::PgConnection; 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 lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View file

@ -13,13 +13,7 @@ use anyhow::{anyhow, Context};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::result::Error::NotFound; use diesel::result::Error::NotFound;
use lemmy_db::{ use lemmy_db::{
naive_now, source::user::User,
source::{
comment::Comment,
community::{Community, CommunityModerator, CommunityModeratorForm},
post::Post,
user::User_,
},
views::{ views::{
comment_view::CommentView, comment_view::CommentView,
community::community_view::CommunityView, community::community_view::CommunityView,
@ -31,6 +25,15 @@ use lemmy_db::{
Joinable, Joinable,
SearchType, 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_structs::{blocking, site::SearchResponse};
use lemmy_utils::{ use lemmy_utils::{
location_info, location_info,

View file

@ -4,7 +4,8 @@ use crate::{
}; };
use actix_web::{body::Body, web, web::Path, HttpResponse}; use actix_web::{body::Body, web, web::Path, HttpResponse};
use diesel::result::Error::NotFound; 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_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -10,9 +10,10 @@ use activitystreams::{
}; };
use actix_web::{body::Body, web, HttpResponse}; use actix_web::{body::Body, web, HttpResponse};
use lemmy_db::{ use lemmy_db::{
source::{community::Community, post::Post}, source::{community::Community_, post::Post_},
views::community::community_follower_view::CommunityFollowerView, views::community::community_follower_view::CommunityFollowerView,
}; };
use lemmy_db_schema::source::{community::Community, post::Post};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -1,6 +1,7 @@
use crate::APUB_JSON_CONTENT_TYPE; use crate::APUB_JSON_CONTENT_TYPE;
use actix_web::{body::Body, web, HttpResponse}; 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_structs::blocking;
use lemmy_utils::{settings::Settings, LemmyError}; use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -4,7 +4,8 @@ use crate::{
}; };
use actix_web::{body::Body, web, HttpResponse}; use actix_web::{body::Body, web, HttpResponse};
use diesel::result::Error::NotFound; 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_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -9,7 +9,8 @@ use activitystreams::{
collection::{CollectionExt, OrderedCollection}, collection::{CollectionExt, OrderedCollection},
}; };
use actix_web::{body::Body, web, HttpResponse}; 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_structs::blocking;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -27,15 +27,16 @@ use activitystreams::{
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{
source::{ source::community::Community_,
community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_,
},
views::community::community_user_ban_view::CommunityUserBanView, views::community::community_user_ban_view::CommunityUserBanView,
ApubObject, ApubObject,
DbPool, DbPool,
Followable, Followable,
}; };
use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -12,11 +12,8 @@ use activitystreams::{
}; };
use actix_web::HttpRequest; use actix_web::HttpRequest;
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{source::activity::Activity_, ApubObject, DbPool};
source::{activity::Activity, community::Community, user::User_}, use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_};
ApubObject,
DbPool,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -41,11 +41,8 @@ use activitystreams::{
}; };
use anyhow::Context; use anyhow::Context;
use diesel::result::Error::NotFound; use diesel::result::Error::NotFound;
use lemmy_db::{ use lemmy_db::{ApubObject, Crud};
source::{comment::Comment, post::Post, site::Site}, use lemmy_db_schema::source::{comment::Comment, post::Post, site::Site};
ApubObject,
Crud,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -15,7 +15,8 @@ use crate::{
use activitystreams::{activity::ActorAndObject, prelude::*}; use activitystreams::{activity::ActorAndObject, prelude::*};
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context; 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_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -48,14 +48,11 @@ use activitystreams::{
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use diesel::NotFound; use diesel::NotFound;
use lemmy_db::{ use lemmy_db::{source::user::User, ApubObject, Followable};
source::{ use lemmy_db_schema::source::{
community::{Community, CommunityFollower}, community::{Community, CommunityFollower},
private_message::PrivateMessage, private_message::PrivateMessage,
user::User_, user::User_,
},
ApubObject,
Followable,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};

View file

@ -22,10 +22,8 @@ use activitystreams::{
}; };
use activitystreams_ext::{Ext1, Ext2}; use activitystreams_ext::{Ext1, Ext2};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{source::activity::Activity_, DbPool};
source::{activity::Activity, user::User_}, use lemmy_db_schema::source::{activity::Activity, user::User_};
DbPool,
};
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, LemmyError}; use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;

View file

@ -23,15 +23,12 @@ use activitystreams::{
prelude::*, prelude::*,
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{Crud, DbPool};
source::{ use lemmy_db_schema::source::{
comment::{Comment, CommentForm}, comment::{Comment, CommentForm},
community::Community, community::Community,
post::Post, post::Post,
user::User_, user::User_,
},
Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -22,11 +22,10 @@ use activitystreams::{
}; };
use activitystreams_ext::Ext2; use activitystreams_ext::Ext2;
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{views::community::community_moderator_view::CommunityModeratorView, DbPool};
use lemmy_db_schema::{
naive_now, naive_now,
source::community::{Community, CommunityForm}, source::community::{Community, CommunityForm},
views::community::community_moderator_view::CommunityModeratorView,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -20,14 +20,11 @@ use activitystreams::{
}; };
use activitystreams_ext::Ext1; use activitystreams_ext::Ext1;
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{Crud, DbPool};
source::{ use lemmy_db_schema::source::{
community::Community, community::Community,
post::{Post, PostForm}, post::{Post, PostForm},
user::User_, user::User_,
},
Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -19,13 +19,10 @@ use activitystreams::{
prelude::*, prelude::*,
}; };
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{Crud, DbPool};
source::{ use lemmy_db_schema::source::{
private_message::{PrivateMessage, PrivateMessageForm}, private_message::{PrivateMessage, PrivateMessageForm},
user::User_, user::User_,
},
Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};

View file

@ -18,11 +18,10 @@ use activitystreams::{
}; };
use activitystreams_ext::Ext1; use activitystreams_ext::Ext1;
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{ApubObject, DbPool};
use lemmy_db_schema::{
naive_now, naive_now,
source::user::{UserForm, User_}, source::user::{UserForm, User_},
ApubObject,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -9,6 +9,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
lemmy_utils = { path = "../lemmy_utils" } lemmy_utils = { path = "../lemmy_utils" }
lemmy_db_schema = { path = "../lemmy_db_schema" }
diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] }
diesel_migrations = "1.4.0" diesel_migrations = "1.4.0"
chrono = { version = "0.4.19", features = ["serde"] } chrono = { version = "0.4.19", features = ["serde"] }
@ -18,7 +19,7 @@ strum = "0.20.0"
strum_macros = "0.20.1" strum_macros = "0.20.1"
log = "0.4.11" log = "0.4.11"
sha2 = "0.9.2" sha2 = "0.9.2"
bcrypt = "0.9.0"
url = { version = "2.2.0", features = ["serde"] } url = { version = "2.2.0", features = ["serde"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.4.2" regex = "1.4.2"
bcrypt = "0.9.0"

View file

@ -1,5 +1,5 @@
use crate::schema::comment_aggregates;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use lemmy_db_schema::schema::comment_aggregates;
use serde::Serialize; use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]

View file

@ -1,5 +1,5 @@
use crate::schema::community_aggregates;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use lemmy_db_schema::schema::community_aggregates;
use serde::Serialize; use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
@ -24,18 +24,18 @@ impl CommunityAggregates {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::community_aggregates::CommunityAggregates, aggregates::community_aggregates::CommunityAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
post::{Post, PostForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
Crud, Crud,
Followable, Followable,
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
post::{Post, PostForm},
user::{UserForm, User_},
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,5 +1,5 @@
use crate::schema::post_aggregates;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use lemmy_db_schema::schema::post_aggregates;
use serde::Serialize; use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
@ -26,18 +26,18 @@ impl PostAggregates {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::post_aggregates::PostAggregates, aggregates::post_aggregates::PostAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
Crud, Crud,
Likeable, Likeable,
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,5 +1,5 @@
use crate::schema::site_aggregates;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use lemmy_db_schema::schema::site_aggregates;
use serde::Serialize; use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
@ -23,18 +23,17 @@ impl SiteAggregates {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::site_aggregates::SiteAggregates, aggregates::site_aggregates::SiteAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm},
site::{Site, SiteForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
Crud, Crud,
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm},
user::{UserForm, User_},
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,5 +1,5 @@
use crate::schema::user_aggregates;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use lemmy_db_schema::schema::user_aggregates;
use serde::Serialize; use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
@ -25,18 +25,18 @@ impl UserAggregates {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::user_aggregates::UserAggregates, 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, tests::establish_unpooled_connection,
Crud, Crud,
Likeable, Likeable,
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_schema::source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
community::{Community, CommunityForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -9,14 +9,12 @@ extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
use chrono::NaiveDateTime;
use diesel::{result::Error, *}; use diesel::{result::Error, *};
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{env, env::VarError}; use std::{env, env::VarError};
pub mod aggregates; pub mod aggregates;
pub mod schema;
pub mod source; pub mod source;
pub mod views; pub mod views;
@ -186,10 +184,6 @@ pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
(limit, offset) (limit, offset)
} }
pub fn naive_now() -> NaiveDateTime {
chrono::prelude::Utc::now().naive_utc()
}
pub fn is_email_regex(test: &str) -> bool { pub fn is_email_regex(test: &str) -> bool {
EMAIL_REGEX.is_match(test) 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 diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::source::activity::*;
use log::debug; use log::debug;
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use std::{ use std::{
fmt::Debug, fmt::Debug,
io::{Error as IoError, ErrorKind}, 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 { impl Crud<ActivityForm> for Activity {
fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> { 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) activity.find(activity_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> { 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) insert_into(activity)
.values(new_activity) .values(new_activity)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -48,25 +26,38 @@ impl Crud<ActivityForm> for Activity {
activity_id: i32, activity_id: i32,
new_activity: &ActivityForm, new_activity: &ActivityForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::activity::dsl::*; use lemmy_db_schema::schema::activity::dsl::*;
diesel::update(activity.find(activity_id)) diesel::update(activity.find(activity_id))
.set(new_activity) .set(new_activity)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> { 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) diesel::delete(activity.find(activity_id)).execute(conn)
} }
} }
impl Activity { pub trait Activity_ {
pub fn insert<T>( fn insert<T>(
conn: &PgConnection, conn: &PgConnection,
ap_id: String, ap_id: String,
data: &T, data: &T,
local: bool, local: bool,
sensitive: 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 where
T: Serialize + Debug, T: Serialize + Debug,
{ {
@ -88,23 +79,18 @@ impl Activity {
} }
} }
pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error> {
use crate::schema::activity::dsl::*; use lemmy_db_schema::schema::activity::dsl::*;
activity.filter(ap_id.eq(object_id)).first::<Self>(conn) activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType};
source::{ use lemmy_db_schema::source::{
activity::{Activity, ActivityForm}, activity::{Activity, ActivityForm},
user::{UserForm, User_}, user::{UserForm, User_},
},
tests::establish_unpooled_connection,
Crud,
ListingType,
SortType,
}; };
use serde_json::Value; use serde_json::Value;

View file

@ -1,22 +1,6 @@
use crate::{ use crate::Crud;
schema::{category, category::dsl::*},
Crud,
};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{schema::category::dsl::*, source::category::*};
#[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,
}
impl Crud<CategoryForm> for Category { impl Crud<CategoryForm> for Category {
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
@ -40,15 +24,20 @@ impl Crud<CategoryForm> for Category {
} }
} }
impl Category { pub trait Category_ {
pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> { 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) category.load::<Self>(conn)
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { 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] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,94 +1,131 @@
use super::post::Post; use crate::{ApubObject, Crud, Likeable, Saveable};
use crate::{
naive_now,
schema::{comment, comment_alias_1, comment_like, comment_saved},
ApubObject,
Crud,
Likeable,
Saveable,
};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{
use url::{ParseError, Url}; naive_now,
source::comment::{
Comment,
CommentForm,
CommentLike,
CommentLikeForm,
CommentSaved,
CommentSavedForm,
},
};
// WITH RECURSIVE MyTree AS ( pub trait Comment_ {
// SELECT * FROM comment WHERE parent_id IS NULL fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Comment, Error>;
// UNION ALL fn permadelete_for_creator(
// SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id conn: &PgConnection,
// ) for_creator_id: i32,
// SELECT * FROM MyTree; ) -> Result<Vec<Comment>, Error>;
fn update_deleted(
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] conn: &PgConnection,
#[belongs_to(Post)] comment_id: i32,
#[table_name = "comment"] new_deleted: bool,
pub struct Comment { ) -> Result<Comment, Error>;
pub id: i32, fn update_removed(
pub creator_id: i32, conn: &PgConnection,
pub post_id: i32, comment_id: i32,
pub parent_id: Option<i32>, new_removed: bool,
pub content: String, ) -> Result<Comment, Error>;
pub removed: bool, fn update_removed_for_creator(
pub read: bool, // Whether the recipient has read the comment or not conn: &PgConnection,
pub published: chrono::NaiveDateTime, for_creator_id: i32,
pub updated: Option<chrono::NaiveDateTime>, new_removed: bool,
pub deleted: bool, ) -> Result<Vec<Comment>, Error>;
pub ap_id: String, fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Comment, Error>;
pub local: bool, fn update_content(
conn: &PgConnection,
comment_id: i32,
new_content: &str,
) -> Result<Comment, Error>;
} }
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] impl Comment_ for Comment {
#[belongs_to(Post)] fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Self, Error> {
#[table_name = "comment_alias_1"] use lemmy_db_schema::schema::comment::dsl::*;
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)] diesel::update(comment.find(comment_id))
#[table_name = "comment"] .set(ap_id.eq(apub_id))
pub struct CommentForm { .get_result::<Self>(conn)
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 { fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
pub fn get_ap_id(&self) -> Result<Url, ParseError> { use lemmy_db_schema::schema::comment::dsl::*;
Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) 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 { impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> { 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) comment.find(comment_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> { 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) diesel::delete(comment.find(comment_id)).execute(conn)
} }
fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> { 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) insert_into(comment)
.values(comment_form) .values(comment_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -99,7 +136,7 @@ impl Crud<CommentForm> for Comment {
comment_id: i32, comment_id: i32,
comment_form: &CommentForm, comment_form: &CommentForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::comment::dsl::*; use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id)) diesel::update(comment.find(comment_id))
.set(comment_form) .set(comment_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -108,12 +145,12 @@ impl Crud<CommentForm> for Comment {
impl ApubObject<CommentForm> for Comment { impl ApubObject<CommentForm> for Comment {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> { 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) comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
} }
fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> { 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) insert_into(comment)
.values(comment_form) .values(comment_form)
.on_conflict(ap_id) .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 { impl Likeable<CommentLikeForm> for CommentLike {
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> { 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) insert_into(comment_like)
.values(comment_like_form) .values(comment_like_form)
.on_conflict((comment_id, user_id)) .on_conflict((comment_id, user_id))
@ -234,7 +171,7 @@ impl Likeable<CommentLikeForm> for CommentLike {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> { 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( diesel::delete(
dsl::comment_like dsl::comment_like
.filter(dsl::comment_id.eq(comment_id)) .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 { impl Saveable<CommentSavedForm> for CommentSaved {
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> { 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) insert_into(comment_saved)
.values(comment_saved_form) .values(comment_saved_form)
.on_conflict((comment_id, user_id)) .on_conflict((comment_id, user_id))
@ -272,7 +192,7 @@ impl Saveable<CommentSavedForm> for CommentSaved {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> { 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( diesel::delete(
comment_saved comment_saved
.filter(comment_id.eq(comment_saved_form.comment_id)) .filter(comment_id.eq(comment_saved_form.comment_id))
@ -285,12 +205,19 @@ impl Saveable<CommentSavedForm> for CommentSaved {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
source::{comment::*, community::*, post::*, user::*},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
Crud, Crud,
Likeable,
ListingType, ListingType,
Saveable,
SortType, SortType,
}; };
use lemmy_db_schema::source::{
comment::*,
community::{Community, CommunityForm},
post::*,
user::{UserForm, User_},
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,33 +1,9 @@
use crate::Reportable;
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::{Deserialize, Serialize}; use lemmy_db_schema::{
naive_now,
use crate::{naive_now, schema::comment_report, source::comment::Comment, Reportable}; source::comment_report::{CommentReport, CommentReportForm},
};
#[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,
}
impl Reportable<CommentReportForm> for CommentReport { impl Reportable<CommentReportForm> for CommentReport {
/// creates a comment report and returns it /// creates a comment report and returns it
@ -35,7 +11,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `conn` - the postgres connection /// * `conn` - the postgres connection
/// * `comment_report_form` - the filled CommentReportForm to insert /// * `comment_report_form` - the filled CommentReportForm to insert
fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> { 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) insert_into(comment_report)
.values(comment_report_form) .values(comment_report_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -47,7 +23,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `report_id` - the id of the report to resolve /// * `report_id` - the id of the report to resolve
/// * `by_resolver_id` - the id of the user resolving the report /// * `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> { 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)) update(comment_report.find(report_id))
.set(( .set((
resolved.eq(true), resolved.eq(true),
@ -63,7 +39,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `report_id` - the id of the report to unresolve /// * `report_id` - the id of the report to unresolve
/// * `by_resolver_id` - the id of the user unresolving the report /// * `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> { 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)) update(comment_report.find(report_id))
.set(( .set((
resolved.eq(false), resolved.eq(false),

View file

@ -1,6 +1,4 @@
use crate::{ use crate::{
naive_now,
schema::{community, community_follower, community_moderator, community_user_ban},
views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}, views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe},
ApubObject, ApubObject,
Bannable, Bannable,
@ -9,54 +7,24 @@ use crate::{
Joinable, Joinable,
}; };
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{
naive_now,
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] source::community::{
#[table_name = "community"] Community,
pub struct Community { CommunityFollower,
pub id: i32, CommunityFollowerForm,
pub name: String, CommunityForm,
pub title: String, CommunityModerator,
pub description: Option<String>, CommunityModeratorForm,
pub category_id: i32, CommunityUserBan,
pub creator_id: i32, CommunityUserBanForm,
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>,
}
mod safe_type { 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 = ( type Columns = (
id, id,
name, 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 { impl Crud<CommunityForm> for Community {
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> { 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) community.find(community_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> { 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) diesel::delete(community.find(community_id)).execute(conn)
} }
fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> { 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) insert_into(community)
.values(new_community) .values(new_community)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -144,7 +90,7 @@ impl Crud<CommunityForm> for Community {
community_id: i32, community_id: i32,
new_community: &CommunityForm, new_community: &CommunityForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id)) diesel::update(community.find(community_id))
.set(new_community) .set(new_community)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -153,14 +99,14 @@ impl Crud<CommunityForm> for Community {
impl ApubObject<CommunityForm> for Community { impl ApubObject<CommunityForm> for Community {
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result<Self, Error> { 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 community
.filter(actor_id.eq(for_actor_id)) .filter(actor_id.eq(for_actor_id))
.first::<Self>(conn) .first::<Self>(conn)
} }
fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> { 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) insert_into(community)
.values(community_form) .values(community_form)
.on_conflict(actor_id) .on_conflict(actor_id)
@ -170,54 +116,81 @@ impl ApubObject<CommunityForm> for Community {
} }
} }
impl Community { pub trait Community_ {
pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Self, Error> { fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
use crate::schema::community::dsl::*; 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 community
.filter(local.eq(true)) .filter(local.eq(true))
.filter(name.eq(community_name)) .filter(name.eq(community_name))
.first::<Self>(conn) .first::<Self>(conn)
} }
pub fn update_deleted( fn update_deleted(
conn: &PgConnection, conn: &PgConnection,
community_id: i32, community_id: i32,
new_deleted: bool, new_deleted: bool,
) -> Result<Self, Error> { ) -> Result<Community, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id)) diesel::update(community.find(community_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now()))) .set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_removed( fn update_removed(
conn: &PgConnection, conn: &PgConnection,
community_id: i32, community_id: i32,
new_removed: bool, new_removed: bool,
) -> Result<Self, Error> { ) -> Result<Community, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id)) diesel::update(community.find(community_id))
.set((removed.eq(new_removed), updated.eq(naive_now()))) .set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_removed_for_creator( fn update_removed_for_creator(
conn: &PgConnection, conn: &PgConnection,
for_creator_id: i32, for_creator_id: i32,
new_removed: bool, new_removed: bool,
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Community>, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.filter(creator_id.eq(for_creator_id))) diesel::update(community.filter(creator_id.eq(for_creator_id)))
.set((removed.eq(new_removed), updated.eq(naive_now()))) .set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(conn) .get_results::<Self>(conn)
} }
pub fn update_creator( fn update_creator(
conn: &PgConnection, conn: &PgConnection,
community_id: i32, community_id: i32,
new_creator_id: i32, new_creator_id: i32,
) -> Result<Self, Error> { ) -> Result<Community, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id)) diesel::update(community.find(community_id))
.set((creator_id.eq(new_creator_id), updated.eq(naive_now()))) .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -234,41 +207,24 @@ impl Community {
Ok(mods_and_admins) Ok(mods_and_admins)
} }
pub fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> { fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error> {
use crate::schema::community::dsl::*; use lemmy_db_schema::schema::community::dsl::*;
community.select(actor_id).distinct().load::<String>(conn) 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) Self::community_mods_and_admins(conn, community_id)
.unwrap_or_default() .unwrap_or_default()
.contains(&user_id) .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 { impl Joinable<CommunityModeratorForm> for CommunityModerator {
fn join( fn join(
conn: &PgConnection, conn: &PgConnection,
community_user_form: &CommunityModeratorForm, community_user_form: &CommunityModeratorForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_moderator::dsl::*; use lemmy_db_schema::schema::community_moderator::dsl::*;
insert_into(community_moderator) insert_into(community_moderator)
.values(community_user_form) .values(community_user_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -278,7 +234,7 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
conn: &PgConnection, conn: &PgConnection,
community_user_form: &CommunityModeratorForm, community_user_form: &CommunityModeratorForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*; use lemmy_db_schema::schema::community_moderator::dsl::*;
diesel::delete( diesel::delete(
community_moderator community_moderator
.filter(community_id.eq(community_user_form.community_id)) .filter(community_id.eq(community_user_form.community_id))
@ -288,17 +244,25 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
} }
} }
impl CommunityModerator { pub trait CommunityModerator_ {
pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> { fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
use crate::schema::community_moderator::dsl::*; 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) 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, conn: &PgConnection,
for_user_id: i32, for_user_id: i32,
) -> Result<Vec<i32>, Error> { ) -> Result<Vec<i32>, Error> {
use crate::schema::community_moderator::dsl::*; use lemmy_db_schema::schema::community_moderator::dsl::*;
community_moderator community_moderator
.filter(user_id.eq(for_user_id)) .filter(user_id.eq(for_user_id))
.select(community_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 { impl Bannable<CommunityUserBanForm> for CommunityUserBan {
fn ban( fn ban(
conn: &PgConnection, conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm, community_user_ban_form: &CommunityUserBanForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_user_ban::dsl::*; use lemmy_db_schema::schema::community_user_ban::dsl::*;
insert_into(community_user_ban) insert_into(community_user_ban)
.values(community_user_ban_form) .values(community_user_ban_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -338,7 +285,7 @@ impl Bannable<CommunityUserBanForm> for CommunityUserBan {
conn: &PgConnection, conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm, community_user_ban_form: &CommunityUserBanForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_user_ban::dsl::*; use lemmy_db_schema::schema::community_user_ban::dsl::*;
diesel::delete( diesel::delete(
community_user_ban community_user_ban
.filter(community_id.eq(community_user_ban_form.community_id)) .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 { impl Followable<CommunityFollowerForm> for CommunityFollower {
fn follow( fn follow(
conn: &PgConnection, conn: &PgConnection,
community_follower_form: &CommunityFollowerForm, community_follower_form: &CommunityFollowerForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_follower::dsl::*; use lemmy_db_schema::schema::community_follower::dsl::*;
insert_into(community_follower) insert_into(community_follower)
.values(community_follower_form) .values(community_follower_form)
.on_conflict((community_id, user_id)) .on_conflict((community_id, user_id))
@ -384,7 +312,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
where where
Self: Sized, Self: Sized,
{ {
use crate::schema::community_follower::dsl::*; use lemmy_db_schema::schema::community_follower::dsl::*;
diesel::update( diesel::update(
community_follower community_follower
.filter(community_id.eq(community_id_)) .filter(community_id.eq(community_id_))
@ -397,7 +325,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
conn: &PgConnection, conn: &PgConnection,
community_follower_form: &CommunityFollowerForm, community_follower_form: &CommunityFollowerForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_follower::dsl::*; use lemmy_db_schema::schema::community_follower::dsl::*;
diesel::delete( diesel::delete(
community_follower community_follower
.filter(community_id.eq(&community_follower_form.community_id)) .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 // 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 // community, it will also return true if only remote followers exist
fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> { 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( diesel::select(exists(
community_follower.filter(community_id.eq(community_id_)), community_follower.filter(community_id.eq(community_id_)),
)) ))
@ -419,11 +347,15 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
source::{community::*, user::*},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
Bannable,
Crud,
Followable,
Joinable,
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_schema::source::{community::*, user::*};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,210 +1,99 @@
use crate::{ use crate::Crud;
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 diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::source::moderator::*;
#[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>,
}
impl Crud<ModRemovePostForm> for ModRemovePost { impl Crud<ModRemovePostForm> for ModRemovePost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_remove_post.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> { 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) insert_into(mod_remove_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> { 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)) diesel::update(mod_remove_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModLockPostForm> for ModLockPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_lock_post.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> { 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) insert_into(mod_lock_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> { 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)) diesel::update(mod_lock_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModStickyPostForm> for ModStickyPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_sticky_post.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> { 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) insert_into(mod_sticky_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> { 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)) diesel::update(mod_sticky_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModRemoveCommentForm> for ModRemoveComment {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_remove_comment.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> { 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) insert_into(mod_remove_comment)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> { 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)) diesel::update(mod_remove_comment.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_remove_community.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> { 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) insert_into(mod_remove_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -215,45 +104,21 @@ impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
from_id: i32, from_id: i32,
form: &ModRemoveCommunityForm, form: &ModRemoveCommunityForm,
) -> Result<Self, Error> { ) -> 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)) diesel::update(mod_remove_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_ban_from_community.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> { 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) insert_into(mod_ban_from_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -264,126 +129,66 @@ impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
from_id: i32, from_id: i32,
form: &ModBanFromCommunityForm, form: &ModBanFromCommunityForm,
) -> Result<Self, Error> { ) -> 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)) diesel::update(mod_ban_from_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModBanForm> for ModBan {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_ban.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> { 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) insert_into(mod_ban).values(form).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> { 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)) diesel::update(mod_ban.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModAddCommunityForm> for ModAddCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_add_community.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> { 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) insert_into(mod_add_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> { 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)) diesel::update(mod_add_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .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 { impl Crud<ModAddForm> for ModAdd {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { 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) mod_add.find(from_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> { 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) insert_into(mod_add).values(form).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> { 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)) diesel::update(mod_add.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -392,12 +197,8 @@ impl Crud<ModAddForm> for ModAdd {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType};
source::{comment::*, community::*, moderator::*, post::*, user::*}, use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*};
tests::establish_unpooled_connection,
ListingType,
SortType,
};
// use Crud; // use Crud;
#[test] #[test]

View file

@ -1,29 +1,10 @@
use crate::{ use crate::Crud;
schema::{password_reset_request, password_reset_request::dsl::*},
Crud,
};
use diesel::{dsl::*, result::Error, PgConnection, *}; use diesel::{dsl::*, result::Error, PgConnection, *};
use lemmy_db_schema::{schema::password_reset_request::dsl::*, source::password_reset_request::*};
use sha2::{Digest, Sha256}; 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 { impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
use crate::schema::password_reset_request::dsl::*;
password_reset_request password_reset_request
.find(password_reset_request_id) .find(password_reset_request_id)
.first::<Self>(conn) .first::<Self>(conn)
@ -44,11 +25,24 @@ impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
} }
} }
impl PasswordResetRequest { pub trait PasswordResetRequest_ {
pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> { 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(); let mut hasher = Sha256::new();
hasher.update(token); 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 { let form = PasswordResetRequestForm {
user_id: from_user_id, user_id: from_user_id,
@ -57,35 +51,29 @@ impl PasswordResetRequest {
Self::create(&conn, &form) 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(); let mut hasher = Sha256::new();
hasher.update(token); 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 password_reset_request
.filter(token_encrypted.eq(token_hash)) .filter(token_encrypted.eq(token_hash))
.filter(published.gt(now - 1.days())) .filter(published.gt(now - 1.days()))
.first::<Self>(conn) .first::<Self>(conn)
} }
}
fn bytes_to_hex(bytes: Vec<u8>) -> String { fn bytes_to_hex(bytes: Vec<u8>) -> String {
let mut str = String::new(); let mut str = String::new();
for byte in bytes { for byte in bytes {
str = format!("{}{:02x}", str, byte); str = format!("{}{:02x}", str, byte);
}
str
} }
str
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::user::*; use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType};
use crate::{ use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*};
source::password_reset_request::PasswordResetRequest,
tests::establish_unpooled_connection,
Crud,
ListingType,
SortType,
};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,116 +1,64 @@
use crate::{ use crate::{ApubObject, Crud, Likeable, Readable, Saveable};
naive_now,
schema::{post, post_like, post_read, post_saved},
ApubObject,
Crud,
Likeable,
Readable,
Saveable,
};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{
use url::{ParseError, Url}; naive_now,
source::post::{
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] Post,
#[table_name = "post"] PostForm,
pub struct Post { PostLike,
pub id: i32, PostLikeForm,
pub name: String, PostRead,
pub url: Option<String>, PostReadForm,
pub body: Option<String>, PostSaved,
pub creator_id: i32, PostSavedForm,
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()))
}
}
impl Crud<PostForm> for Post { impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> { 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) post.find(post_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> { 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) diesel::delete(post.find(post_id)).execute(conn)
} }
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> { 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) insert_into(post).values(new_post).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> { 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)) diesel::update(post.find(post_id))
.set(new_post) .set(new_post)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
} }
impl ApubObject<PostForm> for Post { pub trait Post_ {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> { //fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
use crate::schema::post::dsl::*; fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Post>, Error>;
post.filter(ap_id.eq(object_id)).first::<Self>(conn) 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 upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> { fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Post, Error>;
use crate::schema::post::dsl::*; fn update_removed_for_creator(
insert_into(post) conn: &PgConnection,
.values(post_form) for_creator_id: i32,
.on_conflict(ap_id) for_community_id: Option<i32>,
.do_update() new_removed: bool,
.set(post_form) ) -> Result<Vec<Post>, Error>;
.get_result::<Self>(conn) 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 { impl Post_ for Post {
pub fn list_for_community( fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Self>, Error> {
conn: &PgConnection, use lemmy_db_schema::schema::post::dsl::*;
the_community_id: i32,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
post post
.filter(community_id.eq(the_community_id)) .filter(community_id.eq(the_community_id))
.then_order_by(published.desc()) .then_order_by(published.desc())
@ -119,19 +67,16 @@ impl Post {
.load::<Self>(conn) .load::<Self>(conn)
} }
pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> { fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id)) .set(ap_id.eq(apub_id))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn permadelete_for_creator( fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
conn: &PgConnection, use lemmy_db_schema::schema::post::dsl::*;
for_creator_id: i32,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
let perma_deleted = "*Permananently Deleted*"; let perma_deleted = "*Permananently Deleted*";
let perma_deleted_url = "https://deleted.com"; let perma_deleted_url = "https://deleted.com";
@ -147,35 +92,27 @@ impl Post {
.get_results::<Self>(conn) .get_results::<Self>(conn)
} }
pub fn update_deleted( fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Self, Error> {
conn: &PgConnection, use lemmy_db_schema::schema::post::dsl::*;
post_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now()))) .set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_removed( fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Self, Error> {
conn: &PgConnection, use lemmy_db_schema::schema::post::dsl::*;
post_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now()))) .set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_removed_for_creator( fn update_removed_for_creator(
conn: &PgConnection, conn: &PgConnection,
for_creator_id: i32, for_creator_id: i32,
for_community_id: Option<i32>, for_community_id: Option<i32>,
new_removed: bool, new_removed: bool,
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
let mut update = diesel::update(post).into_boxed(); let mut update = diesel::update(post).into_boxed();
update = update.filter(creator_id.eq(for_creator_id)); update = update.filter(creator_id.eq(for_creator_id));
@ -189,51 +126,45 @@ impl Post {
.get_results::<Self>(conn) .get_results::<Self>(conn)
} }
pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> { fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(locked.eq(new_locked)) .set(locked.eq(new_locked))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_stickied( fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Self, Error> {
conn: &PgConnection, use lemmy_db_schema::schema::post::dsl::*;
post_id: i32,
new_stickied: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied)) .set(stickied.eq(new_stickied))
.get_result::<Self>(conn) .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 user_id == post_creator_id
} }
} }
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] impl ApubObject<PostForm> for Post {
#[belongs_to(Post)] fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
#[table_name = "post_like"] use lemmy_db_schema::schema::post::dsl::*;
pub struct PostLike { post.filter(ap_id.eq(object_id)).first::<Self>(conn)
pub id: i32, }
pub post_id: i32,
pub user_id: i32,
pub score: i16,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset, Clone)] fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
#[table_name = "post_like"] use lemmy_db_schema::schema::post::dsl::*;
pub struct PostLikeForm { insert_into(post)
pub post_id: i32, .values(post_form)
pub user_id: i32, .on_conflict(ap_id)
pub score: i16, .do_update()
.set(post_form)
.get_result::<Self>(conn)
}
} }
impl Likeable<PostLikeForm> for PostLike { impl Likeable<PostLikeForm> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> { 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) insert_into(post_like)
.values(post_like_form) .values(post_like_form)
.on_conflict((post_id, user_id)) .on_conflict((post_id, user_id))
@ -242,7 +173,7 @@ impl Likeable<PostLikeForm> for PostLike {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> { 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( diesel::delete(
dsl::post_like dsl::post_like
.filter(dsl::post_id.eq(post_id)) .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 { impl Saveable<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> { 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) insert_into(post_saved)
.values(post_saved_form) .values(post_saved_form)
.on_conflict((post_id, user_id)) .on_conflict((post_id, user_id))
@ -280,7 +194,7 @@ impl Saveable<PostSavedForm> for PostSaved {
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> { 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( diesel::delete(
post_saved post_saved
.filter(post_id.eq(post_saved_form.post_id)) .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 { impl Readable<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> { 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) insert_into(post_read)
.values(post_read_form) .values(post_read_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> { 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( diesel::delete(
post_read post_read
.filter(post_id.eq(post_read_form.post_id)) .filter(post_id.eq(post_read_form.post_id))
@ -332,11 +225,10 @@ impl Readable<PostReadForm> for PostRead {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{source::post::*, tests::establish_unpooled_connection, ListingType, SortType};
source::{community::*, post::*, user::*}, use lemmy_db_schema::source::{
tests::establish_unpooled_connection, community::{Community, CommunityForm},
ListingType, user::*,
SortType,
}; };
#[test] #[test]

View file

@ -1,37 +1,6 @@
use crate::Reportable;
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::{Deserialize, Serialize}; use lemmy_db_schema::{naive_now, source::post_report::*};
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,
}
impl Reportable<PostReportForm> for PostReport { impl Reportable<PostReportForm> for PostReport {
/// creates a post report and returns it /// creates a post report and returns it
@ -39,7 +8,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `conn` - the postgres connection /// * `conn` - the postgres connection
/// * `post_report_form` - the filled CommentReportForm to insert /// * `post_report_form` - the filled CommentReportForm to insert
fn report(conn: &PgConnection, post_report_form: &PostReportForm) -> Result<Self, Error> { 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) insert_into(post_report)
.values(post_report_form) .values(post_report_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -51,7 +20,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `report_id` - the id of the report to resolve /// * `report_id` - the id of the report to resolve
/// * `by_resolver_id` - the id of the user resolving the report /// * `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> { 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)) update(post_report.find(report_id))
.set(( .set((
resolved.eq(true), resolved.eq(true),
@ -67,7 +36,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `report_id` - the id of the report to unresolve /// * `report_id` - the id of the report to unresolve
/// * `by_resolver_id` - the id of the user unresolving the report /// * `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> { 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)) update(post_report.find(report_id))
.set(( .set((
resolved.eq(false), 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 diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{naive_now, source::private_message::*};
#[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,
}
impl Crud<PrivateMessageForm> for PrivateMessage { impl Crud<PrivateMessageForm> for PrivateMessage {
fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> { 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) private_message.find(private_message_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> { 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) insert_into(private_message)
.values(private_message_form) .values(private_message_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -49,7 +20,7 @@ impl Crud<PrivateMessageForm> for PrivateMessage {
private_message_id: i32, private_message_id: i32,
private_message_form: &PrivateMessageForm, private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> { ) -> 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)) diesel::update(private_message.find(private_message_id))
.set(private_message_form) .set(private_message_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -61,14 +32,14 @@ impl ApubObject<PrivateMessageForm> for PrivateMessage {
where where
Self: Sized, Self: Sized,
{ {
use crate::schema::private_message::dsl::*; use lemmy_db_schema::schema::private_message::dsl::*;
private_message private_message
.filter(ap_id.eq(object_id)) .filter(ap_id.eq(object_id))
.first::<Self>(conn) .first::<Self>(conn)
} }
fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> { 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) insert_into(private_message)
.values(private_message_form) .values(private_message_form)
.on_conflict(ap_id) .on_conflict(ap_id)
@ -78,54 +49,84 @@ impl ApubObject<PrivateMessageForm> for PrivateMessage {
} }
} }
impl PrivateMessage { pub trait PrivateMessage_ {
pub fn update_ap_id( fn update_ap_id(
conn: &PgConnection, conn: &PgConnection,
private_message_id: i32, private_message_id: i32,
apub_id: String, apub_id: String,
) -> Result<Self, Error> { ) -> Result<PrivateMessage, Error>;
use crate::schema::private_message::dsl::*; 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)) diesel::update(private_message.find(private_message_id))
.set(ap_id.eq(apub_id)) .set(ap_id.eq(apub_id))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_content( fn update_content(
conn: &PgConnection, conn: &PgConnection,
private_message_id: i32, private_message_id: i32,
new_content: &str, new_content: &str,
) -> Result<Self, Error> { ) -> Result<PrivateMessage, Error> {
use crate::schema::private_message::dsl::*; use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id)) diesel::update(private_message.find(private_message_id))
.set((content.eq(new_content), updated.eq(naive_now()))) .set((content.eq(new_content), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_deleted( fn update_deleted(
conn: &PgConnection, conn: &PgConnection,
private_message_id: i32, private_message_id: i32,
new_deleted: bool, new_deleted: bool,
) -> Result<Self, Error> { ) -> Result<PrivateMessage, Error> {
use crate::schema::private_message::dsl::*; use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id)) diesel::update(private_message.find(private_message_id))
.set(deleted.eq(new_deleted)) .set(deleted.eq(new_deleted))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn update_read( fn update_read(
conn: &PgConnection, conn: &PgConnection,
private_message_id: i32, private_message_id: i32,
new_read: bool, new_read: bool,
) -> Result<Self, Error> { ) -> Result<PrivateMessage, Error> {
use crate::schema::private_message::dsl::*; use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id)) diesel::update(private_message.find(private_message_id))
.set(read.eq(new_read)) .set(read.eq(new_read))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> { fn mark_all_as_read(
use crate::schema::private_message::dsl::*; conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<PrivateMessage>, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update( diesel::update(
private_message private_message
.filter(recipient_id.eq(for_recipient_id)) .filter(recipient_id.eq(for_recipient_id))
@ -138,12 +139,8 @@ impl PrivateMessage {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{tests::establish_unpooled_connection, ListingType, SortType};
source::{private_message::*, user::*}, use lemmy_db_schema::source::{private_message::*, user::*};
tests::establish_unpooled_connection,
ListingType,
SortType,
};
#[test] #[test]
fn test_crud() { 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 diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::{naive_now, source::site::*};
#[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>>,
}
impl Crud<SiteForm> for Site { impl Crud<SiteForm> for Site {
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> { 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) site.first::<Self>(conn)
} }
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> { 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) insert_into(site).values(new_site).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> { 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)) diesel::update(site.find(site_id))
.set(new_site) .set(new_site)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> { 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) diesel::delete(site.find(site_id)).execute(conn)
} }
} }
impl Site { pub trait Site_ {
pub fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Self, Error> { fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error>;
use crate::schema::site::dsl::*; 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)) diesel::update(site.find(1))
.set((creator_id.eq(new_creator_id), updated.eq(naive_now()))) .set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn read_simple(conn: &PgConnection) -> Result<Self, Error> { fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
use crate::schema::site::dsl::*; use lemmy_db_schema::schema::site::dsl::*;
site.first::<Self>(conn) site.first::<Self>(conn)
} }
} }

View file

@ -1,68 +1,17 @@
use crate::{ use crate::{is_email_regex, ApubObject, Crud};
is_email_regex,
naive_now,
schema::{user_, user_::dsl::*, user_alias_1, user_alias_2},
ApubObject,
Crud,
};
use bcrypt::{hash, DEFAULT_COST}; use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
schema::user_::dsl::*,
source::user::{UserForm, User_},
};
use lemmy_utils::settings::Settings; 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 { 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 = ( type Columns = (
id, id,
name, 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 { 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 = ( type Columns = (
id, id,
name, 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 { 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 = ( type Columns = (
id, id,
name, 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_ { impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_ user_
@ -338,7 +162,7 @@ impl Crud<UserForm> for User_ {
impl ApubObject<UserForm> for User_ { impl ApubObject<UserForm> for User_ {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> { 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_ user_
.filter(deleted.eq(false)) .filter(deleted.eq(false))
.filter(actor_id.eq(object_id)) .filter(actor_id.eq(object_id))
@ -355,8 +179,26 @@ impl ApubObject<UserForm> for User_ {
} }
} }
impl User_ { pub trait User {
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> { 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 mut edited_user = form.clone();
let password_hash = let password_hash =
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password"); hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
@ -366,11 +208,7 @@ impl User_ {
} }
// TODO do more individual updates like these // TODO do more individual updates like these
pub fn update_password( fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) -> Result<Self, Error> {
conn: &PgConnection,
user_id: i32,
new_password: &str,
) -> Result<Self, Error> {
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password"); let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
diesel::update(user_.find(user_id)) diesel::update(user_.find(user_id))
@ -381,7 +219,7 @@ impl User_ {
.get_result::<Self>(conn) .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_ user_
.filter(local.eq(true)) .filter(local.eq(true))
.filter(deleted.eq(false)) .filter(deleted.eq(false))
@ -389,19 +227,19 @@ impl User_ {
.first::<Self>(conn) .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)) diesel::update(user_.find(user_id))
.set(admin.eq(added)) .set(admin.eq(added))
.get_result::<Self>(conn) .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)) diesel::update(user_.find(user_id))
.set(banned.eq(ban)) .set(banned.eq(ban))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn find_by_email_or_username( fn find_by_email_or_username(
conn: &PgConnection, conn: &PgConnection,
username_or_email: &str, username_or_email: &str,
) -> Result<Self, Error> { ) -> 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_ user_
.filter(deleted.eq(false)) .filter(deleted.eq(false))
.filter(local.eq(true)) .filter(local.eq(true))
@ -420,7 +258,7 @@ impl User_ {
.first::<User_>(conn) .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_ user_
.filter(deleted.eq(false)) .filter(deleted.eq(false))
.filter(local.eq(true)) .filter(local.eq(true))
@ -428,7 +266,7 @@ impl User_ {
.first::<User_>(conn) .first::<User_>(conn)
} }
pub fn get_profile_url(&self, hostname: &str) -> String { fn get_profile_url(&self, hostname: &str) -> String {
format!( format!(
"{}://{}/u/{}", "{}://{}/u/{}",
Settings::get().get_protocol_string(), 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)) diesel::update(user_.find(user_id))
.set((last_refreshed_at.eq(naive_now()),)) .set((last_refreshed_at.eq(naive_now()),))
.get_result::<Self>(conn) .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)) diesel::update(user_.find(user_id))
.set(( .set((
preferred_username.eq::<Option<String>>(None), preferred_username.eq::<Option<String>>(None),

View file

@ -1,35 +1,15 @@
use super::comment::Comment; use crate::Crud;
use crate::{schema::user_mention, Crud};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::Serialize; use lemmy_db_schema::source::user_mention::*;
#[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>,
}
impl Crud<UserMentionForm> for UserMention { impl Crud<UserMentionForm> for UserMention {
fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> { 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) user_mention.find(user_mention_id).first::<Self>(conn)
} }
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> { 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 // since the return here isnt utilized, we dont need to do an update
// but get_result doesnt return the existing row here // but get_result doesnt return the existing row here
insert_into(user_mention) insert_into(user_mention)
@ -45,27 +25,42 @@ impl Crud<UserMentionForm> for UserMention {
user_mention_id: i32, user_mention_id: i32,
user_mention_form: &UserMentionForm, user_mention_form: &UserMentionForm,
) -> Result<Self, Error> { ) -> 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)) diesel::update(user_mention.find(user_mention_id))
.set(user_mention_form) .set(user_mention_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
} }
impl UserMention { pub trait UserMention_ {
pub fn update_read( fn update_read(
conn: &PgConnection, conn: &PgConnection,
user_mention_id: i32, user_mention_id: i32,
new_read: bool, new_read: bool,
) -> Result<Self, Error> { ) -> Result<UserMention, Error>;
use crate::schema::user_mention::dsl::*; 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)) diesel::update(user_mention.find(user_mention_id))
.set(read.eq(new_read)) .set(read.eq(new_read))
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
pub fn mark_all_as_read(conn: &PgConnection, for_recipient_id: i32) -> Result<Vec<Self>, Error> { fn mark_all_as_read(
use crate::schema::user_mention::dsl::*; conn: &PgConnection,
for_recipient_id: i32,
) -> Result<Vec<UserMention>, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
diesel::update( diesel::update(
user_mention user_mention
.filter(recipient_id.eq(for_recipient_id)) .filter(recipient_id.eq(for_recipient_id))
@ -78,11 +73,13 @@ impl UserMention {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{tests::establish_unpooled_connection, Crud, ListingType, SortType};
source::{comment::*, community::*, post::*, user::*, user_mention::*}, use lemmy_db_schema::source::{
tests::establish_unpooled_connection, comment::*,
ListingType, community::{Community, CommunityForm},
SortType, post::*,
user::*,
user_mention::*,
}; };
#[test] #[test]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2}, schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2},
source::{ source::{
comment::Comment, comment::Comment,
@ -8,11 +9,7 @@ use crate::{
post::Post, post::Post,
user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_},
}, },
views::ViewToVec,
MaybeOptional,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]

View file

@ -3,6 +3,14 @@ use crate::{
functions::hot_rank, functions::hot_rank,
fuzzy_search, fuzzy_search,
limit_and_offset, limit_and_offset,
views::ViewToVec,
ListingType,
MaybeOptional,
SortType,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{ schema::{
comment, comment,
comment_aggregates, comment_aggregates,
@ -22,13 +30,7 @@ use crate::{
post::Post, post::Post,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ListingType,
MaybeOptional,
SortType,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]
@ -416,14 +418,8 @@ impl ViewToVec for CommentView {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{tests::establish_unpooled_connection, views::comment_view::*, Crud, Likeable, *};
source::{comment::*, community::*, post::*, user::*}, use lemmy_db_schema::source::{comment::*, community::*, post::*, user::*};
tests::establish_unpooled_connection,
views::comment_view::*,
Crud,
Likeable,
*,
};
#[test] #[test]
fn test_crud() { 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_}, schema::{community, community_follower, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[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_}, schema::{community, community_moderator, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[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_}, schema::{community, community_user_ban, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -3,18 +3,20 @@ use crate::{
functions::hot_rank, functions::hot_rank,
fuzzy_search, fuzzy_search,
limit_and_offset, limit_and_offset,
schema::{category, community, community_aggregates, community_follower, user_},
source::{
category::Category,
community::{Community, CommunityFollower, CommunitySafe},
user::{UserSafe, User_},
},
views::ViewToVec, views::ViewToVec,
MaybeOptional, MaybeOptional,
SortType, SortType,
ToSafe, ToSafe,
}; };
use diesel::{result::Error, *}; 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; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,15 +1,13 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_add_community, user_, user_alias_1}, schema::{community, mod_add_community, user_, user_alias_1},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
moderator::ModAddCommunity, moderator::ModAddCommunity,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,14 +1,12 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{mod_add, user_, user_alias_1}, schema::{mod_add, user_, user_alias_1},
source::{ source::{
moderator::ModAdd, moderator::ModAdd,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,15 +1,13 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_ban_from_community, user_, user_alias_1}, schema::{community, mod_ban_from_community, user_, user_alias_1},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
moderator::ModBanFromCommunity, moderator::ModBanFromCommunity,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,14 +1,12 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{mod_ban, user_, user_alias_1}, schema::{mod_ban, user_, user_alias_1},
source::{ source::{
moderator::ModBan, moderator::ModBan,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_lock_post, post, user_}, schema::{community, mod_lock_post, post, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
@ -7,10 +8,7 @@ use crate::{
post::Post, post::Post,
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}, schema::{comment, community, mod_remove_comment, post, user_, user_alias_1},
source::{ source::{
comment::Comment, comment::Comment,
@ -8,10 +9,7 @@ use crate::{
post::Post, post::Post,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,15 +1,13 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_remove_community, user_}, schema::{community, mod_remove_community, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
moderator::ModRemoveCommunity, moderator::ModRemoveCommunity,
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_remove_post, post, user_}, schema::{community, mod_remove_post, post, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
@ -7,10 +8,7 @@ use crate::{
post::Post, post::Post,
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, mod_sticky_post, post, user_}, schema::{community, mod_sticky_post, post, user_},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
@ -7,10 +8,7 @@ use crate::{
post::Post, post::Post,
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{community, post, post_report, user_, user_alias_1, user_alias_2}, schema::{community, post, post_report, user_, user_alias_1, user_alias_2},
source::{ source::{
community::{Community, CommunitySafe}, community::{Community, CommunitySafe},
@ -7,11 +8,7 @@ use crate::{
post_report::PostReport, post_report::PostReport,
user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_}, user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_},
}, },
views::ViewToVec,
MaybeOptional,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]

View file

@ -3,6 +3,14 @@ use crate::{
functions::hot_rank, functions::hot_rank,
fuzzy_search, fuzzy_search,
limit_and_offset, limit_and_offset,
views::ViewToVec,
ListingType,
MaybeOptional,
SortType,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{ schema::{
community, community,
community_follower, community_follower,
@ -19,13 +27,7 @@ use crate::{
post::{Post, PostRead, PostSaved}, post::{Post, PostRead, PostSaved},
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
views::ViewToVec,
ListingType,
MaybeOptional,
SortType,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]
@ -406,13 +408,13 @@ impl ViewToVec for PostView {
mod tests { mod tests {
use crate::{ use crate::{
aggregates::post_aggregates::PostAggregates, aggregates::post_aggregates::PostAggregates,
source::{community::*, post::*, user::*},
tests::establish_unpooled_connection, tests::establish_unpooled_connection,
views::post_view::{PostQueryBuilder, PostView}, views::post_view::{PostQueryBuilder, PostView},
Crud, Crud,
Likeable, Likeable,
*, *,
}; };
use lemmy_db_schema::source::{community::*, post::*, user::*};
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -1,15 +1,12 @@
use crate::{ use crate::{limit_and_offset, views::ViewToVec, MaybeOptional, ToSafe};
limit_and_offset, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{private_message, user_, user_alias_1}, schema::{private_message, user_, user_alias_1},
source::{ source::{
private_message::PrivateMessage, private_message::PrivateMessage,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
}, },
views::ViewToVec,
MaybeOptional,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]

View file

@ -1,13 +1,12 @@
use crate::{ use crate::{aggregates::site_aggregates::SiteAggregates, ToSafe};
aggregates::site_aggregates::SiteAggregates, use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{site, site_aggregates, user_}, schema::{site, site_aggregates, user_},
source::{ source::{
site::Site, site::Site,
user::{UserSafe, User_}, user::{UserSafe, User_},
}, },
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]

View file

@ -2,6 +2,13 @@ use crate::{
aggregates::comment_aggregates::CommentAggregates, aggregates::comment_aggregates::CommentAggregates,
functions::hot_rank, functions::hot_rank,
limit_and_offset, limit_and_offset,
views::ViewToVec,
MaybeOptional,
SortType,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::{
schema::{ schema::{
comment, comment,
comment_aggregates, comment_aggregates,
@ -22,12 +29,7 @@ use crate::{
user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
user_mention::UserMention, user_mention::UserMention,
}, },
views::ViewToVec,
MaybeOptional,
SortType,
ToSafe,
}; };
use diesel::{result::Error, *};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, PartialEq, Serialize, Clone)] #[derive(Debug, PartialEq, Serialize, Clone)]

View file

@ -2,14 +2,16 @@ use crate::{
aggregates::user_aggregates::UserAggregates, aggregates::user_aggregates::UserAggregates,
fuzzy_search, fuzzy_search,
limit_and_offset, limit_and_offset,
schema::{user_, user_aggregates},
source::user::{UserSafe, User_},
views::ViewToVec, views::ViewToVec,
MaybeOptional, MaybeOptional,
SortType, SortType,
ToSafe, ToSafe,
}; };
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
schema::{user_, user_aggregates},
source::user::{UserSafe, User_},
};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize, Clone)] #[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