Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature filter keywords 3710 #5263

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ dev_pgdata/

# database dumps
*.sqldump
tmp.schema
35 changes: 35 additions & 0 deletions crates/api/src/post/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{context::LemmyContext, post::BlockKeywordForPost, SuccessResponse};
use lemmy_db_schema::source::post_keyword_block::{PostKeywordBlock, PostKeywordBlockForm};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::{LemmyErrorType, LemmyResult};

pub async fn user_block_keyword_for_posts(
data: Json<BlockKeywordForPost>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<SuccessResponse>> {
if data.keyword.trim().len() < 3 {
Err(LemmyErrorType::BlockKeywordToShort)?;
}
let person_id = local_user_view.person.id;
let post_keyword_block_form = PostKeywordBlockForm {
person_id,
keyword: data.keyword.clone(),
};
if data.block {
//Get number of post keyword block for that user
if PostKeywordBlock::for_person(&mut context.pool(), person_id)
.await?
.len()
>= 15
{
Err(LemmyErrorType::BlockKeywordLimitReached)?;
}
PostKeywordBlock::block_keyword(&mut context.pool(), &post_keyword_block_form).await?;
} else {
PostKeywordBlock::unblock_keyword(&mut context.pool(), &post_keyword_block_form).await?;
}
Ok(Json(SuccessResponse::default()))
}
1 change: 1 addition & 0 deletions crates/api/src/post/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod block;
pub mod feature;
pub mod get_link_metadata;
pub mod hide;
Expand Down
8 changes: 8 additions & 0 deletions crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,11 @@ pub struct ListPostLikes {
pub struct ListPostLikesResponse {
pub post_likes: Vec<VoteView>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct BlockKeywordForPost {
pub keyword: String,
pub block: bool,
}
2 changes: 2 additions & 0 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use lemmy_db_schema::{
local_site_url_blocklist::LocalSiteUrlBlocklist,
oauth_provider::{OAuthProvider, PublicOAuthProvider},
person::Person,
post_keyword_block::PostKeywordBlock,
tagline::Tagline,
},
CommentSortType,
Expand Down Expand Up @@ -430,6 +431,7 @@ pub struct MyUserInfo {
pub community_blocks: Vec<Community>,
pub instance_blocks: Vec<Instance>,
pub person_blocks: Vec<Person>,
pub post_keyword_blocks: Vec<PostKeywordBlock>,
pub discussion_languages: Vec<LanguageId>,
}

Expand Down
30 changes: 20 additions & 10 deletions crates/api_crud/src/user/my_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use lemmy_db_schema::source::{
community_block::CommunityBlock,
instance_block::InstanceBlock,
person_block::PersonBlock,
post_keyword_block::PostKeywordBlock,
};
use lemmy_db_views::structs::{CommunityFollowerView, CommunityModeratorView, LocalUserView};
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
Expand All @@ -20,16 +21,24 @@ pub async fn get_my_user(
let local_user_id = local_user_view.local_user.id;
let pool = &mut context.pool();

let (follows, community_blocks, instance_blocks, person_blocks, moderates, discussion_languages) =
lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
let (
follows,
community_blocks,
instance_blocks,
person_blocks,
post_keyword_blocks,
moderates,
discussion_languages,
) = lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| PostKeywordBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;

Ok(Json(MyUserInfo {
local_user_view: local_user_view.clone(),
Expand All @@ -38,6 +47,7 @@ pub async fn get_my_user(
community_blocks,
instance_blocks,
person_blocks,
post_keyword_blocks,
discussion_languages,
}))
}
1 change: 1 addition & 0 deletions crates/db_schema/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod person_block;
pub mod person_comment_mention;
pub mod person_post_mention;
pub mod post;
mod post_keyword_block;
pub mod post_report;
pub mod private_message;
pub mod private_message_report;
Expand Down
44 changes: 44 additions & 0 deletions crates/db_schema/src/impls/post_keyword_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{
newtypes::PersonId,
schema::post_keyword_block,
source::post_keyword_block::{PostKeywordBlock, PostKeywordBlockForm},
utils::{get_conn, DbPool},
};
use diesel::{delete, insert_into, prelude::*, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;

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
}
}
6 changes: 6 additions & 0 deletions crates/db_schema/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ pub struct LanguageId(pub i32);
/// The comment reply id.
pub struct CommentReplyId(pub 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,
)]
Expand Down
10 changes: 10 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,15 @@ diesel::table! {
}
}

diesel::table! {
post_keyword_block (id) {
id -> Int4,
#[max_length = 20]
keyword -> Varchar,
person_id -> Int4,
}
}

diesel::table! {
post_tag (post_id, tag_id) {
post_id -> Int4,
Expand Down Expand Up @@ -1283,6 +1292,7 @@ diesel::allow_tables_to_appear_in_same_query!(
post_aggregates,
post_report,
post_tag,
post_keyword_block,
previously_run_sql,
private_message,
private_message_report,
Expand Down
1 change: 1 addition & 0 deletions crates/db_schema/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod person_block;
pub mod person_comment_mention;
pub mod person_post_mention;
pub mod post;
pub mod post_keyword_block;
pub mod post_report;
pub mod private_message;
pub mod private_message_report;
Expand Down
28 changes: 28 additions & 0 deletions crates/db_schema/src/source/post_keyword_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::newtypes::{PersonId, PostKeywordBlockId};
#[cfg(feature = "full")]
use crate::schema::post_keyword_block;
use serde::{Deserialize, Serialize};
#[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,
}
Loading