From 19ae236665df2a1cb827d6983722f08720624491 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 5 Aug 2019 20:13:15 +0100 Subject: [PATCH] Include mentions in new posts --- content.py | 15 +++++++++++++++ daemon.py | 9 ++++++++- webinterface.py | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/content.py b/content.py index a7c384a1..e6eec7d6 100644 --- a/content.py +++ b/content.py @@ -62,3 +62,18 @@ def addMentions(baseDir: str,httpPrefix: str, \ content=content.replace('\n','

') return '

'+content+'

' +def getMentionsFromHtml(htmlText: str,matchStr=" []: + """Extracts mentioned actors from the given html content string + """ + mentions=[] + if matchStr not in htmlText: + return mentions + mentionsList=htmlStr.split(matchStr) + for mentionStr in mentionsList: + if '"' not in mentionStr: + continue + actorStr=mentionStr.split('"')[0] + if actorStr.startswith('http') or \ + actorStr.startswith('dat:'): + mentions.append(actorStr) + return mentions diff --git a/daemon.py b/daemon.py index 99b9c5fd..718a4644 100644 --- a/daemon.py +++ b/daemon.py @@ -823,8 +823,15 @@ class PubServer(BaseHTTPRequestHandler): # reply from the web interface icon inReplyToUrl=None + replyToList=[] if authorized and '?replyto=' in self.path: inReplyToUrl=self.path.split('?replyto=')[1] + if '?' in inReplyToUrl: + mentionsList=inReplyToUrl.split('?') + for m in mentionsList: + if m.startswith('mention='): + replyToList.append(m.replace('mention=','')) + inReplyToUrl=mentionsList[0] self.path=self.path.split('?replyto=')[0]+'/newpost' # edit profile in web interface @@ -842,7 +849,7 @@ class PubServer(BaseHTTPRequestHandler): self.path.endswith('/newdm') or \ self.path.endswith('/newshare')): self._set_headers('text/html',cookie) - self.wfile.write(htmlNewPost(self.server.baseDir,self.path,inReplyToUrl).encode()) + self.wfile.write(htmlNewPost(self.server.baseDir,self.path,inReplyToUrl,replyToList).encode()) self.server.GETbusy=False return diff --git a/webinterface.py b/webinterface.py index d33090e5..2919aeae 100644 --- a/webinterface.py +++ b/webinterface.py @@ -28,6 +28,7 @@ from auth import createPassword from like import likedByPerson from announce import announcedByPerson from blocking import isBlocked +from content import getMentionsFromHtml def htmlEditProfile(baseDir: str,path: str,domain: str,port: int) -> str: """Shows the edit profile screen @@ -179,7 +180,7 @@ def htmlLogin(baseDir: str) -> str: loginForm+=htmlFooter() return loginForm -def htmlNewPost(baseDir: str,path: str,inReplyTo: str) -> str: +def htmlNewPost(baseDir: str,path: str,inReplyTo: str,mentions: []) -> str: replyStr='' if not path.endswith('/newshare'): if not inReplyTo: @@ -237,6 +238,20 @@ def htmlNewPost(baseDir: str,path: str,inReplyTo: str) -> str: shareOptionOnDropdown='' if not replyStr: shareOptionOnDropdown='Share
Describe a shared item
' + + mentionsStr='' + for m in mentions: + mentionNickname=getNicknameFromActor(m) + if not mentionNickname: + continue + mentionDomain,mentionPort=getDomainFromActor(m) + if not mentionDomain: + continue + if mentionPort: + mentionsStr+='@'+mentionNickname+'@'+mentionDomain+':'+str(mentionPort)+' ' + else: + mentionsStr+='@'+mentionNickname+'@'+mentionDomain+' ' + newPostForm+= \ '
' \ '
' \ @@ -258,7 +273,7 @@ def htmlNewPost(baseDir: str,path: str,inReplyTo: str) -> str: replyStr+ \ ' ' \ '' \ - ' ' \ + ' ' \ ''+extraFields+ \ '
' \ ' ' \ @@ -700,8 +715,20 @@ def individualPostAsHtml(baseDir: str, \ '' if showIcons: + replyToLink=postJsonObject['object']['id'] + if postJsonObject['object'].get('attributedTo'): + replyToLink+='?mention='+postJsonObject['object']['attributedTo'] + if postJsonObject['object'].get('content'): + mentionedActors=getMentionsFromHtml(postJsonObject['object']['content']) + if mentionedActors: + for actorUrl in mentionedActors: + if '?mention='+actorUrl not in replyToLink: + replyToLink+='?mention='+actorUrl + if len(replyToLink)>500: + break + footerStr='
' - footerStr+='' + footerStr+='' footerStr+='' footerStr+=announceStr+likeStr+deleteStr footerStr+=''+publishedStr+''