2020-04-03 08:50:43 +00:00
|
|
|
__filename__ = "delete.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-03 08:50:43 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
2021-06-15 15:08:12 +00:00
|
|
|
__module_group__ = "ActivityPub"
|
2019-07-11 21:38:28 +00:00
|
|
|
|
2020-11-09 19:41:01 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
2020-12-23 10:57:44 +00:00
|
|
|
from utils import hasUsersPath
|
2020-12-16 10:30:54 +00:00
|
|
|
from utils import getFullDomain
|
2020-08-23 11:13:35 +00:00
|
|
|
from utils import removeIdEnding
|
2019-07-11 21:38:28 +00:00
|
|
|
from utils import getNicknameFromActor
|
|
|
|
from utils import getDomainFromActor
|
2019-07-17 17:16:48 +00:00
|
|
|
from utils import locatePost
|
|
|
|
from utils import deletePost
|
2019-08-12 18:02:29 +00:00
|
|
|
from utils import removeModerationPostFromIndex
|
2019-07-17 17:16:48 +00:00
|
|
|
from session import postJson
|
|
|
|
from webfinger import webfingerHandle
|
|
|
|
from auth import createBasicAuthHeader
|
|
|
|
from posts import getPersonBox
|
2019-07-11 21:38:28 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
|
|
|
|
def sendDeleteViaServer(baseDir: str, session,
|
|
|
|
fromNickname: str, password: str,
|
|
|
|
fromDomain: str, fromPort: int,
|
|
|
|
httpPrefix: str, deleteObjectUrl: str,
|
|
|
|
cachedWebfingers: {}, personCache: {},
|
|
|
|
debug: bool, projectVersion: str) -> {}:
|
2019-07-17 17:16:48 +00:00
|
|
|
"""Creates a delete request message via c2s
|
|
|
|
"""
|
|
|
|
if not session:
|
|
|
|
print('WARN: No session for sendDeleteViaServer')
|
|
|
|
return 6
|
|
|
|
|
2020-12-16 10:30:54 +00:00
|
|
|
fromDomainFull = getFullDomain(fromDomain, fromPort)
|
2019-07-17 17:16:48 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
actor = httpPrefix + '://' + fromDomainFull + \
|
|
|
|
'/users/' + fromNickname
|
|
|
|
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
ccUrl = actor + '/followers'
|
2019-07-17 17:16:48 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
newDeleteJson = {
|
2019-08-18 11:07:06 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2020-04-03 08:50:43 +00:00
|
|
|
'actor': actor,
|
2019-07-17 17:16:48 +00:00
|
|
|
'cc': [ccUrl],
|
|
|
|
'object': deleteObjectUrl,
|
|
|
|
'to': [toUrl],
|
|
|
|
'type': 'Delete'
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
|
2019-07-17 17:16:48 +00:00
|
|
|
|
|
|
|
# lookup the inbox for the To handle
|
2020-04-03 08:50:43 +00:00
|
|
|
wfRequest = \
|
|
|
|
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
|
2021-03-14 19:22:58 +00:00
|
|
|
fromDomain, projectVersion, debug)
|
2019-07-17 17:16:48 +00:00
|
|
|
if not wfRequest:
|
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: delete webfinger failed for ' + handle)
|
2019-07-17 17:16:48 +00:00
|
|
|
return 1
|
2020-06-23 10:41:12 +00:00
|
|
|
if not isinstance(wfRequest, dict):
|
2021-03-18 10:01:01 +00:00
|
|
|
print('WARN: delete webfinger for ' + handle +
|
|
|
|
' did not return a dict. ' + str(wfRequest))
|
2020-06-23 10:41:12 +00:00
|
|
|
return 1
|
2019-07-17 17:16:48 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
postToBox = 'outbox'
|
2019-07-17 17:16:48 +00:00
|
|
|
|
|
|
|
# get the actor inbox for the To handle
|
2020-04-03 08:50:43 +00:00
|
|
|
(inboxUrl, pubKeyId, pubKey,
|
2020-09-27 19:27:24 +00:00
|
|
|
fromPersonId, sharedInbox, avatarUrl,
|
2020-04-03 08:50:43 +00:00
|
|
|
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
|
|
|
|
projectVersion, httpPrefix, fromNickname,
|
2020-12-18 17:49:17 +00:00
|
|
|
fromDomain, postToBox, 53036)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2019-07-17 17:16:48 +00:00
|
|
|
if not inboxUrl:
|
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: delete no ' + postToBox +
|
|
|
|
' was found for ' + handle)
|
2019-07-17 17:16:48 +00:00
|
|
|
return 3
|
|
|
|
if not fromPersonId:
|
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: delete no actor was found for ' + handle)
|
2019-07-17 17:16:48 +00:00
|
|
|
return 4
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
authHeader = createBasicAuthHeader(fromNickname, password)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
headers = {
|
|
|
|
'host': fromDomain,
|
|
|
|
'Content-type': 'application/json',
|
2020-03-22 20:36:19 +00:00
|
|
|
'Authorization': authHeader
|
|
|
|
}
|
2020-04-03 08:50:43 +00:00
|
|
|
postResult = \
|
2021-03-21 18:37:06 +00:00
|
|
|
postJson(session, newDeleteJson, [], inboxUrl, headers, 3, True)
|
2020-04-03 08:50:43 +00:00
|
|
|
if not postResult:
|
|
|
|
if debug:
|
2021-03-18 10:01:01 +00:00
|
|
|
print('DEBUG: POST delete failed for c2s to ' + inboxUrl)
|
2020-04-03 08:50:43 +00:00
|
|
|
return 5
|
2019-07-17 17:16:48 +00:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s POST delete request success')
|
|
|
|
|
|
|
|
return newDeleteJson
|
|
|
|
|
2020-04-03 08:50:43 +00:00
|
|
|
|
|
|
|
def outboxDelete(baseDir: str, httpPrefix: str,
|
|
|
|
nickname: str, domain: str,
|
|
|
|
messageJson: {}, debug: bool,
|
2020-06-24 13:30:50 +00:00
|
|
|
allowDeletion: bool,
|
|
|
|
recentPostsCache: {}) -> None:
|
2019-07-17 18:05:07 +00:00
|
|
|
""" When a delete request is received by the outbox from c2s
|
2019-07-17 17:16:48 +00:00
|
|
|
"""
|
|
|
|
if not messageJson.get('type'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: delete - no type')
|
|
|
|
return
|
2020-04-03 08:50:43 +00:00
|
|
|
if not messageJson['type'] == 'Delete':
|
2019-07-17 17:16:48 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: not a delete')
|
|
|
|
return
|
|
|
|
if not messageJson.get('object'):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: no object in delete')
|
|
|
|
return
|
|
|
|
if not isinstance(messageJson['object'], str):
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: delete object is not string')
|
|
|
|
return
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s delete request arrived in outbox')
|
2020-04-03 08:50:43 +00:00
|
|
|
deletePrefix = httpPrefix + '://' + domain
|
|
|
|
if (not allowDeletion and
|
|
|
|
(not messageJson['object'].startswith(deletePrefix) or
|
|
|
|
not messageJson['actor'].startswith(deletePrefix))):
|
2019-08-12 18:02:29 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: delete not permitted from other instances')
|
|
|
|
return
|
2020-08-23 11:13:35 +00:00
|
|
|
messageId = removeIdEnding(messageJson['object'])
|
2019-07-17 17:16:48 +00:00
|
|
|
if '/statuses/' not in messageId:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s delete object is not a status')
|
|
|
|
return
|
2020-12-23 10:57:44 +00:00
|
|
|
if not hasUsersPath(messageId):
|
2019-07-17 17:16:48 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s delete object has no nickname')
|
|
|
|
return
|
2020-04-03 08:50:43 +00:00
|
|
|
deleteNickname = getNicknameFromActor(messageId)
|
|
|
|
if deleteNickname != nickname:
|
2019-07-17 18:05:07 +00:00
|
|
|
if debug:
|
2020-04-03 08:50:43 +00:00
|
|
|
print("DEBUG: you can't delete a post which " +
|
|
|
|
"wasn't created by you (nickname does not match)")
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2020-04-03 08:50:43 +00:00
|
|
|
deleteDomain, deletePort = getDomainFromActor(messageId)
|
2019-07-17 18:05:07 +00:00
|
|
|
if ':' in domain:
|
2020-04-03 08:50:43 +00:00
|
|
|
domain = domain.split(':')[0]
|
|
|
|
if deleteDomain != domain:
|
2019-07-17 18:05:07 +00:00
|
|
|
if debug:
|
2020-04-03 08:50:43 +00:00
|
|
|
print("DEBUG: you can't delete a post which " +
|
|
|
|
"wasn't created by you (domain does not match)")
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2020-04-03 08:50:43 +00:00
|
|
|
removeModerationPostFromIndex(baseDir, messageId, debug)
|
|
|
|
postFilename = locatePost(baseDir, deleteNickname, deleteDomain,
|
|
|
|
messageId)
|
2019-07-17 17:16:48 +00:00
|
|
|
if not postFilename:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s delete post not found in inbox or outbox')
|
|
|
|
print(messageId)
|
|
|
|
return True
|
2020-04-03 08:50:43 +00:00
|
|
|
deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain,
|
2020-06-24 13:30:50 +00:00
|
|
|
postFilename, debug, recentPostsCache)
|
2019-07-17 17:16:48 +00:00
|
|
|
if debug:
|
2020-04-03 08:50:43 +00:00
|
|
|
print('DEBUG: post deleted via c2s - ' + postFilename)
|
2020-11-09 19:41:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def removeOldHashtags(baseDir: str, maxMonths: int) -> str:
|
|
|
|
"""Remove old hashtags
|
|
|
|
"""
|
|
|
|
if maxMonths > 11:
|
|
|
|
maxMonths = 11
|
|
|
|
maxDaysSinceEpoch = \
|
|
|
|
(datetime.utcnow() - datetime(1970, 1 + maxMonths, 1)).days
|
|
|
|
removeHashtags = []
|
|
|
|
|
|
|
|
for subdir, dirs, files in os.walk(baseDir + '/tags'):
|
|
|
|
for f in files:
|
|
|
|
tagsFilename = os.path.join(baseDir + '/tags', f)
|
|
|
|
if not os.path.isfile(tagsFilename):
|
|
|
|
continue
|
|
|
|
# get last modified datetime
|
|
|
|
modTimesinceEpoc = os.path.getmtime(tagsFilename)
|
|
|
|
lastModifiedDate = datetime.fromtimestamp(modTimesinceEpoc)
|
|
|
|
fileDaysSinceEpoch = (lastModifiedDate - datetime(1970, 1, 1)).days
|
|
|
|
|
|
|
|
# check of the file is too old
|
|
|
|
if fileDaysSinceEpoch < maxDaysSinceEpoch:
|
|
|
|
removeHashtags.append(tagsFilename)
|
2020-12-13 22:13:45 +00:00
|
|
|
break
|
2020-11-09 19:41:01 +00:00
|
|
|
|
|
|
|
for removeFilename in removeHashtags:
|
|
|
|
try:
|
|
|
|
os.remove(removeFilename)
|
|
|
|
except BaseException:
|
|
|
|
pass
|