forked from indymedia/epicyon
From operator in hashtag rules
parent
77fd759adf
commit
89a9b8d7b3
|
@ -15,7 +15,7 @@ Logical Operators
|
||||||
|
|
||||||
The following operators are available:
|
The following operators are available:
|
||||||
|
|
||||||
not, and, or, xor, contains
|
not, and, or, xor, from, contains
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
|
@ -39,3 +39,7 @@ You can also remove hashtags.
|
||||||
if #garden or #lawn then remove #gardening
|
if #garden or #lawn then remove #gardening
|
||||||
|
|
||||||
Which will remove #gardening if it exists as a hashtag within the news post.
|
Which will remove #gardening if it exists as a hashtag within the news post.
|
||||||
|
|
||||||
|
You can add tags based upon the RSS link, such as:
|
||||||
|
|
||||||
|
if from "mycatsite.com" then add #cats
|
||||||
|
|
|
@ -74,7 +74,7 @@ def removeControlCharacters(content: str) -> str:
|
||||||
|
|
||||||
|
|
||||||
def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
content: str) -> bool:
|
content: str, url: str) -> 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:
|
||||||
|
@ -86,7 +86,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
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, moderated,
|
return not hashtagRuleResolve(tree[1], hashtags, moderated,
|
||||||
content)
|
content, url)
|
||||||
elif tree[0] == 'contains':
|
elif tree[0] == 'contains':
|
||||||
if len(tree) == 2:
|
if len(tree) == 2:
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[1], str):
|
||||||
|
@ -101,6 +101,20 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
matchStr = matchStr[1:]
|
matchStr = matchStr[1:]
|
||||||
matchStr = matchStr[:len(matchStr) - 1]
|
matchStr = matchStr[:len(matchStr) - 1]
|
||||||
return matchStr.lower() in content
|
return matchStr.lower() in content
|
||||||
|
elif tree[0] == 'from':
|
||||||
|
if len(tree) == 2:
|
||||||
|
if isinstance(tree[1], str):
|
||||||
|
matchStr = tree[1]
|
||||||
|
if matchStr.startswith('"') and matchStr.endswith('"'):
|
||||||
|
matchStr = matchStr[1:]
|
||||||
|
matchStr = matchStr[:len(matchStr) - 1]
|
||||||
|
return matchStr.lower() in url
|
||||||
|
elif isinstance(tree[1], list):
|
||||||
|
matchStr = tree[1][0]
|
||||||
|
if matchStr.startswith('"') and matchStr.endswith('"'):
|
||||||
|
matchStr = matchStr[1:]
|
||||||
|
matchStr = matchStr[:len(matchStr) - 1]
|
||||||
|
return matchStr.lower() in url
|
||||||
elif tree[0] == 'and':
|
elif tree[0] == 'and':
|
||||||
if len(tree) >= 3:
|
if len(tree) >= 3:
|
||||||
for argIndex in range(1, len(tree)):
|
for argIndex in range(1, len(tree)):
|
||||||
|
@ -110,7 +124,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
elif isinstance(tree[argIndex], list):
|
elif isinstance(tree[argIndex], list):
|
||||||
argValue = hashtagRuleResolve(tree[argIndex],
|
argValue = hashtagRuleResolve(tree[argIndex],
|
||||||
hashtags, moderated,
|
hashtags, moderated,
|
||||||
content)
|
content, url)
|
||||||
if not argValue:
|
if not argValue:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
@ -123,7 +137,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
elif isinstance(tree[argIndex], list):
|
elif isinstance(tree[argIndex], list):
|
||||||
argValue = hashtagRuleResolve(tree[argIndex],
|
argValue = hashtagRuleResolve(tree[argIndex],
|
||||||
hashtags, moderated,
|
hashtags, moderated,
|
||||||
content)
|
content, url)
|
||||||
if argValue:
|
if argValue:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
@ -137,7 +151,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
elif isinstance(tree[argIndex], list):
|
elif isinstance(tree[argIndex], list):
|
||||||
argValue = hashtagRuleResolve(tree[argIndex],
|
argValue = hashtagRuleResolve(tree[argIndex],
|
||||||
hashtags, moderated,
|
hashtags, moderated,
|
||||||
content)
|
content, url)
|
||||||
if argValue:
|
if argValue:
|
||||||
trueCtr += 1
|
trueCtr += 1
|
||||||
if trueCtr == 1:
|
if trueCtr == 1:
|
||||||
|
@ -219,7 +233,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
||||||
cachedWebfingers: {},
|
cachedWebfingers: {},
|
||||||
federationList: [],
|
federationList: [],
|
||||||
sendThreads: [], postLog: [],
|
sendThreads: [], postLog: [],
|
||||||
moderated: bool) -> bool:
|
moderated: bool, url: str) -> 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
|
||||||
|
@ -245,7 +259,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
||||||
content = content.lower()
|
content = content.lower()
|
||||||
|
|
||||||
# actionOccurred = False
|
# actionOccurred = False
|
||||||
operators = ('not', 'and', 'or', 'xor', 'contains')
|
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
|
||||||
for ruleStr in rules:
|
for ruleStr in rules:
|
||||||
if not ruleStr:
|
if not ruleStr:
|
||||||
continue
|
continue
|
||||||
|
@ -258,7 +272,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
||||||
tagsInConditions = []
|
tagsInConditions = []
|
||||||
tree = hashtagRuleTree(operators, conditionsStr,
|
tree = hashtagRuleTree(operators, conditionsStr,
|
||||||
tagsInConditions, moderated)
|
tagsInConditions, moderated)
|
||||||
if not hashtagRuleResolve(tree, hashtags, moderated, content):
|
if not hashtagRuleResolve(tree, hashtags, moderated, content, url):
|
||||||
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()
|
||||||
|
@ -607,7 +621,8 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
|
||||||
httpPrefix, domain, port,
|
httpPrefix, domain, port,
|
||||||
personCache, cachedWebfingers,
|
personCache, cachedWebfingers,
|
||||||
federationList,
|
federationList,
|
||||||
sendThreads, postLog, moderated)
|
sendThreads, postLog,
|
||||||
|
moderated, url)
|
||||||
|
|
||||||
# save the post and update the index
|
# save the post and update the index
|
||||||
if savePost:
|
if savePost:
|
||||||
|
|
52
tests.py
52
tests.py
|
@ -2177,8 +2177,9 @@ def testRemoveHtmlTag():
|
||||||
|
|
||||||
def testHashtagRuleTree():
|
def testHashtagRuleTree():
|
||||||
print('testHashtagRuleTree')
|
print('testHashtagRuleTree')
|
||||||
operators = ('not', 'and', 'or', 'xor', 'contains')
|
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
|
||||||
|
|
||||||
|
url = 'testsite.com'
|
||||||
moderated = True
|
moderated = True
|
||||||
conditionsStr = \
|
conditionsStr = \
|
||||||
'contains "Cat" or contains "Corvid" or ' + \
|
'contains "Cat" or contains "Corvid" or ' + \
|
||||||
|
@ -2200,9 +2201,24 @@ def testHashtagRuleTree():
|
||||||
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 hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#carrot', '#stick']
|
hashtags = ['#carrot', '#stick']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
|
content = 'This is a test'
|
||||||
|
url = 'https://testsite.com/something'
|
||||||
|
moderated = True
|
||||||
|
conditionsStr = '#foo and from "testsite.com"'
|
||||||
|
tagsInConditions = []
|
||||||
|
tree = hashtagRuleTree(operators, conditionsStr,
|
||||||
|
tagsInConditions, moderated)
|
||||||
|
assert str(tree) == str(['and', ['#foo'], ['from', ['"testsite.com"']]])
|
||||||
|
assert str(tagsInConditions) == str(['#foo'])
|
||||||
|
hashtags = ['#foo']
|
||||||
|
assert hashtagRuleResolve(tree, hashtags, moderated, content,
|
||||||
|
'testsite.com')
|
||||||
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content,
|
||||||
|
'othersite.net')
|
||||||
|
|
||||||
content = 'This is a test'
|
content = 'This is a test'
|
||||||
moderated = True
|
moderated = True
|
||||||
|
@ -2215,9 +2231,9 @@ def testHashtagRuleTree():
|
||||||
['or', ['#foo'], ['#bar']]])
|
['or', ['#foo'], ['#bar']]])
|
||||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||||
hashtags = ['#foo']
|
hashtags = ['#foo']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#carrot', '#stick']
|
hashtags = ['#carrot', '#stick']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
moderated = False
|
moderated = False
|
||||||
conditionsStr = 'not moderated and #foo or #bar'
|
conditionsStr = 'not moderated and #foo or #bar'
|
||||||
|
@ -2228,9 +2244,9 @@ def testHashtagRuleTree():
|
||||||
str(['not', ['and', ['moderated'], ['or', ['#foo'], ['#bar']]]])
|
str(['not', ['and', ['moderated'], ['or', ['#foo'], ['#bar']]]])
|
||||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||||
hashtags = ['#foo']
|
hashtags = ['#foo']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#carrot', '#stick']
|
hashtags = ['#carrot', '#stick']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
moderated = True
|
moderated = True
|
||||||
conditionsStr = 'moderated and #foo or #bar'
|
conditionsStr = 'moderated and #foo or #bar'
|
||||||
|
@ -2241,9 +2257,9 @@ def testHashtagRuleTree():
|
||||||
str(['and', ['moderated'], ['or', ['#foo'], ['#bar']]])
|
str(['and', ['moderated'], ['or', ['#foo'], ['#bar']]])
|
||||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||||
hashtags = ['#foo']
|
hashtags = ['#foo']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#carrot', '#stick']
|
hashtags = ['#carrot', '#stick']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
conditionsStr = 'x'
|
conditionsStr = 'x'
|
||||||
tagsInConditions = []
|
tagsInConditions = []
|
||||||
|
@ -2252,7 +2268,7 @@ def testHashtagRuleTree():
|
||||||
assert tree is None
|
assert tree is None
|
||||||
assert tagsInConditions == []
|
assert tagsInConditions == []
|
||||||
hashtags = ['#foo']
|
hashtags = ['#foo']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
conditionsStr = '#x'
|
conditionsStr = '#x'
|
||||||
tagsInConditions = []
|
tagsInConditions = []
|
||||||
|
@ -2261,9 +2277,9 @@ def testHashtagRuleTree():
|
||||||
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 hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#y', '#z']
|
hashtags = ['#y', '#z']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
conditionsStr = 'not #b'
|
conditionsStr = 'not #b'
|
||||||
tagsInConditions = []
|
tagsInConditions = []
|
||||||
|
@ -2272,9 +2288,9 @@ def testHashtagRuleTree():
|
||||||
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 hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#a', '#b', '#c']
|
hashtags = ['#a', '#b', '#c']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
conditionsStr = '#foo or #bar and #a'
|
conditionsStr = '#foo or #bar and #a'
|
||||||
tagsInConditions = []
|
tagsInConditions = []
|
||||||
|
@ -2283,13 +2299,13 @@ def testHashtagRuleTree():
|
||||||
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 = ['#foo', '#bar', '#a']
|
hashtags = ['#foo', '#bar', '#a']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#bar', '#a']
|
hashtags = ['#bar', '#a']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#foo', '#a']
|
hashtags = ['#foo', '#a']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
hashtags = ['#x', '#a']
|
hashtags = ['#x', '#a']
|
||||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||||
|
|
||||||
|
|
||||||
def runAllTests():
|
def runAllTests():
|
||||||
|
|
Loading…
Reference in New Issue