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