feat(websocket): add notification event with XORM AfterInsert dispatch

Add NotificationCreatedEvent that fires automatically when a
DatabaseNotification is inserted, using XORM's AfterInsertProcessor
interface. The AfterInsert hook dispatches the event after the row
is persisted, without callers needing to manage DispatchOnCommit or
DispatchPending.

The WebSocket listener subscribes to this event, reloads the
notification from the database (ensuring accurate timestamps), and
pushes it to connected clients subscribed to the notification.created
event. Dispatch errors are logged rather than propagated since the
DB notification is already committed at that point.
This commit is contained in:
kolaente 2026-04-02 18:18:27 +02:00 committed by kolaente
parent 55ea5bd966
commit f5385c574e
5 changed files with 140 additions and 1 deletions

View File

@ -19,6 +19,9 @@ package notifications
import (
"time"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"xorm.io/xorm"
)
@ -43,6 +46,18 @@ type DatabaseNotification struct {
Created time.Time `xorm:"created not null" json:"created"`
}
// AfterInsert is called by XORM after the row is inserted. For transactional
// sessions this runs during Commit(), guaranteeing the row is persisted before
// the event fires.
func (d *DatabaseNotification) AfterInsert() {
if err := events.Dispatch(&NotificationCreatedEvent{
NotificationID: d.ID,
UserID: d.NotifiableID,
}); err != nil {
log.Errorf("Failed to dispatch notification created event for notification %d: %v", d.ID, err)
}
}
// TableName resolves to a better table name for notifications
func (d *DatabaseNotification) TableName() string {
return "notifications"
@ -67,6 +82,19 @@ func GetNotificationsForUser(s *xorm.Session, notifiableID int64, limit, start i
return notifications, len(notifications), total, err
}
// GetNotificationByID returns a single notification by its ID.
func GetNotificationByID(s *xorm.Session, id int64) (*DatabaseNotification, error) {
n := &DatabaseNotification{}
has, err := s.ID(id).Get(n)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return n, nil
}
func GetNotificationsForNameAndUser(s *xorm.Session, notifiableID int64, event string, subjectID int64) (notifications []*DatabaseNotification, err error) {
notifications = []*DatabaseNotification{}
err = s.Where("notifiable_id = ? AND name = ? AND subject_id = ?", notifiableID, event, subjectID).

View File

@ -0,0 +1,33 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package notifications
import "code.vikunja.io/api/pkg/events"
// NotificationCreatedEvent is dispatched after a notification is committed to the database.
// The listener reloads the full record from the DB to get accurate timestamps.
type NotificationCreatedEvent struct {
NotificationID int64 `json:"notification_id"`
UserID int64 `json:"user_id"`
}
// Name returns the event name.
func (n *NotificationCreatedEvent) Name() string {
return "notification.created"
}
var _ events.Event = (*NotificationCreatedEvent)(nil)

View File

@ -22,6 +22,7 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/i18n"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/mail"
@ -54,5 +55,6 @@ func TestMain(m *testing.M) {
SetupTests()
mail.Fake()
events.Fake()
os.Exit(m.Run())
}

View File

@ -118,7 +118,7 @@ func notifyDB(notifiable Notifiable, notification Notification, existingSession
dbNotification := &DatabaseNotification{
NotifiableID: notifiable.RouteForDB(),
Notification: content,
Notification: json.RawMessage(content),
Name: notification.Name(),
}

76
pkg/websocket/listener.go Normal file
View File

@ -0,0 +1,76 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package websocket
import (
"encoding/json"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/notifications"
"github.com/ThreeDotsLabs/watermill/message"
)
// NotificationListener pushes new notifications to WebSocket clients.
type NotificationListener struct{}
// Name returns the listener name.
func (n *NotificationListener) Name() string {
return "websocket.notification.push"
}
// Handle processes a notification created event, reloads the notification
// from the database (to get accurate timestamps), and pushes it to the
// relevant WebSocket connections.
func (n *NotificationListener) Handle(msg *message.Message) error {
var event notifications.NotificationCreatedEvent
if err := json.Unmarshal(msg.Payload, &event); err != nil {
return err
}
hub := GetHub()
if hub == nil {
log.Warningf("WebSocket: hub not initialized, skipping notification push")
return nil
}
s := db.NewSession()
defer s.Close()
dbNotification, err := notifications.GetNotificationByID(s, event.NotificationID)
if err != nil {
log.Errorf("WebSocket: failed to load notification %d: %v", event.NotificationID, err)
return nil
}
if dbNotification == nil {
log.Warningf("WebSocket: notification %d not found, skipping push", event.NotificationID)
return nil
}
hub.PublishForUser(event.UserID, "notification.created", dbNotification)
return nil
}
// RegisterListeners registers WebSocket event listeners.
func RegisterListeners() {
events.RegisterListener(
(&notifications.NotificationCreatedEvent{}).Name(),
&NotificationListener{},
)
}