Reduce duplicate prefixes

main
Bob Mottram 2020-06-11 13:26:15 +01:00
parent c68b94f9b1
commit 6e769484aa
6 changed files with 26 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import email.parser
from shutil import copyfile
from utils import loadJson
from utils import fileLastModified
from utils import getLinkPrefixes
def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str:
@ -129,8 +130,7 @@ def addWebLinks(content: str) -> str:
if ':' not in content:
return content
prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://', 'briar:')
prefixes = getLinkPrefixes()
# do any of these prefixes exist within the content?
prefixFound = False

View File

@ -49,6 +49,7 @@ from utils import getDomainFromActor
from utils import getNicknameFromActor
from utils import followPerson
from utils import validNickname
from utils import getProtocolPrefixes
from media import archiveMedia
from media import getAttachmentMediaType
from delete import sendDeleteViaServer
@ -1041,8 +1042,7 @@ if args.actor:
args.actor.startswith('http') or \
args.actor.startswith('dat'):
# format: https://domain/@nick
prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://')
prefixes = getProtocolPrefixes()
for prefix in prefixes:
args.actor = args.actor.replace(prefix, '')
args.actor = args.actor.replace('/@', '/users/')

View File

@ -10,6 +10,7 @@ import json
import os
import datetime
import time
from utils import getProtocolPrefixes
from utils import isBlogPost
from utils import removeAvatarFromCache
from utils import isPublicPost
@ -1359,8 +1360,7 @@ def receiveAnnounce(recentPostsCache: {},
messageJson['type'])
return False
prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://')
prefixes = getProtocolPrefixes()
# is the domain of the announce actor blocked?
objectDomain = messageJson['object']
for prefix in prefixes:

View File

@ -15,6 +15,20 @@ from pprint import pprint
from calendar import monthrange
def getProtocolPrefixes() -> []:
"""Returns a list of valid prefixes
"""
return ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://')
def getLinkPrefixes() -> []:
"""Returns a list of valid web link prefixes
"""
return ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://', 'briar:')
def removeAvatarFromCache(baseDir: str, actorStr: str) -> None:
"""Removes any existing avatar entries from the cache
This avoids duplicate entries with differing extensions

View File

@ -21,13 +21,13 @@ from cache import getWebfingerFromCache
from utils import loadJson
from utils import loadJsonOnionify
from utils import saveJson
from utils import getProtocolPrefixes
def parseHandle(handle: str) -> (str, str):
if '.' not in handle:
return None, None
prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://')
prefixes = getProtocolPrefixes()
handleStr = handle
for prefix in prefixes:
handleStr = handleStr.replace(prefix, '')

View File

@ -24,6 +24,7 @@ from ssb import getSSBAddress
from tox import getToxAddress
from matrix import getMatrixAddress
from donate import getDonationUrl
from utils import getProtocolPrefixes
from utils import getFileCaseInsensitive
from utils import searchBoxPosts
from utils import isBlogPost
@ -100,8 +101,7 @@ def getBlogAddress(actorJson: {}) -> str:
if propertyValue['type'] != 'PropertyValue':
continue
propertyValue['value'] = propertyValue['value'].strip()
prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'hyper://', 'gemini://', 'gopher://')
prefixes = getProtocolPrefixes()
prefixFound = False
for prefix in prefixes:
if propertyValue['value'].startswith(prefix):
@ -139,9 +139,7 @@ def setBlogAddress(actorJson: {}, blogAddress: str) -> None:
if propertyFound:
actorJson['attachment'].remove(propertyFound)
prefixes = ('https://', 'http://', 'dat://', 'i2p://',
'gnunet://', 'hyper://', 'gemini://',
'gopher://')
prefixes = getProtocolPrefixes()
prefixFound = False
for prefix in prefixes:
if blogAddress.startswith(prefix):
@ -4007,9 +4005,7 @@ def individualPostAsHtml(recentPostsCache: {}, maxRecentPosts: int,
else:
postDomain = \
postJsonObject['object']['inReplyTo']
prefixes = ('https://', 'http://', 'dat://', 'i2p://',
'gnunet://', 'hyper://', 'gemini://',
'gopher://')
prefixes = getProtocolPrefixes()
for prefix in prefixes:
postDomain = postDomain.replace(prefix, '')
if '/' in postDomain: