flake8 style

main
Bob Mottram 2020-04-01 19:11:39 +00:00
parent ae5b9f579f
commit 65d3ace372
1 changed files with 260 additions and 237 deletions

View File

@ -7,8 +7,6 @@ __email__="bob@freedombone.net"
__status__ = "Production"
import os
import time
import json
from pprint import pprint
from utils import removePostFromCache
from utils import getStatusNumber
@ -26,7 +24,8 @@ from session import postJson
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
def outboxAnnounce(recentPostsCache: {}, \
def outboxAnnounce(recentPostsCache: {},
baseDir: str, messageJson: {}, debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection
within a given post
@ -45,9 +44,10 @@ def outboxAnnounce(recentPostsCache: {}, \
print('WARN: no nickname found in '+messageJson['actor'])
return False
domain, port = getDomainFromActor(messageJson['actor'])
postFilename=locatePost(baseDir,nickname,domain,messageJson['object'])
postFilename = locatePost(baseDir, nickname, domain,
messageJson['object'])
if postFilename:
updateAnnounceCollection(recentPostsCache,baseDir,postFilename, \
updateAnnounceCollection(recentPostsCache, baseDir, postFilename,
messageJson['actor'], domain, debug)
return True
if messageJson['type'] == 'Undo':
@ -63,19 +63,19 @@ def outboxAnnounce(recentPostsCache: {}, \
print('WARN: no nickname found in ' + messageJson['actor'])
return False
domain, port = getDomainFromActor(messageJson['actor'])
postFilename= \
locatePost(baseDir,nickname,domain, \
postFilename = locatePost(baseDir, nickname, domain,
messageJson['object']['object'])
if postFilename:
undoAnnounceCollectionEntry(recentPostsCache, \
baseDir,postFilename, \
messageJson['actor'], \
undoAnnounceCollectionEntry(recentPostsCache,
baseDir, postFilename,
messageJson['actor'],
domain, debug)
return True
return False
def undoAnnounceCollectionEntry(recentPostsCache: {}, \
baseDir: str,postFilename: str, \
def undoAnnounceCollectionEntry(recentPostsCache: {},
baseDir: str, postFilename: str,
actor: str, domain: str, debug: bool) -> None:
"""Undoes an announce for a particular actor by removing it from
the "shares" collection within a post. Note that the "shares"
@ -87,8 +87,8 @@ def undoAnnounceCollectionEntry(recentPostsCache: {}, \
# remove any cached version of this announce so that the announce
# icon is changed
nickname = getNicknameFromActor(actor)
cachedPostFilename= \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
cachedPostFilename = getCachedPostFilename(baseDir, nickname, domain,
postJsonObject)
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
@ -118,21 +118,25 @@ def undoAnnounceCollectionEntry(recentPostsCache: {}, \
if announceItem['actor'] == actor:
if debug:
print('DEBUG: Announce was removed for ' + actor)
postJsonObject['object']['shares']['items'].remove(announceItem)
anIt = announceItem
postJsonObject['object']['shares']['items'].remove(anIt)
itemFound = True
break
if itemFound:
if totalItems == 1:
if debug:
print('DEBUG: shares (announcements) was removed from post')
print('DEBUG: shares (announcements) ' +
'was removed from post')
del postJsonObject['object']['shares']
else:
postJsonObject['object']['shares']['totalItems']= \
len(postJsonObject['object']['shares']['items'])
itlen = len(postJsonObject['object']['shares']['items'])
postJsonObject['object']['shares']['totalItems'] = itlen
saveJson(postJsonObject, postFilename)
def updateAnnounceCollection(recentPostsCache: {}, \
baseDir: str,postFilename: str, \
def updateAnnounceCollection(recentPostsCache: {},
baseDir: str, postFilename: str,
actor: str, domain: str, debug: bool) -> None:
"""Updates the announcements collection within a post
Confusingly this is known as "shares", but isn't the
@ -144,8 +148,8 @@ def updateAnnounceCollection(recentPostsCache: {}, \
# remove any cached version of this announce so that the announce
# icon is changed
nickname = getNicknameFromActor(actor)
cachedPostFilename= \
getCachedPostFilename(baseDir,nickname,domain,postJsonObject)
cachedPostFilename = getCachedPostFilename(baseDir, nickname, domain,
postJsonObject)
if cachedPostFilename:
if os.path.isfile(cachedPostFilename):
os.remove(cachedPostFilename)
@ -154,14 +158,14 @@ def updateAnnounceCollection(recentPostsCache: {}, \
if not postJsonObject.get('object'):
if debug:
pprint(postJsonObject)
print('DEBUG: post '+announceUrl+' has no object')
print('DEBUG: post ' + postFilename + ' has no object')
return
if not isinstance(postJsonObject['object'], dict):
return
postUrl = postJsonObject['id'].replace('/activity', '') + '/shares'
if not postJsonObject['object'].get('shares'):
if debug:
print('DEBUG: Adding initial shares (announcements) to '+ \
print('DEBUG: Adding initial shares (announcements) to ' +
postUrl)
announcementsJson = {
"@context": "https://www.w3.org/ns/activitystreams",
@ -176,7 +180,8 @@ def updateAnnounceCollection(recentPostsCache: {}, \
postJsonObject['object']['shares'] = announcementsJson
else:
if postJsonObject['object']['shares'].get('items'):
for announceItem in postJsonObject['object']['shares']['items']:
sharesItems = postJsonObject['object']['shares']['items']
for announceItem in sharesItems:
if announceItem.get('actor'):
if announceItem['actor'] == actor:
return
@ -185,17 +190,19 @@ def updateAnnounceCollection(recentPostsCache: {}, \
'actor': actor
}
postJsonObject['object']['shares']['items'].append(newAnnounce)
postJsonObject['object']['shares']['totalItems']= \
len(postJsonObject['object']['shares']['items'])
itlen = len(postJsonObject['object']['shares']['items'])
postJsonObject['object']['shares']['totalItems'] = itlen
else:
if debug:
print('DEBUG: shares (announcements) section of post has no items list')
print('DEBUG: shares (announcements) section of post ' +
'has no items list')
if debug:
print('DEBUG: saving post with shares (announcements) added')
pprint(postJsonObject)
saveJson(postJsonObject, postFilename)
def announcedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool:
"""Returns True if the given post is announced by the given person
"""
@ -212,13 +219,14 @@ def announcedByPerson(postJsonObject: {},nickname: str,domain: str) -> bool:
return True
return False
def createAnnounce(session,baseDir: str,federationList: [], \
nickname: str,domain: str,port: int, \
toUrl: str,ccUrl: str,httpPrefix: str, \
objectUrl: str,saveToFile: bool, \
clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def createAnnounce(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int,
toUrl: str, ccUrl: str, httpPrefix: str,
objectUrl: str, saveToFile: bool,
clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool, projectVersion: str) -> {}:
"""Creates an announce message
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
@ -238,12 +246,14 @@ def createAnnounce(session,baseDir: str,federationList: [], \
fullDomain = domain + ':' + str(port)
statusNumber, published = getStatusNumber()
newAnnounceId= \
httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber
newAnnounceId = httpPrefix + '://' + fullDomain + \
'/users/' + nickname + '/statuses/' + statusNumber
atomUriStr = httpPrefix + '://' + fullDomain + '/users/' + nickname + \
'/statuses/' + statusNumber
newAnnounce = {
"@context": "https://www.w3.org/ns/activitystreams",
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
'atomUri': httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber,
'atomUri': atomUriStr,
'cc': [],
'id': newAnnounceId+'/activity',
'object': objectUrl,
@ -269,20 +279,21 @@ def createAnnounce(session,baseDir: str,federationList: [], \
announceDomain, announcePort = getDomainFromActor(objectUrl)
if announceNickname and announceDomain:
sendSignedJson(newAnnounce,session,baseDir, \
nickname,domain,port, \
announceNickname,announceDomain,announcePort,None, \
httpPrefix,True,clientToServer,federationList, \
sendThreads,postLog,cachedWebfingers,personCache, \
sendSignedJson(newAnnounce, session, baseDir,
nickname, domain, port,
announceNickname, announceDomain, announcePort, None,
httpPrefix, True, clientToServer, federationList,
sendThreads, postLog, cachedWebfingers, personCache,
debug, projectVersion)
return newAnnounce
def announcePublic(session,baseDir: str,federationList: [], \
nickname: str,domain: str,port: int,httpPrefix: str, \
objectUrl: str,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def announcePublic(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
objectUrl: str, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool, projectVersion: str) -> {}:
"""Makes a public announcement
"""
@ -293,22 +304,24 @@ def announcePublic(session,baseDir: str,federationList: [], \
fromDomain = domain + ':' + str(port)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomain+'/users/'+nickname+'/followers'
return createAnnounce(session,baseDir,federationList, \
nickname,domain,port, \
toUrl,ccUrl,httpPrefix, \
objectUrl,True,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
ccUrl = httpPrefix + '://' + fromDomain + '/users/' + nickname + \
'/followers'
return createAnnounce(session, baseDir, federationList,
nickname, domain, port,
toUrl, ccUrl, httpPrefix,
objectUrl, True, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
debug, projectVersion)
def repeatPost(session,baseDir: str,federationList: [], \
nickname: str, domain: str, port: int, httpPrefix: str, \
announceNickname: str, announceDomain: str, \
announcePort: int, announceHttpsPrefix: str, \
announceStatusNumber: int,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def repeatPost(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
announceNickname: str, announceDomain: str,
announcePort: int, announceHttpsPrefix: str,
announceStatusNumber: int, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool, projectVersion: str) -> {}:
"""Repeats a given status post
"""
@ -321,20 +334,21 @@ def repeatPost(session,baseDir: str,federationList: [], \
objectUrl = announceHttpsPrefix + '://' + announcedDomain + '/users/' + \
announceNickname + '/statuses/' + str(announceStatusNumber)
return announcePublic(session,baseDir,federationList, \
nickname,domain,port,httpPrefix, \
objectUrl,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
return announcePublic(session, baseDir, federationList,
nickname, domain, port, httpPrefix,
objectUrl, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
debug, projectVersion)
def undoAnnounce(session,baseDir: str,federationList: [], \
nickname: str,domain: str,port: int, \
toUrl: str,ccUrl: str,httpPrefix: str, \
objectUrl: str,saveToFile: bool, \
clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def undoAnnounce(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int,
toUrl: str, ccUrl: str, httpPrefix: str,
objectUrl: str, saveToFile: bool,
clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool) -> {}:
"""Undoes an announce message
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
@ -381,20 +395,22 @@ def undoAnnounce(session,baseDir: str,federationList: [], \
announceDomain, announcePort = getDomainFromActor(objectUrl)
if announceNickname and announceDomain:
sendSignedJson(newUndoAnnounce,session,baseDir, \
nickname,domain,port, \
announceNickname,announceDomain,announcePort, \
'https://www.w3.org/ns/activitystreams#Public', \
httpPrefix,True,clientToServer,federationList, \
sendThreads,postLog,cachedWebfingers,personCache,debug)
sendSignedJson(newUndoAnnounce, session, baseDir,
nickname, domain, port,
announceNickname, announceDomain, announcePort,
'https://www.w3.org/ns/activitystreams#Public',
httpPrefix, True, clientToServer, federationList,
sendThreads, postLog, cachedWebfingers,
personCache, debug)
return newUndoAnnounce
def undoAnnouncePublic(session,baseDir: str,federationList: [], \
nickname: str,domain: str,port: int,httpPrefix: str, \
objectUrl: str,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def undoAnnouncePublic(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
objectUrl: str, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool) -> {}:
"""Undoes a public announcement
"""
@ -405,22 +421,24 @@ def undoAnnouncePublic(session,baseDir: str,federationList: [], \
fromDomain = domain + ':' + str(port)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomain+'/users/'+nickname+'/followers'
return undoAnnounce(session,baseDir,federationList, \
nickname,domain,port, \
toUrl,ccUrl,httpPrefix, \
objectUrl,True,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
ccUrl = httpPrefix + '://' + fromDomain + '/users/' + nickname + \
'/followers'
return undoAnnounce(session, baseDir, federationList,
nickname, domain, port,
toUrl, ccUrl, httpPrefix,
objectUrl, True, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
debug)
def undoRepeatPost(session,baseDir: str,federationList: [], \
nickname: str,domain: str,port: int,httpPrefix: str, \
announceNickname: str,announceDomain: str, \
announcePort: int,announceHttpsPrefix: str, \
announceStatusNumber: int,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
def undoRepeatPost(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
announceNickname: str, announceDomain: str,
announcePort: int, announceHttpsPrefix: str,
announceStatusNumber: int, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool) -> {}:
"""Undoes a status post repeat
"""
@ -433,18 +451,19 @@ def undoRepeatPost(session,baseDir: str,federationList: [], \
objectUrl = announceHttpsPrefix + '://' + announcedDomain + '/users/' + \
announceNickname + '/statuses/' + str(announceStatusNumber)
return undoAnnouncePublic(session,baseDir,federationList, \
nickname,domain,port,httpPrefix, \
objectUrl,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
return undoAnnouncePublic(session, baseDir, federationList,
nickname, domain, port, httpPrefix,
objectUrl, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
debug)
def sendAnnounceViaServer(baseDir: str,session, \
def sendAnnounceViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str,fromPort: int, \
httpPrefix: str,repeatObjectUrl: str, \
cachedWebfingers: {},personCache: {}, \
fromDomain: str, fromPort: int,
httpPrefix: str, repeatObjectUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates an announce message via c2s
"""
@ -452,8 +471,6 @@ def sendAnnounceViaServer(baseDir: str,session, \
print('WARN: No session for sendAnnounceViaServer')
return 6
withDigest=True
fromDomainFull = fromDomain
if fromPort:
if fromPort != 80 and fromPort != 443:
@ -461,11 +478,11 @@ def sendAnnounceViaServer(baseDir: str,session, \
fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname + \
'/followers'
statusNumber, published = getStatusNumber()
newAnnounceId= \
httpPrefix+'://'+fromDomainFull+'/users/'+ \
newAnnounceId = httpPrefix + '://' + fromDomainFull + '/users/' + \
fromNickname + '/statuses/' + statusNumber
newAnnounceJson = {
"@context": "https://www.w3.org/ns/activitystreams",
@ -482,8 +499,8 @@ def sendAnnounceViaServer(baseDir: str,session, \
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle
wfRequest= \
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest:
if debug:
@ -493,9 +510,13 @@ def sendAnnounceViaServer(baseDir: str,session, \
postToBox = 'outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
getPersonBox(baseDir,session,wfRequest,personCache, \
projectVersion,httpPrefix,fromNickname,fromDomain,postToBox)
(inboxUrl, pubKeyId, pubKey, fromPersonId,
sharedInbox, capabilityAcquisition,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache,
projectVersion, httpPrefix,
fromNickname, fromDomain,
postToBox)
if not inboxUrl:
if debug:
@ -509,12 +530,14 @@ def sendAnnounceViaServer(baseDir: str,session, \
authHeader = createBasicAuthHeader(fromNickname, password)
headers = {
'host': fromDomain, \
'Content-type': 'application/json', \
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
postResult= \
postJson(session,newAnnounceJson,[],inboxUrl,headers,"inbox:write")
postResult = postJson(session, newAnnounceJson, [], inboxUrl,
headers, "inbox:write")
if not postResult:
print('WARN: Announce not posted')
if debug:
print('DEBUG: c2s POST announce success')