mirror of https://gitlab.com/bashrc2/epicyon
Update edits file for delete-and-redraft edits
parent
c4fbdda58d
commit
8f6980ccab
63
inbox.py
63
inbox.py
|
@ -12,6 +12,7 @@ import os
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
from shutil import copyfile
|
||||||
from linked_data_sig import verify_json_signature
|
from linked_data_sig import verify_json_signature
|
||||||
from languages import understood_post_language
|
from languages import understood_post_language
|
||||||
from like import update_likes_collection
|
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']
|
published_str = post_json_object['object']['updated']
|
||||||
else:
|
else:
|
||||||
published_str = post_json_object['object']['published']
|
published_str = post_json_object['object']['published']
|
||||||
post_history_json[published_str] = post_json_object
|
if not post_history_json.get(published_str):
|
||||||
save_json(post_history_json, post_history_filename)
|
post_history_json[published_str] = post_json_object
|
||||||
|
save_json(post_history_json, post_history_filename)
|
||||||
# Change Update to Create
|
# Change Update to Create
|
||||||
message_json['type'] = 'Create'
|
message_json['type'] = 'Create'
|
||||||
save_json(message_json, post_filename)
|
save_json(message_json, post_filename)
|
||||||
|
@ -3930,6 +3932,50 @@ def _inbox_after_initial(server,
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: unable to write ' + destination_filename_muted)
|
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
|
# update the indexes for different timelines
|
||||||
for boxname in update_index_list:
|
for boxname in update_index_list:
|
||||||
if not inbox_update_index(boxname, base_dir, handle,
|
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 ' +
|
boxname + ' post as html to cache in ' +
|
||||||
time_diff + ' mS')
|
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,
|
update_conversation(base_dir, handle_name, domain,
|
||||||
post_json_object)
|
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 the id of the last post made by this actor
|
||||||
_store_last_post_id(base_dir, nickname, domain, post_json_object)
|
_store_last_post_id(base_dir, nickname, domain, post_json_object)
|
||||||
|
|
||||||
|
|
54
posts.py
54
posts.py
|
@ -5476,31 +5476,31 @@ def seconds_between_published(published1: str, published2: str) -> int:
|
||||||
|
|
||||||
def edited_post_filename(base_dir: str, nickname: str, domain: str,
|
def edited_post_filename(base_dir: str, nickname: str, domain: str,
|
||||||
post_json_object: {}, debug: bool,
|
post_json_object: {}, debug: bool,
|
||||||
max_time_diff_seconds: int) -> str:
|
max_time_diff_seconds: int) -> (str, {}):
|
||||||
"""Returns the filename of the edited post
|
"""Returns the filename of the edited post
|
||||||
"""
|
"""
|
||||||
if not has_object_dict(post_json_object):
|
if not has_object_dict(post_json_object):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object.get('type'):
|
if not post_json_object.get('type'):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object['object'].get('type'):
|
if not post_json_object['object'].get('type'):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object['object'].get('published'):
|
if not post_json_object['object'].get('published'):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object['object'].get('id'):
|
if not post_json_object['object'].get('id'):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object['object'].get('content'):
|
if not post_json_object['object'].get('content'):
|
||||||
return ''
|
return '', None
|
||||||
if not post_json_object['object'].get('attributedTo'):
|
if not post_json_object['object'].get('attributedTo'):
|
||||||
return ''
|
return '', None
|
||||||
if not isinstance(post_json_object['object']['attributedTo'], str):
|
if not isinstance(post_json_object['object']['attributedTo'], str):
|
||||||
return ''
|
return '', None
|
||||||
actor = post_json_object['object']['attributedTo']
|
actor = post_json_object['object']['attributedTo']
|
||||||
actor_filename = \
|
actor_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/lastpost/' + \
|
acct_dir(base_dir, nickname, domain) + '/lastpost/' + \
|
||||||
actor.replace('/', '#')
|
actor.replace('/', '#')
|
||||||
if not os.path.isfile(actor_filename):
|
if not os.path.isfile(actor_filename):
|
||||||
return ''
|
return '', None
|
||||||
post_id = remove_id_ending(post_json_object['object']['id'])
|
post_id = remove_id_ending(post_json_object['object']['id'])
|
||||||
lastpost_id = None
|
lastpost_id = None
|
||||||
try:
|
try:
|
||||||
|
@ -5508,48 +5508,48 @@ def edited_post_filename(base_dir: str, nickname: str, domain: str,
|
||||||
lastpost_id = fp_actor.read()
|
lastpost_id = fp_actor.read()
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: edited_post_filename unable to read ' + actor_filename)
|
print('EX: edited_post_filename unable to read ' + actor_filename)
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_id:
|
if not lastpost_id:
|
||||||
return ''
|
return '', None
|
||||||
if lastpost_id == post_id:
|
if lastpost_id == post_id:
|
||||||
return ''
|
return '', None
|
||||||
lastpost_filename = \
|
lastpost_filename = \
|
||||||
locate_post(base_dir, nickname, domain, lastpost_id, False)
|
locate_post(base_dir, nickname, domain, lastpost_id, False)
|
||||||
if not lastpost_filename:
|
if not lastpost_filename:
|
||||||
return ''
|
return '', None
|
||||||
lastpost_json = load_json(lastpost_filename, 0)
|
lastpost_json = load_json(lastpost_filename, 0)
|
||||||
if not lastpost_json:
|
if not lastpost_json:
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_json.get('type'):
|
if not lastpost_json.get('type'):
|
||||||
return ''
|
return '', None
|
||||||
if lastpost_json['type'] != post_json_object['type']:
|
if lastpost_json['type'] != post_json_object['type']:
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_json['object'].get('type'):
|
if not lastpost_json['object'].get('type'):
|
||||||
return ''
|
return '', None
|
||||||
if lastpost_json['object']['type'] != post_json_object['object']['type']:
|
if lastpost_json['object']['type'] != post_json_object['object']['type']:
|
||||||
return
|
return '', None
|
||||||
if not lastpost_json['object'].get('published'):
|
if not lastpost_json['object'].get('published'):
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_json['object'].get('id'):
|
if not lastpost_json['object'].get('id'):
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_json['object'].get('content'):
|
if not lastpost_json['object'].get('content'):
|
||||||
return ''
|
return '', None
|
||||||
if not lastpost_json['object'].get('attributedTo'):
|
if not lastpost_json['object'].get('attributedTo'):
|
||||||
return ''
|
return '', None
|
||||||
if not isinstance(lastpost_json['object']['attributedTo'], str):
|
if not isinstance(lastpost_json['object']['attributedTo'], str):
|
||||||
return ''
|
return '', None
|
||||||
time_diff_seconds = \
|
time_diff_seconds = \
|
||||||
seconds_between_published(lastpost_json['object']['published'],
|
seconds_between_published(lastpost_json['object']['published'],
|
||||||
post_json_object['object']['published'])
|
post_json_object['object']['published'])
|
||||||
if time_diff_seconds > max_time_diff_seconds:
|
if time_diff_seconds > max_time_diff_seconds:
|
||||||
return ''
|
return '', None
|
||||||
if debug:
|
if debug:
|
||||||
print(post_id + ' might be an edit of ' + lastpost_id)
|
print(post_id + ' might be an edit of ' + lastpost_id)
|
||||||
if words_similarity(lastpost_json['object']['content'],
|
if words_similarity(lastpost_json['object']['content'],
|
||||||
post_json_object['object']['content'], 10) < 70:
|
post_json_object['object']['content'], 10) < 70:
|
||||||
return ''
|
return '', None
|
||||||
print(post_id + ' is an edit of ' + lastpost_id)
|
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,
|
def get_original_post_from_announce_url(announce_url: str, base_dir: str,
|
||||||
|
|
Loading…
Reference in New Issue