mirror of https://gitlab.com/bashrc2/epicyon
flake8 style
parent
abe216a2b3
commit
ae5b9f579f
105
acceptreject.py
105
acceptreject.py
|
@ -7,20 +7,18 @@ __email__="bob@freedombone.net"
|
||||||
__status__ = "Production"
|
__status__ = "Production"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
|
||||||
from capabilities import capabilitiesAccept
|
from capabilities import capabilitiesAccept
|
||||||
from capabilities import capabilitiesGrantedSave
|
from capabilities import capabilitiesGrantedSave
|
||||||
from utils import getStatusNumber
|
|
||||||
from utils import createOutboxDir
|
|
||||||
from utils import urlPermitted
|
from utils import urlPermitted
|
||||||
from utils import getDomainFromActor
|
from utils import getDomainFromActor
|
||||||
from utils import getNicknameFromActor
|
from utils import getNicknameFromActor
|
||||||
from utils import domainPermitted
|
from utils import domainPermitted
|
||||||
from utils import followPerson
|
from utils import followPerson
|
||||||
|
|
||||||
def createAcceptReject(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createAcceptReject(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
objectJson: {}, ocapJson, acceptType: str) -> {}:
|
objectJson: {}, ocapJson, acceptType: str) -> {}:
|
||||||
"""Accepts or rejects something (eg. a follow request or offer)
|
"""Accepts or rejects something (eg. a follow request or offer)
|
||||||
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
||||||
|
@ -55,30 +53,33 @@ def createAcceptReject(baseDir: str,federationList: [], \
|
||||||
newAccept['capabilities'] = ocapJson
|
newAccept['capabilities'] = ocapJson
|
||||||
return newAccept
|
return newAccept
|
||||||
|
|
||||||
def createAccept(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createAccept(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
objectJson: {}, \
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
|
objectJson: {},
|
||||||
acceptedCaps=["inbox:write", "objects:read"]) -> {}:
|
acceptedCaps=["inbox:write", "objects:read"]) -> {}:
|
||||||
# create capabilities accept
|
# create capabilities accept
|
||||||
ocapNew= \
|
ocapNew = capabilitiesAccept(baseDir, httpPrefix,
|
||||||
capabilitiesAccept(baseDir,httpPrefix, \
|
nickname, domain, port,
|
||||||
nickname,domain,port,toUrl,True,acceptedCaps)
|
toUrl, True, acceptedCaps)
|
||||||
return createAcceptReject(baseDir,federationList, \
|
return createAcceptReject(baseDir, federationList,
|
||||||
nickname,domain,port, \
|
nickname, domain, port,
|
||||||
toUrl,ccUrl,httpPrefix, \
|
toUrl, ccUrl, httpPrefix,
|
||||||
objectJson, ocapNew, 'Accept')
|
objectJson, ocapNew, 'Accept')
|
||||||
|
|
||||||
def createReject(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createReject(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
objectJson: {}) -> {}:
|
objectJson: {}) -> {}:
|
||||||
return createAcceptReject(baseDir,federationList, \
|
return createAcceptReject(baseDir, federationList,
|
||||||
nickname,domain,port, \
|
nickname, domain, port,
|
||||||
toUrl,ccUrl, \
|
toUrl, ccUrl,
|
||||||
httpPrefix, objectJson, None, 'Reject')
|
httpPrefix, objectJson, None, 'Reject')
|
||||||
|
|
||||||
def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
|
||||||
|
def acceptFollow(baseDir: str, domain: str, messageJson: {},
|
||||||
federationList: [], debug: bool) -> None:
|
federationList: [], debug: bool) -> None:
|
||||||
"""Receiving a follow Accept activity
|
"""Receiving a follow Accept activity
|
||||||
"""
|
"""
|
||||||
|
@ -118,11 +119,12 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
print('DEBUG: nickname not found in '+thisActor)
|
print('DEBUG: nickname not found in '+thisActor)
|
||||||
return
|
return
|
||||||
if acceptedPort:
|
if acceptedPort:
|
||||||
if '/'+acceptedDomain+':'+str(acceptedPort)+'/users/'+nickname not in thisActor:
|
if '/' + acceptedDomain + ':' + str(acceptedPort) + \
|
||||||
|
'/users/' + nickname not in thisActor:
|
||||||
if debug:
|
if debug:
|
||||||
print('Port: '+str(acceptedPort))
|
print('Port: '+str(acceptedPort))
|
||||||
print('Expected: /'+acceptedDomain+':'+str(acceptedPort)+ \
|
print('Expected: /' + acceptedDomain + ':' +
|
||||||
'/users/'+nickname)
|
str(acceptedPort) + '/users/'+nickname)
|
||||||
print('Actual: '+thisActor)
|
print('Actual: '+thisActor)
|
||||||
print('DEBUG: unrecognized actor '+thisActor)
|
print('DEBUG: unrecognized actor '+thisActor)
|
||||||
return
|
return
|
||||||
|
@ -136,7 +138,7 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
followedActor = messageJson['object']['object']
|
followedActor = messageJson['object']['object']
|
||||||
followedDomain, port = getDomainFromActor(followedActor)
|
followedDomain, port = getDomainFromActor(followedActor)
|
||||||
if not followedDomain:
|
if not followedDomain:
|
||||||
print('DEBUG: no domain found within Follow activity object '+ \
|
print('DEBUG: no domain found within Follow activity object ' +
|
||||||
followedActor)
|
followedActor)
|
||||||
return
|
return
|
||||||
followedDomainFull = followedDomain
|
followedDomainFull = followedDomain
|
||||||
|
@ -144,7 +146,7 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
followedDomainFull = followedDomain+':' + str(port)
|
followedDomainFull = followedDomain+':' + str(port)
|
||||||
followedNickname = getNicknameFromActor(followedActor)
|
followedNickname = getNicknameFromActor(followedActor)
|
||||||
if not followedNickname:
|
if not followedNickname:
|
||||||
print('DEBUG: no nickname found within Follow activity object '+ \
|
print('DEBUG: no nickname found within Follow activity object ' +
|
||||||
followedActor)
|
followedActor)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -155,39 +157,41 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
# are capabilities attached? If so then store them
|
# are capabilities attached? If so then store them
|
||||||
if messageJson.get('capabilities'):
|
if messageJson.get('capabilities'):
|
||||||
if isinstance(messageJson['capabilities'], dict):
|
if isinstance(messageJson['capabilities'], dict):
|
||||||
capabilitiesGrantedSave(baseDir, \
|
capabilitiesGrantedSave(baseDir,
|
||||||
nickname,acceptedDomainFull, \
|
nickname, acceptedDomainFull,
|
||||||
messageJson['capabilities'])
|
messageJson['capabilities'])
|
||||||
|
|
||||||
# has this person already been unfollowed?
|
# has this person already been unfollowed?
|
||||||
unfollowedFilename= \
|
unfollowedFilename = baseDir + '/accounts/' + \
|
||||||
baseDir+'/accounts/'+nickname+'@'+acceptedDomainFull+'/unfollowed.txt'
|
nickname + '@' + acceptedDomainFull + '/unfollowed.txt'
|
||||||
if os.path.isfile(unfollowedFilename):
|
if os.path.isfile(unfollowedFilename):
|
||||||
if followedNickname+'@'+followedDomainFull in open(unfollowedFilename).read():
|
if followedNickname + '@' + followedDomainFull in \
|
||||||
|
open(unfollowedFilename).read():
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: follow accept arrived for '+ \
|
print('DEBUG: follow accept arrived for ' +
|
||||||
nickname+'@'+acceptedDomainFull+ \
|
nickname + '@' + acceptedDomainFull +
|
||||||
' from '+followedNickname+'@'+followedDomainFull+ \
|
' from ' + followedNickname + '@' + followedDomainFull +
|
||||||
' but they have been unfollowed')
|
' but they have been unfollowed')
|
||||||
return
|
return
|
||||||
|
|
||||||
if followPerson(baseDir, \
|
if followPerson(baseDir,
|
||||||
nickname,acceptedDomainFull, \
|
nickname, acceptedDomainFull,
|
||||||
followedNickname,followedDomainFull, \
|
followedNickname, followedDomainFull,
|
||||||
federationList, debug):
|
federationList, debug):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+nickname+'@'+acceptedDomainFull+ \
|
print('DEBUG: ' + nickname + '@' + acceptedDomainFull +
|
||||||
' followed ' + followedNickname + '@' + followedDomainFull)
|
' followed ' + followedNickname + '@' + followedDomainFull)
|
||||||
else:
|
else:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Unable to create follow - '+ \
|
print('DEBUG: Unable to create follow - ' +
|
||||||
nickname+'@'+acceptedDomain+' -> '+ \
|
nickname + '@' + acceptedDomain+' -> ' +
|
||||||
followedNickname + '@' + followedDomain)
|
followedNickname + '@' + followedDomain)
|
||||||
|
|
||||||
def receiveAcceptReject(session,baseDir: str, \
|
|
||||||
httpPrefix: str,domain :str,port: int, \
|
def receiveAcceptReject(session, baseDir: str,
|
||||||
sendThreads: [],postLog: [],cachedWebfingers: {}, \
|
httpPrefix: str, domain: str, port: int,
|
||||||
personCache: {},messageJson: {},federationList: [], \
|
sendThreads: [], postLog: [], cachedWebfingers: {},
|
||||||
|
personCache: {}, messageJson: {}, federationList: [],
|
||||||
debug: bool) -> bool:
|
debug: bool) -> bool:
|
||||||
"""Receives an Accept or Reject within the POST section of HTTPServer
|
"""Receives an Accept or Reject within the POST section of HTTPServer
|
||||||
"""
|
"""
|
||||||
|
@ -201,12 +205,12 @@ def receiveAcceptReject(session,baseDir: str, \
|
||||||
'/channel/' not in messageJson['actor'] and \
|
'/channel/' not in messageJson['actor'] and \
|
||||||
'/profile/' not in messageJson['actor']:
|
'/profile/' not in messageJson['actor']:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: "users" or "profile" missing from actor in '+ \
|
print('DEBUG: "users" or "profile" missing from actor in ' +
|
||||||
messageJson['type'] + '. Assuming single user instance.')
|
messageJson['type'] + '. Assuming single user instance.')
|
||||||
domain, tempPort = getDomainFromActor(messageJson['actor'])
|
domain, tempPort = getDomainFromActor(messageJson['actor'])
|
||||||
if not domainPermitted(domain, federationList):
|
if not domainPermitted(domain, federationList):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+messageJson['type']+ \
|
print('DEBUG: ' + messageJson['type'] +
|
||||||
' from domain not permitted - ' + domain)
|
' from domain not permitted - ' + domain)
|
||||||
return False
|
return False
|
||||||
nickname = getNicknameFromActor(messageJson['actor'])
|
nickname = getNicknameFromActor(messageJson['actor'])
|
||||||
|
@ -214,10 +218,9 @@ def receiveAcceptReject(session,baseDir: str, \
|
||||||
# single user instance
|
# single user instance
|
||||||
nickname = 'dev'
|
nickname = 'dev'
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+messageJson['type']+ \
|
print('DEBUG: ' + messageJson['type'] +
|
||||||
' does not contain a nickname. '+ \
|
' does not contain a nickname. ' +
|
||||||
'Assuming single user instance.')
|
'Assuming single user instance.')
|
||||||
handle=nickname.lower()+'@'+domain.lower()
|
|
||||||
# receive follow accept
|
# receive follow accept
|
||||||
acceptFollow(baseDir, domain, messageJson, federationList, debug)
|
acceptFollow(baseDir, domain, messageJson, federationList, debug)
|
||||||
if debug:
|
if debug:
|
||||||
|
|
Loading…
Reference in New Issue