tableChatReadMarkers.go (984B)
1 package database 2 3 import ( 4 "time" 5 ) 6 7 // ChatReadMarker the "read marker" is a line displayed in the chat that indicate the last time you sent a message. 8 // Or if you clicked "update read marker" button, indicate the position in the messages when that was done. 9 // This is useful to quickly visually find the last message you actually read. 10 type ChatReadMarker struct { 11 UserID UserID 12 RoomID RoomID 13 ReadAt time.Time 14 } 15 16 func (d *DkfDB) GetUserReadMarker(userID UserID, roomID RoomID) (out ChatReadMarker, err error) { 17 err = d.db.First(&out, "user_id = ? AND room_id = ?", userID, roomID).Error 18 return 19 } 20 21 func (d *DkfDB) UpdateChatReadMarker(userID UserID, roomID RoomID) { 22 now := time.Now() 23 res := d.db.Table("chat_read_markers").Where("user_id = ? AND room_id = ?", userID, roomID).Update("read_at", now) 24 if res.RowsAffected == 0 { 25 d.db.Create(ChatReadMarker{UserID: userID, RoomID: roomID, ReadAt: now}) 26 } 27 MsgPubSub.Pub("readmarker_"+userID.String(), ChatMessageType{}) 28 }