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 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions crates/api/src/post/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use lemmy_api_common::{post::{BlockKeywordForPost}, context::LemmyContext, SuccessResponse};
use lemmy_db_views::structs::LocalUserView;
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_utils::error::LemmyResult;
use lemmy_db_schema::source::post_keyword_block::{PostKeywordBlock, PostKeywordBlockForm};

pub async fn user_block_keyword_for_posts(
data: Json<BlockKeywordForPost>,
context: Data<LemmyContext>,
local_user_view: LocalUserView
) -> LemmyResult<Json<SuccessResponse>>{

let person_id = local_user_view.person.id;
let post_block_keyword_form = PostKeywordBlockForm {
person_id,
keyword: data.keyword.clone(),
};
if(data.block){
PostKeywordBlock::block_keyword(&mut context.pool(), &post_block_keyword_form).await?;
} else {
PostKeywordBlock::unblock_keyword(&mut context.pool(), &post_block_keyword_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
Expand Up @@ -7,3 +7,4 @@ pub mod lock;
pub mod mark_many_read;
pub mod mark_read;
pub mod save;
pub mod block;
9 changes: 9 additions & 0 deletions crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,12 @@ 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 @@ -65,6 +65,7 @@ use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;
use lemmy_db_schema::source::post_keyword_block::PostKeywordBlock;

#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -479,6 +480,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
5 changes: 4 additions & 1 deletion crates/api_crud/src/user/my_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lemmy_db_schema::source::{
instance_block::InstanceBlock,
person_block::PersonBlock,
};
use lemmy_db_schema::source::post_keyword_block::PostKeywordBlock;
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView};
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
Expand All @@ -22,12 +23,13 @@ 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) =
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)
))
Expand All @@ -40,6 +42,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 @@ -36,3 +36,4 @@ pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tagline;
mod post_keyword_block;
53 changes: 53 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,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
}
}

6 changes: 6 additions & 0 deletions crates/db_schema/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)]
Expand Down
9 changes: 9 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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).
///
Expand Down
31 changes: 31 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,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,
}
10 changes: 10 additions & 0 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use lemmy_db_schema::{
post,
post_actions,
post_aggregates,
post_keyword_block
},
source::{
community::{CommunityFollower, CommunityFollowerState},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -351,6 +357,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.ilike(post_keyword_block::keyword))
.and(not(post::body.ilike(post_keyword_block::keyword)))
.and(not(post::url.ilike(post_keyword_block::keyword))));
}

let (limit, offset) = limit_and_offset(options.page, options.limit)?;
Expand Down
4 changes: 3 additions & 1 deletion src/api_routes_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ use lemmy_api::{
},
sitemap::get_sitemap,
};
use lemmy_api::post::block::user_block_keyword_for_posts;
use lemmy_api_crud::{
comment::{
create::create_comment,
Expand Down Expand Up @@ -223,7 +224,8 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
.route("/report", post().to(create_post_report))
.route("/report/resolve", put().to(resolve_post_report))
.route("/report/list", get().to(list_post_reports))
.route("/site_metadata", get().to(get_link_metadata)),
.route("/site_metadata", get().to(get_link_metadata))
.route("/block",post().to(user_block_keyword_for_posts)),
)
// Comment
.service(
Expand Down
4 changes: 3 additions & 1 deletion src/api_routes_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ use lemmy_api::{
},
sitemap::get_sitemap,
};
use lemmy_api::post::block::user_block_keyword_for_posts;
use lemmy_api_crud::{
comment::{
create::create_comment,
Expand Down Expand Up @@ -320,7 +321,8 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
scope("/block")
.route("/person", post().to(user_block_person))
.route("/community", post().to(user_block_community))
.route("/instance", post().to(user_block_instance)),
.route("/instance", post().to(user_block_instance))
.route("/post",get().to(user_block_keyword_for_posts)),
),
)
// User actions
Expand Down