Skip to content

Commit

Permalink
Only block on community/home/all feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Oct 10, 2023
1 parent a6f7d0c commit 928c190
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
28 changes: 9 additions & 19 deletions src/features/feed/PostCommentFeed.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useCallback, useContext, useEffect, useRef } from "react";
import Feed, { FeedProps, FetchFn } from "./Feed";
import FeedComment from "../comment/inFeed/FeedComment";
import { CommentView, Post as PostType, PostView } from "lemmy-js-client";
import { CommentView, PostView } from "lemmy-js-client";
import { useAppDispatch, useAppSelector } from "../../store";
import { css } from "@emotion/react";
import { postHiddenByIdSelector, receivedPosts } from "../post/postSlice";
import { receivedComments } from "../comment/commentSlice";
import Post from "../post/inFeed/Post";
import CommentHr from "../comment/CommentHr";
import { FeedContext } from "./FeedContext";
import { postHasFilteredKeywords } from "../../helpers/lemmy";

const thickBorderCss = css`
border-bottom: 8px solid var(--thick-separator-color);
Expand All @@ -28,12 +29,14 @@ interface PostCommentFeed
extends Omit<FeedProps<PostCommentItem>, "renderItemContent"> {
communityName?: string;
filterHiddenPosts?: boolean;
filterKeywords?: boolean;
}

export default function PostCommentFeed({
communityName,
fetchFn: _fetchFn,
filterHiddenPosts = true,
filterKeywords = true,
...rest
}: PostCommentFeed) {
const dispatch = useAppDispatch();
Expand Down Expand Up @@ -112,8 +115,11 @@ export default function PostCommentFeed({
const filterFn = useCallback(
(item: PostCommentItem) =>
!postHiddenById[item.post.id] &&
!postHasFilteredKeywords(item.post, filteredKeywords),
[postHiddenById, filteredKeywords],
!postHasFilteredKeywords(
item.post,
filterKeywords ? filteredKeywords : [],
),
[postHiddenById, filteredKeywords, filterKeywords],
);

const getIndex = useCallback(
Expand All @@ -133,19 +139,3 @@ export default function PostCommentFeed({
/>
);
}

function postHasFilteredKeywords(post: PostType, keywords: string[]): boolean {
for (const keyword of keywords) {
if (keywordFoundInSentence(keyword, post.name)) return true;
}

return false;
}

function keywordFoundInSentence(keyword: string, sentence: string): boolean {
// Create a regular expression pattern to match the keyword as a whole word
const pattern = new RegExp(`\\b${keyword}\\b`, "i");

// Use the RegExp test method to check if the pattern is found in the sentence
return pattern.test(sentence);
}
1 change: 1 addition & 0 deletions src/features/user/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default function Profile({ person }: ProfileProps) {
fetchFn={fetchFn}
header={header}
filterHiddenPosts={false}
filterKeywords={false}
/>
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,22 @@ export function isUrlVideo(url: string): boolean {
export function share(item: Post | Comment) {
return Share.share({ url: item.ap_id });
}

export function postHasFilteredKeywords(
post: Post,
keywords: string[],
): boolean {
for (const keyword of keywords) {
if (keywordFoundInSentence(keyword, post.name)) return true;
}

return false;
}

function keywordFoundInSentence(keyword: string, sentence: string): boolean {
// Create a regular expression pattern to match the keyword as a whole word
const pattern = new RegExp(`\\b${keyword}\\b`, "i");

// Use the RegExp test method to check if the pattern is found in the sentence
return pattern.test(sentence);
}
3 changes: 2 additions & 1 deletion src/pages/profile/ProfileFeedHiddenPostsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ export default function ProfileFeedHiddenPostsPage() {
</IonHeader>
<FeedContent>
<PostCommentFeed
filterHiddenPosts={false}
fetchFn={fetchFn}
limit={LIMIT}
filterHiddenPosts={false}
filterKeywords={false}
/>
</FeedContent>
</IonPage>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/profile/ProfileFeedItemsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default function ProfileFeedItemsPage({
</IonToolbar>
</IonHeader>
<FeedContent>
<PostCommentFeed fetchFn={fetchFn} filterHiddenPosts={false} />
<PostCommentFeed
fetchFn={fetchFn}
filterHiddenPosts={false}
filterKeywords={false}
/>
</FeedContent>
</IonPage>
);
Expand Down

0 comments on commit 928c190

Please sign in to comment.