Test for moderated feed

merge-requests/8/head
Bob Mottram 2020-10-17 18:36:10 +01:00
parent aa1017b3a8
commit f934f54528
2 changed files with 71 additions and 30 deletions

View File

@ -73,7 +73,7 @@ def removeControlCharacters(content: str) -> str:
return content return content
def hashtagRuleResolve(tree: [], hashtags: []) -> bool: def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool) -> bool:
"""Returns whether the tree for a hashtag rule evaluates to true or false """Returns whether the tree for a hashtag rule evaluates to true or false
""" """
if not tree: if not tree:
@ -84,7 +84,7 @@ def hashtagRuleResolve(tree: [], hashtags: []) -> bool:
if isinstance(tree[1], str): if isinstance(tree[1], str):
return tree[1] not in hashtags return tree[1] not in hashtags
elif isinstance(tree[1], list): elif isinstance(tree[1], list):
return not hashtagRuleResolve(tree[1], hashtags) return not hashtagRuleResolve(tree[1], hashtags, moderated)
elif tree[0] == 'and': elif tree[0] == 'and':
if len(tree) == 3: if len(tree) == 3:
@ -92,13 +92,13 @@ def hashtagRuleResolve(tree: [], hashtags: []) -> bool:
if isinstance(tree[1], str): if isinstance(tree[1], str):
firstArg = (tree[1] in hashtags) firstArg = (tree[1] in hashtags)
elif isinstance(tree[1], list): elif isinstance(tree[1], list):
firstArg = (hashtagRuleResolve(tree[1], hashtags)) firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated))
secondArg = False secondArg = False
if isinstance(tree[2], str): if isinstance(tree[2], str):
secondArg = (tree[2] in hashtags) secondArg = (tree[2] in hashtags)
elif isinstance(tree[2], list): elif isinstance(tree[2], list):
secondArg = (hashtagRuleResolve(tree[2], hashtags)) secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated))
return firstArg and secondArg return firstArg and secondArg
elif tree[0] == 'or': elif tree[0] == 'or':
if len(tree) == 3: if len(tree) == 3:
@ -107,28 +107,33 @@ def hashtagRuleResolve(tree: [], hashtags: []) -> bool:
if isinstance(tree[1], str): if isinstance(tree[1], str):
firstArg = (tree[1] in hashtags) firstArg = (tree[1] in hashtags)
elif isinstance(tree[1], list): elif isinstance(tree[1], list):
firstArg = (hashtagRuleResolve(tree[1], hashtags)) firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated))
secondArg = False secondArg = False
if isinstance(tree[2], str): if isinstance(tree[2], str):
secondArg = (tree[2] in hashtags) secondArg = (tree[2] in hashtags)
elif isinstance(tree[2], list): elif isinstance(tree[2], list):
secondArg = (hashtagRuleResolve(tree[2], hashtags)) secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated))
return firstArg or secondArg return firstArg or secondArg
elif tree[0].startswith('#') and len(tree) == 1: elif tree[0].startswith('#') and len(tree) == 1:
return tree[0] in hashtags return tree[0] in hashtags
elif tree[0].startswith('moderated'):
return moderated
return False return False
def hashtagRuleTree(operators: [], def hashtagRuleTree(operators: [],
conditionsStr: str, conditionsStr: str,
tagsInConditions: []) -> []: tagsInConditions: [],
moderated: bool) -> []:
"""Walks the tree """Walks the tree
""" """
if not operators and conditionsStr: if not operators and conditionsStr:
conditionsStr = conditionsStr.strip() conditionsStr = conditionsStr.strip()
if conditionsStr.startswith('#') or conditionsStr in operators: if conditionsStr.startswith('#') or \
conditionsStr in operators or \
conditionsStr == 'moderated':
if conditionsStr.startswith('#'): if conditionsStr.startswith('#'):
if conditionsStr not in tagsInConditions: if conditionsStr not in tagsInConditions:
if ' ' not in conditionsStr: if ' ' not in conditionsStr:
@ -140,7 +145,9 @@ def hashtagRuleTree(operators: [],
return None return None
tree = None tree = None
conditionsStr = conditionsStr.strip() conditionsStr = conditionsStr.strip()
if conditionsStr.startswith('#') or conditionsStr in operators: if conditionsStr.startswith('#') or \
conditionsStr in operators or \
conditionsStr == 'moderated':
if conditionsStr.startswith('#'): if conditionsStr.startswith('#'):
if conditionsStr not in tagsInConditions: if conditionsStr not in tagsInConditions:
if ' ' not in conditionsStr: if ' ' not in conditionsStr:
@ -157,7 +164,7 @@ def hashtagRuleTree(operators: [],
sections = conditionsStr.split(op) sections = conditionsStr.split(op)
for subConditionStr in sections: for subConditionStr in sections:
result = hashtagRuleTree(operators[ctr + 1:], subConditionStr, result = hashtagRuleTree(operators[ctr + 1:], subConditionStr,
tagsInConditions) tagsInConditions, moderated)
if result: if result:
tree.append(result) tree.append(result)
break break
@ -170,7 +177,8 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
personCache: {}, personCache: {},
cachedWebfingers: {}, cachedWebfingers: {},
federationList: [], federationList: [],
sendThreads: [], postLog: []) -> bool: sendThreads: [], postLog: [],
moderated: bool) -> bool:
"""Applies hashtag rules to a news post. """Applies hashtag rules to a news post.
Returns true if the post should be saved to the news timeline Returns true if the post should be saved to the news timeline
of this instance of this instance
@ -199,11 +207,12 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
conditionsStr = ruleStr.split('if ', 1)[1] conditionsStr = ruleStr.split('if ', 1)[1]
conditionsStr = conditionsStr.split(' then ')[0] conditionsStr = conditionsStr.split(' then ')[0]
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
# does the rule contain any hashtags? # does the rule contain any hashtags?
if not tagsInConditions: if not tagsInConditions:
continue continue
if not hashtagRuleResolve(tree, hashtags): if not hashtagRuleResolve(tree, hashtags, moderated):
continue continue
# the condition matches, so do something # the condition matches, so do something
actionStr = ruleStr.split(' then ')[1].strip() actionStr = ruleStr.split(' then ')[1].strip()
@ -413,7 +422,7 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
httpPrefix, domain, port, httpPrefix, domain, port,
personCache, cachedWebfingers, personCache, cachedWebfingers,
federationList, federationList,
sendThreads, postLog) sendThreads, postLog, moderated)
# save the post and update the index # save the post and update the index
if savePost: if savePost:

