remove limit logic and use reddit's before again

This commit is contained in:
Andre Medeiros 2021-07-15 12:05:01 -04:00
parent 20fe4d7ddb
commit 412bc04d77

View file

@ -248,7 +248,10 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
nc.logger.WithFields(logrus.Fields{ nc.logger.WithFields(logrus.Fields{
"accountID": id, "accountID": id,
}).Debug("fetching message inbox") }).Debug("fetching message inbox")
msgs, err := rac.MessageInbox(reddit.WithQuery("limit", "10")) msgs, err := rac.MessageInbox(
reddit.WithQuery("limit", "10"),
reddit.WithQuery("before", account.LastMessageID),
)
if err != nil { if err != nil {
nc.logger.WithFields(logrus.Fields{ nc.logger.WithFields(logrus.Fields{
@ -259,28 +262,16 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
} }
// Figure out where we stand // Figure out where we stand
if msgs.Count == 0 || msgs.Children[0].FullName() == account.LastMessageID { if msgs.Count == 0 {
nc.logger.WithFields(logrus.Fields{ nc.logger.WithFields(logrus.Fields{
"accountID": id, "accountID": id,
}).Debug("no new messages, bailing early") }).Debug("no new messages, bailing early")
return return
} }
// Find which one is the oldest we haven't notified on
oldest := 0
for i, t := range msgs.Children {
if t.FullName() == account.LastMessageID {
break
}
oldest = i
}
tt := msgs.Children[:oldest+1]
nc.logger.WithFields(logrus.Fields{ nc.logger.WithFields(logrus.Fields{
"accountID": id, "accountID": id,
"count": len(tt), "count": msgs.Count,
}).Debug("fetched messages") }).Debug("fetched messages")
// Set latest message we alerted on // Set latest message we alerted on
@ -289,7 +280,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
UPDATE accounts UPDATE accounts
SET last_message_id = $1 SET last_message_id = $1
WHERE id = $2` WHERE id = $2`
_, err := tx.Exec(ctx, stmt, tt[0].FullName(), account.ID) _, err := tx.Exec(ctx, stmt, msgs.Children[0].FullName(), account.ID)
return err return err
}); err != nil { }); err != nil {
nc.logger.WithFields(logrus.Fields{ nc.logger.WithFields(logrus.Fields{
@ -329,11 +320,11 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
} }
// Iterate backwards so we notify from older to newer // Iterate backwards so we notify from older to newer
for i := len(tt) - 1; i >= 0; i-- { for i := msgs.Count - 1; i >= 0; i-- {
msg := tt[i] msg := msgs.Children[i]
notification := &apns2.Notification{} notification := &apns2.Notification{}
notification.Topic = "com.christianselig.Apollo" notification.Topic = "com.christianselig.Apollo"
notification.Payload = payloadFromMessage(account, msg, len(tt)) notification.Payload = payloadFromMessage(account, msg, msgs.Count)
for _, device := range devices { for _, device := range devices {
notification.DeviceToken = device.APNSToken notification.DeviceToken = device.APNSToken