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-10-17 16:35:51 +00:00
|
|
|
traits::{Crud, Likeable, Saveable},
|
2023-12-11 10:24:51 +00:00
|
|
|
utils::{
|
2023-12-15 10:34:17 +00:00
|
|
|
functions::coalesce,
|
2023-12-11 10:24:51 +00:00
|
|
|
get_conn,
|
|
|
|
naive_now,
|
|
|
|
DbPool,
|
|
|
|
DELETED_REPLACEMENT_TEXT,
|
|
|
|
FETCH_LIMIT_MAX,
|
|
|
|
SITEMAP_DAYS,
|
|
|
|
SITEMAP_LIMIT,
|
|
|
|
},
|
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;
|
2023-10-17 16:35:51 +00:00
|
|
|
use std::collections::HashSet;
|
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<'_>,
|
2023-08-28 09:09:20 +00:00
|
|
|
) -> Result<Vec<(DbUrl, chrono::DateTime<Utc>)>, Error> {
|
2023-08-22 14:30:15 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
post
|
|
|
|
.select((ap_id, coalesce(updated, published)))
|
2023-08-28 09:09:20 +00:00
|
|
|
.filter(local.eq(true))
|
2023-08-22 14:30:15 +00:00
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(removed.eq(false))
|
2023-12-11 10:24:51 +00:00
|
|
|
.filter(published.ge(Utc::now().naive_utc() - Duration::days(SITEMAP_DAYS)))
|
2023-08-22 14:30:15 +00:00
|
|
|
.order(published.desc())
|
2023-12-11 10:24:51 +00:00
|
|
|
.limit(SITEMAP_LIMIT)
|
2023-08-28 09:09:20 +00:00
|
|
|
.load::<(DbUrl, chrono::DateTime<Utc>)>(conn)
|
2023-08-22 14:30:15 +00:00
|
|
|
.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?;
|
Remove id column and use different primary key on some tables (#4093)
* post_saved
* fmt
* remove unique and not null
* put person_id first in primary key and remove index
* use post_saved.find
* change captcha_answer
* remove removal of not null
* comment_aggregates
* comment_like
* comment_saved
* aggregates
* remove "\"
* deduplicate site_aggregates
* person_post_aggregates
* community_moderator
* community_block
* community_person_ban
* custom_emoji_keyword
* federation allow/block list
* federation_queue_state
* instance_block
* local_site_rate_limit, local_user_language, login_token
* person_ban, person_block, person_follower, post_like, post_read, received_activity
* community_follower, community_language, site_language
* fmt
* image_upload
* remove unused newtypes
* remove more indexes
* use .find
* merge
* fix site_aggregates_site function
* fmt
* Primary keys dess (#17)
* Also order reports by oldest first (ref #4123) (#4129)
* Support signed fetch for federation (fixes #868) (#4125)
* Support signed fetch for federation (fixes #868)
* taplo
* add federation queue state to get_federated_instances api (#4104)
* add federation queue state to get_federated_instances api
* feature gate
* move retry sleep function
* move stuff around
* Add UI setting for collapsing bot comments. Fixes #3838 (#4098)
* Add UI setting for collapsing bot comments. Fixes #3838
* Fixing clippy check.
* Only keep sent and received activities for 7 days (fixes #4113, fixes #4110) (#4131)
* Only check auth secure on release mode. (#4127)
* Only check auth secure on release mode.
* Fixing wrong js-client.
* Adding is_debug_mode var.
* Fixing the desktop image on the README. (#4135)
* Delete dupes and add possibly missing unique constraint on person_aggregates.
* Fixing clippy lints.
---------
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
* fmt
* Update community_block.rs
* Update instance_block.rs
* Update person_block.rs
* Update person_block.rs
---------
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
2023-11-13 13:14:07 +00:00
|
|
|
diesel::delete(dsl::post_like.find((person_id, post_id)))
|
|
|
|
.execute(conn)
|
|
|
|
.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> {
|
Remove id column and use different primary key on some tables (#4093)
* post_saved
* fmt
* remove unique and not null
* put person_id first in primary key and remove index
* use post_saved.find
* change captcha_answer
* remove removal of not null
* comment_aggregates
* comment_like
* comment_saved
* aggregates
* remove "\"
* deduplicate site_aggregates
* person_post_aggregates
* community_moderator
* community_block
* community_person_ban
* custom_emoji_keyword
* federation allow/block list
* federation_queue_state
* instance_block
* local_site_rate_limit, local_user_language, login_token
* person_ban, person_block, person_follower, post_like, post_read, received_activity
* community_follower, community_language, site_language
* fmt
* image_upload
* remove unused newtypes
* remove more indexes
* use .find
* merge
* fix site_aggregates_site function
* fmt
* Primary keys dess (#17)
* Also order reports by oldest first (ref #4123) (#4129)
* Support signed fetch for federation (fixes #868) (#4125)
* Support signed fetch for federation (fixes #868)
* taplo
* add federation queue state to get_federated_instances api (#4104)
* add federation queue state to get_federated_instances api
* feature gate
* move retry sleep function
* move stuff around
* Add UI setting for collapsing bot comments. Fixes #3838 (#4098)
* Add UI setting for collapsing bot comments. Fixes #3838
* Fixing clippy check.
* Only keep sent and received activities for 7 days (fixes #4113, fixes #4110) (#4131)
* Only check auth secure on release mode. (#4127)
* Only check auth secure on release mode.
* Fixing wrong js-client.
* Adding is_debug_mode var.
* Fixing the desktop image on the README. (#4135)
* Delete dupes and add possibly missing unique constraint on person_aggregates.
* Fixing clippy lints.
---------
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
* fmt
* Update community_block.rs
* Update instance_block.rs
* Update person_block.rs
* Update person_block.rs
---------
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
2023-11-13 13:14:07 +00:00
|
|
|
use crate::schema::post_saved::dsl::post_saved;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
Remove id column and use different primary key on some tables (#4093)
* post_saved
* fmt
* remove unique and not null
* put person_id first in primary key and remove index
* use post_saved.find
* change captcha_answer
* remove removal of not null
* comment_aggregates
* comment_like
* comment_saved
* aggregates
* remove "\"
* deduplicate site_aggregates
* person_post_aggregates
* community_moderator
* community_block
* community_person_ban
* custom_emoji_keyword
* federation allow/block list
* federation_queue_state
* instance_block
* local_site_rate_limit, local_user_language, login_token
* person_ban, person_block, person_follower, post_like, post_read, received_activity
* community_follower, community_language, site_language
* fmt
* image_upload
* remove unused newtypes
* remove more indexes
* use .find
* merge
* fix site_aggregates_site function
* fmt
* Primary keys dess (#17)
* Also order reports by oldest first (ref #4123) (#4129)
* Support signed fetch for federation (fixes #868) (#4125)
* Support signed fetch for federation (fixes #868)
* taplo
* add federation queue state to get_federated_instances api (#4104)
* add federation queue state to get_federated_instances api
* feature gate
* move retry sleep function
* move stuff around
* Add UI setting for collapsing bot comments. Fixes #3838 (#4098)
* Add UI setting for collapsing bot comments. Fixes #3838
* Fixing clippy check.
* Only keep sent and received activities for 7 days (fixes #4113, fixes #4110) (#4131)
* Only check auth secure on release mode. (#4127)
* Only check auth secure on release mode.
* Fixing wrong js-client.
* Adding is_debug_mode var.
* Fixing the desktop image on the README. (#4135)
* Delete dupes and add possibly missing unique constraint on person_aggregates.
* Fixing clippy lints.
---------
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
* fmt
* Update community_block.rs
* Update instance_block.rs
* Update person_block.rs
* Update person_block.rs
---------
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
2023-11-13 13:14:07 +00:00
|
|
|
diesel::delete(post_saved.find((post_saved_form.person_id, post_saved_form.post_id)))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 16:35:51 +00:00
|
|
|
impl PostRead {
|
|
|
|
pub async fn mark_as_read(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2023-10-17 16:35:51 +00:00
|
|
|
post_ids: HashSet<PostId>,
|
|
|
|
person_id: PersonId,
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
use crate::schema::post_read::dsl::post_read;
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-10-17 16:35:51 +00:00
|
|
|
|
|
|
|
let forms = post_ids
|
|
|
|
.into_iter()
|
|
|
|
.map(|post_id| PostReadForm { post_id, person_id })
|
|
|
|
.collect::<Vec<PostReadForm>>();
|
2019-04-20 04:06:25 +00:00
|
|
|
insert_into(post_read)
|
2023-10-17 16:35:51 +00:00
|
|
|
.values(forms)
|
|
|
|
.on_conflict_do_nothing()
|
|
|
|
.execute(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-10-17 16:35:51 +00:00
|
|
|
pub async fn mark_as_unread(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2023-10-17 16:35:51 +00:00
|
|
|
post_id_: HashSet<PostId>,
|
|
|
|
person_id_: PersonId,
|
2023-07-11 13:09:59 +00:00
|
|
|
) -> 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?;
|
2023-10-17 16:35:51 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(
|
|
|
|
post_read
|
2023-10-17 16:35:51 +00:00
|
|
|
.filter(post_id.eq_any(post_id_))
|
|
|
|
.filter(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-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,
|
|
|
|
PostSaved,
|
|
|
|
PostSavedForm,
|
|
|
|
PostUpdateForm,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
2023-10-17 16:35:51 +00:00
|
|
|
traits::{Crud, Likeable, 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;
|
2023-10-17 16:35:51 +00:00
|
|
|
use std::collections::HashSet;
|
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
|
|
|
|
2023-10-17 16:35:51 +00:00
|
|
|
let new_post2 = PostInsertForm::builder()
|
|
|
|
.name("A test post 2".into())
|
|
|
|
.creator_id(inserted_person.id)
|
|
|
|
.community_id(inserted_community.id)
|
|
|
|
.build();
|
|
|
|
let inserted_post2 = Post::create(pool, &new_post2).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 {
|
|
|
|
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 {
|
|
|
|
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
|
2023-10-17 16:35:51 +00:00
|
|
|
let marked_as_read = PostRead::mark_as_read(
|
|
|
|
pool,
|
|
|
|
HashSet::from([inserted_post.id, inserted_post2.id]),
|
|
|
|
inserted_person.id,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(2, marked_as_read);
|
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();
|
2023-10-17 16:35:51 +00:00
|
|
|
assert_eq!(1, like_removed);
|
2022-11-09 10:05:00 +00:00
|
|
|
let saved_removed = PostSaved::unsave(pool, &post_saved_form).await.unwrap();
|
2023-10-17 16:35:51 +00:00
|
|
|
assert_eq!(1, saved_removed);
|
|
|
|
let read_removed = PostRead::mark_as_unread(
|
|
|
|
pool,
|
|
|
|
HashSet::from([inserted_post.id, inserted_post2.id]),
|
|
|
|
inserted_person.id,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(2, read_removed);
|
|
|
|
|
|
|
|
let num_deleted = Post::delete(pool, inserted_post.id).await.unwrap()
|
|
|
|
+ Post::delete(pool, inserted_post2.id).await.unwrap();
|
|
|
|
assert_eq!(2, num_deleted);
|
2022-11-09 10:05:00 +00:00
|
|
|
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);
|
2019-03-06 01:00:01 +00:00
|
|
|
}
|
|
|
|
}
|