Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
148 lines
5.9 KiB
Go
148 lines
5.9 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 {
|
|
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) (Notification, bool, error) {
|
|
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=? AND recipient_uuid=? AND event_uuid=? AND kind=?`, 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
|
|
}
|
|
_, 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(?,?,?,?,?,'unread',?,?,?,?,0)`, 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
|
|
}
|
|
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) ([]Notification, string, error) {
|
|
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=? AND recipient_uuid=?`
|
|
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=? AND domain_uuid=? AND recipient_uuid=?)`
|
|
args = append(args, before, domainUUID, recipientUUID)
|
|
}
|
|
query += ` ORDER BY created_at_ms DESC,uuid DESC LIMIT ?`
|
|
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) error {
|
|
if domainUUID == "" || recipientUUID == "" || notificationUUID == "" {
|
|
return ErrNotificationInput
|
|
}
|
|
result, err := s.DB.Exec(`UPDATE notification_inbox SET status='read',read_at_ms=? WHERE uuid=? AND domain_uuid=? AND recipient_uuid=? 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=? AND domain_uuid=? AND recipient_uuid=?`, 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) (Notification, error) {
|
|
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=? AND recipient_uuid=? AND uuid=?`, domainUUID, recipientUUID, notificationUUID), &item)
|
|
return item, err
|
|
}
|