mirror of https://gitlab.com/bashrc2/epicyon
flake8 format
parent
2f8d97701a
commit
dfb1263f1a
271
delete.py
271
delete.py
|
@ -1,15 +1,12 @@
|
||||||
__filename__="delete.py"
|
__filename__ = "delete.py"
|
||||||
__author__="Bob Mottram"
|
__author__ = "Bob Mottram"
|
||||||
__license__="AGPL3+"
|
__license__ = "AGPL3+"
|
||||||
__version__="1.1.0"
|
__version__ = "1.1.0"
|
||||||
__maintainer__="Bob Mottram"
|
__maintainer__ = "Bob Mottram"
|
||||||
__email__="bob@freedombone.net"
|
__email__ = "bob@freedombone.net"
|
||||||
__status__="Production"
|
__status__ = "Production"
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
from utils import getStatusNumber
|
from utils import getStatusNumber
|
||||||
from utils import createOutboxDir
|
|
||||||
from utils import urlPermitted
|
from utils import urlPermitted
|
||||||
from utils import getNicknameFromActor
|
from utils import getNicknameFromActor
|
||||||
from utils import getDomainFromActor
|
from utils import getDomainFromActor
|
||||||
|
@ -22,12 +19,13 @@ from webfinger import webfingerHandle
|
||||||
from auth import createBasicAuthHeader
|
from auth import createBasicAuthHeader
|
||||||
from posts import getPersonBox
|
from posts import getPersonBox
|
||||||
|
|
||||||
def createDelete(session,baseDir: str,federationList: [], \
|
|
||||||
nickname: str, domain: str, port: int, \
|
def createDelete(session, baseDir: str, federationList: [],
|
||||||
toUrl: str, ccUrl: str, httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
objectUrl: str,clientToServer: bool, \
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
sendThreads: [],postLog: [], \
|
objectUrl: str, clientToServer: bool,
|
||||||
personCache: {},cachedWebfingers: {}, \
|
sendThreads: [], postLog: [],
|
||||||
|
personCache: {}, cachedWebfingers: {},
|
||||||
debug: bool) -> {}:
|
debug: bool) -> {}:
|
||||||
"""Creates a delete message
|
"""Creates a delete message
|
||||||
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
||||||
|
@ -35,185 +33,196 @@ def createDelete(session,baseDir: str,federationList: [], \
|
||||||
objectUrl is typically the url of the message, corresponding to url
|
objectUrl is typically the url of the message, corresponding to url
|
||||||
or atomUri in createPostBase
|
or atomUri in createPostBase
|
||||||
"""
|
"""
|
||||||
if not urlPermitted(objectUrl,federationList,"inbox:write"):
|
if not urlPermitted(objectUrl, federationList, "inbox:write"):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if ':' in domain:
|
if ':' in domain:
|
||||||
domain=domain.split(':')[0]
|
domain = domain.split(':')[0]
|
||||||
fullDomain=domain
|
fullDomain = domain
|
||||||
if port:
|
if port:
|
||||||
if port!=80 and port!=443:
|
if port != 80 and port != 443:
|
||||||
if ':' not in domain:
|
if ':' not in domain:
|
||||||
fullDomain=domain+':'+str(port)
|
fullDomain = domain + ':' + str(port)
|
||||||
|
|
||||||
statusNumber,published=getStatusNumber()
|
statusNumber, published = getStatusNumber()
|
||||||
newDeleteId= \
|
newDeleteId = \
|
||||||
httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber
|
httpPrefix + '://' + fullDomain + '/users/' + \
|
||||||
newDelete={
|
nickname + '/statuses/' + statusNumber
|
||||||
|
newDelete = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
|
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
|
||||||
'atomUri': httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber,
|
'atomUri': newDeleteId,
|
||||||
'cc': [],
|
'cc': [],
|
||||||
'id': newDeleteId+'/activity',
|
'id': newDeleteId + '/activity',
|
||||||
'object': objectUrl,
|
'object': objectUrl,
|
||||||
'published': published,
|
'published': published,
|
||||||
'to': [toUrl],
|
'to': [toUrl],
|
||||||
'type': 'Delete'
|
'type': 'Delete'
|
||||||
}
|
}
|
||||||
if ccUrl:
|
if ccUrl:
|
||||||
if len(ccUrl)>0:
|
if len(ccUrl) > 0:
|
||||||
newDelete['cc']=[ccUrl]
|
newDelete['cc'] = [ccUrl]
|
||||||
|
|
||||||
deleteNickname=None
|
deleteNickname = None
|
||||||
deleteDomain=None
|
deleteDomain = None
|
||||||
deletePort=None
|
deletePort = None
|
||||||
if '/users/' in objectUrl or \
|
if '/users/' in objectUrl or \
|
||||||
'/channel/' in objectUrl or \
|
'/channel/' in objectUrl or \
|
||||||
'/profile/' in objectUrl:
|
'/profile/' in objectUrl:
|
||||||
deleteNickname=getNicknameFromActor(objectUrl)
|
deleteNickname = getNicknameFromActor(objectUrl)
|
||||||
deleteDomain,deletePort=getDomainFromActor(objectUrl)
|
deleteDomain, deletePort = getDomainFromActor(objectUrl)
|
||||||
|
|
||||||
if deleteNickname and deleteDomain:
|
if deleteNickname and deleteDomain:
|
||||||
sendSignedJson(newDelete,session,baseDir, \
|
sendSignedJson(newDelete, session, baseDir,
|
||||||
nickname,domain,port, \
|
nickname, domain, port,
|
||||||
deleteNickname,deleteDomain,deletePort, \
|
deleteNickname, deleteDomain, deletePort,
|
||||||
'https://www.w3.org/ns/activitystreams#Public', \
|
'https://www.w3.org/ns/activitystreams#Public',
|
||||||
httpPrefix,True,clientToServer,federationList, \
|
httpPrefix, True, clientToServer, federationList,
|
||||||
sendThreads,postLog,cachedWebfingers,personCache,debug)
|
sendThreads, postLog, cachedWebfingers,
|
||||||
|
personCache, debug)
|
||||||
|
|
||||||
return newDelete
|
return newDelete
|
||||||
|
|
||||||
def sendDeleteViaServer(baseDir: str,session, \
|
|
||||||
fromNickname: str,password: str, \
|
def sendDeleteViaServer(baseDir: str, session,
|
||||||
fromDomain: str,fromPort: int, \
|
fromNickname: str, password: str,
|
||||||
httpPrefix: str,deleteObjectUrl: str, \
|
fromDomain: str, fromPort: int,
|
||||||
cachedWebfingers: {},personCache: {}, \
|
httpPrefix: str, deleteObjectUrl: str,
|
||||||
debug: bool,projectVersion: str) -> {}:
|
cachedWebfingers: {}, personCache: {},
|
||||||
|
debug: bool, projectVersion: str) -> {}:
|
||||||
"""Creates a delete request message via c2s
|
"""Creates a delete request message via c2s
|
||||||
"""
|
"""
|
||||||
if not session:
|
if not session:
|
||||||
print('WARN: No session for sendDeleteViaServer')
|
print('WARN: No session for sendDeleteViaServer')
|
||||||
return 6
|
return 6
|
||||||
|
|
||||||
fromDomainFull=fromDomain
|
fromDomainFull = fromDomain
|
||||||
if fromPort:
|
if fromPort:
|
||||||
if fromPort!=80 and fromPort!=443:
|
if fromPort != 80 and fromPort != 443:
|
||||||
if ':' not in fromDomain:
|
if ':' not in fromDomain:
|
||||||
fromDomainFull=fromDomain+':'+str(fromPort)
|
fromDomainFull = fromDomain + ':' + str(fromPort)
|
||||||
|
|
||||||
toUrl='https://www.w3.org/ns/activitystreams#Public'
|
actor = httpPrefix + '://' + fromDomainFull + \
|
||||||
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
|
'/users/' + fromNickname
|
||||||
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
|
ccUrl = actor + '/followers'
|
||||||
|
|
||||||
newDeleteJson={
|
newDeleteJson = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname,
|
'actor': actor,
|
||||||
'cc': [ccUrl],
|
'cc': [ccUrl],
|
||||||
'object': deleteObjectUrl,
|
'object': deleteObjectUrl,
|
||||||
'to': [toUrl],
|
'to': [toUrl],
|
||||||
'type': 'Delete'
|
'type': 'Delete'
|
||||||
}
|
}
|
||||||
|
|
||||||
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname
|
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest= \
|
wfRequest = \
|
||||||
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
|
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
|
||||||
fromDomain,projectVersion)
|
fromDomain, projectVersion)
|
||||||
if not wfRequest:
|
if not wfRequest:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: announce webfinger failed for '+handle)
|
print('DEBUG: announce webfinger failed for ' + handle)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
postToBox='outbox'
|
postToBox = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
|
(inboxUrl, pubKeyId, pubKey,
|
||||||
getPersonBox(baseDir,session,wfRequest,personCache, \
|
fromPersonId, sharedInbox,
|
||||||
projectVersion,httpPrefix,fromNickname, \
|
capabilityAcquisition, avatarUrl,
|
||||||
fromDomain,postToBox)
|
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
|
||||||
|
projectVersion, httpPrefix, fromNickname,
|
||||||
|
fromDomain, postToBox)
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inboxUrl:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: No '+postToBox+' was found for '+handle)
|
print('DEBUG: No ' + postToBox + ' was found for ' + handle)
|
||||||
return 3
|
return 3
|
||||||
if not fromPersonId:
|
if not fromPersonId:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: No actor was found for '+handle)
|
print('DEBUG: No actor was found for ' + handle)
|
||||||
return 4
|
return 4
|
||||||
|
|
||||||
authHeader=createBasicAuthHeader(fromNickname,password)
|
authHeader = createBasicAuthHeader(fromNickname, password)
|
||||||
|
|
||||||
headers={
|
headers = {
|
||||||
'host': fromDomain, \
|
'host': fromDomain,
|
||||||
'Content-type': 'application/json', \
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': authHeader
|
||||||
}
|
}
|
||||||
postResult= \
|
postResult = \
|
||||||
postJson(session,newDeleteJson,[],inboxUrl,headers,"inbox:write")
|
postJson(session, newDeleteJson, [], inboxUrl, headers, "inbox:write")
|
||||||
#if not postResult:
|
if not postResult:
|
||||||
# if debug:
|
if debug:
|
||||||
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
|
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
|
||||||
# return 5
|
return 5
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s POST delete request success')
|
print('DEBUG: c2s POST delete request success')
|
||||||
|
|
||||||
return newDeleteJson
|
return newDeleteJson
|
||||||
|
|
||||||
def deletePublic(session,baseDir: str,federationList: [], \
|
|
||||||
nickname: str, domain: str, port: int, httpPrefix: str, \
|
def deletePublic(session, baseDir: str, federationList: [],
|
||||||
objectUrl: str,clientToServer: bool, \
|
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||||
sendThreads: [],postLog: [], \
|
objectUrl: str, clientToServer: bool,
|
||||||
personCache: {},cachedWebfingers: {}, \
|
sendThreads: [], postLog: [],
|
||||||
|
personCache: {}, cachedWebfingers: {},
|
||||||
debug: bool) -> {}:
|
debug: bool) -> {}:
|
||||||
"""Makes a public delete activity
|
"""Makes a public delete activity
|
||||||
"""
|
"""
|
||||||
fromDomain=domain
|
fromDomain = domain
|
||||||
if port:
|
if port:
|
||||||
if port!=80 and port!=443:
|
if port != 80 and port != 443:
|
||||||
if ':' not in domain:
|
if ':' not in domain:
|
||||||
fromDomain=domain+':'+str(port)
|
fromDomain = domain + ':' + str(port)
|
||||||
|
|
||||||
toUrl='https://www.w3.org/ns/activitystreams#Public'
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
ccUrl=httpPrefix+'://'+fromDomain+'/users/'+nickname+'/followers'
|
ccUrl = httpPrefix + '://' + fromDomain + \
|
||||||
return createDelete(session,baseDir,federationList, \
|
'/users/' + nickname + '/followers'
|
||||||
nickname,domain,port, \
|
return createDelete(session, baseDir, federationList,
|
||||||
toUrl,ccUrl,httpPrefix, \
|
nickname, domain, port,
|
||||||
objectUrl,clientToServer, \
|
toUrl, ccUrl, httpPrefix,
|
||||||
sendThreads,postLog, \
|
objectUrl, clientToServer,
|
||||||
personCache,cachedWebfingers, \
|
sendThreads, postLog,
|
||||||
|
personCache, cachedWebfingers,
|
||||||
debug)
|
debug)
|
||||||
|
|
||||||
def deletePostPub(session,baseDir: str,federationList: [], \
|
|
||||||
nickname: str, domain: str, port: int, httpPrefix: str, \
|
def deletePostPub(session, baseDir: str, federationList: [],
|
||||||
deleteNickname: str, deleteDomain: str, \
|
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||||
deletePort: int, deleteHttpsPrefix: str, \
|
deleteNickname: str, deleteDomain: str,
|
||||||
deleteStatusNumber: int,clientToServer: bool, \
|
deletePort: int, deleteHttpsPrefix: str,
|
||||||
sendThreads: [],postLog: [], \
|
deleteStatusNumber: int, clientToServer: bool,
|
||||||
personCache: {},cachedWebfingers: {}, \
|
sendThreads: [], postLog: [],
|
||||||
|
personCache: {}, cachedWebfingers: {},
|
||||||
debug: bool) -> {}:
|
debug: bool) -> {}:
|
||||||
"""Deletes a given status post
|
"""Deletes a given status post
|
||||||
"""
|
"""
|
||||||
deletedDomain=deleteDomain
|
deletedDomain = deleteDomain
|
||||||
if deletePort:
|
if deletePort:
|
||||||
if deletePort!=80 and deletePort!=443:
|
if deletePort != 80 and deletePort != 443:
|
||||||
if ':' not in deletedDomain:
|
if ':' not in deletedDomain:
|
||||||
deletedDomain=deletedDomain+':'+str(deletePort)
|
deletedDomain = deletedDomain + ':' + str(deletePort)
|
||||||
|
|
||||||
objectUrl= \
|
objectUrl = \
|
||||||
deleteHttpsPrefix + '://'+deletedDomain+'/users/'+ \
|
deleteHttpsPrefix + '://' + deletedDomain + '/users/' + \
|
||||||
deleteNickname+'/statuses/'+str(deleteStatusNumber)
|
deleteNickname + '/statuses/' + str(deleteStatusNumber)
|
||||||
|
|
||||||
return deletePublic(session,baseDir,federationList, \
|
return deletePublic(session, baseDir, federationList,
|
||||||
nickname,domain,port,httpPrefix, \
|
nickname, domain, port, httpPrefix,
|
||||||
objectUrl,clientToServer, \
|
objectUrl, clientToServer,
|
||||||
sendThreads,postLog, \
|
sendThreads, postLog,
|
||||||
personCache,cachedWebfingers, \
|
personCache, cachedWebfingers,
|
||||||
debug)
|
debug)
|
||||||
|
|
||||||
def outboxDelete(baseDir: str,httpPrefix: str, \
|
|
||||||
nickname: str,domain: str, \
|
def outboxDelete(baseDir: str, httpPrefix: str,
|
||||||
messageJson: {},debug: bool,
|
nickname: str, domain: str,
|
||||||
|
messageJson: {}, debug: bool,
|
||||||
allowDeletion: bool) -> None:
|
allowDeletion: bool) -> None:
|
||||||
""" When a delete request is received by the outbox from c2s
|
""" When a delete request is received by the outbox from c2s
|
||||||
"""
|
"""
|
||||||
|
@ -221,7 +230,7 @@ def outboxDelete(baseDir: str,httpPrefix: str, \
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete - no type')
|
print('DEBUG: delete - no type')
|
||||||
return
|
return
|
||||||
if not messageJson['type']=='Delete':
|
if not messageJson['type'] == 'Delete':
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: not a delete')
|
print('DEBUG: not a delete')
|
||||||
return
|
return
|
||||||
|
@ -235,14 +244,14 @@ def outboxDelete(baseDir: str,httpPrefix: str, \
|
||||||
return
|
return
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete request arrived in outbox')
|
print('DEBUG: c2s delete request arrived in outbox')
|
||||||
deletePrefix=httpPrefix+'://'+domain
|
deletePrefix = httpPrefix + '://' + domain
|
||||||
if not allowDeletion and \
|
if (not allowDeletion and
|
||||||
(not messageJson['object'].startswith(deletePrefix) or \
|
(not messageJson['object'].startswith(deletePrefix) or
|
||||||
not messageJson['actor'].startswith(deletePrefix)):
|
not messageJson['actor'].startswith(deletePrefix))):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete not permitted from other instances')
|
print('DEBUG: delete not permitted from other instances')
|
||||||
return
|
return
|
||||||
messageId=messageJson['object'].replace('/activity','')
|
messageId = messageJson['object'].replace('/activity', '')
|
||||||
if '/statuses/' not in messageId:
|
if '/statuses/' not in messageId:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete object is not a status')
|
print('DEBUG: c2s delete object is not a status')
|
||||||
|
@ -253,25 +262,29 @@ def outboxDelete(baseDir: str,httpPrefix: str, \
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete object has no nickname')
|
print('DEBUG: c2s delete object has no nickname')
|
||||||
return
|
return
|
||||||
deleteNickname=getNicknameFromActor(messageId)
|
deleteNickname = getNicknameFromActor(messageId)
|
||||||
if deleteNickname!=nickname:
|
if deleteNickname != nickname:
|
||||||
if debug:
|
if debug:
|
||||||
print("DEBUG: you can't delete a post which wasn't created by you (nickname does not match)")
|
print("DEBUG: you can't delete a post which " +
|
||||||
|
"wasn't created by you (nickname does not match)")
|
||||||
return
|
return
|
||||||
deleteDomain,deletePort=getDomainFromActor(messageId)
|
deleteDomain, deletePort = getDomainFromActor(messageId)
|
||||||
if ':' in domain:
|
if ':' in domain:
|
||||||
domain=domain.split(':')[0]
|
domain = domain.split(':')[0]
|
||||||
if deleteDomain!=domain:
|
if deleteDomain != domain:
|
||||||
if debug:
|
if debug:
|
||||||
print("DEBUG: you can't delete a post which wasn't created by you (domain does not match)")
|
print("DEBUG: you can't delete a post which " +
|
||||||
|
"wasn't created by you (domain does not match)")
|
||||||
return
|
return
|
||||||
removeModerationPostFromIndex(baseDir,messageId,debug)
|
removeModerationPostFromIndex(baseDir, messageId, debug)
|
||||||
postFilename=locatePost(baseDir,deleteNickname,deleteDomain,messageId)
|
postFilename = locatePost(baseDir, deleteNickname, deleteDomain,
|
||||||
|
messageId)
|
||||||
if not postFilename:
|
if not postFilename:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete post not found in inbox or outbox')
|
print('DEBUG: c2s delete post not found in inbox or outbox')
|
||||||
print(messageId)
|
print(messageId)
|
||||||
return True
|
return True
|
||||||
deletePost(baseDir,httpPrefix,deleteNickname,deleteDomain,postFilename,debug)
|
deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain,
|
||||||
|
postFilename, debug)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post deleted via c2s - '+postFilename)
|
print('DEBUG: post deleted via c2s - ' + postFilename)
|
||||||
|
|
Loading…
Reference in New Issue