Resolve messages duplicates in database and order messages by timestamp

This commit is contained in:
Jeremy Zhang 2017-06-15 17:47:45 +00:00
parent 38df1a8ed0
commit 5505928e5b

View File

@ -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,