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

View file

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

View file

@ -176,9 +176,22 @@ func (icbm *ICBM) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC)
return ctx, errors.New("read insufficient data from message fragment") 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
if err != nil {
return ctx, errors.Wrap(err, "could not insert 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 // Fire the message off into the communication channel to get delivered