mirror of https://gitlab.com/bashrc2/epicyon
From operator in hashtag rules
parent
77fd759adf
commit
89a9b8d7b3
|
@ -15,7 +15,7 @@ Logical Operators
|
|||
|
||||
The following operators are available:
|
||||
|
||||
not, and, or, xor, contains
|
||||
not, and, or, xor, from, contains
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
@ -39,3 +39,7 @@ You can also remove hashtags.
|
|||
if #garden or #lawn then remove #gardening
|
||||
|
||||
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,
|
||||
content: str) -> bool:
|
||||
content: str, url: str) -> bool:
|
||||
"""Returns whether the tree for a hashtag rule evaluates to true or false
|
||||
"""
|
||||
if not tree:
|
||||
|
@ -86,7 +86,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
|||
return tree[1] not in hashtags
|
||||
elif isinstance(tree[1], list):
|
||||
return not hashtagRuleResolve(tree[1], hashtags, moderated,
|
||||
content)
|
||||
content, url)
|
||||
elif tree[0] == 'contains':
|
||||
if len(tree) == 2:
|
||||
if isinstance(tree[1], str):
|
||||
|
@ -101,6 +101,20 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
|||
matchStr = matchStr[1:]
|
||||
matchStr = matchStr[:len(matchStr) - 1]
|
||||
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':
|
||||
if len(tree) >= 3:
|
||||
for argIndex in range(1, len(tree)):
|
||||
|
@ -110,7 +124,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
|||
elif isinstance(tree[argIndex], list):
|
||||
argValue = hashtagRuleResolve(tree[argIndex],
|
||||
hashtags, moderated,
|
||||
content)
|
||||
content, url)
|
||||
if not argValue:
|
||||
return False
|
||||
return True
|
||||
|
@ -123,7 +137,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
|||
elif isinstance(tree[argIndex], list):
|
||||
argValue = hashtagRuleResolve(tree[argIndex],
|
||||
hashtags, moderated,
|
||||
content)
|
||||
content, url)
|
||||
if argValue:
|
||||
return True
|
||||
return False
|
||||
|
@ -137,7 +151,7 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
|||
elif isinstance(tree[argIndex], list):
|
||||
argValue = hashtagRuleResolve(tree[argIndex],
|
||||
hashtags, moderated,
|
||||
content)
|
||||
content, url)
|
||||
if argValue:
|
||||
trueCtr += 1
|
||||
if trueCtr == 1:
|
||||
|
@ -219,7 +233,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
|||
cachedWebfingers: {},
|
||||
federationList: [],
|
||||
sendThreads: [], postLog: [],
|
||||
moderated: bool) -> bool:
|
||||
moderated: bool, url: str) -> bool:
|
||||
"""Applies hashtag rules to a news post.
|
||||
Returns true if the post should be saved to the news timeline
|
||||
of this instance
|
||||
|
@ -245,7 +259,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
|||
content = content.lower()
|
||||
|
||||
# actionOccurred = False
|
||||
operators = ('not', 'and', 'or', 'xor', 'contains')
|
||||
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
|
||||
for ruleStr in rules:
|
||||
if not ruleStr:
|
||||
continue
|
||||
|
@ -258,7 +272,7 @@ def newswireHashtagProcessing(session, baseDir: str, postJsonObject: {},
|
|||
tagsInConditions = []
|
||||
tree = hashtagRuleTree(operators, conditionsStr,
|
||||
tagsInConditions, moderated)
|
||||
if not hashtagRuleResolve(tree, hashtags, moderated, content):
|
||||
if not hashtagRuleResolve(tree, hashtags, moderated, content, url):
|
||||
continue
|
||||
# the condition matches, so do something
|
||||
actionStr = ruleStr.split(' then ')[1].strip()
|
||||
|
@ -607,7 +621,8 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
|
|||
httpPrefix, domain, port,
|
||||
personCache, cachedWebfingers,
|
||||
federationList,
|
||||
sendThreads, postLog, moderated)
|
||||
sendThreads, postLog,
|
||||
moderated, url)
|
||||
|
||||
# save the post and update the index
|
||||
if savePost:
|
||||
|
|
52
tests.py
52
tests.py
|
@ -2177,8 +2177,9 @@ def testRemoveHtmlTag():
|
|||
|
||||
def testHashtagRuleTree():
|
||||
print('testHashtagRuleTree')
|
||||
operators = ('not', 'and', 'or', 'xor', 'contains')
|
||||
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
|
||||
|
||||
url = 'testsite.com'
|
||||
moderated = True
|
||||
conditionsStr = \
|
||||
'contains "Cat" or contains "Corvid" or ' + \
|
||||
|
@ -2200,9 +2201,24 @@ def testHashtagRuleTree():
|
|||
assert str(tree) == str(['or', ['#foo'], ['#bar']])
|
||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||
hashtags = ['#foo']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
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'
|
||||
moderated = True
|
||||
|
@ -2215,9 +2231,9 @@ def testHashtagRuleTree():
|
|||
['or', ['#foo'], ['#bar']]])
|
||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||
hashtags = ['#foo']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#carrot', '#stick']
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
moderated = False
|
||||
conditionsStr = 'not moderated and #foo or #bar'
|
||||
|
@ -2228,9 +2244,9 @@ def testHashtagRuleTree():
|
|||
str(['not', ['and', ['moderated'], ['or', ['#foo'], ['#bar']]]])
|
||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||
hashtags = ['#foo']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#carrot', '#stick']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
moderated = True
|
||||
conditionsStr = 'moderated and #foo or #bar'
|
||||
|
@ -2241,9 +2257,9 @@ def testHashtagRuleTree():
|
|||
str(['and', ['moderated'], ['or', ['#foo'], ['#bar']]])
|
||||
assert str(tagsInConditions) == str(['#foo', '#bar'])
|
||||
hashtags = ['#foo']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#carrot', '#stick']
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
conditionsStr = 'x'
|
||||
tagsInConditions = []
|
||||
|
@ -2252,7 +2268,7 @@ def testHashtagRuleTree():
|
|||
assert tree is None
|
||||
assert tagsInConditions == []
|
||||
hashtags = ['#foo']
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
conditionsStr = '#x'
|
||||
tagsInConditions = []
|
||||
|
@ -2261,9 +2277,9 @@ def testHashtagRuleTree():
|
|||
assert str(tree) == str(['#x'])
|
||||
assert str(tagsInConditions) == str(['#x'])
|
||||
hashtags = ['#x']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#y', '#z']
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
conditionsStr = 'not #b'
|
||||
tagsInConditions = []
|
||||
|
@ -2272,9 +2288,9 @@ def testHashtagRuleTree():
|
|||
assert str(tree) == str(['not', ['#b']])
|
||||
assert str(tagsInConditions) == str(['#b'])
|
||||
hashtags = ['#y', '#z']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
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'
|
||||
tagsInConditions = []
|
||||
|
@ -2283,13 +2299,13 @@ def testHashtagRuleTree():
|
|||
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
|
||||
assert str(tagsInConditions) == str(['#foo', '#bar', '#a'])
|
||||
hashtags = ['#foo', '#bar', '#a']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#bar', '#a']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#foo', '#a']
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
hashtags = ['#x', '#a']
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||
assert not hashtagRuleResolve(tree, hashtags, moderated, content, url)
|
||||
|
||||
|
||||
def runAllTests():
|
||||
|
|
Loading…
Reference in New Issue