2023-08-22 14:30:15 +00:00
|
|
|
use super::instance::coalesce;
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{CommunityId, DbUrl, PersonId, PostId},
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::post::dsl::{
|
|
|
|
ap_id,
|
|
|
|
body,
|
|
|
|
community_id,
|
|
|
|
creator_id,
|
|
|
|
deleted,
|
2022-12-12 11:17:10 +00:00
|
|
|
featured_community,
|
2023-08-22 14:30:15 +00:00
|
|
|
local,
|
2022-11-19 04:33:54 +00:00
|
|
|
name,
|
|
|
|
post,
|
|
|
|
published,
|
|
|
|
removed,
|
|
|
|
thumbnail_url,
|
|
|
|
updated,
|
|
|
|
url,
|
|
|
|
},
|
2020-12-18 18:38:32 +00:00
|
|
|
source::post::{
|
|
|
|
Post,
|
2022-10-27 09:24:07 +00:00
|
|
|
PostInsertForm,
|
2020-12-18 18:38:32 +00:00
|
|
|
PostLike,
|
|
|
|
PostLikeForm,
|
|
|
|
PostRead,
|
|
|
|
PostReadForm,
|
|
|
|
PostSaved,
|
|
|
|
PostSavedForm,
|
2022-10-27 09:24:07 +00:00
|
|
|
PostUpdateForm,
|
2020-12-18 18:38:32 +00:00
|
|
|
},
|
2023-03-01 03:46:15 +00:00
|
|
|
traits::{Crud, Likeable, Readable, Saveable},
|
2023-06-26 08:45:37 +00:00
|
|
|
utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
|
2020-08-12 14:45:04 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use ::url::Url;
|
2023-08-22 14:30:15 +00:00
|
|
|
use chrono::{Duration, Utc};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2020-07-17 21:11:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Post {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = PostInsertForm;
|
|
|
|
type UpdateForm = PostUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = PostId;
|
2020-12-08 17:38:48 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
insert_into(post)
|
|
|
|
.values(form)
|
|
|
|
.on_conflict(ap_id)
|
|
|
|
.do_update()
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-12-08 17:38:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-10-27 09:24:07 +00:00
|
|
|
post_id: PostId,
|
|
|
|
new_post: &Self::UpdateForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-12-08 17:38:48 +00:00
|
|
|
diesel::update(post.find(post_id))
|
|
|
|
.set(new_post)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-12-08 17:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Post {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn list_for_community(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
the_community_id: CommunityId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-04-03 09:00:24 +00:00
|
|
|
post
|
|
|
|
.filter(community_id.eq(the_community_id))
|
2022-04-01 18:18:25 +00:00
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(removed.eq(false))
|
2022-12-12 11:17:10 +00:00
|
|
|
.then_order_by(featured_community.desc())
|
2023-01-04 14:58:11 +00:00
|
|
|
.then_order_by(published.desc())
|
2023-02-18 14:50:28 +00:00
|
|
|
.limit(FETCH_LIMIT_MAX)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list_featured_for_community(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2023-02-18 14:50:28 +00:00
|
|
|
the_community_id: CommunityId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
post
|
|
|
|
.filter(community_id.eq(the_community_id))
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(removed.eq(false))
|
|
|
|
.filter(featured_community.eq(true))
|
|
|
|
.then_order_by(published.desc())
|
2022-07-08 10:21:33 +00:00
|
|
|
.limit(FETCH_LIMIT_MAX)
|
2020-04-03 09:00:24 +00:00
|
|
|
.load::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-04-03 09:00:24 +00:00
|
|
|
}
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2023-08-22 14:30:15 +00:00
|
|
|
pub async fn list_for_sitemap(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
) -> Result<Vec<(DbUrl, chrono::NaiveDateTime)>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
post
|
|
|
|
.select((ap_id, coalesce(updated, published)))
|
|
|
|
.filter(local)
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(removed.eq(false))
|
|
|
|
.filter(published.ge(Utc::now().naive_utc() - Duration::days(1)))
|
|
|
|
.order(published.desc())
|
|
|
|
.load::<(DbUrl, chrono::NaiveDateTime)>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn permadelete_for_creator(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_creator_id: PersonId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2020-08-17 18:12:36 +00:00
|
|
|
diesel::update(post.filter(creator_id.eq(for_creator_id)))
|
2020-04-04 00:04:57 +00:00
|
|
|
.set((
|
2023-06-20 06:17:54 +00:00
|
|
|
name.eq(DELETED_REPLACEMENT_TEXT),
|
2023-06-26 08:45:37 +00:00
|
|
|
url.eq(Option::<&str>::None),
|
2023-06-20 06:17:54 +00:00
|
|
|
body.eq(DELETED_REPLACEMENT_TEXT),
|
2020-04-04 00:04:57 +00:00
|
|
|
deleted.eq(true),
|
|
|
|
updated.eq(naive_now()),
|
|
|
|
))
|
2020-08-17 18:12:36 +00:00
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-04-04 00:04:57 +00:00
|
|
|
}
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn update_removed_for_creator(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
for_creator_id: PersonId,
|
|
|
|
for_community_id: Option<CommunityId>,
|
2020-08-17 18:12:36 +00:00
|
|
|
new_removed: bool,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-08-17 18:12:36 +00:00
|
|
|
|
|
|
|
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)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2020-08-17 18:12:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
pub 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-04-20 04:06:25 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read_from_apub_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
object_id: Url,
|
|
|
|
) -> Result<Option<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
let object_id: DbUrl = object_id.into();
|
|
|
|
Ok(
|
|
|
|
post
|
|
|
|
.filter(ap_id.eq(object_id))
|
|
|
|
.first::<Post>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-10-18 21:36:44 +00:00
|
|
|
.ok()
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn fetch_pictrs_posts_for_creator(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-06-13 19:15:04 +00:00
|
|
|
for_creator_id: PersonId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
let pictrs_search = "%pictrs/image%";
|
|
|
|
|
|
|
|
post
|
|
|
|
.filter(creator_id.eq(for_creator_id))
|
|
|
|
.filter(url.like(pictrs_search))
|
|
|
|
.load::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the url and thumbnails fields to None
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn remove_pictrs_post_images_and_thumbnails_for_creator(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-06-13 19:15:04 +00:00
|
|
|
for_creator_id: PersonId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
let pictrs_search = "%pictrs/image%";
|
|
|
|
|
|
|
|
diesel::update(
|
|
|
|
post
|
|
|
|
.filter(creator_id.eq(for_creator_id))
|
|
|
|
.filter(url.like(pictrs_search)),
|
|
|
|
)
|
|
|
|
.set((
|
|
|
|
url.eq::<Option<String>>(None),
|
|
|
|
thumbnail_url.eq::<Option<String>>(None),
|
|
|
|
))
|
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn fetch_pictrs_posts_for_community(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-06-13 19:15:04 +00:00
|
|
|
for_community_id: CommunityId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
let pictrs_search = "%pictrs/image%";
|
|
|
|
post
|
|
|
|
.filter(community_id.eq(for_community_id))
|
|
|
|
.filter(url.like(pictrs_search))
|
|
|
|
.load::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the url and thumbnails fields to None
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn remove_pictrs_post_images_and_thumbnails_for_community(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-06-13 19:15:04 +00:00
|
|
|
for_community_id: CommunityId,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
let pictrs_search = "%pictrs/image%";
|
|
|
|
|
|
|
|
diesel::update(
|
|
|
|
post
|
|
|
|
.filter(community_id.eq(for_community_id))
|
|
|
|
.filter(url.like(pictrs_search)),
|
|
|
|
)
|
|
|
|
.set((
|
|
|
|
url.eq::<Option<String>>(None),
|
|
|
|
thumbnail_url.eq::<Option<String>>(None),
|
|
|
|
))
|
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Likeable for PostLike {
|
|
|
|
type Form = PostLikeForm;
|
|
|
|
type IdType = PostId;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn like(pool: &mut DbPool<'_>, post_like_form: &PostLikeForm) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::post_like::dsl::{person_id, post_id, post_like};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2020-12-16 14:42:57 +00:00
|
|
|
.do_update()
|
|
|
|
.set(post_like_form)
|
2019-03-23 01:42:57 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-03-06 01:00:01 +00:00
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn remove(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
person_id: PersonId,
|
|
|
|
post_id: PostId,
|
|
|
|
) -> Result<usize, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::post_like::dsl;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
2020-08-12 14:43:45 +00:00
|
|
|
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)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-03-06 01:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Saveable for PostSaved {
|
|
|
|
type Form = PostSavedForm;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn save(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-04-20 04:06:25 +00:00
|
|
|
insert_into(post_saved)
|
|
|
|
.values(post_saved_form)
|
2021-02-26 13:49:58 +00:00
|
|
|
.on_conflict((post_id, person_id))
|
2020-12-16 14:42:57 +00:00
|
|
|
.do_update()
|
|
|
|
.set(post_saved_form)
|
2019-04-20 04:06:25 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn unsave(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
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)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Readable for PostRead {
|
|
|
|
type Form = PostReadForm;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn mark_as_read(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
post_read_form: &PostReadForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::post_read::dsl::{person_id, post_id, post_read};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-04-20 04:06:25 +00:00
|
|
|
insert_into(post_read)
|
|
|
|
.values(post_read_form)
|
2021-04-24 22:26:50 +00:00
|
|
|
.on_conflict((post_id, person_id))
|
|
|
|
.do_update()
|
|
|
|
.set(post_read_form)
|
2019-04-20 04:06:25 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
2020-08-12 12:30:52 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn mark_as_unread(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
post_read_form: &PostReadForm,
|
|
|
|
) -> Result<usize, Error> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use crate::schema::post_read::dsl::{person_id, post_id, post_read};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
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)),
|
2019-09-07 15:35:05 +00:00
|
|
|
)
|
|
|
|
.execute(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 01:00:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::indexing_slicing)]
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
community::{Community, CommunityInsertForm},
|
|
|
|
instance::Instance,
|
2022-11-19 04:33:54 +00:00
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
post::{
|
|
|
|
Post,
|
|
|
|
PostInsertForm,
|
|
|
|
PostLike,
|
|
|
|
PostLikeForm,
|
|
|
|
PostRead,
|
|
|
|
PostReadForm,
|
|
|
|
PostSaved,
|
|
|
|
PostSavedForm,
|
|
|
|
PostUpdateForm,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
|
|
|
traits::{Crud, Likeable, Readable, Saveable},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2020-05-16 14:04:08 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_crud() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2023-03-01 02:36:57 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let new_person = PersonInsertForm::builder()
|
|
|
|
.name("jim".into())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_person = Person::create(pool, &new_person).await.unwrap();
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_community = CommunityInsertForm::builder()
|
|
|
|
.name("test community_3".to_string())
|
|
|
|
.title("nada".to_owned())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
.build();
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_community = Community::create(pool, &new_community).await.unwrap();
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let new_post = PostInsertForm::builder()
|
|
|
|
.name("A test post".into())
|
|
|
|
.creator_id(inserted_person.id)
|
|
|
|
.community_id(inserted_community.id)
|
|
|
|
.build();
|
2019-03-06 01:00:01 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_post = Post::create(pool, &new_post).await.unwrap();
|
2019-03-06 01:00:01 +00:00
|
|
|
|
|
|
|
let expected_post = Post {
|
|
|
|
id: inserted_post.id,
|
|
|
|
name: "A test post".into(),
|
2019-03-26 18:00:18 +00:00
|
|
|
url: None,
|
|
|
|
body: None,
|
2021-02-26 13:49:58 +00:00
|
|
|
creator_id: inserted_person.id,
|
2019-03-26 18:00:18 +00:00
|
|
|
community_id: inserted_community.id,
|
2019-03-06 01:00:01 +00:00
|
|
|
published: inserted_post.published,
|
2019-04-20 04:06:25 +00:00
|
|
|
removed: false,
|
|
|
|
locked: false,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: false,
|
2019-09-07 15:35:05 +00:00
|
|
|
updated: None,
|
2020-03-07 23:31:13 +00:00
|
|
|
embed_title: None,
|
|
|
|
embed_description: None,
|
2022-06-02 21:44:47 +00:00
|
|
|
embed_video_url: None,
|
2020-03-07 23:31:13 +00:00
|
|
|
thumbnail_url: None,
|
2022-11-19 04:33:54 +00:00
|
|
|
ap_id: inserted_post.ap_id.clone(),
|
2020-04-04 00:04:57 +00:00
|
|
|
local: true,
|
2022-08-18 19:11:19 +00:00
|
|
|
language_id: Default::default(),
|
2022-12-12 11:17:10 +00:00
|
|
|
featured_community: false,
|
|
|
|
featured_local: false,
|
2019-03-06 01:00:01 +00:00
|
|
|
};
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Post Like
|
2019-03-06 01:00:01 +00:00
|
|
|
let post_like_form = PostLikeForm {
|
2019-03-23 01:42:57 +00:00
|
|
|
post_id: inserted_post.id,
|
2021-02-26 13:49:58 +00:00
|
|
|
person_id: inserted_person.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
score: 1,
|
2019-03-06 01:00:01 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_post_like = PostLike::like(pool, &post_like_form).await.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,
|
2019-09-07 15:35:05 +00:00
|
|
|
score: 1,
|
2019-03-06 01:00:01 +00:00
|
|
|
};
|
2019-04-20 04:06:25 +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,
|
2019-04-20 04:06:25 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_post_saved = PostSaved::save(pool, &post_saved_form).await.unwrap();
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
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,
|
2019-04-20 04:06:25 +00:00
|
|
|
published: inserted_post_saved.published,
|
|
|
|
};
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// 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
|
|
|
};
|
2019-04-20 04:06:25 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let inserted_post_read = PostRead::mark_as_read(pool, &post_read_form).await.unwrap();
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
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,
|
2019-04-20 04:06:25 +00:00
|
|
|
};
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let read_post = Post::read(pool, inserted_post.id).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let new_post_update = PostUpdateForm {
|
|
|
|
name: Some("A test post".into()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
let updated_post = Post::update(pool, inserted_post.id, &new_post_update)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let like_removed = PostLike::remove(pool, inserted_person.id, inserted_post.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let saved_removed = PostSaved::unsave(pool, &post_saved_form).await.unwrap();
|
|
|
|
let read_removed = PostRead::mark_as_unread(pool, &post_read_form)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let num_deleted = Post::delete(pool, inserted_post.id).await.unwrap();
|
|
|
|
Community::delete(pool, inserted_community.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Person::delete(pool, inserted_person.id).await.unwrap();
|
|
|
|
Instance::delete(pool, inserted_instance.id).await.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);
|
2019-04-20 04:06:25 +00:00
|
|
|
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);
|
2019-04-20 04:06:25 +00:00
|
|
|
assert_eq!(1, saved_removed);
|
|
|
|
assert_eq!(1, read_removed);
|
2019-03-06 01:00:01 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|