lemmy/crates/db_queries/src/source/post.rs

432 lines
12 KiB
Rust
Raw Normal View History

use crate::{ApubObject, Crud, Likeable, Readable, Saveable};
2020-05-16 14:04:08 +00:00
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
2020-08-12 14:45:04 +00:00
naive_now,
source::post::{
Post,
PostForm,
PostLike,
PostLikeForm,
PostRead,
PostReadForm,
PostSaved,
PostSavedForm,
},
CommunityId,
DbUrl,
PersonId,
PostId,
2020-08-12 14:45:04 +00:00
};
impl Crud<PostForm, PostId> for Post {
fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: PostId) -> Result<usize, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn)
}
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: PostId, new_post: &PostForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(conn)
}
}
pub trait Post_ {
//fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
fn list_for_community(
conn: &PgConnection,
the_community_id: CommunityId,
) -> Result<Vec<Post>, Error>;
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Post, Error>;
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
) -> Result<Vec<Post>, Error>;
fn update_deleted(conn: &PgConnection, post_id: PostId, new_deleted: bool)
-> Result<Post, Error>;
fn update_removed(conn: &PgConnection, post_id: PostId, new_removed: bool)
-> Result<Post, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_community_id: Option<CommunityId>,
new_removed: bool,
) -> Result<Vec<Post>, Error>;
fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Post, Error>;
fn update_stickied(
conn: &PgConnection,
post_id: PostId,
new_stickied: bool,
) -> Result<Post, Error>;
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool;
}
impl Post_ for Post {
fn list_for_community(
conn: &PgConnection,
the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post
.filter(community_id.eq(the_community_id))
2020-07-24 15:07:33 +00:00
.then_order_by(published.desc())
.then_order_by(stickied.desc())
.limit(20)
.load::<Self>(conn)
}
2020-04-04 00:04:57 +00:00
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
2020-04-04 00:04:57 +00:00
diesel::update(post.find(post_id))
.set(ap_id.eq(apub_id))
2020-04-04 00:04:57 +00:00
.get_result::<Self>(conn)
}
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
2020-04-04 00:04:57 +00:00
let perma_deleted = "*Permananently Deleted*";
let perma_deleted_url = "https://deleted.com";
diesel::update(post.filter(creator_id.eq(for_creator_id)))
2020-04-04 00:04:57 +00:00
.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)
2020-04-04 00:04:57 +00:00
}
fn update_deleted(
conn: &PgConnection,
post_id: PostId,
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)
}
fn update_removed(
conn: &PgConnection,
post_id: PostId,
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)
}
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_community_id: Option<CommunityId>,
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)
}
fn update_locked(conn: &PgConnection, post_id: PostId, 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)
}
fn update_stickied(
conn: &PgConnection,
post_id: PostId,
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)
}
2020-07-21 17:52:57 +00:00
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
2021-02-26 13:49:58 +00:00
person_id == post_creator_id
2020-07-21 17:52:57 +00:00
}
2019-03-06 01:00:01 +00:00
}
impl ApubObject<PostForm> for Post {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
use lemmy_db_schema::schema::post::dsl::*;
insert_into(post)
.values(post_form)
.on_conflict(ap_id)
.do_update()
.set(post_form)
.get_result::<Self>(conn)
}
}
impl Likeable<PostLikeForm, PostId> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_like::dsl::*;
2019-03-06 01:00:01 +00:00
insert_into(post_like)
.values(post_like_form)
2021-02-26 13:49:58 +00:00
.on_conflict((post_id, person_id))
.do_update()
.set(post_like_form)
.get_result::<Self>(conn)
2019-03-06 01:00:01 +00:00
}
fn remove(conn: &PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_like::dsl;
diesel::delete(
dsl::post_like
.filter(dsl::post_id.eq(post_id))
2021-02-26 13:49:58 +00:00
.filter(dsl::person_id.eq(person_id)),
)
.execute(conn)
2019-03-06 01:00:01 +00:00
}
}
impl Saveable<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_saved::dsl::*;
insert_into(post_saved)
.values(post_saved_form)
2021-02-26 13:49:58 +00:00
.on_conflict((post_id, person_id))
.do_update()
.set(post_saved_form)
.get_result::<Self>(conn)
}
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_saved::dsl::*;
diesel::delete(
post_saved
.filter(post_id.eq(post_saved_form.post_id))
2021-02-26 13:49:58 +00:00
.filter(person_id.eq(post_saved_form.person_id)),
)
.execute(conn)
}
}
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::*;
insert_into(post_read)
.values(post_read_form)
.get_result::<Self>(conn)
}
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_read::dsl::*;
diesel::delete(
post_read
.filter(post_id.eq(post_read_form.post_id))
2021-02-26 13:49:58 +00:00
.filter(person_id.eq(post_read_form.person_id)),
)
.execute(conn)
}
}
2019-03-06 01:00:01 +00:00
#[cfg(test)]
mod tests {
2021-03-10 22:33:55 +00:00
use crate::{establish_unpooled_connection, source::post::*};
2020-12-21 12:28:12 +00:00
use lemmy_db_schema::source::{
community::{Community, CommunityForm},
2021-02-26 13:49:58 +00:00
person::*,
2020-05-16 14:04:08 +00:00
};
use serial_test::serial;
2020-05-16 14:04:08 +00:00
#[test]
#[serial]
2019-03-06 01:00:01 +00:00
fn test_crud() {
let conn = establish_unpooled_connection();
2021-02-26 13:49:58 +00:00
let new_person = PersonForm {
name: "jim".into(),
preferred_username: None,
avatar: None,
banner: None,
2021-03-10 22:33:55 +00:00
banned: None,
deleted: None,
published: None,
2019-08-14 03:15:52 +00:00
updated: None,
actor_id: None,
bio: None,
2021-03-10 22:33:55 +00:00
local: None,
private_key: None,
public_key: None,
last_refreshed_at: None,
inbox_url: None,
shared_inbox_url: None,
};
2021-02-26 13:49:58 +00:00
let inserted_person = Person::create(&conn, &new_person).unwrap();
let new_community = CommunityForm {
name: "test community_3".to_string(),
title: "nada".to_owned(),
description: None,
2021-02-26 13:49:58 +00:00
creator_id: inserted_person.id,
2019-04-20 07:24:59 +00:00
removed: None,
deleted: None,
2019-08-14 03:15:52 +00:00
updated: None,
nsfw: false,
actor_id: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
published: None,
icon: None,
banner: None,
followers_url: None,
inbox_url: None,
shared_inbox_url: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
2019-03-06 01:00:01 +00:00
let new_post = PostForm {
name: "A test post".into(),
url: None,
body: None,
2021-02-26 13:49:58 +00:00
creator_id: inserted_person.id,
community_id: inserted_community.id,
2019-04-20 07:24:59 +00:00
removed: None,
deleted: None,
2019-04-20 07:24:59 +00:00
locked: None,
2019-09-09 06:14:13 +00:00
stickied: None,
nsfw: false,
updated: None,
embed_title: None,
embed_description: None,
embed_html: None,
thumbnail_url: None,
ap_id: None,
2020-04-04 00:04:57 +00:00
local: true,
published: None,
2019-03-06 01:00:01 +00:00
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
2019-03-06 01:00:01 +00:00
let expected_post = Post {
id: inserted_post.id,
name: "A test post".into(),
url: None,
body: None,
2021-02-26 13:49:58 +00:00
creator_id: inserted_person.id,
community_id: inserted_community.id,
2019-03-06 01:00:01 +00:00
published: inserted_post.published,
removed: false,
locked: false,
2019-09-09 06:14:13 +00:00
stickied: false,
nsfw: false,
deleted: false,
updated: None,
embed_title: None,
embed_description: None,
embed_html: None,
thumbnail_url: None,
ap_id: inserted_post.ap_id.to_owned(),
2020-04-04 00:04:57 +00:00
local: true,
2019-03-06 01:00:01 +00:00
};
// Post Like
2019-03-06 01:00:01 +00:00
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
score: 1,
2019-03-06 01:00:01 +00:00
};
let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
2019-03-06 01:00:01 +00:00
let expected_post_like = PostLike {
id: inserted_post_like.id,
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
2019-03-06 01:00:01 +00:00
published: inserted_post_like.published,
score: 1,
2019-03-06 01:00:01 +00:00
};
// Post Save
let post_saved_form = PostSavedForm {
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
};
let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
let expected_post_saved = PostSaved {
id: inserted_post_saved.id,
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
published: inserted_post_saved.published,
};
// Post Read
let post_read_form = PostReadForm {
2020-08-12 14:45:04 +00:00
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
2020-08-12 14:45:04 +00:00
};
let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
let expected_post_read = PostRead {
2020-08-12 14:45:04 +00:00
id: inserted_post_read.id,
post_id: inserted_post.id,
2021-02-26 13:49:58 +00:00
person_id: inserted_person.id,
2020-08-12 14:45:04 +00:00
published: inserted_post_read.published,
};
let read_post = Post::read(&conn, inserted_post.id).unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
2021-02-26 13:49:58 +00:00
let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
2021-02-26 13:49:58 +00:00
Person::delete(&conn, inserted_person.id).unwrap();
2019-03-06 01:00:01 +00:00
assert_eq!(expected_post, read_post);
assert_eq!(expected_post, inserted_post);
assert_eq!(expected_post, updated_post);
assert_eq!(expected_post_like, inserted_post_like);
assert_eq!(expected_post_saved, inserted_post_saved);
assert_eq!(expected_post_read, inserted_post_read);
2019-03-06 01:00:01 +00:00
assert_eq!(1, like_removed);
assert_eq!(1, saved_removed);
assert_eq!(1, read_removed);
2019-03-06 01:00:01 +00:00
assert_eq!(1, num_deleted);
}
}