Move comment, post definitions into lemmy_db_schema

This commit is contained in:
Felix Ableitner 2020-12-18 18:27:25 +01:00
parent 089d812dc8
commit 114f3cbfb5
58 changed files with 642 additions and 574 deletions

9
Cargo.lock generated
View file

@ -1718,6 +1718,7 @@ dependencies = [
"lazy_static",
"lemmy_apub",
"lemmy_db",
"lemmy_db_schema",
"lemmy_rate_limit",
"lemmy_structs",
"lemmy_utils",
@ -1762,6 +1763,7 @@ dependencies = [
"itertools",
"lazy_static",
"lemmy_db",
"lemmy_db_schema",
"lemmy_structs",
"lemmy_utils",
"lemmy_websocket",
@ -1805,7 +1807,12 @@ dependencies = [
name = "lemmy_db_schema"
version = "0.1.0"
dependencies = [
"chrono",
"diesel",
"log",
"serde 1.0.118",
"serde_json",
"url",
]
[[package]]
@ -1868,6 +1875,7 @@ dependencies = [
"chrono",
"diesel",
"lemmy_db",
"lemmy_db_schema",
"lemmy_utils",
"log",
"serde 1.0.118",
@ -1909,6 +1917,7 @@ dependencies = [
"chrono",
"diesel",
"lemmy_db",
"lemmy_db_schema",
"lemmy_rate_limit",
"lemmy_structs",
"lemmy_utils",

View file

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

View file

@ -11,10 +11,8 @@ use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{
source::{
comment::*,
comment_report::{CommentReport, CommentReportForm},
moderator::*,
post::*,
user::*,
},
views::{
@ -29,6 +27,10 @@ use lemmy_db::{
Saveable,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm, CommentSaved, CommentSavedForm},
post::Post,
};
use lemmy_structs::{blocking, comment::*, send_local_notifs};
use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType},

View file

@ -11,8 +11,7 @@ use anyhow::Context;
use lemmy_apub::ActorType;
use lemmy_db::{
diesel_option_overwrite,
naive_now,
source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
source::{community::*, moderator::*, site::*},
views::{
comment_view::CommentQueryBuilder,
community::{
@ -29,6 +28,10 @@ use lemmy_db::{
Joinable,
SortType,
};
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, post::Post},
};
use lemmy_structs::{blocking, community::*};
use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},

View file

@ -3,13 +3,13 @@ use actix_web::{web, web::Data};
use lemmy_db::{
source::{
community::{Community, CommunityModerator},
post::Post,
user::User_,
},
views::community::community_user_ban_view::CommunityUserBanView,
Crud,
DbPool,
};
use lemmy_db_schema::source::post::Post;
use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*};
use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError};
use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};

View file

@ -10,10 +10,8 @@ use crate::{
use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{
naive_now,
source::{
moderator::*,
post::*,
post_report::{PostReport, PostReportForm},
},
views::{
@ -30,6 +28,10 @@ use lemmy_db::{
Saveable,
SortType,
};
use lemmy_db_schema::{
naive_now,
source::post::{Post, PostForm, PostLike, PostLikeForm, PostSaved, PostSavedForm},
};
use lemmy_structs::{blocking, post::*};
use lemmy_utils::{
apub::{make_apub_endpoint, EndpointType},

View file

@ -12,7 +12,6 @@ use lemmy_apub::fetcher::search_by_apub_id;
use lemmy_db::{
aggregates::site_aggregates::SiteAggregates,
diesel_option_overwrite,
naive_now,
source::{category::*, moderator::*, site::*},
views::{
comment_view::CommentQueryBuilder,
@ -36,6 +35,7 @@ use lemmy_db::{
SearchType,
SortType,
};
use lemmy_db_schema::naive_now;
use lemmy_structs::{blocking, site::*, user::Register};
use lemmy_utils::{
location_info,

View file

@ -16,13 +16,10 @@ use chrono::Duration;
use lemmy_apub::ApubObjectType;
use lemmy_db::{
diesel_option_overwrite,
naive_now,
source::{
comment::*,
community::*,
moderator::*,
password_reset_request::*,
post::*,
private_message::*,
site::*,
user::*,
@ -48,6 +45,10 @@ use lemmy_db::{
ListingType,
SortType,
};
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, post::Post},
};
use lemmy_structs::{blocking, send_email_to_user, user::*};
use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},

View file

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

View file

@ -4,13 +4,10 @@ use activitystreams::{
base::ExtendsExt,
};
use anyhow::Context;
use lemmy_db::{
source::{
use lemmy_db::{views::comment_view::CommentView, Likeable};
use lemmy_db_schema::source::{
comment::{Comment, CommentLike, CommentLikeForm},
post::Post,
},
views::comment_view::CommentView,
Likeable,
};
use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};

View file

@ -1,10 +1,7 @@
use crate::activities::receive::get_actor_as_user;
use activitystreams::activity::{Dislike, Like};
use lemmy_db::{
source::comment::{Comment, CommentLike},
views::comment_view::CommentView,
Likeable,
};
use lemmy_db::{views::comment_view::CommentView, Likeable};
use lemmy_db_schema::source::comment::{Comment, CommentLike};
use lemmy_structs::{blocking, comment::CommentResponse};
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};

View file

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

View file

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

View file

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

View file

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

View file

@ -13,11 +13,8 @@ use anyhow::{anyhow, Context};
use chrono::NaiveDateTime;
use diesel::result::Error::NotFound;
use lemmy_db::{
naive_now,
source::{
comment::Comment,
community::{Community, CommunityModerator, CommunityModeratorForm},
post::Post,
user::User_,
},
views::{
@ -30,6 +27,10 @@ use lemmy_db::{
Joinable,
SearchType,
};
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, post::Post},
};
use lemmy_structs::{blocking, site::SearchResponse};
use lemmy_utils::{
location_info,

View file

@ -4,7 +4,8 @@ use crate::{
};
use actix_web::{body::Body, web, web::Path, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_db::{source::comment::Comment, Crud};
use lemmy_db::Crud;
use lemmy_db_schema::source::comment::Comment;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;

View file

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

View file

@ -4,7 +4,7 @@ use crate::{
};
use actix_web::{body::Body, web, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_db::source::post::Post;
use lemmy_db_schema::source::post::Post;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -25,9 +25,7 @@ mod tests {
use crate::{
aggregates::community_aggregates::CommunityAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
post::{Post, PostForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection,
@ -36,6 +34,10 @@ mod tests {
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
post::{Post, PostForm},
};
#[test]
fn test_crud() {

View file

@ -27,9 +27,7 @@ mod tests {
use crate::{
aggregates::post_aggregates::PostAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection,
@ -38,6 +36,10 @@ mod tests {
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
post::{Post, PostForm, PostLike, PostLikeForm},
};
#[test]
fn test_crud() {

View file

@ -23,9 +23,7 @@ mod tests {
use crate::{
aggregates::site_aggregates::SiteAggregates,
source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
post::{Post, PostForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection,
@ -33,6 +31,10 @@ mod tests {
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
post::{Post, PostForm},
};
#[test]
fn test_crud() {

View file

@ -26,9 +26,7 @@ mod tests {
use crate::{
aggregates::user_aggregates::UserAggregates,
source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
community::{Community, CommunityForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
},
tests::establish_unpooled_connection,
@ -37,6 +35,10 @@ mod tests {
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
post::{Post, PostForm, PostLike, PostLikeForm},
};
#[test]
fn test_crud() {

View file

@ -5,7 +5,6 @@ extern crate strum_macros;
#[macro_use]
extern crate lazy_static;
use chrono::NaiveDateTime;
use diesel::{result::Error, *};
use regex::Regex;
use serde::{Deserialize, Serialize};
@ -181,10 +180,6 @@ pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
(limit, offset)
}
pub fn naive_now() -> NaiveDateTime {
chrono::prelude::Utc::now().naive_utc()
}
pub fn is_email_regex(test: &str) -> bool {
EMAIL_REGEX.is_match(test)
}

View file

@ -1,74 +1,13 @@
use super::post::Post;
use crate::{naive_now, ApubObject, Crud, Likeable, Saveable};
use crate::{ApubObject, Crud, Likeable, Saveable};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::{comment, comment_alias_1, comment_like, comment_saved};
use serde::Serialize;
use url::{ParseError, Url};
// WITH RECURSIVE MyTree AS (
// SELECT * FROM comment WHERE parent_id IS NULL
// UNION ALL
// SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
// )
// SELECT * FROM MyTree;
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
#[belongs_to(Post)]
#[table_name = "comment"]
pub struct Comment {
pub id: i32,
pub creator_id: i32,
pub post_id: i32,
pub parent_id: Option<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()))
}
}
use lemmy_db_schema::source::comment::{
Comment,
CommentForm,
CommentLike,
CommentLikeForm,
CommentSaved,
CommentSavedForm,
};
impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
@ -117,106 +56,6 @@ impl ApubObject<CommentForm> for Comment {
}
}
impl Comment {
pub fn update_ap_id(
conn: &PgConnection,
comment_id: i32,
apub_id: String,
) -> Result<Self, Error> {
use lemmy_db_schema::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 lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.filter(creator_id.eq(for_creator_id)))
.set((
content.eq("*Permananently Deleted*"),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_results::<Self>(conn)
}
pub 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)
}
pub 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)
}
pub 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)
}
pub 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)
}
pub 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)
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
#[belongs_to(Comment)]
#[table_name = "comment_like"]
pub struct CommentLike {
pub id: i32,
pub user_id: i32,
pub comment_id: i32,
pub post_id: i32, // TODO this is redundant
pub score: i16,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "comment_like"]
pub struct CommentLikeForm {
pub user_id: i32,
pub comment_id: i32,
pub post_id: i32, // TODO this is redundant
pub score: i16,
}
impl Likeable<CommentLikeForm> for CommentLike {
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment_like::dsl::*;
@ -238,23 +77,6 @@ impl Likeable<CommentLikeForm> for CommentLike {
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Comment)]
#[table_name = "comment_saved"]
pub struct CommentSaved {
pub id: i32,
pub comment_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "comment_saved"]
pub struct CommentSavedForm {
pub comment_id: i32,
pub user_id: i32,
}
impl Saveable<CommentSavedForm> for CommentSaved {
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment_saved::dsl::*;
@ -279,12 +101,15 @@ impl Saveable<CommentSavedForm> for CommentSaved {
#[cfg(test)]
mod tests {
use crate::{
source::{comment::*, community::*, post::*, user::*},
source::{community::*, user::*},
tests::establish_unpooled_connection,
Crud,
Likeable,
ListingType,
Saveable,
SortType,
};
use lemmy_db_schema::source::{comment::*, post::*};
#[test]
fn test_crud() {

View file

@ -1,6 +1,6 @@
use crate::{naive_now, source::comment::Comment, Reportable};
use crate::Reportable;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::comment_report;
use lemmy_db_schema::{naive_now, schema::comment_report, source::comment::Comment};
use serde::{Deserialize, Serialize};
#[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)]

View file

@ -1,5 +1,4 @@
use crate::{
naive_now,
views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe},
ApubObject,
Bannable,
@ -8,11 +7,9 @@ use crate::{
Joinable,
};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::{
community,
community_follower,
community_moderator,
community_user_ban,
use lemmy_db_schema::{
naive_now,
schema::{community, community_follower, community_moderator, community_user_ban},
};
use serde::Serialize;

View file

@ -391,11 +391,12 @@ impl Crud<ModAddForm> for ModAdd {
#[cfg(test)]
mod tests {
use crate::{
source::{comment::*, community::*, moderator::*, post::*, user::*},
source::{community::*, moderator::*, user::*},
tests::establish_unpooled_connection,
ListingType,
SortType,
};
use lemmy_db_schema::source::{comment::*, post::*};
// use Crud;
#[test]

View file

@ -1,61 +1,15 @@
use crate::{naive_now, ApubObject, Crud, Likeable, Readable, Saveable};
use crate::{ApubObject, Crud, Likeable, Readable, Saveable};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::{post, post_like, post_read, post_saved};
use serde::Serialize;
use url::{ParseError, Url};
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
#[table_name = "post"]
pub struct Post {
pub id: i32,
pub name: String,
pub url: Option<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: bool,
pub locked: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub stickied: bool,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: String,
pub local: bool,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "post"]
pub struct PostForm {
pub name: String,
pub url: Option<String>,
pub body: Option<String>,
pub creator_id: i32,
pub community_id: i32,
pub removed: Option<bool>,
pub locked: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
pub stickied: Option<bool>,
pub embed_title: Option<String>,
pub embed_description: Option<String>,
pub embed_html: Option<String>,
pub thumbnail_url: Option<String>,
pub ap_id: Option<String>,
pub local: bool,
}
impl PostForm {
pub fn get_ap_id(&self) -> Result<Url, ParseError> {
Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string()))
}
}
use lemmy_db_schema::source::post::{
Post,
PostForm,
PostLike,
PostLikeForm,
PostRead,
PostReadForm,
PostSaved,
PostSavedForm,
};
impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
@ -98,137 +52,6 @@ impl ApubObject<PostForm> for Post {
}
}
impl Post {
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.filter(id.eq(post_id)).first::<Self>(conn)
}
pub fn list_for_community(
conn: &PgConnection,
the_community_id: i32,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post
.filter(community_id.eq(the_community_id))
.then_order_by(published.desc())
.then_order_by(stickied.desc())
.limit(20)
.load::<Self>(conn)
}
pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
pub fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: i32,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
let perma_deleted = "*Permananently Deleted*";
let perma_deleted_url = "https://deleted.com";
diesel::update(post.filter(creator_id.eq(for_creator_id)))
.set((
name.eq(perma_deleted),
url.eq(perma_deleted_url),
body.eq(perma_deleted),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_results::<Self>(conn)
}
pub fn update_deleted(
conn: &PgConnection,
post_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed(
conn: &PgConnection,
post_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
let mut update = diesel::update(post).into_boxed();
update = update.filter(creator_id.eq(for_creator_id));
if let Some(for_community_id) = for_community_id {
update = update.filter(community_id.eq(for_community_id));
}
update
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(conn)
}
pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(locked.eq(new_locked))
.get_result::<Self>(conn)
}
pub fn update_stickied(
conn: &PgConnection,
post_id: i32,
new_stickied: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied))
.get_result::<Self>(conn)
}
pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
user_id == post_creator_id
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Post)]
#[table_name = "post_like"]
pub struct PostLike {
pub id: i32,
pub post_id: i32,
pub user_id: i32,
pub score: i16,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "post_like"]
pub struct PostLikeForm {
pub post_id: i32,
pub user_id: i32,
pub score: i16,
}
impl Likeable<PostLikeForm> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_like::dsl::*;
@ -250,23 +73,6 @@ impl Likeable<PostLikeForm> for PostLike {
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Post)]
#[table_name = "post_saved"]
pub struct PostSaved {
pub id: i32,
pub post_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "post_saved"]
pub struct PostSavedForm {
pub post_id: i32,
pub user_id: i32,
}
impl Saveable<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_saved::dsl::*;
@ -288,27 +94,6 @@ impl Saveable<PostSavedForm> for PostSaved {
}
}
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Post)]
#[table_name = "post_read"]
pub struct PostRead {
pub id: i32,
pub post_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "post_read"]
pub struct PostReadForm {
pub post_id: i32,
pub user_id: i32,
}
impl Readable<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_read::dsl::*;

View file

@ -1,6 +1,6 @@
use crate::{naive_now, source::post::Post, Reportable};
use crate::Reportable;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::post_report;
use lemmy_db_schema::{naive_now, schema::post_report, source::post::Post};
use serde::{Deserialize, Serialize};
#[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug, Clone)]

View file

@ -1,6 +1,6 @@
use crate::{naive_now, ApubObject, Crud};
use crate::{ApubObject, Crud};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::private_message;
use lemmy_db_schema::{naive_now, schema::private_message};
use serde::Serialize;
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]

View file

@ -1,6 +1,6 @@
use crate::{naive_now, Crud};
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::site;
use lemmy_db_schema::{naive_now, schema::site};
use serde::Serialize;
#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)]

View file

@ -1,7 +1,10 @@
use crate::{is_email_regex, naive_now, ApubObject, Crud};
use crate::{is_email_regex, ApubObject, Crud};
use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::{user_, user_::dsl::*, user_alias_1, user_alias_2};
use lemmy_db_schema::{
naive_now,
schema::{user_, user_::dsl::*, user_alias_1, user_alias_2},
};
use lemmy_utils::settings::Settings;
use serde::Serialize;

View file

@ -1,7 +1,6 @@
use super::comment::Comment;
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::schema::user_mention;
use lemmy_db_schema::{schema::user_mention, source::comment::Comment};
use serde::Serialize;
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
@ -80,11 +79,12 @@ impl UserMention {
#[cfg(test)]
mod tests {
use crate::{
source::{comment::*, community::*, post::*, user::*, user_mention::*},
source::{community::*, user::*, user_mention::*},
tests::establish_unpooled_connection,
ListingType,
SortType,
};
use lemmy_db_schema::source::{comment::*, post::*};
#[test]
fn test_crud() {

View file

@ -1,10 +1,8 @@
use crate::{
limit_and_offset,
source::{
comment::Comment,
comment_report::CommentReport,
community::{Community, CommunitySafe},
post::Post,
user::{UserAlias1, UserAlias2, UserSafe, UserSafeAlias1, UserSafeAlias2, User_},
},
views::ViewToVec,
@ -12,14 +10,9 @@ use crate::{
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{
comment,
comment_report,
community,
post,
user_,
user_alias_1,
user_alias_2,
use lemmy_db_schema::{
schema::{comment, comment_report, community, post, user_, user_alias_1, user_alias_2},
source::{comment::Comment, post::Post},
};
use serde::Serialize;

View file

@ -4,9 +4,7 @@ use crate::{
fuzzy_search,
limit_and_offset,
source::{
comment::{Comment, CommentAlias1, CommentSaved},
community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
post::Post,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
},
views::ViewToVec,
@ -16,7 +14,8 @@ use crate::{
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{
use lemmy_db_schema::{
schema::{
comment,
comment_aggregates,
comment_alias_1,
@ -28,6 +27,11 @@ use lemmy_db_schema::schema::{
post,
user_,
user_alias_1,
},
source::{
comment::{Comment, CommentAlias1, CommentSaved},
post::Post,
},
};
use serde::Serialize;
@ -408,13 +412,14 @@ impl ViewToVec for CommentView {
#[cfg(test)]
mod tests {
use crate::{
source::{comment::*, community::*, post::*, user::*},
source::{community::*, user::*},
tests::establish_unpooled_connection,
views::comment_view::*,
Crud,
Likeable,
*,
};
use lemmy_db_schema::source::{comment::*, post::*};
#[test]
fn test_crud() {

View file

@ -3,14 +3,16 @@ use crate::{
source::{
community::{Community, CommunitySafe},
moderator::ModLockPost,
post::Post,
user::{UserSafe, User_},
},
views::ViewToVec,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{community, mod_lock_post, post, user_};
use lemmy_db_schema::{
schema::{community, mod_lock_post, post, user_},
source::post::Post,
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]

View file

@ -1,17 +1,18 @@
use crate::{
limit_and_offset,
source::{
comment::Comment,
community::{Community, CommunitySafe},
moderator::ModRemoveComment,
post::Post,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
},
views::ViewToVec,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{comment, community, mod_remove_comment, post, user_, user_alias_1};
use lemmy_db_schema::{
schema::{comment, community, mod_remove_comment, post, user_, user_alias_1},
source::{comment::Comment, post::Post},
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]

View file

@ -3,14 +3,16 @@ use crate::{
source::{
community::{Community, CommunitySafe},
moderator::ModRemovePost,
post::Post,
user::{UserSafe, User_},
},
views::ViewToVec,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{community, mod_remove_post, post, user_};
use lemmy_db_schema::{
schema::{community, mod_remove_post, post, user_},
source::post::Post,
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]

View file

@ -3,14 +3,16 @@ use crate::{
source::{
community::{Community, CommunitySafe},
moderator::ModStickyPost,
post::Post,
user::{UserSafe, User_},
},
views::ViewToVec,
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{community, mod_sticky_post, post, user_};
use lemmy_db_schema::{
schema::{community, mod_sticky_post, post, user_},
source::post::Post,
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone)]

View file

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

View file

@ -5,7 +5,6 @@ use crate::{
limit_and_offset,
source::{
community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
post::{Post, PostRead, PostSaved},
user::{UserSafe, User_},
},
views::ViewToVec,
@ -15,7 +14,8 @@ use crate::{
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{
use lemmy_db_schema::{
schema::{
community,
community_follower,
community_user_ban,
@ -25,6 +25,8 @@ use lemmy_db_schema::schema::{
post_read,
post_saved,
user_,
},
source::post::{Post, PostRead, PostSaved},
};
use serde::Serialize;
@ -406,13 +408,14 @@ impl ViewToVec for PostView {
mod tests {
use crate::{
aggregates::post_aggregates::PostAggregates,
source::{community::*, post::*, user::*},
source::{community::*, user::*},
tests::establish_unpooled_connection,
views::post_view::{PostQueryBuilder, PostView},
Crud,
Likeable,
*,
};
use lemmy_db_schema::source::post::*;
#[test]
fn test_crud() {

View file

@ -3,9 +3,7 @@ use crate::{
functions::hot_rank,
limit_and_offset,
source::{
comment::{Comment, CommentSaved},
community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
post::Post,
user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
user_mention::UserMention,
},
@ -15,7 +13,8 @@ use crate::{
ToSafe,
};
use diesel::{result::Error, *};
use lemmy_db_schema::schema::{
use lemmy_db_schema::{
schema::{
comment,
comment_aggregates,
comment_like,
@ -27,6 +26,11 @@ use lemmy_db_schema::schema::{
user_,
user_alias_1,
user_mention,
},
source::{
comment::{Comment, CommentSaved},
post::Post,
},
};
use serde::Serialize;

View file

@ -5,3 +5,8 @@ edition = "2018"
[dependencies]
diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] }
chrono = { version = "0.4.19", features = ["serde"] }
serde = { version = "1.0.118", features = ["derive"] }
serde_json = { version = "1.0.60", features = ["preserve_order"] }
log = "0.4.11"
url = { version = "2.2.0", features = ["serde"] }

View file

@ -1,4 +1,11 @@
#[macro_use]
extern crate diesel;
use chrono::NaiveDateTime;
pub mod schema;
pub mod source;
pub fn naive_now() -> NaiveDateTime {
chrono::prelude::Utc::now().naive_utc()
}

View file

@ -0,0 +1,190 @@
use crate::{
naive_now,
schema::{comment, comment_alias_1, comment_like, comment_saved},
source::post::Post,
};
use diesel::{result::Error, PgConnection, *};
use serde::Serialize;
use url::{ParseError, Url};
// WITH RECURSIVE MyTree AS (
// SELECT * FROM comment WHERE parent_id IS NULL
// UNION ALL
// SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
// )
// SELECT * FROM MyTree;
#[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
#[belongs_to(Post)]
#[table_name = "comment"]
pub struct Comment {
pub id: i32,
pub creator_id: i32,
pub post_id: i32,
pub parent_id: Option<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 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)
}
}
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,2 @@
pub mod comment;
pub mod post;

View file

@ -0,0 +1,229 @@
use crate::{
naive_now,
schema::{post, post_like, post_read, post_saved},
};
use diesel::{result::Error, *};
use serde::Serialize;
use url::{ParseError, Url};
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
#[table_name = "post"]
pub struct Post {
pub id: i32,
pub name: String,
pub url: Option<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 Post {
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
post.filter(id.eq(post_id)).first::<Self>(conn)
}
pub fn list_for_community(
conn: &PgConnection,
the_community_id: i32,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
post
.filter(community_id.eq(the_community_id))
.then_order_by(published.desc())
.then_order_by(stickied.desc())
.limit(20)
.load::<Self>(conn)
}
pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id))
.get_result::<Self>(conn)
}
pub fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: i32,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
let perma_deleted = "*Permananently Deleted*";
let perma_deleted_url = "https://deleted.com";
diesel::update(post.filter(creator_id.eq(for_creator_id)))
.set((
name.eq(perma_deleted),
url.eq(perma_deleted_url),
body.eq(perma_deleted),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_results::<Self>(conn)
}
pub fn update_deleted(
conn: &PgConnection,
post_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed(
conn: &PgConnection,
post_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
pub fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
use crate::schema::post::dsl::*;
let mut update = diesel::update(post).into_boxed();
update = update.filter(creator_id.eq(for_creator_id));
if let Some(for_community_id) = for_community_id {
update = update.filter(community_id.eq(for_community_id));
}
update
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(conn)
}
pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(locked.eq(new_locked))
.get_result::<Self>(conn)
}
pub fn update_stickied(
conn: &PgConnection,
post_id: i32,
new_stickied: bool,
) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied))
.get_result::<Self>(conn)
}
pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
user_id == post_creator_id
}
}
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

@ -10,6 +10,7 @@ path = "src/lib.rs"
[dependencies]
lemmy_db = { path = "../lemmy_db" }
lemmy_db_schema = { path = "../lemmy_db_schema" }
lemmy_utils = { path = "../lemmy_utils" }
serde = { version = "1.0.118", features = ["derive"] }
log = "0.4.11"

View file

@ -8,14 +8,13 @@ pub mod websocket;
use diesel::PgConnection;
use lemmy_db::{
source::{
comment::Comment,
post::Post,
user::User_,
user_mention::{UserMention, UserMentionForm},
},
Crud,
DbPool,
};
use lemmy_db_schema::source::{comment::Comment, post::Post};
use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
use log::error;
use serde::{Deserialize, Serialize};

View file

@ -12,6 +12,7 @@ path = "src/lib.rs"
lemmy_utils = { path = "../lemmy_utils" }
lemmy_structs = { path = "../lemmy_structs" }
lemmy_db = { path = "../lemmy_db" }
lemmy_db_schema = { path = "../lemmy_db_schema" }
lemmy_rate_limit = { path = "../lemmy_rate_limit" }
reqwest = { version = "0.10.10", features = ["json"] }
log = "0.4.11"

View file

@ -3,7 +3,7 @@ use crate::{
messages::*,
};
use actix::{Actor, Context, Handler, ResponseFuture};
use lemmy_db::naive_now;
use lemmy_db_schema::naive_now;
use log::{error, info};
use rand::Rng;
use serde::Serialize;

View file

@ -4,16 +4,17 @@ use diesel::{
*,
};
use lemmy_db::{
naive_now,
source::{
comment::Comment,
community::{Community, CommunityForm},
post::Post,
private_message::PrivateMessage,
user::{UserForm, User_},
},
Crud,
};
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, post::Post},
};
use lemmy_utils::{
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
settings::Settings,