created new table post_keyword_block related to a person and corresponding functions in diesel

This commit is contained in:
lseeger 2024-12-14 15:32:26 +01:00
parent 8d91543a13
commit 924b76a6e6
7 changed files with 113 additions and 0 deletions

View file

@ -36,3 +36,4 @@ pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tagline;
mod post_keyword_block;

View file

@ -0,0 +1,53 @@
use diesel::{delete, insert_into};
use diesel::result::Error;
use diesel::prelude::*;
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use crate::{
newtypes::{PersonId},
schema::post_keyword_block,
source::{
post_keyword_block::{PostKeywordBlock, PostKeywordBlockForm},
}
};
use crate::traits::Crud;
use crate::utils::{get_conn, DbPool};
impl PostKeywordBlock {
pub async fn for_person(
pool: &mut DbPool<'_>,
person_id: PersonId,
) -> Result<Vec<PostKeywordBlock>, Error> {
let conn = &mut get_conn(pool).await?;
post_keyword_block::table
.filter(post_keyword_block::person_id.eq(person_id))
.load::<PostKeywordBlock>(conn)
.await
}
pub async fn block_keyword(
pool: &mut DbPool<'_>,
post_keyword_block_form: &PostKeywordBlockForm
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(post_keyword_block::table)
.values(post_keyword_block_form)
.get_result::<Self>(conn)
.await
}
pub async fn unblock_keyword(
pool: &mut DbPool<'_>,
post_keyword_block_form: &PostKeywordBlockForm,
) -> QueryResult<usize> {
let conn = &mut get_conn(pool).await?;
delete(post_keyword_block::table)
.filter(post_keyword_block::person_id.eq(post_keyword_block_form.person_id))
.filter(post_keyword_block::keyword.eq(&post_keyword_block_form.keyword))
.execute(conn)
.await
}
}

View file

@ -121,6 +121,12 @@ pub struct LanguageId(pub i32);
/// The comment reply id.
pub struct CommentReplyId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The comment reply id.
pub struct PostKeywordBlockId(i32);
#[derive(
Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default, Ord, PartialOrd,
)]

View file

@ -826,6 +826,15 @@ diesel::table! {
}
}
diesel::table! {
post_keyword_block (id) {
id -> Int4,
#[max_length = 50]
keyword -> Varchar,
person_id -> Int4,
}
}
diesel::table! {
private_message (id) {
id -> Int4,

View file

@ -41,6 +41,7 @@ pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tagline;
pub mod post_keyword_block;
/// Default value for columns like [community::Community.inbox_url] which are marked as serde(skip).
///

View file

@ -0,0 +1,31 @@
use crate::newtypes::{PersonId, PostKeywordBlockId};
use serde::{Deserialize, Serialize};
#[cfg(feature = "full")]
use crate::schema::post_keyword_block;
#[cfg(feature = "full")]
use ts_rs::TS;
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(Queryable, Selectable, Associations, Identifiable,TS)
)]
#[cfg_attr(
feature = "full",
diesel(belongs_to(crate::source::person::Person))
)]
#[cfg_attr(feature = "full", diesel(table_name = post_keyword_block))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct PostKeywordBlock {
pub id : PostKeywordBlockId,
pub keyword: String,
pub person_id: PersonId,
}
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_keyword_block))]
pub struct PostKeywordBlockForm {
pub person_id: PersonId,
pub keyword: String,
}

View file

@ -32,6 +32,7 @@ use lemmy_db_schema::{
post,
post_actions,
post_aggregates,
post_keyword_block
},
source::{
community::{CommunityFollower, CommunityFollowerState},
@ -85,6 +86,10 @@ fn queries<'a>() -> Queries<
.inner_join(community::table)
.inner_join(post::table)
.left_join(image_details::table.on(post::thumbnail_url.eq(image_details::link.nullable())))
.left_join(post_keyword_block::table.on(
post_keyword_block::person_id.eq(my_person_id.unwrap_or(PersonId(-1))),
),
)
.left_join(actions(
community_actions::table,
my_person_id,
@ -115,6 +120,7 @@ fn queries<'a>() -> Queries<
person::all_columns,
community::all_columns,
image_details::all_columns.nullable(),
post_keyword_block::keyword,
creator_community_actions
.field(community_actions::received_ban)
.nullable()
@ -208,6 +214,8 @@ fn queries<'a>() -> Queries<
options.local_user.person_id(),
);
query.filter
// hide posts from deleted communities
query = query.filter(community::deleted.eq(false));
@ -351,6 +359,10 @@ fn queries<'a>() -> Queries<
query = query.filter(community_actions::blocked.is_null());
query = query.filter(instance_actions::blocked.is_null());
query = query.filter(person_actions::blocked.is_null());
query = query.filter(
not(post::name.like(any(post_keyword_block::keyword)))
.and(not(post::body.like(any(post_keyword_block::keyword))))
.and(not(post::url.like(any(post_keyword_block::keyword)))));
}
let (limit, offset) = limit_and_offset(options.page, options.limit)?;