From 5505928e5b3404dd77b10bd43209388a6274c71e Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 15 Jun 2017 17:47:45 +0000 Subject: [PATCH] Resolve messages duplicates in database and order messages by timestamp --- webapp/titanembeds/database/messages.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/webapp/titanembeds/database/messages.py b/webapp/titanembeds/database/messages.py index fd7d131..0142a75 100644 --- a/webapp/titanembeds/database/messages.py +++ b/webapp/titanembeds/database/messages.py @@ -31,11 +31,15 @@ class Messages(db.Model): def get_channel_messages(channel_id, after_snowflake=None): if not after_snowflake: - q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.id.desc()).limit(50) + q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50) else: - q = db.session.query(Messages).filter(cast(Messages.channel_id, db.Integer) == int(channel_id)).filter(Messages.message_id > after_snowflake).order_by(Messages.id.desc()).limit(50) + q = db.session.query(Messages).filter(cast(Messages.channel_id, db.Integer) == int(channel_id)).filter(Messages.message_id > after_snowflake).order_by(Messages.timestamp.desc()).limit(50) msgs = [] + snowflakes = [] for x in q: + if x.message_id in snowflakes: + continue + snowflakes.append(x.message_id) msgs.append({ "attachments": json.loads(x.attachments), "timestamp": x.timestamp,