Chore: Refactor MarkRead and MarkUnread functions to only broadcast changes of modified messages

This commit is contained in:
Ralph Slooten
2026-05-09 16:13:05 +12:00
parent 8bc966e618
commit 40c5936f79

View File

@@ -549,15 +549,23 @@ func LatestID(r *http.Request) (string, error) {
// MarkRead will mark a message as read
func MarkRead(ids []string) error {
for _, id := range ids {
_, err := sqlf.Update(tenant("mailbox")).
res, err := sqlf.Update(tenant("mailbox")).
Set("Read", 1).
Where("ID = ?", id).
Where("Read = ?", 0).
ExecAndClose(context.Background(), db)
if err == nil {
logger.Log().Debugf("[db] marked message %s as read", id)
if err != nil {
continue
}
affected, err := res.RowsAffected()
if err != nil || affected == 0 {
continue
}
logger.Log().Debugf("[db] marked message %s as read", id)
d := struct {
ID string
Read bool
@@ -571,6 +579,41 @@ func MarkRead(ids []string) error {
return nil
}
// MarkUnread will mark a message as unread
func MarkUnread(ids []string) error {
for _, id := range ids {
res, err := sqlf.Update(tenant("mailbox")).
Set("Read", 0).
Where("ID = ?", id).
Where("Read = ?", 1).
ExecAndClose(context.Background(), db)
if err != nil {
continue
}
affected, err := res.RowsAffected()
if err != nil || affected == 0 {
continue
}
logger.Log().Debugf("[db] marked message %s as unread", id)
dbLastAction = time.Now()
d := struct {
ID string
Read bool
}{ID: id, Read: false}
websockets.Broadcast("update", d)
}
BroadcastMailboxStats()
return nil
}
// MarkAllRead will mark all messages as read
func MarkAllRead() error {
var (
@@ -621,33 +664,6 @@ func MarkAllUnread() error {
return nil
}
// MarkUnread will mark a message as unread
func MarkUnread(ids []string) error {
for _, id := range ids {
_, err := sqlf.Update(tenant("mailbox")).
Set("Read", 0).
Where("ID = ?", id).
ExecAndClose(context.Background(), db)
if err == nil {
logger.Log().Debugf("[db] marked message %s as unread", id)
}
dbLastAction = time.Now()
d := struct {
ID string
Read bool
}{ID: id, Read: false}
websockets.Broadcast("update", d)
}
BroadcastMailboxStats()
return nil
}
// DeleteMessages deletes one or more messages in bulk
func DeleteMessages(ids []string) error {
if len(ids) == 0 {