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

Add match: MuteWordMatch to muted-word mod decision cause #2934

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions packages/api/src/moderation/decision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './types'
import { ModerationUI } from './ui'
import { LABELS } from './const/labels'
import { MuteWordMatch } from './mutewords'

enum ModerationBehaviorSeverity {
High,
Expand Down Expand Up @@ -197,12 +198,13 @@ export class ModerationDecision {
}
}

addMutedWord(mutedWord: boolean) {
if (mutedWord) {
addMutedWord(match: MuteWordMatch) {
if (match !== null) {
this.causes.push({
type: 'mute-word',
source: { type: 'user' },
priority: 6,
match,
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/api/src/moderation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import { decidePost } from './subjects/post'
import { decideFeedGenerator } from './subjects/feed-generator'
import { decideUserList } from './subjects/user-list'
import { ModerationDecision } from './decision'
import { matchMuteWord } from './mutewords'

export { ModerationUI } from './ui'
export { ModerationDecision } from './decision'
export { hasMutedWord } from './mutewords'
export { matchMuteWord } from './mutewords'
/**
* @deprecated Use `matchMuteWord` instead.
*/
export const hasMutedWord = matchMuteWord
export {
interpretLabelValueDefinition,
interpretLabelValueDefinitions,
Expand Down
63 changes: 48 additions & 15 deletions packages/api/src/moderation/mutewords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const LANGUAGE_EXCEPTIONS = [
'vi', // Vietnamese
]

export function hasMutedWord({
export type MuteWordMatch =
| {
word: AppBskyActorDefs.MutedWord
}[]
| null

export function matchMuteWord({
mutedWords,
text,
facets,
Expand All @@ -35,7 +41,7 @@ export function hasMutedWord({
outlineTags?: string[]
languages?: string[]
actor?: AppBskyActorDefs.ProfileView
}) {
}): MuteWordMatch {
const exception = LANGUAGE_EXCEPTIONS.includes(languages?.[0] || '')
const tags = ([] as string[])
.concat(outlineTags || [])
Expand All @@ -46,7 +52,9 @@ export function hasMutedWord({
)
.map((t) => t.toLowerCase())

for (const mute of mutedWords) {
const matches: Exclude<MuteWordMatch, null> = []

outer: for (const mute of mutedWords) {
const mutedWord = mute.value.toLowerCase()
const postText = text.toLowerCase()

Expand All @@ -60,24 +68,37 @@ export function hasMutedWord({
continue

// `content` applies to tags as well
if (tags.includes(mutedWord)) return true
if (tags.includes(mutedWord)) {
matches.push({ word: mute })
continue
}
// rest of the checks are for `content` only
if (!mute.targets.includes('content')) continue
// single character or other exception, has to use includes
if ((mutedWord.length === 1 || exception) && postText.includes(mutedWord))
return true
if ((mutedWord.length === 1 || exception) && postText.includes(mutedWord)) {
matches.push({ word: mute })
continue
}
// too long
if (mutedWord.length > postText.length) continue
// exact match
if (mutedWord === postText) return true
if (mutedWord === postText) {
matches.push({ word: mute })
continue
}
// any muted phrase with space or punctuation
if (/(?:\s|\p{P})+?/u.test(mutedWord) && postText.includes(mutedWord))
return true
if (/(?:\s|\p{P})+?/u.test(mutedWord) && postText.includes(mutedWord)) {
matches.push({ word: mute })
continue
}

// check individual character groups
const words = postText.split(REGEX.WORD_BOUNDARY)
for (const word of words) {
if (word === mutedWord) return true
if (word === mutedWord) {
matches.push({ word: mute })
continue outer
}

// compare word without leading/trailing punctuation, but allow internal
// punctuation (such as `s@ssy`)
Expand All @@ -86,23 +107,35 @@ export function hasMutedWord({
'',
)

if (mutedWord === wordTrimmedPunctuation) return true
if (mutedWord === wordTrimmedPunctuation) {
matches.push({ word: mute })
continue outer
}
if (mutedWord.length > wordTrimmedPunctuation.length) continue

if (/\p{P}+/u.test(wordTrimmedPunctuation)) {
const spacedWord = wordTrimmedPunctuation.replace(/\p{P}+/gu, ' ')
if (spacedWord === mutedWord) return true
if (spacedWord === mutedWord) {
matches.push({ word: mute })
continue outer
}

const contiguousWord = spacedWord.replace(/\s/gu, '')
if (contiguousWord === mutedWord) return true
if (contiguousWord === mutedWord) {
matches.push({ word: mute })
continue outer
}

const wordParts = wordTrimmedPunctuation.split(/\p{P}+/u)
for (const wordPart of wordParts) {
if (wordPart === mutedWord) return true
if (wordPart === mutedWord) {
matches.push({ word: mute })
continue outer
}
}
}
}
}

return false
return matches.length ? matches : null
}
Loading
Loading