View File

@ -83,7 +83,7 @@ from theme import setCSSparam
from jsonldsig import testSignJsonld from jsonldsig import testSignJsonld
from jsonldsig import jsonldVerify from jsonldsig import jsonldVerify
from newsdaemon import hashtagRuleTree from newsdaemon import hashtagRuleTree
from newsdaemon import hasttagRuleResolve from newsdaemon import hashtagRuleResolve
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -2179,55 +2179,87 @@ def testHashtagRuleTree():
print('testHashtagRuleTree') print('testHashtagRuleTree')
operators = ('not', 'and', 'or') operators = ('not', 'and', 'or')
moderated = True
conditionsStr = '#foo or #bar' conditionsStr = '#foo or #bar'
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == str(['or', ['#foo'], ['#bar']]) assert str(tree) == str(['or', ['#foo'], ['#bar']])
assert str(tagsInConditions) == str(['#foo', '#bar']) assert str(tagsInConditions) == str(['#foo', '#bar'])
hashtags = ['#foo'] hashtags = ['#foo']
assert hasttagRuleResolve(tree, hashtags) assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#carrot', '#stick'] hashtags = ['#carrot', '#stick']
assert not hasttagRuleResolve(tree, hashtags) assert not hashtagRuleResolve(tree, hashtags, moderated)
moderated = False
conditionsStr = 'not moderated and #foo or #bar'
tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == \
str(['not', ['and', ['moderated'], ['or', ['#foo'], ['#bar']]]])
assert str(tagsInConditions) == str(['#foo', '#bar'])
hashtags = ['#foo']
assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#carrot', '#stick']
assert hashtagRuleResolve(tree, hashtags, moderated)
moderated = True
conditionsStr = 'moderated and #foo or #bar'
tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == \
str(['and', ['moderated'], ['or', ['#foo'], ['#bar']]])
assert str(tagsInConditions) == str(['#foo', '#bar'])
hashtags = ['#foo']
assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#carrot', '#stick']
assert not hashtagRuleResolve(tree, hashtags, moderated)
conditionsStr = 'x' conditionsStr = 'x'
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert tree is None assert tree is None
assert tagsInConditions == [] assert tagsInConditions == []
hashtags = ['#foo'] hashtags = ['#foo']
assert not hasttagRuleResolve(tree, hashtags) assert not hashtagRuleResolve(tree, hashtags, moderated)
conditionsStr = '#x' conditionsStr = '#x'
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == str(['#x']) assert str(tree) == str(['#x'])
assert str(tagsInConditions) == str(['#x']) assert str(tagsInConditions) == str(['#x'])
hashtags = ['#x'] hashtags = ['#x']
assert hasttagRuleResolve(tree, hashtags) assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#y', '#z'] hashtags = ['#y', '#z']
assert not hasttagRuleResolve(tree, hashtags) assert not hashtagRuleResolve(tree, hashtags, moderated)
conditionsStr = 'not #b' conditionsStr = 'not #b'
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == str(['not', ['#b']]) assert str(tree) == str(['not', ['#b']])
assert str(tagsInConditions) == str(['#b']) assert str(tagsInConditions) == str(['#b'])
hashtags = ['#y', '#z'] hashtags = ['#y', '#z']
assert hasttagRuleResolve(tree, hashtags) assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#a', '#b', '#c'] hashtags = ['#a', '#b', '#c']
assert not hasttagRuleResolve(tree, hashtags) assert not hashtagRuleResolve(tree, hashtags, moderated)
conditionsStr = '#foo or #bar and #a' conditionsStr = '#foo or #bar and #a'
tagsInConditions = [] tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr, tagsInConditions) tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']]) assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
assert str(tagsInConditions) == str(['#foo', '#bar', '#a']) assert str(tagsInConditions) == str(['#foo', '#bar', '#a'])
hashtags = ['#bar', '#a'] hashtags = ['#bar', '#a']
assert hasttagRuleResolve(tree, hashtags) assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#foo', '#a'] hashtags = ['#foo', '#a']
assert hasttagRuleResolve(tree, hashtags) assert hashtagRuleResolve(tree, hashtags, moderated)
hashtags = ['#x', '#a'] hashtags = ['#x', '#a']
assert not hasttagRuleResolve(tree, hashtags) assert not hashtagRuleResolve(tree, hashtags, moderated)
def runAllTests(): def runAllTests():