mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
3a2a24baa4
commit
1fa09d3dbf
145
delete.py
145
delete.py
|
@ -27,9 +27,9 @@ from posts import get_person_box
|
||||||
|
|
||||||
|
|
||||||
def send_delete_via_server(base_dir: str, session,
|
def send_delete_via_server(base_dir: str, session,
|
||||||
fromNickname: str, password: str,
|
from_nickname: str, password: str,
|
||||||
fromDomain: str, fromPort: int,
|
from_domain: str, fromPort: int,
|
||||||
http_prefix: str, deleteObjectUrl: str,
|
http_prefix: str, delete_object_url: str,
|
||||||
cached_webfingers: {}, person_cache: {},
|
cached_webfingers: {}, person_cache: {},
|
||||||
debug: bool, project_version: str,
|
debug: bool, project_version: str,
|
||||||
signing_priv_key_pem: str) -> {}:
|
signing_priv_key_pem: str) -> {}:
|
||||||
|
@ -39,79 +39,78 @@ def send_delete_via_server(base_dir: str, session,
|
||||||
print('WARN: No session for send_delete_via_server')
|
print('WARN: No session for send_delete_via_server')
|
||||||
return 6
|
return 6
|
||||||
|
|
||||||
fromDomainFull = get_full_domain(fromDomain, fromPort)
|
from_domain_full = get_full_domain(from_domain, fromPort)
|
||||||
|
|
||||||
actor = local_actor_url(http_prefix, fromNickname, fromDomainFull)
|
actor = local_actor_url(http_prefix, from_nickname, from_domain_full)
|
||||||
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
|
to_url = 'https://www.w3.org/ns/activitystreams#Public'
|
||||||
ccUrl = actor + '/followers'
|
cc_url = actor + '/followers'
|
||||||
|
|
||||||
newDeleteJson = {
|
new_delete_json = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'actor': actor,
|
'actor': actor,
|
||||||
'cc': [ccUrl],
|
'cc': [cc_url],
|
||||||
'object': deleteObjectUrl,
|
'object': delete_object_url,
|
||||||
'to': [toUrl],
|
'to': [to_url],
|
||||||
'type': 'Delete'
|
'type': 'Delete'
|
||||||
}
|
}
|
||||||
|
|
||||||
handle = http_prefix + '://' + fromDomainFull + '/@' + fromNickname
|
handle = http_prefix + '://' + from_domain_full + '/@' + from_nickname
|
||||||
|
|
||||||
# lookup the inbox for the To handle
|
# lookup the inbox for the To handle
|
||||||
wfRequest = \
|
wf_request = \
|
||||||
webfinger_handle(session, handle, http_prefix, cached_webfingers,
|
webfinger_handle(session, handle, http_prefix, cached_webfingers,
|
||||||
fromDomain, project_version, debug, False,
|
from_domain, project_version, debug, False,
|
||||||
signing_priv_key_pem)
|
signing_priv_key_pem)
|
||||||
if not wfRequest:
|
if not wf_request:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete webfinger failed for ' + handle)
|
print('DEBUG: delete webfinger failed for ' + handle)
|
||||||
return 1
|
return 1
|
||||||
if not isinstance(wfRequest, dict):
|
if not isinstance(wf_request, dict):
|
||||||
print('WARN: delete webfinger for ' + handle +
|
print('WARN: delete webfinger for ' + handle +
|
||||||
' did not return a dict. ' + str(wfRequest))
|
' did not return a dict. ' + str(wf_request))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
postToBox = 'outbox'
|
post_to_box = 'outbox'
|
||||||
|
|
||||||
# get the actor inbox for the To handle
|
# get the actor inbox for the To handle
|
||||||
originDomain = fromDomain
|
origin_domain = from_domain
|
||||||
(inboxUrl, pubKeyId, pubKey,
|
(inbox_url, _, _, from_person_id, _, _,
|
||||||
fromPersonId, sharedInbox, avatarUrl,
|
_, _) = get_person_box(signing_priv_key_pem, origin_domain,
|
||||||
displayName, _) = get_person_box(signing_priv_key_pem, originDomain,
|
base_dir, session,
|
||||||
base_dir, session,
|
wf_request, person_cache,
|
||||||
wfRequest, person_cache,
|
project_version, http_prefix,
|
||||||
project_version, http_prefix,
|
from_nickname,
|
||||||
fromNickname,
|
from_domain, post_to_box, 53036)
|
||||||
fromDomain, postToBox, 53036)
|
|
||||||
|
|
||||||
if not inboxUrl:
|
if not inbox_url:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete no ' + postToBox +
|
print('DEBUG: delete no ' + post_to_box +
|
||||||
' was found for ' + handle)
|
' was found for ' + handle)
|
||||||
return 3
|
return 3
|
||||||
if not fromPersonId:
|
if not from_person_id:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete no actor was found for ' + handle)
|
print('DEBUG: delete no actor was found for ' + handle)
|
||||||
return 4
|
return 4
|
||||||
|
|
||||||
authHeader = create_basic_auth_header(fromNickname, password)
|
auth_header = create_basic_auth_header(from_nickname, password)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'host': fromDomain,
|
'host': from_domain,
|
||||||
'Content-type': 'application/json',
|
'Content-type': 'application/json',
|
||||||
'Authorization': authHeader
|
'Authorization': auth_header
|
||||||
}
|
}
|
||||||
postResult = \
|
post_result = \
|
||||||
post_json(http_prefix, fromDomainFull,
|
post_json(http_prefix, from_domain_full,
|
||||||
session, newDeleteJson, [], inboxUrl, headers, 3, True)
|
session, new_delete_json, [], inbox_url, headers, 3, True)
|
||||||
if not postResult:
|
if not post_result:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: POST delete failed for c2s to ' + inboxUrl)
|
print('DEBUG: POST delete failed for c2s to ' + inbox_url)
|
||||||
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 new_delete_json
|
||||||
|
|
||||||
|
|
||||||
def outbox_delete(base_dir: str, http_prefix: str,
|
def outbox_delete(base_dir: str, http_prefix: str,
|
||||||
|
@ -133,75 +132,77 @@ def outbox_delete(base_dir: str, http_prefix: str,
|
||||||
return
|
return
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete request arrived in outbox')
|
print('DEBUG: c2s delete request arrived in outbox')
|
||||||
deletePrefix = http_prefix + '://' + domain
|
delete_prefix = http_prefix + '://' + domain
|
||||||
if (not allow_deletion and
|
if (not allow_deletion and
|
||||||
(not message_json['object'].startswith(deletePrefix) or
|
(not message_json['object'].startswith(delete_prefix) or
|
||||||
not message_json['actor'].startswith(deletePrefix))):
|
not message_json['actor'].startswith(delete_prefix))):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: delete not permitted from other instances')
|
print('DEBUG: delete not permitted from other instances')
|
||||||
return
|
return
|
||||||
messageId = remove_id_ending(message_json['object'])
|
message_id = remove_id_ending(message_json['object'])
|
||||||
if '/statuses/' not in messageId:
|
if '/statuses/' not in message_id:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete object is not a status')
|
print('DEBUG: c2s delete object is not a status')
|
||||||
return
|
return
|
||||||
if not has_users_path(messageId):
|
if not has_users_path(message_id):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: c2s delete object has no nickname')
|
print('DEBUG: c2s delete object has no nickname')
|
||||||
return
|
return
|
||||||
deleteNickname = get_nickname_from_actor(messageId)
|
delete_nickname = get_nickname_from_actor(message_id)
|
||||||
if deleteNickname != nickname:
|
if delete_nickname != nickname:
|
||||||
if debug:
|
if debug:
|
||||||
print("DEBUG: you can't delete a post which " +
|
print("DEBUG: you can't delete a post which " +
|
||||||
"wasn't created by you (nickname does not match)")
|
"wasn't created by you (nickname does not match)")
|
||||||
return
|
return
|
||||||
deleteDomain, deletePort = get_domain_from_actor(messageId)
|
delete_domain, _ = get_domain_from_actor(message_id)
|
||||||
domain = remove_domain_port(domain)
|
domain = remove_domain_port(domain)
|
||||||
if deleteDomain != domain:
|
if delete_domain != domain:
|
||||||
if debug:
|
if debug:
|
||||||
print("DEBUG: you can't delete a post which " +
|
print("DEBUG: you can't delete a post which " +
|
||||||
"wasn't created by you (domain does not match)")
|
"wasn't created by you (domain does not match)")
|
||||||
return
|
return
|
||||||
remove_moderation_post_from_index(base_dir, messageId, debug)
|
remove_moderation_post_from_index(base_dir, message_id, debug)
|
||||||
post_filename = locate_post(base_dir, deleteNickname, deleteDomain,
|
post_filename = locate_post(base_dir, delete_nickname, delete_domain,
|
||||||
messageId)
|
message_id)
|
||||||
if not post_filename:
|
if not post_filename:
|
||||||
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(message_id)
|
||||||
return True
|
return True
|
||||||
delete_post(base_dir, http_prefix, deleteNickname, deleteDomain,
|
delete_post(base_dir, http_prefix, delete_nickname, delete_domain,
|
||||||
post_filename, debug, recent_posts_cache)
|
post_filename, debug, recent_posts_cache)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: post deleted via c2s - ' + post_filename)
|
print('DEBUG: post deleted via c2s - ' + post_filename)
|
||||||
|
|
||||||
|
|
||||||
def remove_old_hashtags(base_dir: str, maxMonths: int) -> str:
|
def remove_old_hashtags(base_dir: str, max_months: int) -> str:
|
||||||
"""Remove old hashtags
|
"""Remove old hashtags
|
||||||
"""
|
"""
|
||||||
if maxMonths > 11:
|
if max_months > 11:
|
||||||
maxMonths = 11
|
max_months = 11
|
||||||
maxDaysSinceEpoch = \
|
max_days_since_epoch = \
|
||||||
(datetime.utcnow() - datetime(1970, 1 + maxMonths, 1)).days
|
(datetime.utcnow() - datetime(1970, 1 + max_months, 1)).days
|
||||||
removeHashtags = []
|
remove_hashtags = []
|
||||||
|
|
||||||
for subdir, dirs, files in os.walk(base_dir + '/tags'):
|
for _, _, files in os.walk(base_dir + '/tags'):
|
||||||
for f in files:
|
for fname in files:
|
||||||
tagsFilename = os.path.join(base_dir + '/tags', f)
|
tags_filename = os.path.join(base_dir + '/tags', fname)
|
||||||
if not os.path.isfile(tagsFilename):
|
if not os.path.isfile(tags_filename):
|
||||||
continue
|
continue
|
||||||
# get last modified datetime
|
# get last modified datetime
|
||||||
modTimesinceEpoc = os.path.getmtime(tagsFilename)
|
mod_time_since_epoc = os.path.getmtime(tags_filename)
|
||||||
lastModifiedDate = datetime.fromtimestamp(modTimesinceEpoc)
|
last_modified_date = datetime.fromtimestamp(mod_time_since_epoc)
|
||||||
fileDaysSinceEpoch = (lastModifiedDate - datetime(1970, 1, 1)).days
|
file_days_since_epoch = \
|
||||||
|
(last_modified_date - datetime(1970, 1, 1)).days
|
||||||
|
|
||||||
# check of the file is too old
|
# check of the file is too old
|
||||||
if fileDaysSinceEpoch < maxDaysSinceEpoch:
|
if file_days_since_epoch < max_days_since_epoch:
|
||||||
removeHashtags.append(tagsFilename)
|
remove_hashtags.append(tags_filename)
|
||||||
break
|
break
|
||||||
|
|
||||||
for removeFilename in removeHashtags:
|
for remove_filename in remove_hashtags:
|
||||||
try:
|
try:
|
||||||
os.remove(removeFilename)
|
os.remove(remove_filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: remove_old_hashtags unable to delete ' + removeFilename)
|
print('EX: remove_old_hashtags unable to delete ' +
|
||||||
|
remove_filename)
|
||||||
|
|
Loading…
Reference in New Issue