From 1e8a6fee520bc692a14bc94a7eee3a6a5c2bc04e Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 29 Jun 2020 20:15:51 +0100 Subject: [PATCH] Resolve petnames --- petnames.py | 43 +++++++++++++++++++++++++++++++++++++++++++ posts.py | 2 ++ 2 files changed, 45 insertions(+) diff --git a/petnames.py b/petnames.py index 3888f5bb..a758c50f 100644 --- a/petnames.py +++ b/petnames.py @@ -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 diff --git a/posts.py b/posts.py index efa33e06..31319e82 100644 --- a/posts.py +++ b/posts.py @@ -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: