epicyon/delete.py

206 lines
7.0 KiB
Python
Raw Normal View History

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"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-03 08:50:43 +00:00
__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
2021-12-26 17:12:07 +00:00
from utils import has_object_string
2021-06-26 14:21:24 +00:00
from utils import removeDomainPort
2021-12-26 12:19:00 +00:00
from utils import has_users_path
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
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
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
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
2021-12-25 16:17:53 +00:00
def sendDeleteViaServer(base_dir: str, session,
2020-04-03 08:50:43 +00:00
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
2021-12-25 17:09:22 +00:00
http_prefix: str, deleteObjectUrl: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: 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
2021-12-26 12:45:03 +00:00
fromDomainFull = get_full_domain(fromDomain, fromPort)
2019-07-17 17:16:48 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, fromNickname, fromDomainFull)
2020-04-03 08:50:43 +00:00
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'
}
2021-12-25 17:09:22 +00:00
handle = http_prefix + '://' + 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 = \
2021-12-25 22:28:18 +00:00
webfingerHandle(session, handle, http_prefix, cached_webfingers,
2021-12-25 20:34:38 +00:00
fromDomain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
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
2021-09-15 14:05:08 +00:00
originDomain = fromDomain
2020-04-03 08:50:43 +00:00
(inboxUrl, pubKeyId, pubKey,
2020-09-27 19:27:24 +00:00
fromPersonId, sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem, originDomain,
2021-12-25 22:17:49 +00:00
base_dir, session, wfRequest, person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix, fromNickname,
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-12-25 17:09:22 +00:00
postJson(http_prefix, fromDomainFull,
2021-06-20 13:39:53 +00:00
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
2021-12-25 17:09:22 +00:00
def outboxDelete(base_dir: str, http_prefix: str,
2020-04-03 08:50:43 +00:00
nickname: str, domain: str,
2021-12-25 23:51:19 +00:00
message_json: {}, debug: bool,
2021-12-25 21:29:53 +00:00
allow_deletion: bool,
recentPostsCache: {}) -> None:
""" When a delete request is received by the outbox from c2s
2019-07-17 17:16:48 +00:00
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: delete - no type')
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Delete':
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: not a delete')
return
2021-12-26 17:12:07 +00:00
if not has_object_string(message_json, debug):
2019-07-17 17:16:48 +00:00
return
if debug:
print('DEBUG: c2s delete request arrived in outbox')
2021-12-25 17:09:22 +00:00
deletePrefix = http_prefix + '://' + domain
2021-12-25 21:29:53 +00:00
if (not allow_deletion and
2021-12-25 23:51:19 +00:00
(not message_json['object'].startswith(deletePrefix) or
not message_json['actor'].startswith(deletePrefix))):
2019-08-12 18:02:29 +00:00
if debug:
print('DEBUG: delete not permitted from other instances')
return
2021-12-25 23:51:19 +00:00
messageId = removeIdEnding(message_json['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
2021-12-26 12:19:00 +00:00
if not has_users_path(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:
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)
domain = removeDomainPort(domain)
2020-04-03 08:50:43 +00:00
if deleteDomain != domain:
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
2021-12-25 16:17:53 +00:00
removeModerationPostFromIndex(base_dir, messageId, debug)
postFilename = locatePost(base_dir, deleteNickname, deleteDomain,
2020-04-03 08:50:43 +00:00
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
2021-12-25 17:09:22 +00:00
deletePost(base_dir, http_prefix, deleteNickname, deleteDomain,
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
2021-12-25 16:17:53 +00:00
def removeOldHashtags(base_dir: str, maxMonths: int) -> str:
2020-11-09 19:41:01 +00:00
"""Remove old hashtags
"""
if maxMonths > 11:
maxMonths = 11
maxDaysSinceEpoch = \
(datetime.utcnow() - datetime(1970, 1 + maxMonths, 1)).days
removeHashtags = []
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/tags'):
2020-11-09 19:41:01 +00:00
for f in files:
2021-12-25 16:17:53 +00:00
tagsFilename = os.path.join(base_dir + '/tags', f)
2020-11-09 19:41:01 +00:00
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)
2021-11-25 18:42:38 +00:00
except OSError:
2021-10-29 18:48:15 +00:00
print('EX: removeOldHashtags unable to delete ' + removeFilename)