epicyon/delete.py

208 lines
7.2 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-12-26 18:17:37 +00:00
from utils import remove_domain_port
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
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2021-12-26 20:36:08 +00:00
from utils import locate_post
2021-12-28 14:55:45 +00:00
from utils import delete_post
2021-12-28 13:12:10 +00:00
from utils import remove_moderation_post_from_index
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-29 21:55:09 +00:00
from session import post_json
from webfinger import webfinger_handle
2021-12-28 21:36:27 +00:00
from auth import create_basic_auth_header
2021-12-29 21:55:09 +00:00
from posts import get_person_box
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
2021-12-29 21:55:09 +00:00
def send_delete_via_server(base_dir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
http_prefix: str, deleteObjectUrl: str,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str) -> {}:
2019-07-17 17:16:48 +00:00
"""Creates a delete request message via c2s
"""
if not session:
2021-12-29 21:55:09 +00:00
print('WARN: No session for send_delete_via_server')
2019-07-17 17:16:48 +00:00
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-29 21:55:09 +00:00
webfinger_handle(session, handle, http_prefix, cached_webfingers,
fromDomain, project_version, debug, False,
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-29 21:55:09 +00:00
displayName, _) = get_person_box(signing_priv_key_pem, originDomain,
base_dir, session,
wfRequest, person_cache,
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
2021-12-28 21:36:27 +00:00
authHeader = create_basic_auth_header(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-29 21:55:09 +00:00
post_json(http_prefix, fromDomainFull,
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-29 21:55:09 +00:00
def outbox_delete(base_dir: str, http_prefix: str,
nickname: str, domain: str,
message_json: {}, debug: bool,
allow_deletion: bool,
recent_posts_cache: {}) -> 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-27 11:20:57 +00:00
messageId = remove_id_ending(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
2021-12-27 22:19:18 +00:00
deleteNickname = get_nickname_from_actor(messageId)
2020-04-03 08:50:43 +00:00
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
2021-12-27 19:05:25 +00:00
deleteDomain, deletePort = get_domain_from_actor(messageId)
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(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-28 13:12:10 +00:00
remove_moderation_post_from_index(base_dir, messageId, debug)
2021-12-26 23:41:34 +00:00
post_filename = locate_post(base_dir, deleteNickname, deleteDomain,
messageId)
if not post_filename:
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: c2s delete post not found in inbox or outbox')
print(messageId)
return True
2021-12-28 14:55:45 +00:00
delete_post(base_dir, http_prefix, deleteNickname, deleteDomain,
post_filename, debug, recent_posts_cache)
2019-07-17 17:16:48 +00:00
if debug:
2021-12-26 23:41:34 +00:00
print('DEBUG: post deleted via c2s - ' + post_filename)
2020-11-09 19:41:01 +00:00
2021-12-29 21:55:09 +00:00
def remove_old_hashtags(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-12-29 21:55:09 +00:00
print('EX: remove_old_hashtags unable to delete ' + removeFilename)