From af32aa08d67b378835af7ba8c5063abec49bdb05 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 9 Aug 2019 10:09:21 +0100 Subject: [PATCH] Refactor mentions replacements --- content.py | 72 +++++++++++++++++++++++++++++++++--------------------- daemon.py | 4 +-- posts.py | 4 +-- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/content.py b/content.py index e2335852..d7ade1fe 100644 --- a/content.py +++ b/content.py @@ -9,7 +9,41 @@ __status__ = "Production" import os import commentjson -def addMentions(baseDir: str,httpPrefix: str, \ +def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},recipients: []) -> bool: + """Detects mentions and adds them to the replacements dict and recipients list + """ + if not wordStr.startswith('@'): + return False + if len(wordStr)<2: + return False + possibleHandle=wordStr[1:] + if '@' not in possibleHandle: + return False + replaceFound=False + possibleNickname=possibleHandle.split('@')[0] + possibleDomain=possibleHandle.split('@')[1] + for follow in following: + if follow.replace('\n','')==possibleHandle: + recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname + if recipientActor not in recipients: + recipients.append(recipientActor) + replaceMentions[wordStr]="@"+possibleNickname+"" + replaceFound=True + break + if not replaceFound: + # fall back to a best effort match if an exact one is not found + for follow in following: + if follow.startswith(possibleNickname+'@'): + replaceDomain=follow.replace('\n','').split('@')[1] + recipientActor=httpPrefix+"://"+replaceDomain+"/users/"+possibleNickname + if recipientActor not in recipients: + recipients.append(recipientActor) + replaceMentions[wordStr]="@"+possibleNickname+"" + replaceFound=True + break + return replaceFound + +def addHtmlTags(baseDir: str,httpPrefix: str, \ nickname: str,domain: str,content: str, \ recipients: []) -> str: """ Replaces plaintext mentions such as @nick@domain into html @@ -27,36 +61,18 @@ def addMentions(baseDir: str,httpPrefix: str, \ content=content.replace('\n','

') content='

'+content+'

' return content.replace('

','') + + # read the following list so that we can detect just @nick + # in addition to @nick@domain with open(followingFilename, "r") as f: following = f.readlines() + + # extract mentions and tags from words for wordStr in words: - if wordStr.startswith('@'): - if len(wordStr)>1: - possibleHandle=wordStr[1:] - if '@' in possibleHandle: - possibleNickname=possibleHandle.split('@')[0] - possibleDomain=possibleHandle.split('@')[1] - replaceFound=False - for follow in following: - if follow.replace('\n','')==possibleHandle: - recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname - if recipientActor not in recipients: - recipients.append(recipientActor) - replaceMentions[wordStr]="@"+possibleNickname+"" - replaceFound=True - break - if not replaceFound: - # fall back to a best effort match if an exact one is not found - for follow in following: - if follow.startswith(possibleNickname+'@'): - replaceDomain=follow.replace('\n','').split('@')[1] - recipientActor=httpPrefix+"://"+replaceDomain+"/users/"+possibleNickname - if recipientActor not in recipients: - recipients.append(recipientActor) - replaceMentions[wordStr]="@"+possibleNickname+"" - replaceFound=True - break - # do the mention replacements + if addMention(wordStr,httpPrefix,following,replaceMentions,recipients): + continue + + # replace words with their html versions for wordStr,replaceStr in replaceMentions.items(): content=content.replace(wordStr,replaceStr) content=content.replace('\n','

') diff --git a/daemon.py b/daemon.py index 61163dd8..8552bb9c 100644 --- a/daemon.py +++ b/daemon.py @@ -86,7 +86,7 @@ from manualapprove import manualDenyFollowRequest from manualapprove import manualApproveFollowRequest from announce import createAnnounce from announce import outboxAnnounce -from content import addMentions +from content import addHtmlTags from media import removeMetaData import os import sys @@ -1835,7 +1835,7 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('bio'): if fields['bio']!=actorJson['summary']: actorJson['summary']= \ - addMentions(self.server.baseDir, \ + addHtmlTags(self.server.baseDir, \ self.server.httpPrefix, \ nickname, \ self.server.domain,fields['bio'],[]) diff --git a/posts.py b/posts.py index fc22978f..8f6aa2e9 100644 --- a/posts.py +++ b/posts.py @@ -39,7 +39,7 @@ from utils import validNickname from capabilities import getOcapFilename from capabilities import capabilitiesUpdate from media import attachImage -from content import addMentions +from content import addHtmlTags from auth import createBasicAuthHeader try: from BeautifulSoup import BeautifulSoup @@ -381,7 +381,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \ mentionedRecipients=[] if not clientToServer: # convert content to html - content=addMentions(baseDir,httpPrefix, \ + content=addHtmlTags(baseDir,httpPrefix, \ nickname,domain,content, \ mentionedRecipients)