tooot/src/components/Timeline/Shared/Filtered.tsx

110 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-05-30 23:39:07 +02:00
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(
() => {
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2021-05-30 23:39:07 +02:00
const { t } = useTranslation('componentTimeline')
return (
2022-02-12 14:51:01 +01:00
<View style={{ backgroundColor: colors.backgroundDefault }}>
2021-05-30 23:39:07 +02:00
<Text
style={{
...StyleConstants.FontStyle.S,
2022-02-12 14:51:01 +01:00
color: colors.secondary,
2021-05-30 23:39:07 +02:00
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 =
2022-02-14 22:10:07 +01:00
getInstanceAccount(store.getState())?.id === status.account?.id
2021-05-30 23:39:07 +02:00
let shouldFilter = false
2021-06-02 22:27:51 +02:00
if (!ownAccount) {
2021-05-30 23:39:07 +02:00
const parser = new htmlparser2.Parser({
2022-02-10 23:09:40 +01:00
ontext: (text: string) => {
2021-05-30 23:39:07 +02:00
const checkFilter = (filter: Mastodon.Filter) => {
2021-06-02 22:27:51 +02:00
const escapedPhrase = filter.phrase.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
) // $& means the whole matched string
2021-05-30 23:39:07 +02:00
switch (filter.whole_word) {
case true:
2022-02-10 23:09:40 +01:00
if (new RegExp('\\B' + escapedPhrase + '\\B').test(text)) {
2021-05-30 23:39:07 +02:00
shouldFilter = true
}
break
case false:
2021-06-02 22:27:51 +02:00
if (new RegExp(escapedPhrase).test(text)) {
2021-05-30 23:39:07 +02:00
shouldFilter = true
}
break
}
}
2021-06-02 22:27:51 +02:00
instance?.filters?.forEach(filter => {
2021-05-30 23:39:07 +02:00
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