pleroma-fe/src/services/status_parser/status_parser.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

export const muteFilterHits = (muteFilters, status) => {
const statusText = status.text.toLowerCase()
const statusSummary = status.summary.toLowerCase()
2025-04-03 00:06:44 +03:00
const replyToUser = status.in_reply_to_screen_name?.toLowerCase()
2025-10-07 06:48:17 +00:00
const poster = status.user.screen_name?.toLowerCase()
2026-01-06 16:22:52 +02:00
const mentions = (status.attentions || []).map((att) =>
att.screen_name.toLowerCase(),
)
2025-04-03 00:06:44 +03:00
2026-01-06 16:22:52 +02:00
return muteFilters
.toSorted((a, b) => b.order - a.order)
.map((filter) => {
2026-05-13 16:12:52 +03:00
const {
hide,
expires,
name,
value,
type,
enabled,
caseSensitive = false,
} = filter
2026-01-06 16:22:52 +02:00
if (!enabled) return false
if (value === '') return false
if (expires !== null && expires < Date.now()) return false
switch (type) {
case 'word': {
let match = false
if (caseSensitive) {
2026-05-13 16:12:52 +03:00
match = statusText.includes(value) || statusSummary.includes(value)
} else {
const lowercaseValue = value.toLowerCase()
match =
statusText.toLowerCase().includes(lowercaseValue) ||
statusSummary.toLowerCase().includes(lowercaseValue)
}
if (match) {
2025-03-25 19:48:12 +02:00
return { hide, name }
}
2026-01-06 16:22:52 +02:00
break
2025-03-25 19:48:12 +02:00
}
2026-01-06 16:22:52 +02:00
case 'regexp': {
try {
const re = new RegExp(value, caseSensitive ? '' : 'i')
2026-01-06 16:22:52 +02:00
if (re.test(statusText) || re.test(statusSummary)) {
return { hide, name }
}
return false
} catch {
return false
}
2025-04-03 00:06:44 +03:00
}
2026-01-06 16:22:52 +02:00
case 'user': {
let match = false
if (caseSensitive) {
match =
poster.includes(value) ||
replyToUser.includes(value) ||
mentions.some((mention) => mention.includes(value))
} else {
const lowercaseValue = value.toLowerCase()
match =
poster.toLowerCase().includes(lowercaseValue) ||
replyToUser.toLowerCase().includes(lowercaseValue) ||
2026-05-13 16:12:52 +03:00
mentions.some((mention) =>
mention.toLowerCase().includes(lowercaseValue),
)
}
if (match) {
2025-04-03 00:06:44 +03:00
return { hide, name }
}
2026-01-06 16:22:52 +02:00
break
}
case 'user_regexp': {
try {
const re = new RegExp(value, caseSensitive ? '' : 'i')
2026-01-06 16:22:52 +02:00
if (
re.test(poster) ||
re.test(replyToUser) ||
mentions.some((mention) => re.test(mention))
) {
return { hide, name }
}
return false
} catch {
return false
}
2025-04-03 00:06:44 +03:00
}
}
2026-01-06 16:22:52 +02:00
})
2026-08-04 19:55:44 +03:00
.filter(Boolean)
}