only store messages if they're meant to be stored

This commit is contained in:
Artem Titoulenko 2021-12-26 21:56:59 -05:00
parent 7af076ff6a
commit 9a28132dae
3 changed files with 26 additions and 9 deletions

View file

@ -68,9 +68,11 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) {
log.Printf("sent message %d to user %s at %s", message.Cookie, message.To, s.RemoteAddr())
}
if message.StoreOffline {
if err := message.MarkDelivered(context.Background(), db); err != nil {
log.Panicf("could not mark message %d as delivered: %s", message.Cookie, err.Error())
}
}
} else {
log.Printf("could not find session for user %s", message.To)
}

View file

@ -16,6 +16,7 @@ type Message struct {
From string
To string
Contents string
StoreOffline bool
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp"`
DeliveredAt time.Time `bun:",nullzero"`
}
@ -26,6 +27,7 @@ func InsertMessage(ctx context.Context, db *bun.DB, cookie uint64, from string,
From: from,
To: to,
Contents: contents,
StoreOffline: true,
}
if _, err := db.NewInsert().Model(msg).Exec(ctx, msg); err != nil {
return nil, errors.Wrap(err, "could not update user")

View file

@ -176,10 +176,23 @@ func (icbm *ICBM) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC)
return ctx, errors.New("read insufficient data from message fragment")
}
message, err := models.InsertMessage(ctx, db, msgID, user.Username, to, string(messageContents))
var message *models.Message
// TLV 0x6 is the client telling the server to store the message if the recipient is offline
saveofflineTLV := oscar.FindTLV(tlvs, 6)
if saveofflineTLV != nil {
message, err = models.InsertMessage(ctx, db, msgID, user.Username, to, string(messageContents))
if err != nil {
return ctx, errors.Wrap(err, "could not insert message")
}
} else {
message = &models.Message{
Cookie: msgID,
From: user.Username,
To: to,
Contents: string(messageContents),
}
}
// Fire the message off into the communication channel to get delivered
icbm.CommCh <- message