Skip to content

Commit

Permalink
fix: Unnecessary restrictions on bad word filters
Browse files Browse the repository at this point in the history
Bad words can now contain almost any character and match domains, accounts or hashtags more precisely. For example: `example.com` is no longer split into `example` and `com` which would block more than intended. `#example` is no longer redurced to `example` which would also block the word, not only the hashtag.
  • Loading branch information
defnull committed Feb 10, 2024
1 parent 80af91f commit 0ac5852
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/components/ConfigModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const formTags = computed({
set: (value) => config.value.tags = arrayUnique([...(value || "").matchAll(tagPattern)].map(m => m[1])),
});
const badwordPattern = /([^, ]+)/igu
const formBadWords = computed({
get: () => config.value.badWords.join(" "),
set: (value) => config.value.badWords = arrayUnique([...(value || "").matchAll(tagPattern)].map(m => m[1])),
set: (value) => config.value.badWords = arrayUnique([...(value || "").matchAll(badwordPattern)].map(m => m[1])),
});
const accountPattern = /\b([a-z0-9_]+)(@([a-z0-9.-]+\.[a-z]{2,}))?\b/ig;
Expand Down Expand Up @@ -239,7 +240,7 @@ const onSubmit = () => {
<label for="edit-server" class="form-label">Filter bad words:</label>
<div class="ms-5">
<input type="text" class="form-control" id="edit-server" v-model.lazy="formBadWords">
<div class="form-text">Hide posts containing certain words or hashtags. Only exact matches are
<div class="form-text">Hide posts containing certain words, domains or hashtags. Only full word matches are
filtered.</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function sanatizeConfig(config: any): Config {
result.loadTrends = boolOr(config.loadTrends, fallback.loadTrends)

result.languages = arrayUnique((Array.isArray(config.languages) ? [...config.languages] : [...fallback.languages]).filter(isLanguage));
result.badWords = arrayUnique((Array.isArray(config.badWords) ? [...config.badWords] : [...fallback.badWords]).filter(isTag));
result.badWords = arrayUnique((Array.isArray(config.badWords) ? [...config.badWords] : [...fallback.badWords]));
result.hideSensitive = boolOr(config.hideSensitive, fallback.hideSensitive)
result.hideBoosts = boolOr(config.hideBoosts, fallback.hideBoosts)
result.hideBots = boolOr(config.hideBots, fallback.hideBots)
Expand Down
4 changes: 2 additions & 2 deletions src/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ const filterStatus = (cfg: Config, status: MastodonStatus) => {
if (cfg.hideReplies && status.in_reply_to_id) return false;
if (cfg.hideBots && status.account?.bot) return false;
if (cfg.badWords.length) {
const pattern = new RegExp(`\\b(${cfg.badWords.map(regexEscape).join("|")})\\b`, 'i');
const pattern = new RegExp(`(?:\\b|^)(${cfg.badWords.map(regexEscape).join("|")})(?:\\b|$)`, 'i');
if (status.account?.display_name?.match(pattern)
|| status.account?.acct?.match(pattern)
|| status.content.match(pattern)
|| status.spoiler_text?.match(pattern)
|| status.tags?.some(tag => tag.name?.match(pattern))
|| status.tags?.some(tag => `#${tag.name}`.match(pattern))
|| status.media_attachments?.some(media => media.description?.match(pattern)))
return false;
}
Expand Down

0 comments on commit 0ac5852

Please sign in to comment.