Add keyword and URL filtering for posts (#711)
* feat: Add keyword and URL filtering for posts * impr: add minimizing filters, improve UI, fix some reactivity problems
This commit is contained in:
1 parent
21a0baacf9
commit
5630ac3bf8
8 files changed
+232
-42
No files matched your search
@@ -903,6 +903,14 @@
|
|||||||
"deleted": "Deleted by user",
|
"deleted": "Deleted by user",
|
||||||
"removed": "Removed by moderator"
|
"removed": "Removed by moderator"
|
||||||
},
|
},
|
||||||
|
"contentFilter": {
|
||||||
|
"title": "Content filters",
|
||||||
|
"description": "Hide or minimize posts matching given strings. Supports regular expressions.",
|
||||||
|
"hide": "Hide",
|
||||||
|
"minimize": "Minimize",
|
||||||
|
"minimized": "Content hidden by your filters",
|
||||||
|
"disabled": "Off"
|
||||||
|
},
|
||||||
"nsfwBlur": {
|
"nsfwBlur": {
|
||||||
"title": "NSFW blur",
|
"title": "NSFW blur",
|
||||||
"description": "Blurs images and remove post bodies of NSFW posts."
|
"description": "Blurs images and remove post bodies of NSFW posts."
|
||||||
|
|||||||
@@ -47,6 +47,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function rerender() {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
updateVisibleItems()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
restore = {
|
restore = {
|
||||||
itemHeights: itemHeights,
|
itemHeights: itemHeights,
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
import { env } from '$env/dynamic/public'
|
||||||
import type { CommentSortType, ListingType, SortType } from '$lib/api/types'
|
import type { CommentSortType, ListingType, SortType } from '$lib/api/types'
|
||||||
|
import type { Filter } from '$lib/feature/post/filters'
|
||||||
import { locale } from './i18n'
|
import { locale } from './i18n'
|
||||||
|
|
||||||
|
// TODO extract settings into submodules
|
||||||
|
|
||||||
export type View = 'cozy' | 'compact'
|
export type View = 'cozy' | 'compact'
|
||||||
|
|
||||||
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
|
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
|
||||||
@@ -89,6 +92,7 @@ interface Settings {
|
|||||||
reverseActions: boolean
|
reverseActions: boolean
|
||||||
titleOpensUrl: boolean
|
titleOpensUrl: boolean
|
||||||
}
|
}
|
||||||
|
filters: Filter[]
|
||||||
forms: {
|
forms: {
|
||||||
autosubmitAutofill: boolean
|
autosubmitAutofill: boolean
|
||||||
}
|
}
|
||||||
@@ -97,7 +101,6 @@ interface Settings {
|
|||||||
useRtl: boolean
|
useRtl: boolean
|
||||||
parseTags: boolean
|
parseTags: boolean
|
||||||
logoColorMonth: number | null
|
logoColorMonth: number | null
|
||||||
|
|
||||||
absoluteDates: boolean
|
absoluteDates: boolean
|
||||||
messages: {
|
messages: {
|
||||||
fullMarkdown: boolean
|
fullMarkdown: boolean
|
||||||
@@ -106,7 +109,7 @@ interface Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const defaultSettings: Settings = {
|
export const defaultSettings: Settings = {
|
||||||
settingsVer: 7,
|
settingsVer: 8,
|
||||||
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
|
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
|
||||||
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
|
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
|
||||||
showInstances: {
|
showInstances: {
|
||||||
@@ -172,6 +175,7 @@ export const defaultSettings: Settings = {
|
|||||||
reverseActions: toBool(env.PUBLIC_REVERSE_ACTIONS) ?? false,
|
reverseActions: toBool(env.PUBLIC_REVERSE_ACTIONS) ?? false,
|
||||||
titleOpensUrl: toBool(env.PUBLIC_TITLE_OPENS_URL) ?? false,
|
titleOpensUrl: toBool(env.PUBLIC_TITLE_OPENS_URL) ?? false,
|
||||||
},
|
},
|
||||||
|
filters: [],
|
||||||
forms: {
|
forms: {
|
||||||
autosubmitAutofill: false,
|
autosubmitAutofill: false,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,8 +4,10 @@
|
|||||||
import Placeholder from '$lib/ui/info/Placeholder.svelte'
|
import Placeholder from '$lib/ui/info/Placeholder.svelte'
|
||||||
import { Button } from 'mono-svelte'
|
import { Button } from 'mono-svelte'
|
||||||
import type { Snippet } from 'svelte'
|
import type { Snippet } from 'svelte'
|
||||||
import { ArchiveBox, Plus } from 'svelte-hero-icons/dist'
|
import { ArchiveBox, ArrowsPointingOut, Plus } from 'svelte-hero-icons/dist'
|
||||||
import { Post } from '..'
|
import { Post } from '..'
|
||||||
|
import { filterPost, type FilteredItem } from '../filters'
|
||||||
|
import { t } from '$lib/app/i18n'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
posts: PostView[]
|
posts: PostView[]
|
||||||
@@ -14,12 +16,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let { posts = $bindable(), community = false, children }: Props = $props()
|
let { posts = $bindable(), community = false, children }: Props = $props()
|
||||||
|
|
||||||
|
let filteredPosts: FilteredItem[] = $derived(
|
||||||
|
posts.map((post) => ({
|
||||||
|
id: post.post.id,
|
||||||
|
action: filterPost(post, settings.filters),
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
|
const removePost = (postId: number) => {
|
||||||
|
const index = posts.findIndex((post) => post.post.id === postId)
|
||||||
|
if (index === -1) return
|
||||||
|
posts = posts.toSpliced(index, 1)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul
|
<ul
|
||||||
class="flex flex-col list-none divide-y divide-slate-200 dark:divide-zinc-800"
|
class="flex flex-col list-none divide-y divide-slate-200 dark:divide-zinc-800"
|
||||||
>
|
>
|
||||||
{#if posts?.length == 0}
|
{#if posts.length === 0}
|
||||||
<div class="h-full grid place-items-center">
|
<div class="h-full grid place-items-center">
|
||||||
<Placeholder
|
<Placeholder
|
||||||
icon={ArchiveBox}
|
icon={ArchiveBox}
|
||||||
@@ -32,21 +47,36 @@
|
|||||||
</Placeholder>
|
</Placeholder>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
{#each posts as post, index (post.post.id)}
|
{#each posts as post, row (post.post.id)}
|
||||||
<li class="relative post-container">
|
{@const filter = filteredPosts[row]}
|
||||||
<Post
|
{#if filter.action != 'hide'}
|
||||||
hideCommunity={community}
|
<li class="relative post-container">
|
||||||
view={(post.post.featured_community || post.post.featured_local) &&
|
{#if filter.action == 'none'}
|
||||||
settings.posts.compactFeatured
|
<Post
|
||||||
? 'compact'
|
hideCommunity={community}
|
||||||
: settings.view}
|
view={(post.post.featured_community ||
|
||||||
{post}
|
post.post.featured_local) &&
|
||||||
class="transition-all duration-250"
|
settings.posts.compactFeatured
|
||||||
onhide={() => {
|
? 'compact'
|
||||||
posts = posts.toSpliced(index, 1)
|
: settings.view}
|
||||||
}}
|
{post}
|
||||||
/>
|
class="transition-all duration-250"
|
||||||
</li>
|
onhide={() => removePost(post.post.id)}
|
||||||
|
/>
|
||||||
|
{:else if filteredPosts[row].action == 'minimize'}
|
||||||
|
<Button
|
||||||
|
onclick={() => (filteredPosts[row].action = 'none')}
|
||||||
|
color="tertiary"
|
||||||
|
rounding="none"
|
||||||
|
icon={ArrowsPointingOut}
|
||||||
|
class="text-slate-400 dark:text-zinc-600 w-full"
|
||||||
|
size="xs"
|
||||||
|
>
|
||||||
|
{$t('settings.lemmy.contentFilter.minimized')}
|
||||||
|
</Button>
|
||||||
|
{/if}
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
{/if}
|
{/if}
|
||||||
{@render children?.()}
|
{@render children?.()}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
import { onDestroy, onMount, untrack } from 'svelte'
|
import { onDestroy, onMount, untrack } from 'svelte'
|
||||||
import {
|
import {
|
||||||
ArchiveBox,
|
ArchiveBox,
|
||||||
|
ArrowsPointingOut,
|
||||||
ArrowTopRightOnSquare,
|
ArrowTopRightOnSquare,
|
||||||
ChevronDoubleUp,
|
ChevronDoubleUp,
|
||||||
ExclamationTriangle,
|
ExclamationTriangle,
|
||||||
@@ -22,6 +23,8 @@
|
|||||||
import { SvelteSet } from 'svelte/reactivity'
|
import { SvelteSet } from 'svelte/reactivity'
|
||||||
import { fly } from 'svelte/transition'
|
import { fly } from 'svelte/transition'
|
||||||
import { Post } from '..'
|
import { Post } from '..'
|
||||||
|
import { filterPost, type FilteredItem } from '../filters'
|
||||||
|
import { ReactiveState } from '$lib/app/util.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
posts: PostView[]
|
posts: PostView[]
|
||||||
@@ -41,9 +44,17 @@
|
|||||||
children,
|
children,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
|
let filteredPosts: FilteredItem[] = $derived(
|
||||||
|
posts.map((post) => ({
|
||||||
|
id: post.post.id,
|
||||||
|
action: filterPost(post, settings.filters),
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
let listEl = $state<HTMLUListElement>()
|
let listEl = $state<HTMLUListElement>()
|
||||||
let listComp = $state<{
|
let listComp = $state<{
|
||||||
scrollToIndex: (index: number, window?: boolean) => void
|
scrollToIndex: (index: number, window?: boolean) => void
|
||||||
|
rerender: () => void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
let error = $state()
|
let error = $state()
|
||||||
@@ -53,6 +64,12 @@
|
|||||||
const abortLoad = new AbortController()
|
const abortLoad = new AbortController()
|
||||||
let seenIds = new SvelteSet<number>(posts.map((post) => post.post.id))
|
let seenIds = new SvelteSet<number>(posts.map((post) => post.post.id))
|
||||||
|
|
||||||
|
const removePost = (postId: number) => {
|
||||||
|
const index = posts.findIndex((post) => post.post.id === postId)
|
||||||
|
if (index === -1) return
|
||||||
|
posts = posts.toSpliced(index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
if (!hasMore || loading) return
|
if (!hasMore || loading) return
|
||||||
|
|
||||||
@@ -157,7 +174,7 @@
|
|||||||
|
|
||||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||||
{#key posts}
|
{#key posts}
|
||||||
{#if posts?.length == 0}
|
{#if posts.length == 0}
|
||||||
<div class="h-full grid place-items-center my-8">
|
<div class="h-full grid place-items-center my-8">
|
||||||
<Placeholder
|
<Placeholder
|
||||||
icon={ArchiveBox}
|
icon={ArchiveBox}
|
||||||
@@ -186,27 +203,47 @@
|
|||||||
bind:this={listComp}
|
bind:this={listComp}
|
||||||
>
|
>
|
||||||
{#snippet item(row)}
|
{#snippet item(row)}
|
||||||
<li
|
<!--god svelte is gonna make me lose it-->
|
||||||
in:fly={row < 7
|
{@const filter = new ReactiveState(filteredPosts[row])}
|
||||||
? { duration: 800, easing: expoOut, y: 24, delay: row * 50 }
|
{#if posts[row] && filter.value.action != 'hide'}
|
||||||
: { opacity: 1, duration: 0 }}
|
<li
|
||||||
data-index={row}
|
in:fly={row < 7
|
||||||
class={['relative post-container', row < 7 && '']}
|
? { duration: 800, easing: expoOut, y: 24, delay: row * 50 }
|
||||||
>
|
: { opacity: 1, duration: 0 }}
|
||||||
<Post
|
data-index={row}
|
||||||
bind:post={posts[row]}
|
class={['relative post-container', row < 7 && '']}
|
||||||
hideCommunity={community}
|
>
|
||||||
view={(posts[row]?.post.featured_community ||
|
<!--TODO make my component isolation not abysmal-->
|
||||||
posts[row]?.post.featured_local) &&
|
{#if filter.value.action == 'none'}
|
||||||
settings.posts.compactFeatured
|
<Post
|
||||||
? 'compact'
|
bind:post={posts[row]}
|
||||||
: settings.view}
|
hideCommunity={community}
|
||||||
onhide={() => {
|
view={(posts[row].post.featured_community ||
|
||||||
posts = posts.toSpliced(row, 1)
|
posts[row].post.featured_local) &&
|
||||||
}}
|
settings.posts.compactFeatured
|
||||||
class="px-3 sm:px-6 hover:bg-slate-100/30 hover:dark:bg-zinc-900/30 transition-colors"
|
? 'compact'
|
||||||
></Post>
|
: settings.view}
|
||||||
</li>
|
onhide={() => removePost(posts[row].post.id)}
|
||||||
|
class="px-3 sm:px-6 hover:bg-slate-100/30 hover:dark:bg-zinc-900/30 transition-colors"
|
||||||
|
></Post>
|
||||||
|
{:else if filter.value.action == 'minimize'}
|
||||||
|
<Button
|
||||||
|
onclick={() => {
|
||||||
|
filteredPosts[row].action = 'none'
|
||||||
|
filter.value.action = 'none'
|
||||||
|
listComp?.rerender()
|
||||||
|
}}
|
||||||
|
color="tertiary"
|
||||||
|
rounding="none"
|
||||||
|
icon={ArrowsPointingOut}
|
||||||
|
class="text-slate-400 dark:text-zinc-600 w-full"
|
||||||
|
size="xs"
|
||||||
|
>
|
||||||
|
{$t('settings.lemmy.contentFilter.minimized')}
|
||||||
|
</Button>
|
||||||
|
{/if}
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</VirtualList>
|
</VirtualList>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import type { PostView } from '$lib/api/types'
|
||||||
|
|
||||||
|
type FilterAction = 'none' | 'minimize' | 'hide'
|
||||||
|
type FilterType = 'keyword'
|
||||||
|
// since we can test regexes on URLs, why not unify them?
|
||||||
|
|
||||||
|
export interface Filter {
|
||||||
|
match: string
|
||||||
|
action: FilterAction
|
||||||
|
type: FilterType
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FilteredItem = { id: number; action: FilterAction }
|
||||||
|
|
||||||
|
export function filterPost(post: PostView, filters: Filter[]): FilterAction {
|
||||||
|
for (const filter of filters) {
|
||||||
|
try {
|
||||||
|
const regex = new RegExp(filter.match, 'i')
|
||||||
|
if (
|
||||||
|
regex.test(post.post.url ?? '') ||
|
||||||
|
regex.test(post.post.name) ||
|
||||||
|
regex.test(post.post.body ?? '')
|
||||||
|
)
|
||||||
|
return filter.action
|
||||||
|
} catch {
|
||||||
|
/* */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'none'
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
import Link from '$lib/ui/form/Link.svelte'
|
import Link from '$lib/ui/form/Link.svelte'
|
||||||
import Switch from '$lib/ui/form/Switch.svelte'
|
import Switch from '$lib/ui/form/Switch.svelte'
|
||||||
import { CommonList } from '$lib/ui/layout'
|
import { CommonList } from '$lib/ui/layout'
|
||||||
import { Button, Option, Select } from 'mono-svelte'
|
import { Button, Option, Select, TextArea } from 'mono-svelte'
|
||||||
import {
|
import {
|
||||||
ArrowsPointingOut,
|
ArrowsPointingOut,
|
||||||
ArrowsRightLeft,
|
ArrowsRightLeft,
|
||||||
@@ -22,12 +22,14 @@
|
|||||||
Clock,
|
Clock,
|
||||||
CubeTransparent,
|
CubeTransparent,
|
||||||
DocumentText,
|
DocumentText,
|
||||||
|
EyeSlash,
|
||||||
Fire,
|
Fire,
|
||||||
GlobeAmericas,
|
GlobeAmericas,
|
||||||
Heart,
|
Heart,
|
||||||
Icon,
|
Icon,
|
||||||
Language,
|
Language,
|
||||||
Photo,
|
Photo,
|
||||||
|
Plus,
|
||||||
Star,
|
Star,
|
||||||
TableCells,
|
TableCells,
|
||||||
Tag,
|
Tag,
|
||||||
@@ -36,6 +38,7 @@
|
|||||||
} from 'svelte-hero-icons/dist'
|
} from 'svelte-hero-icons/dist'
|
||||||
import Setting from '../Setting.svelte'
|
import Setting from '../Setting.svelte'
|
||||||
import ToggleSetting from '../ToggleSetting.svelte'
|
import ToggleSetting from '../ToggleSetting.svelte'
|
||||||
|
import Filter from './Filter.svelte'
|
||||||
|
|
||||||
let localeMap: Map<
|
let localeMap: Map<
|
||||||
string,
|
string,
|
||||||
@@ -191,6 +194,32 @@
|
|||||||
title={$t('settings.app.infiniteScroll.title')}
|
title={$t('settings.app.infiniteScroll.title')}
|
||||||
description={$t('settings.app.infiniteScroll.description')}
|
description={$t('settings.app.infiniteScroll.description')}
|
||||||
/>
|
/>
|
||||||
|
<Setting icon={EyeSlash} adaptive={false} mainClass="rounded-b-none!">
|
||||||
|
{#snippet title()}
|
||||||
|
<span>{$t('settings.lemmy.contentFilter.title')}</span>
|
||||||
|
{/snippet}
|
||||||
|
{#snippet description()}
|
||||||
|
<span>{$t('settings.lemmy.contentFilter.description')}</span>
|
||||||
|
{/snippet}
|
||||||
|
</Setting>
|
||||||
|
{#each settings.filters as filter, index}
|
||||||
|
<Filter onremove={() => settings.filters.splice(index, 1)} bind:filter />
|
||||||
|
{/each}
|
||||||
|
<li class="rounded-t-none! -mt-1! border-t-0!">
|
||||||
|
<Button
|
||||||
|
color="none"
|
||||||
|
class="w-full p-2"
|
||||||
|
icon={Plus}
|
||||||
|
onclick={() =>
|
||||||
|
settings.filters.push({
|
||||||
|
match: 'New filter.',
|
||||||
|
action: 'none',
|
||||||
|
type: 'keyword',
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
{$t('common.add')}
|
||||||
|
</Button>
|
||||||
|
</li>
|
||||||
<Setting icon={Photo}>
|
<Setting icon={Photo}>
|
||||||
{#snippet title()}
|
{#snippet title()}
|
||||||
<span>{$t('settings.app.thumbnailSide.title')}</span>
|
<span>{$t('settings.app.thumbnailSide.title')}</span>
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { t } from '$lib/app/i18n'
|
||||||
|
import type { Filter } from '$lib/feature/post/filters'
|
||||||
|
import Switch from '$lib/ui/form/Switch.svelte'
|
||||||
|
import { Button, TextInput } from 'mono-svelte'
|
||||||
|
import { Trash } from 'svelte-hero-icons/dist'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
filter: Filter
|
||||||
|
onremove?: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
let { filter = $bindable(), onremove }: Props = $props()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<li
|
||||||
|
class="adaptive px-3 py-1 flex flex-col md:flex-row md:items-center xs gap-4"
|
||||||
|
>
|
||||||
|
<TextInput bind:value={filter.match} label="" class="flex-1"></TextInput>
|
||||||
|
<Switch
|
||||||
|
options={['hide', 'minimize', 'none']}
|
||||||
|
optionNames={[
|
||||||
|
$t('settings.lemmy.contentFilter.hide'),
|
||||||
|
$t('settings.lemmy.contentFilter.minimize'),
|
||||||
|
$t('settings.lemmy.contentFilter.disabled'),
|
||||||
|
]}
|
||||||
|
bind:selected={filter.action}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
icon={Trash}
|
||||||
|
size="square-md"
|
||||||
|
onclick={onremove}
|
||||||
|
aria-label={$t('common.remove')}
|
||||||
|
/>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
li {
|
||||||
|
border-top-left-radius: 0 !important;
|
||||||
|
border-top-right-radius: 0 !important;
|
||||||
|
border-bottom-left-radius: 0 !important;
|
||||||
|
border-bottom-right-radius: 0 !important;
|
||||||
|
margin-top: calc(var(--spacing) * -1);
|
||||||
|
border-top: none !important;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user