178 lines
7.7 KiB
Go
178 lines
7.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/ordoschnellic"
|
|
)
|
|
|
|
type Notification = ordoschnellic.Notification
|
|
|
|
var ErrNotificationInput = ordoschnellic.ErrNotificationInput
|
|
|
|
func (s *Store) EnsureNotificationSchema() error {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
if err := s.ensurePostgreSQLNotificationInbox(ctx); err != nil {
|
|
return err
|
|
}
|
|
return s.ensurePostgreSQLPushStorage(ctx)
|
|
}
|
|
for _, statement := range []string{
|
|
`CREATE TABLE IF NOT EXISTS notification_inbox (
|
|
uuid TEXT PRIMARY KEY,
|
|
domain_uuid TEXT NOT NULL,
|
|
recipient_uuid TEXT NOT NULL,
|
|
event_uuid TEXT NOT NULL DEFAULT '',
|
|
kind TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'unread',
|
|
request_uuid TEXT NOT NULL DEFAULT '',
|
|
href TEXT NOT NULL DEFAULT '',
|
|
message TEXT NOT NULL DEFAULT '',
|
|
created_at_ms INTEGER NOT NULL,
|
|
read_at_ms INTEGER NOT NULL DEFAULT 0,
|
|
UNIQUE(domain_uuid,recipient_uuid,event_uuid,kind)
|
|
)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_notification_recipient ON notification_inbox(domain_uuid,recipient_uuid,status,created_at_ms DESC,uuid DESC)`,
|
|
`CREATE INDEX IF NOT EXISTS idx_notification_timeline ON notification_inbox(domain_uuid,recipient_uuid,created_at_ms DESC,uuid DESC)`,
|
|
} {
|
|
if _, err := s.DB.Exec(statement); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.ensureNotificationPushSchema()
|
|
}
|
|
|
|
func (s *Store) CreateNotificationTx(tx *sql.Tx, notification Notification) (result0 Notification, result1 bool, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
if s.ReadOnly {
|
|
return Notification{}, false, ErrReadOnly
|
|
}
|
|
notification.DomainUUID = strings.TrimSpace(notification.DomainUUID)
|
|
notification.RecipientUUID = strings.TrimSpace(notification.RecipientUUID)
|
|
notification.Kind = strings.TrimSpace(notification.Kind)
|
|
if notification.DomainUUID == "" || notification.RecipientUUID == "" || notification.Kind == "" {
|
|
return Notification{}, false, ErrNotificationInput
|
|
}
|
|
if notification.UUID == "" {
|
|
notification.UUID = uuid.NewString()
|
|
}
|
|
if notification.CreatedAtMS == 0 {
|
|
notification.CreatedAtMS = time.Now().UnixMilli()
|
|
}
|
|
var existing Notification
|
|
err := scanNotification(tx.QueryRow(`SELECT uuid,domain_uuid,recipient_uuid,event_uuid,kind,status,request_uuid,href,message,created_at_ms,read_at_ms FROM notification_inbox WHERE domain_uuid=`+s.sqlParam(1)+` AND recipient_uuid=`+s.sqlParam(2)+` AND event_uuid=`+s.sqlParam(3)+` AND kind=`+s.sqlParam(4)+``, notification.DomainUUID, notification.RecipientUUID, notification.EventUUID, notification.Kind), &existing)
|
|
if err == nil {
|
|
return existing, false, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return Notification{}, false, err
|
|
}
|
|
result, err := tx.Exec(`INSERT INTO notification_inbox(uuid,domain_uuid,recipient_uuid,event_uuid,kind,status,request_uuid,href,message,created_at_ms,read_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,'unread',`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,`+s.sqlParam(9)+`,0) ON CONFLICT(domain_uuid,recipient_uuid,event_uuid,kind) DO NOTHING`, notification.UUID, notification.DomainUUID, notification.RecipientUUID, notification.EventUUID, notification.Kind, notification.RequestUUID, notification.Href, notification.Message, notification.CreatedAtMS)
|
|
if err != nil {
|
|
return Notification{}, false, err
|
|
}
|
|
count, err := result.RowsAffected()
|
|
if err != nil {
|
|
return Notification{}, false, err
|
|
}
|
|
if count == 0 {
|
|
err = scanNotification(tx.QueryRow(`SELECT `+notificationColumns+` FROM notification_inbox WHERE domain_uuid=`+s.sqlParam(1)+` AND recipient_uuid=`+s.sqlParam(2)+` AND event_uuid=`+s.sqlParam(3)+` AND kind=`+s.sqlParam(4), notification.DomainUUID, notification.RecipientUUID, notification.EventUUID, notification.Kind), &existing)
|
|
return existing, false, err
|
|
}
|
|
notification.Status = "unread"
|
|
return notification, true, nil
|
|
}
|
|
|
|
const notificationColumns = `uuid,domain_uuid,recipient_uuid,event_uuid,kind,status,request_uuid,href,message,created_at_ms,read_at_ms`
|
|
|
|
func scanNotification(scanner interface{ Scan(...any) error }, item *Notification) error {
|
|
return scanner.Scan(&item.UUID, &item.DomainUUID, &item.RecipientUUID, &item.EventUUID, &item.Kind, &item.Status, &item.RequestUUID, &item.Href, &item.Message, &item.CreatedAtMS, &item.ReadAtMS)
|
|
}
|
|
|
|
func (s *Store) ListNotifications(ctx context.Context, domainUUID, recipientUUID string, unreadOnly bool, before string, limit int) (result0 []Notification, result1 string, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
if strings.TrimSpace(domainUUID) == "" || strings.TrimSpace(recipientUUID) == "" {
|
|
return nil, "", ErrNotificationInput
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 50
|
|
}
|
|
query := `SELECT ` + notificationColumns + ` FROM notification_inbox WHERE domain_uuid=` + s.sqlParam(1) + ` AND recipient_uuid=` + s.sqlParam(2) + ``
|
|
args := []any{domainUUID, recipientUUID}
|
|
if unreadOnly {
|
|
query += ` AND status='unread'`
|
|
}
|
|
if before != "" {
|
|
query += ` AND (created_at_ms,uuid)<(SELECT created_at_ms,uuid FROM notification_inbox WHERE uuid=` + s.sqlParam(len(args)+1) + ` AND domain_uuid=` + s.sqlParam(len(args)+2) + ` AND recipient_uuid=` + s.sqlParam(len(args)+3) + `)`
|
|
args = append(args, before, domainUUID, recipientUUID)
|
|
}
|
|
query += ` ORDER BY created_at_ms DESC,uuid DESC LIMIT ` + s.sqlParam(len(args)+1) + ``
|
|
args = append(args, limit+1)
|
|
rows, err := s.DB.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer rows.Close()
|
|
items := make([]Notification, 0, limit)
|
|
for rows.Next() {
|
|
var item Notification
|
|
if err := scanNotification(rows, &item); err != nil {
|
|
return nil, "", err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, "", err
|
|
}
|
|
next := ""
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
next = items[len(items)-1].UUID
|
|
}
|
|
return items, next, nil
|
|
}
|
|
|
|
func (s *Store) MarkNotificationRead(domainUUID, recipientUUID, notificationUUID string) (resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
if domainUUID == "" || recipientUUID == "" || notificationUUID == "" {
|
|
return ErrNotificationInput
|
|
}
|
|
result, err := s.DB.Exec(`UPDATE notification_inbox SET status='read',read_at_ms=`+s.sqlParam(1)+` WHERE uuid=`+s.sqlParam(2)+` AND domain_uuid=`+s.sqlParam(3)+` AND recipient_uuid=`+s.sqlParam(4)+` AND status='unread'`, time.Now().UnixMilli(), notificationUUID, domainUUID, recipientUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count, _ := result.RowsAffected(); count == 0 {
|
|
var exists int
|
|
if err := s.DB.QueryRow(`SELECT COUNT(*) FROM notification_inbox WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND recipient_uuid=`+s.sqlParam(3)+``, notificationUUID, domainUUID, recipientUUID).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if exists == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) GetNotification(ctx context.Context, domainUUID, recipientUUID, notificationUUID string) (result0 Notification, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
var item Notification
|
|
if strings.TrimSpace(domainUUID) == "" || strings.TrimSpace(recipientUUID) == "" || strings.TrimSpace(notificationUUID) == "" {
|
|
return item, ErrNotificationInput
|
|
}
|
|
err := scanNotification(s.DB.QueryRowContext(ctx, `SELECT `+notificationColumns+` FROM notification_inbox WHERE domain_uuid=`+s.sqlParam(1)+` AND recipient_uuid=`+s.sqlParam(2)+` AND uuid=`+s.sqlParam(3)+``, domainUUID, recipientUUID, notificationUUID), &item)
|
|
return item, err
|
|
}
|