epicyon/delete.py

218 lines
7.7 KiB
Python
Raw Normal View History

2020-04-03 08:50:43 +00:00
__filename__ = "delete.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2024-01-21 19:01:20 +00:00
__version__ = "1.5.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
2023-11-21 11:43:16 +00:00
from datetime import datetime, timezone
from utils import date_from_numbers
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
2023-11-20 22:27:58 +00:00
from utils import date_utcnow
2023-11-20 23:59:28 +00:00
from utils import date_epoch
2024-01-09 16:59:23 +00:00
from utils import get_actor_from_post
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,
2022-01-02 10:42:11 +00:00
from_nickname: str, password: str,
2022-05-30 21:41:18 +00:00
from_domain: str, from_port: int,
2022-01-02 10:42:11 +00:00
http_prefix: str, delete_object_url: str,
2021-12-29 21:55:09 +00:00
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
2023-10-25 19:55:40 +00:00
signing_priv_key_pem: str,
system_language: 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
2022-05-30 21:41:18 +00:00
from_domain_full = get_full_domain(from_domain, from_port)
2019-07-17 17:16:48 +00:00
2022-01-02 10:42:11 +00:00
actor = local_actor_url(http_prefix, from_nickname, from_domain_full)
to_url = 'https://www.w3.org/ns/activitystreams#Public'
cc_url = actor + '/followers'
2019-07-17 17:16:48 +00:00
2022-01-02 10:42:11 +00:00
new_delete_json = {
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,
2022-01-02 10:42:11 +00:00
'cc': [cc_url],
'object': delete_object_url,
'to': [to_url],
2019-07-17 17:16:48 +00:00
'type': 'Delete'
}
2022-01-02 10:42:11 +00:00
handle = http_prefix + '://' + from_domain_full + '/@' + from_nickname
2019-07-17 17:16:48 +00:00
# lookup the inbox for the To handle
2022-01-02 10:42:11 +00:00
wf_request = \
2021-12-29 21:55:09 +00:00
webfinger_handle(session, handle, http_prefix, cached_webfingers,
2022-01-02 10:42:11 +00:00
from_domain, project_version, debug, False,
2021-12-29 21:55:09 +00:00
signing_priv_key_pem)
2022-01-02 10:42:11 +00:00
if not wf_request:
2019-07-17 17:16:48 +00:00
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
2022-01-02 10:42:11 +00:00
if not isinstance(wf_request, dict):
2021-03-18 10:01:01 +00:00
print('WARN: delete webfinger for ' + handle +
2022-01-02 10:42:11 +00:00
' did not return a dict. ' + str(wf_request))
2020-06-23 10:41:12 +00:00
return 1
2019-07-17 17:16:48 +00:00
2022-01-02 10:42:11 +00:00
post_to_box = 'outbox'
2019-07-17 17:16:48 +00:00
# get the actor inbox for the To handle
2022-01-02 10:42:11 +00:00
origin_domain = from_domain
(inbox_url, _, _, from_person_id, _, _,
_, _) = get_person_box(signing_priv_key_pem, origin_domain,
base_dir, session,
wf_request, person_cache,
project_version, http_prefix,
from_nickname,
2023-10-25 19:55:40 +00:00
from_domain, post_to_box, 53036,
system_language)
2022-01-02 10:42:11 +00:00
if not inbox_url:
2019-07-17 17:16:48 +00:00
if debug:
2022-01-02 10:42:11 +00:00
print('DEBUG: delete no ' + post_to_box +
2021-03-18 10:01:01 +00:00
' was found for ' + handle)
2019-07-17 17:16:48 +00:00
return 3
2022-01-02 10:42:11 +00:00
if not from_person_id:
2019-07-17 17:16:48 +00:00
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
2022-01-02 10:42:11 +00:00
auth_header = create_basic_auth_header(from_nickname, password)
2020-03-22 21:16:02 +00:00
2020-04-03 08:50:43 +00:00
headers = {
2022-01-02 10:42:11 +00:00
'host': from_domain,
2020-04-03 08:50:43 +00:00
'Content-type': 'application/json',
2022-01-02 10:42:11 +00:00
'Authorization': auth_header
2020-03-22 20:36:19 +00:00
}
2022-01-02 10:42:11 +00:00
post_result = \
post_json(http_prefix, from_domain_full,
session, new_delete_json, [], inbox_url, headers, 3, True)
if not post_result:
2020-04-03 08:50:43 +00:00
if debug:
2022-01-02 10:42:11 +00:00
print('DEBUG: POST delete failed for c2s to ' + inbox_url)
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')
2022-01-02 10:42:11 +00:00
return new_delete_json
2019-07-17 17:16:48 +00:00
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')
2022-01-02 10:42:11 +00:00
delete_prefix = http_prefix + '://' + domain
2024-01-09 16:59:23 +00:00
actor_url = get_actor_from_post(message_json)
2021-12-25 21:29:53 +00:00
if (not allow_deletion and
2022-01-02 10:42:11 +00:00
(not message_json['object'].startswith(delete_prefix) or
2024-01-09 16:59:23 +00:00
not actor_url.startswith(delete_prefix))):
2019-08-12 18:02:29 +00:00
if debug:
print('DEBUG: delete not permitted from other instances')
return
2022-01-02 10:42:11 +00:00
message_id = remove_id_ending(message_json['object'])
if '/statuses/' not in message_id:
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: c2s delete object is not a status')
return
2022-01-02 10:42:11 +00:00
if not has_users_path(message_id):
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: c2s delete object has no nickname')
return
2022-01-02 10:42:11 +00:00
delete_nickname = get_nickname_from_actor(message_id)
if delete_nickname != 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
2022-01-02 10:42:11 +00:00
delete_domain, _ = get_domain_from_actor(message_id)
2021-12-26 18:17:37 +00:00
domain = remove_domain_port(domain)
2022-01-02 10:42:11 +00:00
if delete_domain != 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
2022-01-02 10:42:11 +00:00
remove_moderation_post_from_index(base_dir, message_id, debug)
post_filename = locate_post(base_dir, delete_nickname, delete_domain,
message_id)
2021-12-26 23:41:34 +00:00
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')
2022-01-02 10:42:11 +00:00
print(message_id)
2019-07-17 17:16:48 +00:00
return True
2022-01-02 10:42:11 +00:00
delete_post(base_dir, http_prefix, delete_nickname, delete_domain,
post_filename, debug, recent_posts_cache, True)
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
2022-01-02 10:42:11 +00:00
def remove_old_hashtags(base_dir: str, max_months: int) -> str:
2020-11-09 19:41:01 +00:00
"""Remove old hashtags
"""
2022-05-30 21:41:18 +00:00
max_months = min(max_months, 11)
prev_date = date_from_numbers(1970, 1 + max_months, 1, 0, 0)
max_days_since_epoch = (date_utcnow() - prev_date).days
2022-01-02 10:42:11 +00:00
remove_hashtags = []
for _, _, files in os.walk(base_dir + '/tags'):
for fname in files:
tags_filename = os.path.join(base_dir + '/tags', fname)
if not os.path.isfile(tags_filename):
2020-11-09 19:41:01 +00:00
continue
# get last modified datetime
2022-01-02 10:42:11 +00:00
mod_time_since_epoc = os.path.getmtime(tags_filename)
2023-11-20 22:27:58 +00:00
last_modified_date = \
datetime.fromtimestamp(mod_time_since_epoc,
2023-11-21 11:43:16 +00:00
timezone.utc)
2023-11-20 23:59:28 +00:00
prev_date_epoch = date_epoch()
2022-01-02 10:42:11 +00:00
file_days_since_epoch = \
2023-11-20 23:59:28 +00:00
(last_modified_date - prev_date_epoch).days
2020-11-09 19:41:01 +00:00
# check of the file is too old
2022-01-02 10:42:11 +00:00
if file_days_since_epoch < max_days_since_epoch:
remove_hashtags.append(tags_filename)
2020-12-13 22:13:45 +00:00
break
2020-11-09 19:41:01 +00:00
2022-01-02 10:42:11 +00:00
for remove_filename in remove_hashtags:
2020-11-09 19:41:01 +00:00
try:
2022-01-02 10:42:11 +00:00
os.remove(remove_filename)
2021-11-25 18:42:38 +00:00
except OSError:
2022-01-02 10:42:11 +00:00
print('EX: remove_old_hashtags unable to delete ' +
remove_filename)