diasporadiaries

a platform for writing stories with personal accounts and messages
git clone git://parazyd.org/diasporadiaries.git
Log | Files | Refs | Submodules | README | LICENSE

commit 8715a4680458eb6e8d1cddc9bb7588e307ed59b6
parent bfe03c596893b173b7ce85400f5eb1ce082aa210
Author: parazyd <parazyd@dyne.org>
Date:   Mon, 21 Jan 2019 15:46:00 +0100

Use some try/except magic when creating/fetching messages.

Diffstat:
Mutils.py | 19++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/utils.py b/utils.py @@ -245,11 +245,16 @@ def get_latest_messages(user_id): ppl = listdir(msgpath) latest = [] for i in ppl: - with open(join(msgpath, i)) as msgfile: - data = json.load(msgfile) - user = find_user_by_email(i) - data[-1]['message'] = data[-1]['message'][:64] + "..." - latest.append([user['name'], user['id'], data[-1]]) + try: + with open(join(msgpath, i)) as msgfile: + data = json.load(msgfile) + user = find_user_by_email(i) + if not user: + continue + data[-1]['message'] = data[-1]['message'][:64] + "..." + latest.append([user['name'], user['id'], data[-1]]) + except json.decoder.JSONDecodeError: + continue sorted_latest = [] for i in sorted(latest, key=lambda x: x[2]['time'], reverse=True): @@ -295,7 +300,11 @@ def send_message(id_to, msg, id_us): Function for sending/recieving a message. """ ours = find_user_by_id(id_us) + if not ours: + return them = find_user_by_id(id_to) + if not them: + return makedirs(join('messages', ours['email']), exist_ok=True) our_msgpath = join('messages', ours['email'], them['email']) makedirs(join('messages', them['email']), exist_ok=True)