mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fixed #118
This commit is contained in:
105
src/components/Timeline/Shared/Filtered.tsx
Normal file
105
src/components/Timeline/Shared/Filtered.tsx
Normal file
@ -0,0 +1,105 @@
|
||||
import { store } from '@root/store'
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import { getInstance, getInstanceAccount } from '@utils/slices/instancesSlice'
|
||||
import { StyleConstants } from '@utils/styles/constants'
|
||||
import { useTheme } from '@utils/styles/ThemeManager'
|
||||
import htmlparser2 from 'htmlparser2-without-node-native'
|
||||
import React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Text, View } from 'react-native'
|
||||
|
||||
const TimelineFiltered = React.memo(
|
||||
() => {
|
||||
const { theme } = useTheme()
|
||||
const { t } = useTranslation('componentTimeline')
|
||||
|
||||
return (
|
||||
<View style={{ backgroundColor: theme.backgroundDefault }}>
|
||||
<Text
|
||||
style={{
|
||||
...StyleConstants.FontStyle.S,
|
||||
color: theme.secondary,
|
||||
textAlign: 'center',
|
||||
paddingVertical: StyleConstants.Spacing.S,
|
||||
paddingLeft: StyleConstants.Avatar.M + StyleConstants.Spacing.S
|
||||
}}
|
||||
>
|
||||
{t('shared.filtered')}
|
||||
</Text>
|
||||
</View>
|
||||
)
|
||||
},
|
||||
() => true
|
||||
)
|
||||
|
||||
export const shouldFilter = ({
|
||||
status,
|
||||
queryKey
|
||||
}: {
|
||||
status: Mastodon.Status
|
||||
queryKey: QueryKeyTimeline
|
||||
}) => {
|
||||
const instance = getInstance(store.getState())
|
||||
const ownAccount =
|
||||
getInstanceAccount(store.getState())?.id === status.account.id
|
||||
|
||||
let shouldFilter = false
|
||||
if (queryKey && !ownAccount) {
|
||||
const parser = new htmlparser2.Parser({
|
||||
ontext (text: string) {
|
||||
const checkFilter = (filter: Mastodon.Filter) => {
|
||||
switch (filter.whole_word) {
|
||||
case true:
|
||||
if (new RegExp('\\b' + filter.phrase + '\\b').test(text)) {
|
||||
shouldFilter = true
|
||||
}
|
||||
break
|
||||
case false:
|
||||
if (new RegExp(filter.phrase).test(text)) {
|
||||
shouldFilter = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
instance?.filters.forEach(filter => {
|
||||
if (filter.expires_at) {
|
||||
if (new Date().getTime() > new Date(filter.expires_at).getTime()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch (queryKey[1].page) {
|
||||
case 'Following':
|
||||
case 'Local':
|
||||
case 'List':
|
||||
case 'Account_Default':
|
||||
if (filter.context.includes('home')) {
|
||||
checkFilter(filter)
|
||||
}
|
||||
break
|
||||
case 'Notifications':
|
||||
if (filter.context.includes('notifications')) {
|
||||
checkFilter(filter)
|
||||
}
|
||||
break
|
||||
case 'LocalPublic':
|
||||
if (filter.context.includes('public')) {
|
||||
checkFilter(filter)
|
||||
}
|
||||
break
|
||||
case 'Toot':
|
||||
if (filter.context.includes('thread')) {
|
||||
checkFilter(filter)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
parser.write(status.content)
|
||||
parser.end()
|
||||
}
|
||||
|
||||
return shouldFilter
|
||||
}
|
||||
|
||||
export default TimelineFiltered
|
Reference in New Issue
Block a user