forked from indymedia/epicyon
Refactor mentions replacements
parent
8fdcb93546
commit
af32aa08d6
72
content.py
72
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]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+possibleDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||
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]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||
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','</p><p>')
|
||||
content='<p>'+content+'</p>'
|
||||
return content.replace('<p></p>','')
|
||||
|
||||
# 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]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+possibleDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||
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]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||
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','</p><p>')
|
||||
|
|
|
@ -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'],[])
|
||||
|
|
4
posts.py
4
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue