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

View File

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

View File

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

View File

@ -15,6 +15,20 @@ from pprint import pprint
from calendar import monthrange 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: def removeAvatarFromCache(baseDir: str, actorStr: str) -> None:
"""Removes any existing avatar entries from the cache """Removes any existing avatar entries from the cache
This avoids duplicate entries with differing extensions This avoids duplicate entries with differing extensions

View File

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

View File

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