forked from indymedia/epicyon
Refactor mentions replacements
parent
8fdcb93546
commit
af32aa08d6
66
content.py
66
content.py
|
@ -9,34 +9,19 @@ __status__ = "Production"
|
||||||
import os
|
import os
|
||||||
import commentjson
|
import commentjson
|
||||||
|
|
||||||
def addMentions(baseDir: str,httpPrefix: str, \
|
def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},recipients: []) -> bool:
|
||||||
nickname: str,domain: str,content: str, \
|
"""Detects mentions and adds them to the replacements dict and recipients list
|
||||||
recipients: []) -> str:
|
|
||||||
""" Replaces plaintext mentions such as @nick@domain into html
|
|
||||||
by matching against known following accounts
|
|
||||||
"""
|
"""
|
||||||
if content.startswith('<p>'):
|
if not wordStr.startswith('@'):
|
||||||
return content
|
return False
|
||||||
wordsOnly=content.replace(',',' ').replace(';',' ').replace('.',' ').replace(':',' ')
|
if len(wordStr)<2:
|
||||||
words=wordsOnly.split(' ')
|
return False
|
||||||
replaceMentions={}
|
|
||||||
if ':' in domain:
|
|
||||||
domain=domain.split(':')[0]
|
|
||||||
followingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt'
|
|
||||||
if not os.path.isfile(followingFilename):
|
|
||||||
content=content.replace('\n','</p><p>')
|
|
||||||
content='<p>'+content+'</p>'
|
|
||||||
return content.replace('<p></p>','')
|
|
||||||
with open(followingFilename, "r") as f:
|
|
||||||
following = f.readlines()
|
|
||||||
for wordStr in words:
|
|
||||||
if wordStr.startswith('@'):
|
|
||||||
if len(wordStr)>1:
|
|
||||||
possibleHandle=wordStr[1:]
|
possibleHandle=wordStr[1:]
|
||||||
if '@' in possibleHandle:
|
if '@' not in possibleHandle:
|
||||||
|
return False
|
||||||
|
replaceFound=False
|
||||||
possibleNickname=possibleHandle.split('@')[0]
|
possibleNickname=possibleHandle.split('@')[0]
|
||||||
possibleDomain=possibleHandle.split('@')[1]
|
possibleDomain=possibleHandle.split('@')[1]
|
||||||
replaceFound=False
|
|
||||||
for follow in following:
|
for follow in following:
|
||||||
if follow.replace('\n','')==possibleHandle:
|
if follow.replace('\n','')==possibleHandle:
|
||||||
recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname
|
recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname
|
||||||
|
@ -56,7 +41,38 @@ def addMentions(baseDir: str,httpPrefix: str, \
|
||||||
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||||
replaceFound=True
|
replaceFound=True
|
||||||
break
|
break
|
||||||
# do the mention replacements
|
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
|
||||||
|
by matching against known following accounts
|
||||||
|
"""
|
||||||
|
if content.startswith('<p>'):
|
||||||
|
return content
|
||||||
|
wordsOnly=content.replace(',',' ').replace(';',' ').replace('.',' ').replace(':',' ')
|
||||||
|
words=wordsOnly.split(' ')
|
||||||
|
replaceMentions={}
|
||||||
|
if ':' in domain:
|
||||||
|
domain=domain.split(':')[0]
|
||||||
|
followingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt'
|
||||||
|
if not os.path.isfile(followingFilename):
|
||||||
|
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 addMention(wordStr,httpPrefix,following,replaceMentions,recipients):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# replace words with their html versions
|
||||||
for wordStr,replaceStr in replaceMentions.items():
|
for wordStr,replaceStr in replaceMentions.items():
|
||||||
content=content.replace(wordStr,replaceStr)
|
content=content.replace(wordStr,replaceStr)
|
||||||
content=content.replace('\n','</p><p>')
|
content=content.replace('\n','</p><p>')
|
||||||
|
|
|
@ -86,7 +86,7 @@ from manualapprove import manualDenyFollowRequest
|
||||||
from manualapprove import manualApproveFollowRequest
|
from manualapprove import manualApproveFollowRequest
|
||||||
from announce import createAnnounce
|
from announce import createAnnounce
|
||||||
from announce import outboxAnnounce
|
from announce import outboxAnnounce
|
||||||
from content import addMentions
|
from content import addHtmlTags
|
||||||
from media import removeMetaData
|
from media import removeMetaData
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -1835,7 +1835,7 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
if fields.get('bio'):
|
if fields.get('bio'):
|
||||||
if fields['bio']!=actorJson['summary']:
|
if fields['bio']!=actorJson['summary']:
|
||||||
actorJson['summary']= \
|
actorJson['summary']= \
|
||||||
addMentions(self.server.baseDir, \
|
addHtmlTags(self.server.baseDir, \
|
||||||
self.server.httpPrefix, \
|
self.server.httpPrefix, \
|
||||||
nickname, \
|
nickname, \
|
||||||
self.server.domain,fields['bio'],[])
|
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 getOcapFilename
|
||||||
from capabilities import capabilitiesUpdate
|
from capabilities import capabilitiesUpdate
|
||||||
from media import attachImage
|
from media import attachImage
|
||||||
from content import addMentions
|
from content import addHtmlTags
|
||||||
from auth import createBasicAuthHeader
|
from auth import createBasicAuthHeader
|
||||||
try:
|
try:
|
||||||
from BeautifulSoup import BeautifulSoup
|
from BeautifulSoup import BeautifulSoup
|
||||||
|
@ -381,7 +381,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
|
||||||
mentionedRecipients=[]
|
mentionedRecipients=[]
|
||||||
if not clientToServer:
|
if not clientToServer:
|
||||||
# convert content to html
|
# convert content to html
|
||||||
content=addMentions(baseDir,httpPrefix, \
|
content=addHtmlTags(baseDir,httpPrefix, \
|
||||||
nickname,domain,content, \
|
nickname,domain,content, \
|
||||||
mentionedRecipients)
|
mentionedRecipients)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue