Check that announces are understood

main
Bob Mottram 2021-07-18 20:35:34 +01:00
parent 8c8c279cd0
commit 93a30f1255
3 changed files with 40 additions and 33 deletions

View File

@ -13,6 +13,7 @@ import datetime
import time
import random
from linked_data_sig import verifyJsonSignature
from utils import understoodPostLanguage
from utils import getContentFromPost
from utils import acctDir
from utils import removeDomainPort
@ -67,7 +68,6 @@ from utils import undoAnnounceCollectionEntry
from utils import dangerousMarkup
from utils import isDM
from utils import isReply
from utils import getActorLanguagesList
from httpsig import messageContentDigest
from posts import createDirectMessagePost
from posts import validContentWarning
@ -1597,33 +1597,6 @@ def _estimateNumberOfEmoji(content: str) -> int:
return int(content.count(':') / 2)
def _understoodPostLanguage(baseDir: str, nickname: str, domain: str,
messageJson: {}, systemLanguage: str) -> bool:
"""Returns true if the post is written in a language
understood by this account
"""
if not messageJson['object'].get('contentMap'):
return True
if not isinstance(messageJson['object']['contentMap'], dict):
return True
if messageJson['object']['contentMap'].get(systemLanguage):
return True
actorFilename = acctDir(baseDir, nickname, domain)
if not os.path.isfile(actorFilename):
return False
actorJson = loadJson(actorFilename)
if not actorJson:
print('WARN: unable to load actor to check languages ' + actorFilename)
return False
languagesUnderstood = getActorLanguagesList(actorJson)
if not languagesUnderstood:
return True
for lang in languagesUnderstood:
if messageJson['object']['contentMap'].get(lang):
return True
return False
def _validPostContent(baseDir: str, nickname: str, domain: str,
messageJson: {}, maxMentions: int, maxEmoji: int,
allowLocalNetworkAccess: bool, debug: bool,
@ -1696,8 +1669,8 @@ def _validPostContent(baseDir: str, nickname: str, domain: str,
messageJson['object']['tag'])
return False
# check that the post is in a language suitable for this account
if not _understoodPostLanguage(baseDir, nickname, domain,
messageJson, systemLanguage):
if not understoodPostLanguage(baseDir, nickname, domain,
messageJson, systemLanguage):
return False
# check for filtered content
if isFiltered(baseDir, nickname, domain, contentStr):

View File

@ -31,6 +31,7 @@ from session import postImage
from webfinger import webfingerHandle
from httpsig import createSignedHeader
from siteactive import siteIsActive
from utils import understoodPostLanguage
from utils import getContentFromPost
from utils import removeDomainPort
from utils import getPortFromDomain
@ -4027,7 +4028,9 @@ def downloadAnnounce(session, baseDir: str, httpPrefix: str,
baseDir, nickname, domain, postId,
recentPostsCache)
return None
if not understoodPostLanguage(baseDir, nickname, domain,
announcedJson, systemLanguage):
return None
# Check the content of the announce
contentStr = announcedJson['content']
if dangerousMarkup(contentStr, allowLocalNetworkAccess):

View File

@ -2564,7 +2564,7 @@ def validUrlPrefix(url: str) -> bool:
return False
def getActorLanguagesList(actorJson: {}) -> []:
def _getActorLanguagesList(actorJson: {}) -> []:
"""Returns a list containing languages used by the given actor
"""
if not actorJson.get('attachment'):
@ -2589,7 +2589,7 @@ def getActorLanguagesList(actorJson: {}) -> []:
def getActorLanguages(actorJson: {}) -> str:
"""Returns a string containing languages used by the given actor
"""
langList = getActorLanguagesList(actorJson)
langList = _getActorLanguagesList(actorJson)
if not langList:
return ''
languagesStr = ''
@ -2640,3 +2640,34 @@ def setActorLanguages(baseDir: str, actorJson: {}, languagesStr: str) -> None:
"value": langList2
}
actorJson['attachment'].append(newLanguages)
def understoodPostLanguage(baseDir: str, nickname: str, domain: str,
messageJson: {}, systemLanguage: str) -> bool:
"""Returns true if the post is written in a language
understood by this account
"""
msgObject = messageJson
if msgObject.get('object'):
if isinstance(msgObject['object'], dict):
msgObject = messageJson['object']
if not msgObject.get('contentMap'):
return True
if not isinstance(msgObject['contentMap'], dict):
return True
if msgObject['contentMap'].get(systemLanguage):
return True
actorFilename = acctDir(baseDir, nickname, domain)
if not os.path.isfile(actorFilename):
return False
actorJson = loadJson(actorFilename)
if not actorJson:
print('WARN: unable to load actor to check languages ' + actorFilename)
return False
languagesUnderstood = _getActorLanguagesList(actorJson)
if not languagesUnderstood:
return True
for lang in languagesUnderstood:
if msgObject['contentMap'].get(lang):
return True
return False