Resolve petnames

main
Bob Mottram 2020-06-29 20:15:51 +01:00
parent 54daa6a77c
commit 1e8a6fee52
2 changed files with 45 additions and 0 deletions

View File

@ -75,3 +75,46 @@ def getPetName(baseDir: str, nickname: str, domain: str,
if pet.endswith(' ' + handle):
return pet.replace(' ' + handle, '').strip()
return ''
def getPetNameHandle(baseDir: str, nickname: str, domain: str,
petname: str) -> str:
"""Given a petname returns the handle
"""
if petname.startswith('@'):
petname = petname[1:]
petnamesFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/petnames.txt'
if not os.path.isfile(petnamesFilename):
return ''
with open(petnamesFilename, 'r') as petnamesFile:
petnamesStr = petnamesFile.read()
if petname + ' ' in petnamesStr:
petnamesList = petnamesStr.split('\n')
for pet in petnamesList:
if pet.startswith(petname + ' '):
return pet.replace(petname + ' ', '').strip()
return ''
def resolvePetnames(baseDir: str, nickname: str, domain: str,
content: str) -> str:
"""Replaces petnames with their full handles
"""
if not content:
return content
if ' ' not in content:
return content
words = content.strip().split(' ')
for wrd in words:
# check initial words beginning with @
if not wrd.startswith('@'):
break
# does a petname handle exist for this?
handle = getPetNameHandle(baseDir, nickname, domain, wrd)
if not handle:
continue
# replace the petname with the handle
content = content.replace(wrd + ' ', handle)
return content

View File

@ -55,6 +55,7 @@ from blocking import isBlocked
from filters import isFiltered
from git import convertPostToPatch
from jsonldsig import jsonldSign
from petnames import resolvePetnames
# try:
# from BeautifulSoup import BeautifulSoup
# except ImportError:
@ -1151,6 +1152,7 @@ def createDirectMessagePost(baseDir: str,
location=None) -> {}:
"""Direct Message post
"""
content = resolvePetnames(baseDir, nickname, domain, content)
mentionedPeople = \
getMentionedPeople(baseDir, httpPrefix, content, domain, debug)
if debug: