mirror of https://gitlab.com/bashrc2/epicyon
Remove map links from tags when posts are deleted
parent
c0176832a9
commit
539ba01ba6
61
utils.py
61
utils.py
|
@ -1900,6 +1900,35 @@ def delete_cached_html(base_dir: str, nickname: str, domain: str,
|
||||||
str(cached_post_filename))
|
str(cached_post_filename))
|
||||||
|
|
||||||
|
|
||||||
|
def _remove_post_id_from_tag_index(tag_index_filename: str,
|
||||||
|
post_id: str) -> None:
|
||||||
|
"""Remove post_id from the tag index file
|
||||||
|
"""
|
||||||
|
lines = None
|
||||||
|
with open(tag_index_filename, 'r', encoding='utf-8') as index_file:
|
||||||
|
lines = index_file.readlines()
|
||||||
|
if not lines:
|
||||||
|
return
|
||||||
|
newlines = ''
|
||||||
|
for file_line in lines:
|
||||||
|
if post_id in file_line:
|
||||||
|
# skip over the deleted post
|
||||||
|
continue
|
||||||
|
newlines += file_line
|
||||||
|
if not newlines.strip():
|
||||||
|
# if there are no lines then remove the hashtag file
|
||||||
|
try:
|
||||||
|
os.remove(tag_index_filename)
|
||||||
|
except OSError:
|
||||||
|
print('EX: _delete_hashtags_on_post ' +
|
||||||
|
'unable to delete tag index ' + str(tag_index_filename))
|
||||||
|
else:
|
||||||
|
# write the new hashtag index without the given post in it
|
||||||
|
with open(tag_index_filename, 'w+',
|
||||||
|
encoding='utf-8') as index_file:
|
||||||
|
index_file.write(newlines)
|
||||||
|
|
||||||
|
|
||||||
def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
|
def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
|
||||||
"""Removes hashtags when a post is deleted
|
"""Removes hashtags when a post is deleted
|
||||||
"""
|
"""
|
||||||
|
@ -1926,33 +1955,13 @@ def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
|
||||||
if not tag.get('name'):
|
if not tag.get('name'):
|
||||||
continue
|
continue
|
||||||
# find the index file for this tag
|
# find the index file for this tag
|
||||||
|
tag_map_filename = base_dir + '/tagmaps/' + tag['name'][1:] + '.txt'
|
||||||
|
if os.path.isfile(tag_map_filename):
|
||||||
|
_remove_post_id_from_tag_index(tag_map_filename, post_id)
|
||||||
|
# find the index file for this tag
|
||||||
tag_index_filename = base_dir + '/tags/' + tag['name'][1:] + '.txt'
|
tag_index_filename = base_dir + '/tags/' + tag['name'][1:] + '.txt'
|
||||||
if not os.path.isfile(tag_index_filename):
|
if os.path.isfile(tag_index_filename):
|
||||||
continue
|
_remove_post_id_from_tag_index(tag_index_filename, post_id)
|
||||||
# remove post_id from the tag index file
|
|
||||||
lines = None
|
|
||||||
with open(tag_index_filename, 'r', encoding='utf-8') as index_file:
|
|
||||||
lines = index_file.readlines()
|
|
||||||
if not lines:
|
|
||||||
continue
|
|
||||||
newlines = ''
|
|
||||||
for file_line in lines:
|
|
||||||
if post_id in file_line:
|
|
||||||
# skip over the deleted post
|
|
||||||
continue
|
|
||||||
newlines += file_line
|
|
||||||
if not newlines.strip():
|
|
||||||
# if there are no lines then remove the hashtag file
|
|
||||||
try:
|
|
||||||
os.remove(tag_index_filename)
|
|
||||||
except OSError:
|
|
||||||
print('EX: _delete_hashtags_on_post ' +
|
|
||||||
'unable to delete tag index ' + str(tag_index_filename))
|
|
||||||
else:
|
|
||||||
# write the new hashtag index without the given post in it
|
|
||||||
with open(tag_index_filename, 'w+',
|
|
||||||
encoding='utf-8') as index_file:
|
|
||||||
index_file.write(newlines)
|
|
||||||
|
|
||||||
|
|
||||||
def _delete_conversation_post(base_dir: str, nickname: str, domain: str,
|
def _delete_conversation_post(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
Loading…
Reference in New Issue