Add and remove global word filters from moderator screen

alt-html-css
Bob Mottram 2020-12-19 11:29:55 +00:00
parent bc1ab2dc37
commit c447f90ec9
2 changed files with 45 additions and 3 deletions

View File

@ -239,6 +239,8 @@ from newswire import loadHashtagCategories
from newsdaemon import runNewswireWatchdog from newsdaemon import runNewswireWatchdog
from newsdaemon import runNewswireDaemon from newsdaemon import runNewswireDaemon
from filters import isFiltered from filters import isFiltered
from filters import addGlobalFilter
from filters import removeGlobalFilter
import os import os
@ -1506,6 +1508,10 @@ class PubServer(BaseHTTPRequestHandler):
moderationButton = 'block' moderationButton = 'block'
elif moderationStr.startswith('submitUnblock'): elif moderationStr.startswith('submitUnblock'):
moderationButton = 'unblock' moderationButton = 'unblock'
elif moderationStr.startswith('submitFilter'):
moderationButton = 'filter'
elif moderationStr.startswith('submitUnfilter'):
moderationButton = 'unfilter'
elif moderationStr.startswith('submitSuspend'): elif moderationStr.startswith('submitSuspend'):
moderationButton = 'suspend' moderationButton = 'suspend'
elif moderationStr.startswith('submitUnsuspend'): elif moderationStr.startswith('submitUnsuspend'):
@ -1526,6 +1532,10 @@ class PubServer(BaseHTTPRequestHandler):
suspendAccount(baseDir, nickname, domain) suspendAccount(baseDir, nickname, domain)
if moderationButton == 'unsuspend': if moderationButton == 'unsuspend':
unsuspendAccount(baseDir, nickname) unsuspendAccount(baseDir, nickname)
if moderationButton == 'filter':
addGlobalFilter(baseDir, moderationText)
if moderationButton == 'unfilter':
removeGlobalFilter(baseDir, moderationText)
if moderationButton == 'block': if moderationButton == 'block':
fullBlockDomain = None fullBlockDomain = None
if moderationText.startswith('http') or \ if moderationText.startswith('http') or \

View File

@ -23,6 +23,20 @@ def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
return True return True
def addGlobalFilter(baseDir: str, words: str) -> bool:
"""Adds a global filter for particular words within
the content of a incoming posts
"""
filtersFilename = baseDir + '/accounts/filters.txt'
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
return False
filtersFile = open(filtersFilename, "a+")
filtersFile.write(words + '\n')
filtersFile.close()
return True
def removeFilter(baseDir: str, nickname: str, domain: str, def removeFilter(baseDir: str, nickname: str, domain: str,
words: str) -> bool: words: str) -> bool:
"""Removes a word filter """Removes a word filter
@ -43,6 +57,24 @@ def removeFilter(baseDir: str, nickname: str, domain: str,
return False return False
def removeGlobalFilter(baseDir: str, words: str) -> bool:
"""Removes a global word filter
"""
filtersFilename = baseDir + '/accounts/filters.txt'
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
with open(filtersFilename, 'r') as fp:
with open(filtersFilename + '.new', 'w+') as fpnew:
for line in fp:
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
if os.path.isfile(filtersFilename + '.new'):
os.rename(filtersFilename + '.new', filtersFilename)
return True
return False
def isTwitterPost(content: str) -> bool: def isTwitterPost(content: str) -> bool:
"""Returns true if the given post content is a retweet or twitter crosspost """Returns true if the given post content is a retweet or twitter crosspost
""" """
@ -66,9 +98,9 @@ def isFiltered(baseDir: str, nickname: str, domain: str, content: str) -> bool:
if isTwitterPost(content): if isTwitterPost(content):
return True return True
instanceFiltersFilename = baseDir + '/accounts/filters.txt' globalFiltersFilename = baseDir + '/accounts/filters.txt'
if os.path.isfile(instanceFiltersFilename): if os.path.isfile(globalFiltersFilename):
if content + '\n' in open(instanceFiltersFilename).read(): if content + '\n' in open(globalFiltersFilename).read():
return True return True
accountFiltersFilename = baseDir + '/accounts/' + \ accountFiltersFilename = baseDir + '/accounts/' + \