Update edits file for delete-and-redraft edits

main
Bob Mottram 2022-04-11 10:59:35 +01:00
parent c4fbdda58d
commit 8f6980ccab
2 changed files with 75 additions and 42 deletions

View File

@ -12,6 +12,7 @@ import os
import datetime
import time
import random
from shutil import copyfile
from linked_data_sig import verify_json_signature
from languages import understood_post_language
from like import update_likes_collection
@ -1064,8 +1065,9 @@ def _receive_edit_to_post(recent_posts_cache: {}, message_json: {},
published_str = post_json_object['object']['updated']
else:
published_str = post_json_object['object']['published']
post_history_json[published_str] = post_json_object
save_json(post_history_json, post_history_filename)
if not post_history_json.get(published_str):
post_history_json[published_str] = post_json_object
save_json(post_history_json, post_history_filename)
# Change Update to Create
message_json['type'] = 'Create'
save_json(message_json, post_filename)
@ -3930,6 +3932,50 @@ def _inbox_after_initial(server,
except OSError:
print('EX: unable to write ' + destination_filename_muted)
# is this an edit of a previous post?
# in Mastodon "delete and redraft"
# NOTE: this must be done before update_conversation is called
edited_filename, edited_json = \
edited_post_filename(base_dir, handle_name, domain,
post_json_object, debug, 300)
# If this was an edit then update the edits json file and
# delete the previous version of the post
if edited_filename and edited_json:
prev_edits_filename = \
edited_filename.replace('.json', '.edits')
edits_filename = \
destination_filename.replace('.json', '.edits')
modified = edited_json['object']['published']
if os.path.isfile(edits_filename):
edits_json = load_json(edits_filename)
if edits_json:
if not edits_json.get(modified):
edits_json[modified] = edited_json
save_json(edits_json, edits_filename)
else:
if os.path.isfile(prev_edits_filename):
if prev_edits_filename != edits_filename:
try:
copyfile(prev_edits_filename, edits_filename)
except OSError:
print('EX: failed to copy edits file')
edits_json = load_json(edits_filename)
if edits_json:
if not edits_json.get(modified):
edits_json[modified] = edited_json
save_json(edits_json, edits_filename)
else:
edits_json = {
modified: edited_json
}
save_json(edits_json, edits_filename)
if edited_filename != destination_filename:
delete_post(base_dir, http_prefix,
nickname, domain, edited_filename,
debug, recent_posts_cache)
# update the indexes for different timelines
for boxname in update_index_list:
if not inbox_update_index(boxname, base_dir, handle,
@ -3983,22 +4029,9 @@ def _inbox_after_initial(server,
boxname + ' post as html to cache in ' +
time_diff + ' mS')
# is this an edit of a previous post?
# in Mastodon "delete and redraft"
# NOTE: this must be done before update_conversation is called
edited_filename = \
edited_post_filename(base_dir, handle_name, domain,
post_json_object, debug, 300)
update_conversation(base_dir, handle_name, domain,
post_json_object)
# If this was an edit then delete the previous version of the post
if edited_filename:
delete_post(base_dir, http_prefix,
nickname, domain, edited_filename,
debug, recent_posts_cache)
# store the id of the last post made by this actor
_store_last_post_id(base_dir, nickname, domain, post_json_object)

View File

@ -5476,31 +5476,31 @@ def seconds_between_published(published1: str, published2: str) -> int:
def edited_post_filename(base_dir: str, nickname: str, domain: str,
post_json_object: {}, debug: bool,
max_time_diff_seconds: int) -> str:
max_time_diff_seconds: int) -> (str, {}):
"""Returns the filename of the edited post
"""
if not has_object_dict(post_json_object):
return ''
return '', None
if not post_json_object.get('type'):
return ''
return '', None
if not post_json_object['object'].get('type'):
return ''
return '', None
if not post_json_object['object'].get('published'):
return ''
return '', None
if not post_json_object['object'].get('id'):
return ''
return '', None
if not post_json_object['object'].get('content'):
return ''
return '', None
if not post_json_object['object'].get('attributedTo'):
return ''
return '', None
if not isinstance(post_json_object['object']['attributedTo'], str):
return ''
return '', None
actor = post_json_object['object']['attributedTo']
actor_filename = \
acct_dir(base_dir, nickname, domain) + '/lastpost/' + \
actor.replace('/', '#')
if not os.path.isfile(actor_filename):
return ''
return '', None
post_id = remove_id_ending(post_json_object['object']['id'])
lastpost_id = None
try:
@ -5508,48 +5508,48 @@ def edited_post_filename(base_dir: str, nickname: str, domain: str,
lastpost_id = fp_actor.read()
except OSError:
print('EX: edited_post_filename unable to read ' + actor_filename)
return ''
return '', None
if not lastpost_id:
return ''
return '', None
if lastpost_id == post_id:
return ''
return '', None
lastpost_filename = \
locate_post(base_dir, nickname, domain, lastpost_id, False)
if not lastpost_filename:
return ''
return '', None
lastpost_json = load_json(lastpost_filename, 0)
if not lastpost_json:
return ''
return '', None
if not lastpost_json.get('type'):
return ''
return '', None
if lastpost_json['type'] != post_json_object['type']:
return ''
return '', None
if not lastpost_json['object'].get('type'):
return ''
return '', None
if lastpost_json['object']['type'] != post_json_object['object']['type']:
return
return '', None
if not lastpost_json['object'].get('published'):
return ''
return '', None
if not lastpost_json['object'].get('id'):
return ''
return '', None
if not lastpost_json['object'].get('content'):
return ''
return '', None
if not lastpost_json['object'].get('attributedTo'):
return ''
return '', None
if not isinstance(lastpost_json['object']['attributedTo'], str):
return ''
return '', None
time_diff_seconds = \
seconds_between_published(lastpost_json['object']['published'],
post_json_object['object']['published'])
if time_diff_seconds > max_time_diff_seconds:
return ''
return '', None
if debug:
print(post_id + ' might be an edit of ' + lastpost_id)
if words_similarity(lastpost_json['object']['content'],
post_json_object['object']['content'], 10) < 70:
return ''
return '', None
print(post_id + ' is an edit of ' + lastpost_id)
return lastpost_filename
return lastpost_filename, lastpost_json
def get_original_post_from_announce_url(announce_url: str, base_dir: str,