flake8 format

main
Bob Mottram 2020-04-03 17:15:33 +00:00
parent 08a1b05a76
commit 2bbfd60969
1 changed files with 173 additions and 163 deletions

200
outbox.py
View File

@ -7,14 +7,12 @@ __email__="bob@freedombone.net"
__status__ = "Production" __status__ = "Production"
import os import os
import json
from session import createSession from session import createSession
from auth import createPassword from auth import createPassword
from posts import outboxMessageCreateWrap from posts import outboxMessageCreateWrap
from posts import savePostToBox from posts import savePostToBox
from posts import sendToFollowersThread from posts import sendToFollowersThread
from posts import sendToNamedAddresses from posts import sendToNamedAddresses
from utils import isBlogPost
from utils import getDomainFromActor from utils import getDomainFromActor
from blocking import isBlockedDomain from blocking import isBlockedDomain
from blocking import outboxBlock from blocking import outboxBlock
@ -36,14 +34,15 @@ from delete import outboxDelete
from shares import outboxShareUpload from shares import outboxShareUpload
from shares import outboxUndoShareUpload from shares import outboxUndoShareUpload
def postMessageToOutbox(messageJson: {},postToNickname: str, \
server,baseDir: str,httpPrefix: str, \ def postMessageToOutbox(messageJson: {}, postToNickname: str,
domain: str,domainFull: str,onionDomain: str, \ server, baseDir: str, httpPrefix: str,
port: int, \ domain: str, domainFull: str, onionDomain: str,
recentPostsCache: {},followersThreads: [], \ port: int,
federationList: [],sendThreads: [], \ recentPostsCache: {}, followersThreads: [],
postLog: [],cachedWebfingers: {}, \ federationList: [], sendThreads: [],
personCache: {},allowDeletion: bool, \ postLog: [], cachedWebfingers: {},
personCache: {}, allowDeletion: bool,
useTor: bool, version: str, debug: bool) -> bool: useTor: bool, version: str, debug: bool) -> bool:
"""post is received by the outbox """post is received by the outbox
Client to server message post Client to server message post
@ -59,31 +58,41 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
if debug: if debug:
print('DEBUG: POST to outbox - adding Create wrapper') print('DEBUG: POST to outbox - adding Create wrapper')
messageJson = \ messageJson = \
outboxMessageCreateWrap(httpPrefix, \ outboxMessageCreateWrap(httpPrefix,
postToNickname, \ postToNickname,
domain,port, \ domain, port,
messageJson) messageJson)
if messageJson['type'] == 'Create': if messageJson['type'] == 'Create':
if not (messageJson.get('id') and \ if not (messageJson.get('id') and
messageJson.get('type') and \ messageJson.get('type') and
messageJson.get('actor') and \ messageJson.get('actor') and
messageJson.get('object') and \ messageJson.get('object') and
messageJson.get('to')): messageJson.get('to')):
if not messageJson.get('id'): if not messageJson.get('id'):
if debug: if debug:
print('DEBUG: POST to outbox - Create does not have the id parameter '+str(messageJson)) print('DEBUG: POST to outbox - ' +
'Create does not have the id parameter ' +
str(messageJson))
elif not messageJson.get('id'): elif not messageJson.get('id'):
if debug: if debug:
print('DEBUG: POST to outbox - Create does not have the type parameter '+str(messageJson)) print('DEBUG: POST to outbox - ' +
'Create does not have the type parameter ' +
str(messageJson))
elif not messageJson.get('id'): elif not messageJson.get('id'):
if debug: if debug:
print('DEBUG: POST to outbox - Create does not have the actor parameter '+str(messageJson)) print('DEBUG: POST to outbox - ' +
'Create does not have the actor parameter ' +
str(messageJson))
elif not messageJson.get('id'): elif not messageJson.get('id'):
if debug: if debug:
print('DEBUG: POST to outbox - Create does not have the object parameter '+str(messageJson)) print('DEBUG: POST to outbox - ' +
'Create does not have the object parameter ' +
str(messageJson))
else: else:
if debug: if debug:
print('DEBUG: POST to outbox - Create does not have the "to" parameter '+str(messageJson)) print('DEBUG: POST to outbox - ' +
'Create does not have the "to" parameter ' +
str(messageJson))
return False return False
testDomain, testPort = getDomainFromActor(messageJson['actor']) testDomain, testPort = getDomainFromActor(messageJson['actor'])
if testPort: if testPort:
@ -99,10 +108,11 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
messageJson['object']['attributedTo'] = messageJson['actor'] messageJson['object']['attributedTo'] = messageJson['actor']
if messageJson['object'].get('attachment'): if messageJson['object'].get('attachment'):
attachmentIndex = 0 attachmentIndex = 0
if messageJson['object']['attachment'][attachmentIndex].get('mediaType'): attach = messageJson['object']['attachment'][attachmentIndex]
if attach.get('mediaType'):
fileExtension = 'png' fileExtension = 'png'
mediaTypeStr = \ mediaTypeStr = \
messageJson['object']['attachment'][attachmentIndex]['mediaType'] attach['mediaType']
if mediaTypeStr.endswith('jpeg'): if mediaTypeStr.endswith('jpeg'):
fileExtension = 'jpg' fileExtension = 'jpg'
elif mediaTypeStr.endswith('gif'): elif mediaTypeStr.endswith('gif'):
@ -128,27 +138,27 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
else: else:
# generate a path for the uploaded image # generate a path for the uploaded image
mPath = getMediaPath() mPath = getMediaPath()
mediaPath=mPath+'/'+createPassword(32)+'.'+fileExtension mediaPath = mPath + '/' + \
createPassword(32) + '.' + fileExtension
createMediaDirs(baseDir, mPath) createMediaDirs(baseDir, mPath)
mediaFilename = baseDir + '/' + mediaPath mediaFilename = baseDir + '/' + mediaPath
# move the uploaded image to its new path # move the uploaded image to its new path
os.rename(uploadMediaFilename, mediaFilename) os.rename(uploadMediaFilename, mediaFilename)
# change the url of the attachment # change the url of the attachment
messageJson['object']['attachment'][attachmentIndex]['url']= \ attach['url'] = \
httpPrefix + '://' + domainFull + '/' + mediaPath httpPrefix + '://' + domainFull + '/' + mediaPath
permittedOutboxTypes=[ permittedOutboxTypes = ('Create', 'Announce', 'Like', 'Follow', 'Undo',
'Create','Announce','Like','Follow','Undo', \ 'Update', 'Add', 'Remove', 'Block', 'Delete',
'Update','Add','Remove','Block','Delete', \ 'Delegate', 'Skill', 'Bookmark')
'Delegate','Skill','Bookmark'
]
if messageJson['type'] not in permittedOutboxTypes: if messageJson['type'] not in permittedOutboxTypes:
if debug: if debug:
print('DEBUG: POST to outbox - '+messageJson['type']+ \ print('DEBUG: POST to outbox - ' + messageJson['type'] +
' is not a permitted activity type') ' is not a permitted activity type')
return False return False
if messageJson.get('id'): if messageJson.get('id'):
postId=messageJson['id'].replace('/activity','').replace('/undo','') postId = \
messageJson['id'].replace('/activity', '').replace('/undo', '')
if debug: if debug:
print('DEBUG: id attribute exists within POST to outbox') print('DEBUG: id attribute exists within POST to outbox')
else: else:
@ -169,10 +179,10 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
outboxName = 'tlblogs' outboxName = 'tlblogs'
savedFilename = \ savedFilename = \
savePostToBox(baseDir, \ savePostToBox(baseDir,
httpPrefix, \ httpPrefix,
postId, \ postId,
postToNickname, \ postToNickname,
domainFull, messageJson, outboxName) domainFull, messageJson, outboxName)
if messageJson['type'] == 'Create' or \ if messageJson['type'] == 'Create' or \
messageJson['type'] == 'Question' or \ messageJson['type'] == 'Question' or \
@ -183,13 +193,14 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
for boxNameIndex in indexes: for boxNameIndex in indexes:
if boxNameIndex == 'inbox' and outboxName == 'tlblogs': if boxNameIndex == 'inbox' and outboxName == 'tlblogs':
continue continue
inboxUpdateIndex(boxNameIndex,baseDir, \ inboxUpdateIndex(boxNameIndex, baseDir,
postToNickname+'@'+domain, \ postToNickname + '@' + domain,
savedFilename, debug) savedFilename, debug)
if outboxAnnounce(recentPostsCache, \ if outboxAnnounce(recentPostsCache,
baseDir, messageJson, debug): baseDir, messageJson, debug):
if debug: if debug:
print('DEBUG: Updated announcements (shares) collection for the post associated with the Announce activity') print('DEBUG: Updated announcements (shares) collection ' +
'for the post associated with the Announce activity')
if not server.session: if not server.session:
if debug: if debug:
print('DEBUG: creating new session for c2s') print('DEBUG: creating new session for c2s')
@ -205,7 +216,8 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
for th in inactiveFollowerThreads: for th in inactiveFollowerThreads:
followersThreads.remove(th) followersThreads.remove(th)
if debug: if debug:
print('DEBUG: '+str(len(followersThreads))+' followers threads active') print('DEBUG: ' + str(len(followersThreads)) +
' followers threads active')
# retain up to 200 threads # retain up to 200 threads
if len(followersThreads) > 200: if len(followersThreads) > 200:
# kill the thread if it is still alive # kill the thread if it is still alive
@ -215,18 +227,17 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
followersThreads.pop(0) followersThreads.pop(0)
# create a thread to send the post to followers # create a thread to send the post to followers
followersThread = \ followersThread = \
sendToFollowersThread(server.session, \ sendToFollowersThread(server.session,
baseDir, \ baseDir,
postToNickname, \ postToNickname,
domain,onionDomain, \ domain, onionDomain,
port, \ port, httpPrefix,
httpPrefix, \ federationList,
federationList, \ sendThreads,
sendThreads, \ postLog,
postLog, \ cachedWebfingers,
cachedWebfingers, \ personCache,
personCache, \ messageJson, debug,
messageJson,debug, \
version) version)
followersThreads.append(followersThread) followersThreads.append(followersThread)
@ -248,81 +259,80 @@ def postMessageToOutbox(messageJson: {},postToNickname: str, \
if debug: if debug:
print('DEBUG: handle any like requests') print('DEBUG: handle any like requests')
outboxLike(recentPostsCache, \ outboxLike(recentPostsCache,
baseDir,httpPrefix, \ baseDir, httpPrefix,
postToNickname,domain,port, \ postToNickname, domain, port,
messageJson, debug) messageJson, debug)
if debug: if debug:
print('DEBUG: handle any undo like requests') print('DEBUG: handle any undo like requests')
outboxUndoLike(recentPostsCache, \ outboxUndoLike(recentPostsCache,
baseDir,httpPrefix, \ baseDir, httpPrefix,
postToNickname,domain,port, \ postToNickname, domain, port,
messageJson, debug) messageJson, debug)
if debug: if debug:
print('DEBUG: handle any bookmark requests') print('DEBUG: handle any bookmark requests')
outboxBookmark(recentPostsCache, \ outboxBookmark(recentPostsCache,
baseDir,httpPrefix, \ baseDir, httpPrefix,
postToNickname,domain,port, \ postToNickname, domain, port,
messageJson, debug) messageJson, debug)
if debug: if debug:
print('DEBUG: handle any undo bookmark requests') print('DEBUG: handle any undo bookmark requests')
outboxUndoBookmark(recentPostsCache, \ outboxUndoBookmark(recentPostsCache,
baseDir,httpPrefix, \ baseDir, httpPrefix,
postToNickname,domain,port, \ postToNickname, domain, port,
messageJson, debug) messageJson, debug)
if debug: if debug:
print('DEBUG: handle delete requests') print('DEBUG: handle delete requests')
outboxDelete(baseDir,httpPrefix, \ outboxDelete(baseDir, httpPrefix,
postToNickname,domain, \ postToNickname, domain,
messageJson,debug, \ messageJson, debug,
allowDeletion) allowDeletion)
if debug: if debug:
print('DEBUG: handle block requests') print('DEBUG: handle block requests')
outboxBlock(baseDir,httpPrefix, \ outboxBlock(baseDir, httpPrefix,
postToNickname,domain, \ postToNickname, domain,
port, port,
messageJson, debug) messageJson, debug)
if debug: if debug:
print('DEBUG: handle undo block requests') print('DEBUG: handle undo block requests')
outboxUndoBlock(baseDir,httpPrefix, \ outboxUndoBlock(baseDir, httpPrefix,
postToNickname,domain, \ postToNickname, domain,
port, port, messageJson, debug)
messageJson,debug)
if debug: if debug:
print('DEBUG: handle share uploads') print('DEBUG: handle share uploads')
outboxShareUpload(baseDir,httpPrefix, \ outboxShareUpload(baseDir, httpPrefix,
postToNickname,domain, \ postToNickname, domain,
port, port, messageJson, debug)
messageJson,debug)
if debug: if debug:
print('DEBUG: handle undo share uploads') print('DEBUG: handle undo share uploads')
outboxUndoShareUpload(baseDir,httpPrefix, \ outboxUndoShareUpload(baseDir, httpPrefix,
postToNickname,domain, \ postToNickname, domain,
port, port, messageJson, debug)
messageJson,debug)
if debug: if debug:
print('DEBUG: sending c2s post to named addresses') print('DEBUG: sending c2s post to named addresses')
if messageJson.get('to'): if messageJson.get('to'):
print('c2s sender: '+postToNickname+'@'+domain+':'+str(port)+ \ print('c2s sender: ' +
postToNickname + '@' + domain + ':' + str(port) +
' recipient: ' + str(messageJson['to'])) ' recipient: ' + str(messageJson['to']))
else: else:
print('c2s sender: '+postToNickname+'@'+domain+':'+str(port)) print('c2s sender: ' +
sendToNamedAddresses(server.session,baseDir, \ postToNickname + '@' + domain + ':' + str(port))
postToNickname, \ sendToNamedAddresses(server.session, baseDir,
domain,onionDomain,port, \ postToNickname,
httpPrefix, \ domain, onionDomain, port,
federationList, \ httpPrefix,
sendThreads, \ federationList,
postLog, \ sendThreads,
cachedWebfingers, \ postLog,
personCache, \ cachedWebfingers,
messageJson,debug, \ personCache,
messageJson, debug,
version) version)
return True return True