feat: add audited work records and controlled digest retries
Build and publish container images / images (push) Successful in 12m27s
Build and publish container images / images (push) Successful in 12m27s
This commit is contained in:
1 parent
6e0aecdfb3
commit
e65ca56f92
33 files changed
+2262
-114
No files matched your search
@@ -22,7 +22,7 @@ type Manifest struct {
|
||||
func Catalog() []Manifest {
|
||||
return []Manifest{
|
||||
{ID: "organizer.contacts", APIVersion: 1, Version: "0.2.0", Kind: "contacts", Scope: "domain", NameKey: "contacts.title", DescriptionKey: "contacts.help", Capabilities: []string{"contacts.own.read", "contacts.own.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
|
||||
{ID: "organizer.tasks", APIVersion: 1, Version: "0.12.0", Kind: "tasks", Scope: "domain", NameKey: "tasks.title", DescriptionKey: "tasks.help", Capabilities: []string{"tasks.own.read", "tasks.own.write"}, Events: []string{"organizer.task.changed", "organizer.project.changed", "task.digest.deliver"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+background+index"},
|
||||
{ID: "organizer.tasks", APIVersion: 1, Version: "0.14.0", Kind: "tasks", Scope: "domain", NameKey: "tasks.title", DescriptionKey: "tasks.help", Capabilities: []string{"tasks.own.read", "tasks.own.write"}, Events: []string{"organizer.task.changed", "organizer.project.changed", "task.digest.deliver"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+background+index"},
|
||||
{ID: "organizer.calendar", APIVersion: 1, Version: "0.2.0", Kind: "calendar", Scope: "domain", NameKey: "calendar.title", DescriptionKey: "calendar.help", Capabilities: []string{"calendar.read", "calendar.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
|
||||
{ID: "content.diary", APIVersion: 1, Version: "0.1.0", Kind: "diary", Scope: "domain", NameKey: "periodical.diary", DescriptionKey: "periodical.diary_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "diary", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
|
||||
{ID: "content.blog", APIVersion: 1, Version: "0.1.0", Kind: "blog", Scope: "domain", NameKey: "periodical.blog", DescriptionKey: "periodical.blog_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "blog", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
|
||||
|
||||
@@ -108,6 +108,8 @@ func organizerSection(path string) string {
|
||||
}
|
||||
|
||||
func init() {
|
||||
translations["en"]["task.work_history"] = "Work history"
|
||||
translations["ja"]["task.work_history"] = "作業記録"
|
||||
for lang, entries := range map[string]map[string]string{
|
||||
"en": {"organizer.title": "Organizer", "organizer.dashboard": "Dashboard", "organizer.settings": "Personal settings", "organizer.contacts": "Contacts", "organizer.private": "Preferences apply only to you in this Domain. System administration is separate.", "organizer.calendar_defaults": "Default calendars", "organizer.open": "Open"},
|
||||
"ja": {"organizer.title": "オーガナイザー", "organizer.dashboard": "ダッシュボード", "organizer.settings": "個人設定", "organizer.contacts": "連絡先", "organizer.private": "このドメイン内の自分だけに適用する設定です。システムの管理設定とは別です。", "organizer.calendar_defaults": "既定のカレンダー", "organizer.open": "開く"},
|
||||
|
||||
@@ -38,67 +38,110 @@ func sendTaskDigestSMTP(ctx context.Context, config store.AuthProviderConfig, re
|
||||
func (s *Server) deliverTaskDigests(ctx context.Context, job *store.EventJob) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
if _, err := s.store.MaintainTaskDigests(ctx, time.Now()); err != nil {
|
||||
return err
|
||||
}
|
||||
queued, err := s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
due, err := s.store.DueTaskDigests(ctx, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sent, uncertain := 0, 0
|
||||
for _, candidate := range due {
|
||||
sent, uncertain, checked := 0, 0, 0
|
||||
deliver := func(claim store.TaskDigestClaim) error {
|
||||
state, reason := s.deliverTaskDigest(ctx, claim)
|
||||
if state == "sent" {
|
||||
sent++
|
||||
}
|
||||
if state == "uncertain" {
|
||||
uncertain++
|
||||
}
|
||||
checked++
|
||||
finish, stop := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer stop()
|
||||
err := s.store.FinishTaskDigestReason(finish, claim, state, reason, time.Now())
|
||||
// A reclaimed generation cannot complete a newer attempt.
|
||||
if errors.Is(err, store.ErrTaskConflict) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
// Alternate automatic delivery and manual retries within the shared time
|
||||
// budget, so a retry backlog cannot monopolize scheduled deliveries.
|
||||
for i := 0; i < max(len(due), len(queued)); i++ {
|
||||
for _, manual := range []bool{false, true} {
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
var claim store.TaskDigestClaim
|
||||
var e error
|
||||
if manual {
|
||||
if i >= len(queued) {
|
||||
continue
|
||||
}
|
||||
claim, e = s.store.ClaimQueuedTaskDigest(ctx, queued[i], time.Now())
|
||||
} else {
|
||||
if i >= len(due) {
|
||||
continue
|
||||
}
|
||||
claim, e = s.store.ClaimTaskDigest(ctx, due[i], time.Now())
|
||||
}
|
||||
if errors.Is(e, sql.ErrNoRows) {
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = deliver(claim); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
claim, e := s.store.ClaimTaskDigest(ctx, candidate, time.Now())
|
||||
if errors.Is(e, sql.ErrNoRows) {
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
state := "cancelled"
|
||||
if s.systemExtensionEnabled(claim.Domain, "organizer.tasks") {
|
||||
config, e := s.store.AuthProviderConfig(claim.Domain, "smtp")
|
||||
if e == nil && config.Enabled {
|
||||
items, next, e := s.store.ListTasks(ctx, claim.Domain, claim.Owner, store.TaskQuery{Kind: "task", View: "today", Today: claim.Day, Limit: 100})
|
||||
if e == nil {
|
||||
var body strings.Builder
|
||||
host, _ := s.store.GetDomainEffectiveHost(claim.Domain)
|
||||
base := ""
|
||||
if host != "" && !strings.ContainsAny(host, "/\\\r\n\t @") {
|
||||
base = "https://" + host
|
||||
}
|
||||
fmt.Fprintf(&body, "Tasks / やることリスト — %s (%s)\n\n", claim.Day, claim.Timezone)
|
||||
if len(items) == 0 {
|
||||
body.WriteString("No tasks due or planned today. / 今日の対象タスクはありません。\n")
|
||||
}
|
||||
for _, item := range items {
|
||||
fmt.Fprintf(&body, "- %s\n due/期限: %s | planned/予定: %s | estimate/見積: %d min\n %s/v/plan/task/%s\n", item.Title, item.DueOn, item.PlannedOn, item.Estimate, base, item.UUID)
|
||||
}
|
||||
if next != "" {
|
||||
body.WriteString("\nFirst 100 items only. / 最初の100件です。続きはタスク画面で確認してください。\n")
|
||||
}
|
||||
// Check consent and current account email immediately before send.
|
||||
if recipient, e := s.store.TaskDigestRecipient(ctx, claim); e == nil {
|
||||
state = "uncertain"
|
||||
if e = sendTaskDigestSMTP(ctx, config, recipient, claim.Day, body.String()); e == nil {
|
||||
state = "sent"
|
||||
sent++
|
||||
} else {
|
||||
uncertain++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Persist delivery outcome even if the scheduler context was cancelled.
|
||||
finish, stop := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
e = s.store.FinishTaskDigest(finish, claim, state)
|
||||
stop()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if len(due) > 0 {
|
||||
s.addEventProgressLog(ctx, job, fmt.Sprintf("task digest checked=%d sent=%d uncertain=%d", len(due), sent, uncertain))
|
||||
if checked > 0 {
|
||||
s.addEventProgressLog(ctx, job, fmt.Sprintf("task digest checked=%d sent=%d uncertain=%d", checked, sent, uncertain))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) deliverTaskDigest(ctx context.Context, claim store.TaskDigestClaim) (string, string) {
|
||||
if !s.systemExtensionEnabled(claim.Domain, "organizer.tasks") {
|
||||
return "cancelled", "extension_disabled"
|
||||
}
|
||||
config, err := s.store.AuthProviderConfig(claim.Domain, "smtp")
|
||||
if err != nil || !config.Enabled {
|
||||
return "cancelled", "smtp_unavailable"
|
||||
}
|
||||
items, next, err := s.store.ListTasks(ctx, claim.Domain, claim.Owner, store.TaskQuery{Kind: "task", View: "today", Today: claim.Day, Limit: 100})
|
||||
if err != nil {
|
||||
return "cancelled", "tasks_unavailable"
|
||||
}
|
||||
var body strings.Builder
|
||||
host, _ := s.store.GetDomainEffectiveHost(claim.Domain)
|
||||
base := ""
|
||||
if host != "" && !strings.ContainsAny(host, "/\\\r\n\t @") {
|
||||
base = "https://" + host
|
||||
}
|
||||
fmt.Fprintf(&body, "Tasks / やることリスト — %s (%s)\n\n", claim.Day, claim.Timezone)
|
||||
if len(items) == 0 {
|
||||
body.WriteString("No tasks due or planned today. / 今日の対象タスクはありません。\n")
|
||||
}
|
||||
for _, item := range items {
|
||||
fmt.Fprintf(&body, "- %s\n due/期限: %s | planned/予定: %s | estimate/見積: %d min\n %s/v/plan/task/%s\n", item.Title, item.DueOn, item.PlannedOn, item.Estimate, base, item.UUID)
|
||||
}
|
||||
if next != "" {
|
||||
body.WriteString("\nFirst 100 items only. / 最初の100件です。続きはタスク画面で確認してください。\n")
|
||||
}
|
||||
recipient, err := s.store.TaskDigestRecipient(ctx, claim)
|
||||
if err != nil {
|
||||
return "cancelled", "consent_changed"
|
||||
}
|
||||
if err = sendTaskDigestSMTP(ctx, config, recipient, claim.Day, body.String()); err != nil {
|
||||
return "uncertain", "transport_uncertain"
|
||||
}
|
||||
return "sent", ""
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleTaskDigestHistory(w http.ResponseWriter, r *http.Request, actor string, active bool, parts []string) {
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
if len(parts) == 1 && r.Method == "GET" {
|
||||
limit := 50
|
||||
if raw := r.URL.Query().Get("limit"); raw != "" {
|
||||
v, e := strconv.Atoi(raw)
|
||||
if e != nil {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
limit = v
|
||||
}
|
||||
result, err := s.store.TaskDigestHistory(r.Context(), domain, actor, r.URL.Query().Get("after"), limit, time.Now())
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, result)
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "retry" && r.Method == "POST" {
|
||||
if !active {
|
||||
taskError(w, store.ErrAccessDenied)
|
||||
return
|
||||
}
|
||||
var input store.TaskDigestRetry
|
||||
if !taskJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
if id, err := s.store.TaskDigestRetryReceipt(r.Context(), domain, actor, input); err == nil {
|
||||
writeEventJSON(w, 202, map[string]string{"request_id": id})
|
||||
return
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
principal, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
taskError(w, store.ErrAccessDenied)
|
||||
return
|
||||
}
|
||||
if _, err = smtpMailbox(principal.Email); err != nil {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "email_required"})
|
||||
return
|
||||
}
|
||||
config, err := s.store.AuthProviderConfig(domain, "smtp")
|
||||
if err != nil || !config.Enabled {
|
||||
writeEventJSON(w, 422, map[string]string{"error": "smtp_unavailable"})
|
||||
return
|
||||
}
|
||||
id, err := s.store.QueueTaskDigestRetry(r.Context(), domain, actor, input, time.Now())
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 202, map[string]string{"request_id": id})
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
w.Header().Set("Allow", "GET")
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "retry" {
|
||||
w.Header().Set("Allow", "POST")
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
@@ -0,0 +1,447 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func digestHistoryFixture(t *testing.T) (*Server, string, string, string, time.Time, store.TaskDigestClaim) {
|
||||
t.Helper()
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
p := store.TaskPreferences{Complexity: 1, DigestEnabled: true, DigestTime: "00:00", Timezone: "UTC"}
|
||||
if _, err := s.store.SaveTaskPreferences(ctx, domain, actor, p, now.Add(-24*time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`UPDATE principals SET email='reader@example.test' WHERE uuid=?`, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := s.store.ClaimTaskDigest(ctx, store.TaskDigestDue{Domain: domain, Owner: actor}, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, token, domain, actor, now, c
|
||||
}
|
||||
|
||||
func TestTaskDigestHistoryRetryGenerationAndConsent(t *testing.T) {
|
||||
s, token, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
if recipient, err := s.store.TaskDigestRecipient(ctx, c); err != nil || recipient != "reader@example.test" {
|
||||
t.Fatal(recipient, err)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
|
||||
post := func(v store.TaskDigestRetry) int {
|
||||
return taskRequest(s, token, "POST", "digest-history/retry", v, "").Code
|
||||
}
|
||||
if got := post(input); got != 412 {
|
||||
t.Fatal("retried active delivery", got)
|
||||
}
|
||||
if err := s.store.FinishTaskDigest(ctx, c, "sent"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := post(input); got != 422 {
|
||||
t.Fatal("missing duplicate acknowledgment", got)
|
||||
}
|
||||
input.DuplicateAcknowledged = true
|
||||
var wg sync.WaitGroup
|
||||
results := make(chan int, 2)
|
||||
for range 2 {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); results <- post(input) }()
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
for code := range results {
|
||||
if code != 202 {
|
||||
t.Fatal("idempotent retry", code)
|
||||
}
|
||||
}
|
||||
q, err := s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil || len(q) != 1 || q[0].AttemptUUID != input.RequestID {
|
||||
t.Fatal(q, err)
|
||||
}
|
||||
if err = s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: false, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := post(input); got != 202 {
|
||||
t.Fatal("accepted receipt lost after SMTP disabled", got)
|
||||
}
|
||||
if err = s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
changed := input
|
||||
changed.DuplicateAcknowledged = false
|
||||
if got := post(changed); got != 412 {
|
||||
t.Fatal("changed replay", got)
|
||||
}
|
||||
changed = input
|
||||
changed.RequestID = uuid.NewString()
|
||||
if got := post(changed); got != 412 {
|
||||
t.Fatal("second queued request", got)
|
||||
}
|
||||
newClaim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.ClaimQueuedTaskDigest(ctx, q[0], now); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("double claim", err)
|
||||
}
|
||||
if err = s.store.FinishTaskDigest(ctx, c, "cancelled"); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal("old generation finished new attempt", err)
|
||||
}
|
||||
if _, err = s.store.TaskDigestRecipient(ctx, c); err == nil {
|
||||
t.Fatal("old generation can send")
|
||||
}
|
||||
if _, err = s.store.TaskDigestRecipient(ctx, newClaim); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, err := s.store.TaskPreferences(ctx, domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.DigestEnabled = false
|
||||
if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.TaskDigestRecipient(ctx, newClaim); err == nil {
|
||||
t.Fatal("opt out ignored")
|
||||
}
|
||||
if err = s.store.FinishTaskDigestReason(ctx, newClaim, "cancelled", "consent_changed", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := taskRequest(s, token, "GET", "digest-history?limit=1", nil, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
var page store.TaskDigestHistory
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &page); err != nil || len(page.Items) != 1 || page.Next == "" || page.Retries != 1 || page.Current.UUID != input.RequestID {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
w = taskRequest(s, token, "GET", "digest-history?limit=1&after="+page.Next, nil, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
for _, suffix := range []string{"?limit=101", "?after=bad", "?limit=NaN"} {
|
||||
if w = taskRequest(s, token, "GET", "digest-history"+suffix, nil, ""); w.Code != 422 {
|
||||
t.Fatal(suffix, w.Code)
|
||||
}
|
||||
}
|
||||
if strings.Contains(w.Body.String(), "reader@example.test") {
|
||||
t.Fatal("email in history")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskDigestHistoryQueueExpiryAndPermissions(t *testing.T) {
|
||||
for _, change := range []string{"preference", "extension", "suspended", "tomorrow"} {
|
||||
t.Run(change, func(t *testing.T) {
|
||||
s, _, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q, err := s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
switch change {
|
||||
case "preference":
|
||||
p, e := s.store.TaskPreferences(ctx, domain, actor)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
p.Estimate++
|
||||
_, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, now)
|
||||
case "extension":
|
||||
err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`)
|
||||
case "suspended":
|
||||
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor)
|
||||
case "tomorrow":
|
||||
now = now.Add(24 * time.Hour)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.ClaimQueuedTaskDigest(ctx, q[0], now); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("cancelled queue returned live claim", err)
|
||||
}
|
||||
var state string
|
||||
if err = s.store.DB.QueryRow(`SELECT state FROM organizer_digest_attempts WHERE uuid=?`, input.RequestID).Scan(&state); err != nil || state != "cancelled" {
|
||||
t.Fatal(state, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
s, token, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other, err := s.store.CreatePasswordAccount(domain, "digest-other", "a long test password", "Other", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := taskRequest(s, otherToken, "GET", "digest-history", nil, "")
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"items":[]`) {
|
||||
t.Fatal("history privacy", w.Code, w.Body)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
|
||||
if _, err = s.store.QueueTaskDigestRetry(ctx, domain, other.UUID, input, now); err == nil {
|
||||
t.Fatal("retried other's mail")
|
||||
}
|
||||
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err = s.store.CreateAuthSession(domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = taskRequest(s, token, "POST", "digest-history/retry", input, ""); w.Code != 403 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w = taskRequest(s, token, "GET", "digest-history", nil, ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w = taskRequest(s, "", "GET", "digest-history", nil, ""); w.Code == 200 {
|
||||
t.Fatal("anonymous history")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskDigestHistoryRetentionAndStaleWorker(t *testing.T) {
|
||||
s, _, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
// Keep synthetic stale-worker time within the same date even near midnight.
|
||||
now = time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
|
||||
if _, err := s.store.DB.Exec(`UPDATE organizer_digest_attempts SET updated_ms=? WHERE uuid=?`, now.UnixMilli(), c.AttemptUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.MaintainTaskDigests(ctx, now.Add(11*time.Minute)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page, err := s.store.TaskDigestHistory(ctx, domain, actor, "", 50, now)
|
||||
if err != nil || page.Current.State != "uncertain" {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID, DuplicateAcknowledged: true}
|
||||
if _, err = s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now.Add(12*time.Minute)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q, err := s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now.Add(12*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.FinishTaskDigest(ctx, c, "sent"); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal("stale completion", err)
|
||||
}
|
||||
if err = s.store.FinishTaskDigestReason(ctx, claim, "sent", "", now.Add(12*time.Minute)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deleted, err := s.store.MaintainTaskDigests(ctx, now.Add(91*24*time.Hour))
|
||||
if err != nil || deleted != 2 {
|
||||
t.Fatal("retention", deleted, err)
|
||||
}
|
||||
var reservations, receipts int
|
||||
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&reservations); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&receipts); err != nil || reservations != 1 || receipts != 1 {
|
||||
t.Fatal("deduplication lost", reservations, receipts, err)
|
||||
}
|
||||
// Even an old request replay after retention returns its receipt without requeueing.
|
||||
if id, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now.Add(91*24*time.Hour)); err != nil || id != input.RequestID {
|
||||
t.Fatal(id, err)
|
||||
}
|
||||
q, err = s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil || len(q) != 0 {
|
||||
t.Fatal("retention resurrected mail", q, err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE organizer_task_preferences SET next_digest_ms=? WHERE domain_uuid=? AND owner_uuid=?`, now.Add(-time.Minute).UnixMilli(), domain, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.ClaimTaskDigest(ctx, store.TaskDigestDue{Domain: domain, Owner: actor}, now); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("reservation lost", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskDigestHistorySMTPWorker(t *testing.T) {
|
||||
s, _, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address, _, done := fakeSMTP(t, "", false)
|
||||
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: address, SMTPFrom: "wiki@example.test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.deliverTaskDigests(ctx, &store.EventJob{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery := <-done
|
||||
if !strings.Contains(delivery.message, "Subject: PagesWiki tasks "+c.Day) {
|
||||
t.Fatal("message missing")
|
||||
}
|
||||
page, err := s.store.TaskDigestHistory(ctx, domain, actor, "", 50, now)
|
||||
if err != nil || page.Current.State != "sent" || len(page.Items) != 2 {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
if err := s.deliverTaskDigests(ctx, &store.EventJob{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskDigestHistoryLimitsAndAtomicQueue(t *testing.T) {
|
||||
s, _, domain, actor, now, c := digestHistoryFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
|
||||
if _, err := s.store.DB.Exec(`CREATE TRIGGER fail_digest_queue BEFORE INSERT ON organizer_digest_attempts WHEN NEW.kind='manual' BEGIN SELECT RAISE(ABORT,'test queue failure'); END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err == nil {
|
||||
t.Fatal("injected failure ignored")
|
||||
}
|
||||
var count int
|
||||
if err := s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_requests`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("partial receipt", count, err)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`DROP TRIGGER fail_digest_queue`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 5; i++ {
|
||||
input.RequestID = uuid.NewString()
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
|
||||
t.Fatal(i, err)
|
||||
}
|
||||
q, err := s.store.QueuedTaskDigests(ctx)
|
||||
if err != nil || len(q) != 1 {
|
||||
t.Fatal(q, err)
|
||||
}
|
||||
claim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.FinishTaskDigest(ctx, claim, "cancelled"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input.ExpectedAttempt = claim.AttemptUUID
|
||||
}
|
||||
input.RequestID = uuid.NewString()
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal("retry cap", err)
|
||||
}
|
||||
input.Day = now.AddDate(0, 0, -1).Format("2006-01-02")
|
||||
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); !errors.Is(err, store.ErrTaskInvalid) {
|
||||
t.Fatal("past day accepted", err)
|
||||
}
|
||||
// A bounded retention pass leaves queued rows and durable guards untouched.
|
||||
_, err := s.store.DB.Exec(`WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<501) INSERT INTO organizer_digest_attempts SELECT 'old-'||i,'d','u','2000-01-01','scheduled','sent',1,'UTC','',1,1 FROM n`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deleted, err := s.store.MaintainTaskDigests(ctx, now)
|
||||
if err != nil || deleted != 500 {
|
||||
t.Fatal("unbounded retention", deleted, err)
|
||||
}
|
||||
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_attempts WHERE uuid LIKE 'old-%'`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal(count, err)
|
||||
}
|
||||
var integrity string
|
||||
if err = s.store.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); err != nil || integrity != "ok" {
|
||||
t.Fatal(integrity, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskDigestHistoryChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain, actor, now, c := digestHistoryFixture(t)
|
||||
s.store.BackgroundJobsDisabled = true
|
||||
s = New(s.store)
|
||||
if err := s.store.FinishTaskDigest(context.Background(), c, "uncertain"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address, _, done := fakeSMTP(t, "", false)
|
||||
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: address, SMTPFrom: "wiki@example.test"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/task-digest-history.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := s.handler()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/fixture" {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/fixture/tick" && r.Method == "POST" {
|
||||
if e := s.deliverTaskDigests(r.Context(), &store.EventJob{}); e != nil {
|
||||
http.Error(w, "worker failed", 500)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
|
||||
defer cancel()
|
||||
width := os.Getenv("DIGEST_HISTORY_WIDTH")
|
||||
if width == "" {
|
||||
width = "1440"
|
||||
}
|
||||
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir(), os.Getenv("DIGEST_HISTORY_SCREENSHOT"), width, "850"}
|
||||
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
|
||||
t.Fatalf("browser: %v\n%s", err, out)
|
||||
}
|
||||
select {
|
||||
case delivery := <-done:
|
||||
if !strings.Contains(delivery.message, "Subject: PagesWiki tasks "+c.Day) {
|
||||
t.Fatal("mail missing")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("SMTP not observed")
|
||||
}
|
||||
page, err := s.store.TaskDigestHistory(context.Background(), domain, actor, "", 50, now)
|
||||
if err != nil || page.Retries != 1 || len(page.Items) != 2 || page.Current.State != "sent" {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
}
|
||||
@@ -301,11 +301,24 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
|
||||
var input struct {
|
||||
store.TaskWork
|
||||
Action string `json:"action"`
|
||||
Reason string `json:"reason"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
}
|
||||
if !taskJSON(w, r, &input) {
|
||||
return
|
||||
}
|
||||
v, err := s.store.ChangeTaskWork(r.Context(), domain, actor, input.Action, input.TaskWork, time.Now())
|
||||
if input.Action == "correct" && (input.Start != "" || input.End != "") {
|
||||
a, e1 := parseWorkInstant(input.Start, s.getLocation(domain))
|
||||
b, e2 := parseWorkInstant(input.End, s.getLocation(domain))
|
||||
if e1 != nil || e2 != nil {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
input.StartMS = a.UnixMilli()
|
||||
input.EndMS = b.UnixMilli()
|
||||
}
|
||||
v, err := s.store.ChangeTaskWork(r.Context(), domain, actor, input.Action, input.TaskWork, time.Now(), input.Reason)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
|
||||
@@ -222,6 +222,9 @@ func TestTaskWorkReportSplitsMidnightAndCompletion(t *testing.T) {
|
||||
if err != nil || report.Days[0].Points != 7 || report.Days[0].Completed != 1 {
|
||||
t.Fatal("points", report, err)
|
||||
}
|
||||
if len(report.Projects) != 1 || report.Projects[0].UUID != "" || report.Projects[0].Points != 7 || report.Projects[0].Completed != 1 {
|
||||
t.Fatal("unassigned project totals", report.Projects)
|
||||
}
|
||||
item.Status = "todo"
|
||||
if _, err = s.store.SaveTask(ctx, domain, actor, item, item.Version); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pageswiki/internal/store"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func parseWorkInstant(raw string, loc *time.Location) (time.Time, error) {
|
||||
if v, err := time.Parse(time.RFC3339Nano, raw); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
for _, format := range []string{"2006-01-02T15:04:05", "2006-01-02T15:04"} {
|
||||
v, err := time.ParseInLocation(format, raw, loc)
|
||||
if err != nil || v.Format(format) != raw {
|
||||
continue
|
||||
}
|
||||
// A repeated wall time needs an explicit offset. Do not silently choose
|
||||
// the earlier/later occurrence when a browser datetime-local cannot express it.
|
||||
for _, delta := range []time.Duration{-24 * time.Hour, -2 * time.Hour, -time.Hour, -30 * time.Minute, 30 * time.Minute, time.Hour, 2 * time.Hour, 24 * time.Hour} {
|
||||
if v.Add(delta).In(loc).Format(format) == raw {
|
||||
return time.Time{}, store.ErrTaskInvalid
|
||||
}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
return time.Time{}, store.ErrTaskInvalid
|
||||
}
|
||||
|
||||
func (s *Server) handleTaskWorkHistory(w http.ResponseWriter, r *http.Request, actor string, parts []string) {
|
||||
if r.Method != "GET" {
|
||||
w.Header().Set("Allow", "GET")
|
||||
w.WriteHeader(405)
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
if len(parts) == 2 && parts[1] != "" {
|
||||
var before int64
|
||||
if raw := r.URL.Query().Get("before"); raw != "" {
|
||||
var err error
|
||||
before, err = strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || before < 0 {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
}
|
||||
items, err := s.store.TaskWorkRevisions(r.Context(), domain, actor, parts[1], before)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
var next int64
|
||||
if len(items) == 100 {
|
||||
next = items[len(items)-1].Item.Version
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"items": items, "next": next})
|
||||
return
|
||||
}
|
||||
if len(parts) != 1 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
now := time.Now().In(s.getLocation(domain))
|
||||
end := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
|
||||
start := end.AddDate(0, 0, -30)
|
||||
for _, v := range []struct {
|
||||
key string
|
||||
target *time.Time
|
||||
}{{"from", &start}, {"to", &end}} {
|
||||
if raw := r.URL.Query().Get(v.key); raw != "" {
|
||||
date, err := time.ParseInLocation("2006-01-02", raw, now.Location())
|
||||
if err != nil || date.Format("2006-01-02") != raw {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
*v.target = date
|
||||
}
|
||||
}
|
||||
limit := 50
|
||||
if raw := r.URL.Query().Get("limit"); raw != "" {
|
||||
var err error
|
||||
limit, err = strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return
|
||||
}
|
||||
}
|
||||
result, err := s.store.TaskWorkHistory(r.Context(), domain, actor, start, end, r.URL.Query().Get("after"), limit)
|
||||
if err != nil {
|
||||
taskError(w, err)
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]any{"items": result.Items, "next": result.Next, "from": start.Format("2006-01-02"), "to": end.Format("2006-01-02"), "timezone": now.Location().String()})
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func recordedWork(t *testing.T, s *Server, domain, actor, task string, start time.Time) store.TaskWork {
|
||||
t.Helper()
|
||||
v, err := s.store.ChangeTaskWork(context.Background(), domain, actor, "start", store.TaskWork{UUID: uuid.NewString(), TaskUUID: task, CancelledMS: 99}, start)
|
||||
if err != nil || v.CancelledMS != 0 {
|
||||
t.Fatal(v, err)
|
||||
}
|
||||
v, err = s.store.ChangeTaskWork(context.Background(), domain, actor, "stop", v, start.Add(time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func TestWorkHistoryCorrectionCancellationAndReports(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
project, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("project", "Project A"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
item := newTaskRecord("task", "Audited work")
|
||||
item.ProjectUUID = project.UUID
|
||||
item, err = s.store.SaveTask(ctx, domain, actor, item, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC)
|
||||
first := recordedWork(t, s, domain, actor, item.UUID, start)
|
||||
second := recordedWork(t, s, domain, actor, item.UUID, start.Add(2*time.Hour))
|
||||
post := func(v store.TaskWork, action, reason string) int {
|
||||
return taskRequest(s, token, "POST", "work", map[string]any{"uuid": v.UUID, "task_uuid": v.TaskUUID, "version": v.Version, "action": action, "reason": reason, "start_ms": v.StartMS, "end_ms": v.EndMS}, "").Code
|
||||
}
|
||||
if got := post(first, "correct", ""); got != 422 {
|
||||
t.Fatal("missing reason", got)
|
||||
}
|
||||
bad := first
|
||||
bad.EndMS = second.EndMS
|
||||
if got := post(bad, "correct", "overlap"); got != 412 {
|
||||
t.Fatal("overlap", got)
|
||||
}
|
||||
bad = first
|
||||
bad.EndMS = bad.StartMS
|
||||
if got := post(bad, "correct", "empty"); got != 422 {
|
||||
t.Fatal("empty interval", got)
|
||||
}
|
||||
first.EndMS = start.Add(2 * time.Hour).UnixMilli() // adjacency is valid
|
||||
if got := post(first, "correct", "Finished later"); got != 200 {
|
||||
t.Fatal("correct", got)
|
||||
}
|
||||
if got := post(first, "correct", "stale mutation"); got != 412 {
|
||||
t.Fatal("stale version", got)
|
||||
}
|
||||
first.Version++
|
||||
report, err := s.store.TaskReport(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), time.Now())
|
||||
if err != nil || len(report.Projects) != 1 || report.Projects[0].Title != "Project A" || report.Projects[0].WorkMS != 3*3600000 {
|
||||
t.Fatal(report, err)
|
||||
}
|
||||
if got := post(first, "cancel", "Duplicate recording"); got != 200 {
|
||||
t.Fatal("cancel", got)
|
||||
}
|
||||
report, err = s.store.TaskReport(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), time.Now())
|
||||
if err != nil || report.Projects[0].WorkMS != 3600000 {
|
||||
t.Fatal("cancelled counted", report, err)
|
||||
}
|
||||
audit, err := s.store.TaskWorkRevisions(ctx, domain, actor, first.UUID, 0)
|
||||
if err != nil || len(audit) != 4 || audit[0].Action != "cancel" || audit[1].Action != "correct" || audit[2].Action != "stop" || audit[3].Action != "start" || audit[2].Item.EndMS != start.Add(time.Hour).UnixMilli() {
|
||||
t.Fatal(audit, err)
|
||||
}
|
||||
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), "", 1)
|
||||
if err != nil || len(page.Items) != 1 || page.Items[0].UUID != second.UUID || page.Next == "" {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
page, err = s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), page.Next, 1)
|
||||
if err != nil || len(page.Items) != 1 || page.Items[0].UUID != first.UUID || page.Items[0].CancelledMS == 0 || page.Next != "" {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
for _, query := range []string{"limit=101", "after=bad", "from=2026-01-02&to=2026-01-01", "from=2020-01-01&to=2026-01-01"} {
|
||||
if w := taskRequest(s, token, "GET", "work-history?"+query, nil, ""); w.Code != 422 {
|
||||
t.Fatal(query, w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
if w := taskRequest(s, token, "GET", "work-history/"+first.UUID+"?before=3", nil, ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w := taskRequest(s, token, "POST", "work-history", nil, ""); w.Code != 405 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
stranger, err := s.store.CreatePasswordAccount(domain, "history-stranger", "a long test password", "Stranger", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
strangerToken, err := s.store.CreateAuthSession(domain, stranger.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := taskRequest(s, strangerToken, "GET", "work-history/"+first.UUID, nil, ""); w.Code != 404 {
|
||||
t.Fatal("history leaked", w.Code, w.Body)
|
||||
}
|
||||
if w := taskRequest(s, strangerToken, "GET", "work-history?from=2026-01-01&to=2026-01-04", nil, ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"items":[]`) {
|
||||
t.Fatal("list leaked", w.Code, w.Body)
|
||||
}
|
||||
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err = s.store.CreateAuthSession(domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := post(second, "cancel", "No permission"); got != 403 {
|
||||
t.Fatal("readonly mutation", got)
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := taskRequest(s, token, "GET", "work-history", nil, ""); w.Code != 404 {
|
||||
t.Fatal("disabled extension", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkInstantTimezone(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
zone, input string
|
||||
valid bool
|
||||
}{{"Asia/Tokyo", "2026-01-02T12:30", true}, {"America/New_York", "2026-03-08T02:30", false}, {"America/New_York", "2026-11-01T01:30", false}, {"America/New_York", "2026-11-01T01:30:00-04:00", true}, {"Australia/Lord_Howe", "2026-04-05T01:45", false}, {"UTC", "bad", false}} {
|
||||
loc, err := time.LoadLocation(tc.zone)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = parseWorkInstant(tc.input, loc)
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatal(tc, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkHistoryFencingAndAtomicAudit(t *testing.T) {
|
||||
s, _, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
item, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Fenced work"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now().Add(-10 * time.Hour)
|
||||
v := recordedWork(t, s, domain, actor, item.UUID, start)
|
||||
// Simulate a pre-upgrade session: capture its old value, never invent older versions.
|
||||
if _, err = s.store.DB.Exec(`DELETE FROM organizer_work_revisions WHERE work_uuid=?`, v.UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`CREATE TRIGGER fail_work_audit BEFORE INSERT ON organizer_work_revisions WHEN NEW.action='correct' BEGIN SELECT RAISE(ABORT,'test audit unavailable'); END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
edit := v
|
||||
edit.EndMS += 60000
|
||||
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "must rollback"); err == nil {
|
||||
t.Fatal("audit failure accepted")
|
||||
}
|
||||
var version int64
|
||||
if err = s.store.DB.QueryRow(`SELECT version FROM organizer_work_sessions WHERE uuid=?`, v.UUID).Scan(&version); err != nil || version != 2 {
|
||||
t.Fatal("partial mutation", version, err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`DROP TRIGGER fail_work_audit`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Two concurrent corrections must not overwrite one another.
|
||||
var wg sync.WaitGroup
|
||||
results := make(chan error, 2)
|
||||
for range 2 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, e := s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "racing correction")
|
||||
results <- e
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
wins, conflicts := 0, 0
|
||||
for err := range results {
|
||||
if err == nil {
|
||||
wins++
|
||||
} else if errors.Is(err, store.ErrTaskConflict) {
|
||||
conflicts++
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if wins != 1 || conflicts != 1 {
|
||||
t.Fatal(wins, conflicts)
|
||||
}
|
||||
history, err := s.store.TaskWorkRevisions(ctx, domain, actor, v.UUID, 0)
|
||||
if err != nil || len(history) != 2 || history[1].Action != "baseline" || history[1].Item.EndMS != v.EndMS {
|
||||
t.Fatal(history, err)
|
||||
}
|
||||
// Cross-Domain intervals belong to the same person, but never appear in this list.
|
||||
d, err := s.store.GetDomainByUUID(domain)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.CreateDomain(d.CampusUUID, "work-other", "Other", "work-other", "local", "", "", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domains, err := s.store.ListDomains()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := ""
|
||||
for _, d := range domains {
|
||||
if d.Name == "work-other" {
|
||||
other = d.UUID
|
||||
}
|
||||
}
|
||||
if other == "" {
|
||||
t.Fatal("missing domain")
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`INSERT OR IGNORE INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(?,?,?,'active',0)`, uuid.NewString(), other, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
otherTask, err := s.store.SaveTask(ctx, other, actor, newTaskRecord("task", "Private other work"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreign := recordedWork(t, s, other, actor, otherTask.UUID, start.Add(2*time.Hour))
|
||||
edit = history[0].Item
|
||||
edit.EndMS = foreign.EndMS
|
||||
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "overlaps other domain"); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.TaskWorkRevisions(ctx, domain, actor, foreign.UUID, 0); err == nil {
|
||||
t.Fatal("cross-domain audit leaked")
|
||||
}
|
||||
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), time.Now(), "", 50)
|
||||
if err != nil || len(page.Items) != 1 {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
active, err := s.store.ChangeTaskWork(ctx, domain, actor, "start", store.TaskWork{UUID: uuid.NewString(), TaskUUID: item.UUID}, start.Add(5*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, action := range []string{"correct", "cancel"} {
|
||||
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, action, active, time.Now(), "active recording"); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal(action, err)
|
||||
}
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "cancel", history[0].Item, time.Now(), "revoked"); !errors.Is(err, store.ErrAccessDenied) {
|
||||
t.Fatal("revocation not fenced", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkHistoryChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
s.store.BackgroundJobsDisabled = true
|
||||
s = New(s.store)
|
||||
item, err := s.store.SaveTask(context.Background(), domain, actor, newTaskRecord("task", "Browser work record"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v := recordedWork(t, s, domain, actor, item.UUID, time.Now().Add(-3*time.Hour))
|
||||
script, err := os.ReadFile("internal/server/testdata/task-work-history.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := s.handler()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.URL.Path == "/fixture" {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
|
||||
defer cancel()
|
||||
width := os.Getenv("WORK_HISTORY_WIDTH")
|
||||
if width == "" {
|
||||
width = "1440"
|
||||
}
|
||||
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir(), os.Getenv("WORK_HISTORY_SCREENSHOT"), width, "850"}
|
||||
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
|
||||
t.Fatalf("browser: %v\n%s", err, out)
|
||||
}
|
||||
history, err := s.store.TaskWorkRevisions(context.Background(), domain, actor, v.UUID, 0)
|
||||
if err != nil || len(history) != 4 || history[0].Action != "cancel" || history[1].Action != "correct" {
|
||||
t.Fatal(history, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkHistoryLocalTimestampAPI(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
item, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Timestamp"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Date(2026, 1, 2, 12, 0, 0, 123000000, time.UTC)
|
||||
v := recordedWork(t, s, domain, actor, item.UUID, start)
|
||||
input := map[string]any{"uuid": v.UUID, "version": v.Version, "action": "correct", "reason": "API timestamp", "start": start.Format(time.RFC3339Nano), "end": start.Add(90 * time.Minute).Format(time.RFC3339Nano)}
|
||||
w := taskRequest(s, token, "POST", "work", input, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
var result struct {
|
||||
Item store.TaskWork `json:"item"`
|
||||
}
|
||||
if err = json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(3*time.Hour), "", 50)
|
||||
if err != nil || page.Items[0].StartMS != start.UnixMilli() {
|
||||
t.Fatal(page, err)
|
||||
}
|
||||
if w = taskRequest(s, token, "GET", "work-history?"+url.Values{"from": {"2026-01-02"}, "to": {"2026-01-03"}}.Encode(), nil, ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,10 @@ func (s *Server) handleTaskPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
if r.URL.Path == "/v/plan/tasks/work" {
|
||||
s.renderWithContext(w, r, "task_work_history.gohtml", map[string]any{"TaskWritable": active, "Timezone": s.getTimezoneName(domain)})
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/v/plan/tasks/report" || r.URL.Path == "/v/plan/tasks/settings" {
|
||||
s.handleTaskProductivityPage(w, r, active)
|
||||
return
|
||||
@@ -212,6 +216,14 @@ func (s *Server) handleTasksAPI(w http.ResponseWriter, r *http.Request) {
|
||||
r = r.WithContext(ctx)
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, tasksAPI), "/")
|
||||
if parts[0] == "digest-history" {
|
||||
s.handleTaskDigestHistory(w, r, actor, active, parts)
|
||||
return
|
||||
}
|
||||
if parts[0] == "work-history" {
|
||||
s.handleTaskWorkHistory(w, r, actor, parts)
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 && (parts[0] == "dashboard" || parts[0] == "progress" || parts[0] == "calendar-capture" || parts[0] == "capture" || parts[0] == "preferences" || parts[0] == "work" || parts[0] == "report") {
|
||||
s.handleTaskProductivity(w, r, actor, active, parts[0])
|
||||
return
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
(async()=>{
|
||||
const assert=(ok,msg)=>{if(!ok)throw Error(msg);},delay=ms=>new Promise(r=>setTimeout(r,ms));
|
||||
const wait=async check=>{for(let n=0;n<240;n++){if(check())return;await delay(25);}throw Error('Timed out: '+check);};
|
||||
const frame=document.createElement('iframe');frame.style='display:block;width:100%;height:100vh;border:0';document.body.append(frame);
|
||||
try{
|
||||
await new Promise(resolve=>{frame.onload=resolve;frame.src='/-/sys/config?panel=organizer';});
|
||||
const w=frame.contentWindow,d=frame.contentDocument;await wait(()=>d.querySelector('#task-digest-history details'));
|
||||
const root=d.getElementById('task-digest-history'),details=root.querySelector('details');
|
||||
assert(!details.open,'History not lazy');assert(!w.performance.getEntriesByType('resource').some(x=>x.name.includes('/digest-history')),'History eagerly fetched');
|
||||
details.open=true;await wait(()=>root.querySelector('.digest-retry form'));
|
||||
const form=root.querySelector('.digest-retry form'),ack=form.querySelector('input');
|
||||
assert(ack.required&&!ack.checked,'Uncertain delivery must require confirmation');assert(!form.checkValidity(),'Unchecked acknowledgment valid');ack.checked=true;
|
||||
const original=w.fetch,requests=[];let lose=true;
|
||||
w.fetch=async(url,options)=>{if(String(url).endsWith('/digest-history/retry')){requests.push(JSON.parse(options.body));const response=await original(url,options);if(lose){lose=false;await response.text();throw new TypeError('Simulated lost response');}return response;}return original(url,options);};
|
||||
form.requestSubmit();await wait(()=>root.querySelector('[role=status]').textContent&&!form.querySelector('button').disabled);
|
||||
assert(root.contains(form),'Lost response removed retry controls');form.requestSubmit();
|
||||
await wait(()=>!root.querySelector('.digest-retry form')&&!root.querySelector('.productivity-controls button').disabled);
|
||||
assert(requests.length===2&&requests[0].request_id===requests[1].request_id,'Retry generated a second request ID');
|
||||
assert(root.querySelectorAll('tbody tr').length===2,'Duplicate attempt created');
|
||||
const tick=await fetch('/fixture/tick',{method:'POST'});assert(tick.ok,'Worker failed');
|
||||
root.querySelector('.productivity-controls button').click();await wait(()=>root.querySelector('.digest-retry form'));
|
||||
assert(root.querySelector('.digest-retry input').required,'Sent retry must require confirmation');
|
||||
assert(root.textContent.includes('メールサーバー受付済み')||root.textContent.includes('Accepted by mail server'),'Sent status missing');
|
||||
assert(!root.textContent.includes('reader@example.test'),'Recipient leaked in history');
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'Settings overflow viewport');
|
||||
details.scrollIntoView({block:'start'});document.body.dataset.vimReady='1';
|
||||
}catch(error){const out=document.createElement('pre');out.id='error';out.textContent=error.stack;document.body.append(out);}
|
||||
})();
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
(async()=>{
|
||||
const assert=(ok,msg)=>{if(!ok)throw Error(msg);},delay=ms=>new Promise(r=>setTimeout(r,ms));
|
||||
const wait=async check=>{for(let n=0;n<200;n++){if(check())return;await delay(25);}throw Error('Timed out: '+check);};
|
||||
const frame=document.createElement('iframe');frame.style='display:block;width:100%;height:100vh;border:0';document.body.append(frame);
|
||||
try{
|
||||
await new Promise(resolve=>{frame.onload=resolve;frame.src='/v/plan/dashboard';});
|
||||
await wait(()=>frame.contentDocument.querySelector('.productivity-table tbody tr'));
|
||||
const reportTable=frame.contentDocument.querySelector('.productivity-table');
|
||||
assert(reportTable.querySelectorAll('thead th').length===4,'Project aggregate table missing');
|
||||
assert(reportTable.querySelector('tbody tr').lastElementChild.textContent==='1.00','Project duration incorrect');
|
||||
const historyLink=frame.contentDocument.querySelector('a[href="/v/plan/tasks/work"]');assert(historyLink,'Dashboard history link missing');
|
||||
await new Promise(resolve=>{frame.onload=resolve;historyLink.click();});
|
||||
const w=frame.contentWindow,d=frame.contentDocument;
|
||||
await wait(()=>d.querySelector('.work-history-table tbody button'));
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'History overflows viewport');
|
||||
d.querySelector('.work-history-table tbody button').click();
|
||||
let form=d.querySelector('.work-history-edit');assert(form,'Inline correction missing');
|
||||
const inputs=form.querySelectorAll('input');inputs[2].value='Browser correction';
|
||||
form.requestSubmit();await wait(()=>!d.querySelector('.work-history-edit')&&!d.getElementById('work-history-list').inert);
|
||||
d.querySelector('#work-history-list details').open=true;await wait(()=>d.querySelector('#work-history-list details strong'));
|
||||
assert(d.querySelector('#work-history-list details').textContent.includes('Browser correction'),'Audit missing');
|
||||
const originalFetch=w.fetch;w.fetch=async(url,options)=>options?.method==='POST'?new w.Response(JSON.stringify({error:'conflict'}),{status:412}):originalFetch(url,options);
|
||||
d.querySelector('.work-history-table tbody button').click();form=d.querySelector('.work-history-edit');form.querySelector('input[type=text]').value='Retain my input';form.requestSubmit();
|
||||
await wait(()=>form.querySelector('[role=status]').textContent);assert(form.querySelector('input[type=text]').value==='Retain my input','Conflict discarded input');
|
||||
w.fetch=originalFetch;w.confirm=()=>true;form.querySelector('button[type=button]').click();
|
||||
d.querySelectorAll('.work-history-table tbody tr:first-child button')[1].click();form=d.querySelector('.work-history-edit');form.querySelector('input').value='Browser cancellation';form.requestSubmit();await wait(()=>!d.querySelector('.work-history-edit')&&!d.getElementById('work-history-list').inert);
|
||||
assert(!d.querySelector('.work-history-table tbody tr:first-child button'),'Cancelled work remains editable');
|
||||
d.querySelector('#work-history-list details').open=true;await wait(()=>d.querySelector('#work-history-list details strong'));
|
||||
assert(d.querySelector('#work-history-list details').textContent.includes('Browser cancellation'),'Cancellation audit missing');
|
||||
const report=await (await fetch('/_/api/v1/extension/organizer.tasks/report')).json();assert(report.days.every(day=>day.work_ms===0),'Cancelled time remains in report');
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'Expanded history overflows viewport');
|
||||
document.body.dataset.vimReady='1';
|
||||
}catch(error){const out=document.createElement('pre');out.id='error';out.textContent=error.stack;document.body.append(out);}
|
||||
})();
|
||||
@@ -110,6 +110,88 @@ func TestDashboardUpgradePreservesTaskPreferences(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkHistoryUpgradePreservesLegacySessions(t *testing.T) {
|
||||
st := newSchemaTestStore(t)
|
||||
if err := db.ApplySQLiteMigrations(context.Background(), st.DB, "task-productivity", taskProductivityMigrations[:2]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.DB.Exec(`INSERT INTO organizer_work_sessions VALUES('w','d','u','t',100,200,2)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for range 2 {
|
||||
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var start, end, version, cancelled int64
|
||||
if err := st.DB.QueryRow(`SELECT start_ms,end_ms,version,cancelled_ms FROM organizer_work_sessions WHERE uuid='w'`).Scan(&start, &end, &version, &cancelled); err != nil || start != 100 || end != 200 || version != 2 || cancelled != 0 {
|
||||
t.Fatal(start, end, version, cancelled, err)
|
||||
}
|
||||
var count int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM organizer_work_revisions`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("invented history", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDigestHistoryUpgradePreservesReservations(t *testing.T) {
|
||||
st := newSchemaTestStore(t)
|
||||
ctx := context.Background()
|
||||
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", taskProductivityMigrations[:3]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, state := range []string{"sent", "sending", "uncertain", "cancelled"} {
|
||||
if _, err := st.DB.Exec(`INSERT INTO organizer_digest_deliveries VALUES(?,?,?, ?,?)`, "d", state, "2026-11-01", state, 100+i); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for range 2 {
|
||||
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM organizer_digest_deliveries d JOIN organizer_digest_attempts a ON a.uuid=d.attempt_uuid AND a.domain_uuid=d.domain_uuid AND a.owner_uuid=d.owner_uuid AND a.day=d.day AND a.state=d.state AND a.updated_ms=d.updated_ms WHERE a.kind='legacy' AND a.preference_version=0`).Scan(&count); err != nil || count != 4 {
|
||||
t.Fatal("reservation migration", count, err)
|
||||
}
|
||||
var integrity string
|
||||
if err := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); err != nil || integrity != "ok" {
|
||||
t.Fatal(integrity, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDigestHistoryMigrationRollback(t *testing.T) {
|
||||
st := newSchemaTestStore(t)
|
||||
ctx := context.Background()
|
||||
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", taskProductivityMigrations[:3]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.DB.Exec(`INSERT INTO organizer_digest_deliveries VALUES('d','u','2026-11-01','sent',123)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
steps := append([]db.SQLMigration(nil), taskProductivityMigrations...)
|
||||
steps[3].Statements = append(append([]string(nil), steps[3].Statements...), `INVALID MIGRATION STATEMENT`)
|
||||
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", steps); err == nil {
|
||||
t.Fatal("failed migration committed")
|
||||
}
|
||||
var columns, ledger, attempts int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('organizer_digest_deliveries')`).Scan(&columns); err != nil || columns != 5 {
|
||||
t.Fatal("DDL not rolled back", columns, err)
|
||||
}
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM schema_migration_steps WHERE component='task-productivity'`).Scan(&ledger); err != nil || ledger != 3 {
|
||||
t.Fatal(ledger, err)
|
||||
}
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='organizer_digest_attempts'`).Scan(&attempts); err != nil || attempts != 0 {
|
||||
t.Fatal(attempts, err)
|
||||
}
|
||||
var state string
|
||||
if err := st.DB.QueryRow(`SELECT state FROM organizer_digest_deliveries`).Scan(&state); err != nil || state != "sent" {
|
||||
t.Fatal(state, err)
|
||||
}
|
||||
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
|
||||
t.Fatal("retry migration", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedSchemasFreshRepeatAndReadOnly(t *testing.T) {
|
||||
for component := range managedStoreSchemas {
|
||||
t.Run(component, func(t *testing.T) {
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const TaskDigestEventType = "task.digest.deliver"
|
||||
@@ -16,9 +18,10 @@ type TaskDigestDue struct {
|
||||
}
|
||||
type TaskDigestClaim struct {
|
||||
TaskDigestDue
|
||||
Day string
|
||||
Version int64
|
||||
Timezone string
|
||||
Day string
|
||||
Version int64
|
||||
Timezone string
|
||||
AttemptUUID string
|
||||
}
|
||||
|
||||
func (s *Store) EnsureTaskDigestEvent() error {
|
||||
@@ -124,11 +127,17 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
|
||||
}
|
||||
return out, sql.ErrNoRows
|
||||
}
|
||||
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries VALUES(?,?,?,'sending',?)`, due.Domain, due.Owner, out.Day, now.UnixMilli())
|
||||
out.AttemptUUID = uuid.NewString()
|
||||
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries(domain_uuid,owner_uuid,day,state,updated_ms,attempt_uuid) VALUES(?,?,?,'sending',?,?)`, due.Domain, due.Owner, out.Day, now.UnixMilli(), out.AttemptUUID)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
n, _ = res.RowsAffected()
|
||||
if n == 1 {
|
||||
if err = insertDigestAttempt(ctx, tx, out, "scheduled", "sending", now); err != nil {
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
@@ -139,12 +148,20 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
|
||||
}
|
||||
|
||||
func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (string, error) {
|
||||
if err := taskActor(ctx, s.DB, c.Domain, c.Owner, true); err != nil {
|
||||
if s.ReadOnly {
|
||||
return "", ErrReadOnly
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := taskActor(ctx, tx, c.Domain, c.Owner, true); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var raw, email string
|
||||
var version int64
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid WHERE f.domain_uuid=? AND f.owner_uuid=?`, c.Domain, c.Owner).Scan(&raw, &version, &email)
|
||||
err = tx.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid JOIN organizer_digest_deliveries d ON d.domain_uuid=f.domain_uuid AND d.owner_uuid=f.owner_uuid JOIN organizer_digest_attempts a ON a.uuid=d.attempt_uuid WHERE f.domain_uuid=? AND f.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='sending' AND a.state='sending' AND a.updated_ms>?`, c.Domain, c.Owner, c.Day, c.AttemptUUID, time.Now().Add(-2*time.Minute).UnixMilli()).Scan(&raw, &version, &email)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -152,15 +169,22 @@ func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (str
|
||||
if json.Unmarshal([]byte(raw), &p) != nil || !p.DigestEnabled || version != c.Version {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return email, nil
|
||||
loc, err := time.LoadLocation(c.Timezone)
|
||||
if err != nil || time.Now().In(loc).Format("2006-01-02") != c.Day {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !on {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return email, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) FinishTaskDigest(ctx context.Context, c TaskDigestClaim, state string) error {
|
||||
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
_, err := s.DB.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND state='sending'`, state, time.Now().UnixMilli(), c.Domain, c.Owner, c.Day)
|
||||
return err
|
||||
return s.FinishTaskDigestReason(ctx, c, state, "", time.Now())
|
||||
}
|
||||
|
||||
func (s *Store) LastTaskDigest(ctx context.Context, domain, actor string) (map[string]any, error) {
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const TaskDigestRetentionDays = 90
|
||||
|
||||
type TaskDigestAttempt struct {
|
||||
UUID string `json:"uuid"`
|
||||
Day string `json:"day"`
|
||||
Kind string `json:"kind"`
|
||||
State string `json:"state"`
|
||||
Reason string `json:"reason"`
|
||||
Timezone string `json:"timezone"`
|
||||
CreatedMS int64 `json:"created_ms"`
|
||||
UpdatedMS int64 `json:"updated_ms"`
|
||||
}
|
||||
type TaskDigestRetry struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Day string `json:"day"`
|
||||
ExpectedAttempt string `json:"expected_attempt"`
|
||||
DuplicateAcknowledged bool `json:"duplicate_acknowledged"`
|
||||
}
|
||||
type TaskDigestHistory struct {
|
||||
Items []TaskDigestAttempt `json:"items"`
|
||||
Next string `json:"next"`
|
||||
Today string `json:"today"`
|
||||
Current *TaskDigestAttempt `json:"current"`
|
||||
Retries int `json:"retries"`
|
||||
Enabled bool `json:"enabled"`
|
||||
RetentionDays int `json:"retention_days"`
|
||||
}
|
||||
|
||||
const digestAttemptColumns = `uuid,day,kind,state,reason,timezone,created_ms,updated_ms`
|
||||
|
||||
func scanDigestAttempt(row interface{ Scan(...any) error }) (TaskDigestAttempt, error) {
|
||||
var v TaskDigestAttempt
|
||||
err := row.Scan(&v.UUID, &v.Day, &v.Kind, &v.State, &v.Reason, &v.Timezone, &v.CreatedMS, &v.UpdatedMS)
|
||||
return v, err
|
||||
}
|
||||
|
||||
func insertDigestAttempt(ctx context.Context, tx *sql.Tx, c TaskDigestClaim, kind, state string, now time.Time) error {
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO organizer_digest_attempts(uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,created_ms,updated_ms) VALUES(?,?,?,?,?,?,?,?,?,?)`, c.AttemptUUID, c.Domain, c.Owner, c.Day, kind, state, c.Version, c.Timezone, now.UnixMilli(), now.UnixMilli())
|
||||
return err
|
||||
}
|
||||
|
||||
// Receipt lookup is read-only. It lets clients confirm an accepted request even
|
||||
// when SMTP is subsequently disabled; it never reactivates a delivery.
|
||||
func (s *Store) TaskDigestRetryReceipt(ctx context.Context, domain, actor string, input TaskDigestRetry) (string, error) {
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return "", err
|
||||
}
|
||||
id, err := uuid.Parse(input.RequestID)
|
||||
if err != nil || id.String() != input.RequestID {
|
||||
return "", ErrTaskInvalid
|
||||
}
|
||||
var day, expected string
|
||||
var ack bool
|
||||
err = s.DB.QueryRowContext(ctx, `SELECT day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, input.RequestID, domain, actor).Scan(&day, &expected, &ack)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if day != input.Day || expected != input.ExpectedAttempt || ack != input.DuplicateAcknowledged {
|
||||
return "", ErrTaskConflict
|
||||
}
|
||||
return input.RequestID, nil
|
||||
}
|
||||
|
||||
// A retry is durable queue admission, never an SMTP send in a browser request.
|
||||
// Keep request receipts and daily reservations even after detail retention.
|
||||
func (s *Store) QueueTaskDigestRetry(ctx context.Context, domain, actor string, input TaskDigestRetry, now time.Time) (string, error) {
|
||||
if s.ReadOnly {
|
||||
return "", ErrReadOnly
|
||||
}
|
||||
id, err := uuid.Parse(input.RequestID)
|
||||
if err != nil || id.String() != input.RequestID {
|
||||
return "", ErrTaskInvalid
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = taskProductivityWriter(ctx, tx, domain, actor); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var oldDomain, oldOwner, oldDay, oldExpected string
|
||||
var oldAck bool
|
||||
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=?`, input.RequestID).Scan(&oldDomain, &oldOwner, &oldDay, &oldExpected, &oldAck)
|
||||
if err == nil {
|
||||
if oldDomain != domain || oldOwner != actor || oldDay != input.Day || oldExpected != input.ExpectedAttempt || oldAck != input.DuplicateAcknowledged {
|
||||
return "", ErrTaskConflict
|
||||
}
|
||||
return input.RequestID, tx.Commit()
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return "", err
|
||||
}
|
||||
var raw string
|
||||
if err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&raw); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var p TaskPreferences
|
||||
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
loc, err := time.LoadLocation(p.Timezone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !p.DigestEnabled {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
if input.Day != now.In(loc).Format("2006-01-02") {
|
||||
return "", ErrTaskInvalid
|
||||
}
|
||||
var state, current string
|
||||
if err = tx.QueryRowContext(ctx, `SELECT state,attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&state, ¤t); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if current != input.ExpectedAttempt || state == "queued" || state == "sending" {
|
||||
return "", ErrTaskConflict
|
||||
}
|
||||
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
||||
return "", ErrTaskConflict
|
||||
}
|
||||
if (state == "sent" || state == "uncertain") && !input.DuplicateAcknowledged {
|
||||
return "", ErrTaskInvalid
|
||||
}
|
||||
var count int
|
||||
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&count); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if count >= 5 {
|
||||
return "", ErrTaskConflict
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_digest_requests VALUES(?,?,?,?,?,?,?)`, input.RequestID, domain, actor, input.Day, input.ExpectedAttempt, input.DuplicateAcknowledged, now.UnixMilli())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c := TaskDigestClaim{TaskDigestDue: TaskDigestDue{domain, actor}, Day: input.Day, Version: p.Version, Timezone: p.Timezone, AttemptUUID: input.RequestID}
|
||||
if err = insertDigestAttempt(ctx, tx, c, "manual", "queued", now); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='queued',attempt_uuid=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=?`, c.AttemptUUID, now.UnixMilli(), domain, actor, input.Day); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return c.AttemptUUID, tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) QueuedTaskDigests(ctx context.Context) ([]TaskDigestClaim, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT uuid,domain_uuid,owner_uuid,day,preference_version,timezone FROM organizer_digest_attempts WHERE state='queued' ORDER BY updated_ms,uuid LIMIT 10`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []TaskDigestClaim{}
|
||||
for rows.Next() {
|
||||
var c TaskDigestClaim
|
||||
if err = rows.Scan(&c.AttemptUUID, &c.Domain, &c.Owner, &c.Day, &c.Version, &c.Timezone); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ClaimQueuedTaskDigest(ctx context.Context, c TaskDigestClaim, now time.Time) (TaskDigestClaim, error) {
|
||||
if s.ReadOnly {
|
||||
return c, ErrReadOnly
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='sending',updated_ms=? WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND state='queued' AND EXISTS(SELECT 1 FROM organizer_digest_deliveries d WHERE d.domain_uuid=? AND d.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='queued')`, now.UnixMilli(), c.AttemptUUID, c.Domain, c.Owner, c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
if n != 1 {
|
||||
return c, sql.ErrNoRows
|
||||
}
|
||||
// Reload all claim fields from the queue, not from an earlier enumeration.
|
||||
if err = tx.QueryRowContext(ctx, `SELECT day,preference_version,timezone FROM organizer_digest_attempts WHERE uuid=?`, c.AttemptUUID).Scan(&c.Day, &c.Version, &c.Timezone); err != nil {
|
||||
return c, err
|
||||
}
|
||||
state, reason := "sending", ""
|
||||
allowed := taskActor(ctx, tx, c.Domain, c.Owner, true)
|
||||
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
|
||||
return c, allowed
|
||||
}
|
||||
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
var raw string
|
||||
err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, c.Domain, c.Owner).Scan(&raw)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return c, err
|
||||
}
|
||||
var p TaskPreferences
|
||||
if err == nil {
|
||||
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
||||
return c, err
|
||||
}
|
||||
}
|
||||
if allowed != nil || !on || !p.DigestEnabled || p.Version != c.Version {
|
||||
state, reason = "cancelled", "consent_changed"
|
||||
}
|
||||
loc, e := time.LoadLocation(c.Timezone)
|
||||
if e != nil || now.In(loc).Format("2006-01-02") != c.Day {
|
||||
state, reason = "cancelled", "day_expired"
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=? WHERE uuid=?`, state, reason, c.AttemptUUID); err != nil {
|
||||
return c, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=?`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID); err != nil {
|
||||
return c, err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return c, err
|
||||
}
|
||||
if state != "sending" {
|
||||
return c, sql.ErrNoRows
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *Store) FinishTaskDigestReason(ctx context.Context, c TaskDigestClaim, state, reason string, now time.Time) error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
if state != "sent" && state != "uncertain" && state != "cancelled" {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
switch reason {
|
||||
case "", "smtp_unavailable", "tasks_unavailable", "consent_changed", "transport_uncertain", "extension_disabled":
|
||||
default:
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=? AND state='sending'`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return ErrTaskConflict
|
||||
}
|
||||
res, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=?,updated_ms=? WHERE uuid=? AND state='sending'`, state, reason, now.UnixMilli(), c.AttemptUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err = res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return ErrTaskConflict
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) TaskDigestHistory(ctx context.Context, domain, actor, after string, limit int, now time.Time) (TaskDigestHistory, error) {
|
||||
out := TaskDigestHistory{Items: []TaskDigestAttempt{}, RetentionDays: TaskDigestRetentionDays}
|
||||
p, err := s.TaskPreferences(ctx, domain, actor)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
loc, err := time.LoadLocation(p.Timezone)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.Today = now.In(loc).Format("2006-01-02")
|
||||
out.Enabled = p.DigestEnabled
|
||||
if limit < 1 || limit > 100 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
query := `SELECT ` + digestAttemptColumns + ` FROM organizer_digest_attempts WHERE domain_uuid=? AND owner_uuid=?`
|
||||
args := []any{domain, actor}
|
||||
if after != "" {
|
||||
if len(after) > 256 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
var cursor struct {
|
||||
MS int64
|
||||
ID string
|
||||
}
|
||||
raw, e := base64.RawURLEncoding.DecodeString(after)
|
||||
if e != nil || json.Unmarshal(raw, &cursor) != nil || cursor.MS < 0 || len(cursor.ID) > 80 || cursor.ID == "" {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
query += ` AND (created_ms,uuid)<(?,?)`
|
||||
args = append(args, cursor.MS, cursor.ID)
|
||||
}
|
||||
query += ` ORDER BY created_ms DESC,uuid DESC LIMIT ?`
|
||||
args = append(args, limit+1)
|
||||
rows, err := s.DB.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
for rows.Next() {
|
||||
v, e := scanDigestAttempt(rows)
|
||||
if e != nil {
|
||||
rows.Close()
|
||||
return out, e
|
||||
}
|
||||
out.Items = append(out.Items, v)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if len(out.Items) > limit {
|
||||
out.Items = out.Items[:limit]
|
||||
last := out.Items[limit-1]
|
||||
raw, _ := json.Marshal(struct {
|
||||
MS int64
|
||||
ID string
|
||||
}{last.CreatedMS, last.UUID})
|
||||
out.Next = base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
current, e := scanDigestAttempt(s.DB.QueryRowContext(ctx, `SELECT `+digestAttemptColumns+` FROM organizer_digest_attempts WHERE uuid=(SELECT attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?)`, domain, actor, out.Today))
|
||||
if e == nil {
|
||||
out.Current = ¤t
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return out, e
|
||||
}
|
||||
err = s.DB.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, out.Today).Scan(&out.Retries)
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Only detailed terminal attempts expire. Reservations and request receipts are
|
||||
// permanent deduplication state; retention must never delete them.
|
||||
func (s *Store) MaintainTaskDigests(ctx context.Context, now time.Time) (int64, error) {
|
||||
if s.ReadOnly {
|
||||
return 0, ErrReadOnly
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='uncertain',reason='worker_interrupted',updated_ms=? WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='sending' AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 100)`, now.UnixMilli(), now.Add(-10*time.Minute).UnixMilli())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='uncertain',updated_ms=? WHERE state='sending' AND attempt_uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='uncertain' AND reason='worker_interrupted' AND updated_ms=?)`, now.UnixMilli(), now.UnixMilli())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, `DELETE FROM organizer_digest_attempts WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state IN ('sent','uncertain','cancelled') AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 500)`, now.Add(-TaskDigestRetentionDays*24*time.Hour).UnixMilli())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return n, tx.Commit()
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -28,7 +29,36 @@ var taskProductivityMigrations = []db.SQLMigration{{Version: 1, Name: "task_capt
|
||||
schemaIndex("organizer_work_sessions", "organizer_work_period", "domain_uuid,owner_uuid,start_ms,uuid"),
|
||||
}}, {Version: 2, Name: "personal_dashboard_layout", Statements: []string{
|
||||
`CREATE TABLE organizer_dashboard_layouts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,version INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid))`,
|
||||
}, Assertions: []string{schemaColumns("organizer_dashboard_layouts", "domain_uuid,owner_uuid,version,data"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")}}}
|
||||
}, Assertions: []string{schemaColumns("organizer_dashboard_layouts", "domain_uuid,owner_uuid,version,data"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")}}, {
|
||||
Version: 3, Name: "work_corrections_and_audit",
|
||||
AddColumns: []db.SQLiteColumn{{Table: "organizer_work_sessions", Name: "cancelled_ms", Declaration: "INTEGER NOT NULL DEFAULT 0"}},
|
||||
Statements: []string{`CREATE TABLE organizer_work_revisions(work_uuid TEXT NOT NULL,version INTEGER NOT NULL,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,action TEXT NOT NULL,reason TEXT NOT NULL,changed_ms INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(work_uuid,version))`, `CREATE INDEX organizer_work_owner_period ON organizer_work_sessions(owner_uuid,start_ms,uuid)`},
|
||||
Assertions: []string{schemaColumns("organizer_work_sessions", "uuid,domain_uuid,owner_uuid,task_uuid,start_ms,end_ms,version,cancelled_ms"), schemaUnique("organizer_work_revisions", "work_uuid,version"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")},
|
||||
}, {
|
||||
Version: 4, Name: "digest_attempts_and_retry_receipts",
|
||||
Statements: []string{
|
||||
`ALTER TABLE organizer_digest_deliveries ADD COLUMN attempt_uuid TEXT NOT NULL DEFAULT ''`,
|
||||
`CREATE INDEX organizer_digest_delivery_state ON organizer_digest_deliveries(state,attempt_uuid)`,
|
||||
`CREATE TABLE organizer_digest_attempts(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,day TEXT NOT NULL,kind TEXT NOT NULL,state TEXT NOT NULL,preference_version INTEGER NOT NULL,timezone TEXT NOT NULL,reason TEXT NOT NULL DEFAULT '',created_ms INTEGER NOT NULL,updated_ms INTEGER NOT NULL)`,
|
||||
`CREATE INDEX organizer_digest_history ON organizer_digest_attempts(domain_uuid,owner_uuid,created_ms,uuid)`,
|
||||
`CREATE INDEX organizer_digest_pending ON organizer_digest_attempts(state,updated_ms,uuid)`,
|
||||
`CREATE TABLE organizer_digest_requests(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,day TEXT NOT NULL,expected_attempt TEXT NOT NULL,duplicate_ack INTEGER NOT NULL,created_ms INTEGER NOT NULL)`,
|
||||
`CREATE INDEX organizer_digest_request_day ON organizer_digest_requests(domain_uuid,owner_uuid,day)`,
|
||||
`UPDATE organizer_digest_deliveries SET attempt_uuid='legacy-'||lower(hex(randomblob(16)))`,
|
||||
`INSERT INTO organizer_digest_attempts SELECT attempt_uuid,domain_uuid,owner_uuid,day,'legacy',state,0,'','legacy',updated_ms,updated_ms FROM organizer_digest_deliveries`,
|
||||
}, Assertions: []string{
|
||||
schemaColumns("organizer_work_sessions", "uuid,domain_uuid,owner_uuid,task_uuid,start_ms,end_ms,version,cancelled_ms"),
|
||||
schemaUnique("organizer_work_revisions", "work_uuid,version"),
|
||||
schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid"),
|
||||
schemaColumns("organizer_digest_deliveries", "domain_uuid,owner_uuid,day,state,updated_ms,attempt_uuid"),
|
||||
schemaUnique("organizer_digest_deliveries", "domain_uuid,owner_uuid,day"),
|
||||
schemaColumns("organizer_digest_attempts", "uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,reason,created_ms,updated_ms"),
|
||||
schemaUnique("organizer_digest_attempts", "uuid"),
|
||||
schemaIndex("organizer_digest_attempts", "organizer_digest_pending", "state,updated_ms,uuid"),
|
||||
schemaColumns("organizer_digest_requests", "uuid,domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack,created_ms"),
|
||||
schemaUnique("organizer_digest_requests", "uuid"),
|
||||
},
|
||||
}}
|
||||
|
||||
type TaskPreferences struct {
|
||||
Version int64 `json:"version"`
|
||||
@@ -235,20 +265,21 @@ func (s *Store) CaptureTaskWithWiki(ctx context.Context, domain, actor, request,
|
||||
}
|
||||
|
||||
type TaskWork struct {
|
||||
UUID string `json:"uuid"`
|
||||
TaskUUID string `json:"task_uuid"`
|
||||
StartMS int64 `json:"start_ms"`
|
||||
EndMS int64 `json:"end_ms"`
|
||||
Version int64 `json:"version"`
|
||||
UUID string `json:"uuid"`
|
||||
TaskUUID string `json:"task_uuid"`
|
||||
StartMS int64 `json:"start_ms"`
|
||||
EndMS int64 `json:"end_ms"`
|
||||
Version int64 `json:"version"`
|
||||
CancelledMS int64 `json:"cancelled_ms,omitempty"`
|
||||
}
|
||||
|
||||
func scanTaskWork(row interface{ Scan(...any) error }) (TaskWork, error) {
|
||||
var v TaskWork
|
||||
err := row.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version)
|
||||
err := row.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &v.CancelledMS)
|
||||
return v, err
|
||||
}
|
||||
|
||||
const taskWorkColumns = `uuid,task_uuid,start_ms,end_ms,version`
|
||||
const taskWorkColumns = `uuid,task_uuid,start_ms,end_ms,version,cancelled_ms`
|
||||
|
||||
func (s *Store) ActiveTaskWork(ctx context.Context, domain, actor string) (*TaskWork, error) {
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
@@ -261,7 +292,7 @@ func (s *Store) ActiveTaskWork(ctx context.Context, domain, actor string) (*Task
|
||||
return &v, err
|
||||
}
|
||||
|
||||
func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string, v TaskWork, now time.Time) (TaskWork, error) {
|
||||
func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string, v TaskWork, now time.Time, reasons ...string) (TaskWork, error) {
|
||||
if s.ReadOnly {
|
||||
return v, ErrReadOnly
|
||||
}
|
||||
@@ -280,6 +311,16 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return v, err
|
||||
}
|
||||
if action == "correct" || action == "cancel" {
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
reason := ""
|
||||
if len(reasons) > 0 {
|
||||
reason = reasons[0]
|
||||
}
|
||||
return correctTaskWork(ctx, tx, domain, actor, action, reason, v, old, now)
|
||||
}
|
||||
switch action {
|
||||
case "start":
|
||||
if v.Version != 0 {
|
||||
@@ -300,6 +341,7 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
|
||||
}
|
||||
v.StartMS = now.UnixMilli()
|
||||
v.EndMS = 0
|
||||
v.CancelledMS = 0
|
||||
v.Version = 1
|
||||
res, e := tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_work_sessions(uuid,domain_uuid,owner_uuid,task_uuid,start_ms) VALUES(?,?,?,?,?)`, v.UUID, domain, actor, v.TaskUUID, v.StartMS)
|
||||
if e != nil {
|
||||
@@ -313,6 +355,9 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
if old.CancelledMS != 0 {
|
||||
return v, ErrTaskConflict
|
||||
}
|
||||
if old.EndMS > 0 {
|
||||
if v.Version == old.Version || v.Version == old.Version-1 {
|
||||
return old, tx.Commit()
|
||||
@@ -332,6 +377,14 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
|
||||
default:
|
||||
return v, ErrTaskInvalid
|
||||
}
|
||||
if action == "stop" {
|
||||
if err = auditTaskWork(ctx, tx, domain, actor, "baseline", "", old, now); err != nil {
|
||||
return v, err
|
||||
}
|
||||
}
|
||||
if err = auditTaskWork(ctx, tx, domain, actor, action, "", v, now); err != nil {
|
||||
return v, err
|
||||
}
|
||||
return v, tx.Commit()
|
||||
}
|
||||
|
||||
@@ -344,19 +397,21 @@ type TaskReportDay struct {
|
||||
FocusMS int64 `json:"focus_ms"`
|
||||
}
|
||||
type TaskReportItem struct {
|
||||
UUID string `json:"uuid"`
|
||||
Title string `json:"title"`
|
||||
ProjectUUID string `json:"project_uuid"`
|
||||
WorkMS int64 `json:"work_ms"`
|
||||
Points int `json:"points"`
|
||||
Completed bool `json:"completed"`
|
||||
UUID string `json:"uuid"`
|
||||
Title string `json:"title"`
|
||||
ProjectUUID string `json:"project_uuid"`
|
||||
ProjectTitle string `json:"project_title"`
|
||||
WorkMS int64 `json:"work_ms"`
|
||||
Points int `json:"points"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
type TaskReport struct {
|
||||
Days []TaskReportDay `json:"days"`
|
||||
Tasks []TaskReportItem `json:"tasks"`
|
||||
Sessions []TaskWork `json:"sessions"`
|
||||
Partial bool `json:"partial"`
|
||||
Timezone string `json:"timezone"`
|
||||
Days []TaskReportDay `json:"days"`
|
||||
Tasks []TaskReportItem `json:"tasks"`
|
||||
Sessions []TaskWork `json:"sessions"`
|
||||
Partial bool `json:"partial"`
|
||||
Timezone string `json:"timezone"`
|
||||
Projects []TaskReportProject `json:"projects"`
|
||||
}
|
||||
|
||||
func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end, now time.Time) (TaskReport, error) {
|
||||
@@ -373,7 +428,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
out.Days = append(out.Days, TaskReportDay{Date: day.Format("2006-01-02")})
|
||||
}
|
||||
byTask := map[string]int{}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT m.completed_ms,m.estimate,m.complexity,m.task_uuid,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_task_metrics m LEFT JOIN organizer_items i ON i.domain_uuid=m.domain_uuid AND i.owner_uuid=m.owner_uuid AND i.uuid=m.task_uuid WHERE m.domain_uuid=? AND m.owner_uuid=? AND m.completed_ms>=? AND m.completed_ms<? ORDER BY m.completed_ms,m.task_uuid LIMIT 10001`, domain, actor, start.UnixMilli(), end.UnixMilli())
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT m.completed_ms,m.estimate,m.complexity,m.task_uuid,COALESCE(i.title,''),COALESCE(i.project_uuid,''),COALESCE(p.title,'') FROM organizer_task_metrics m LEFT JOIN organizer_items i ON i.domain_uuid=m.domain_uuid AND i.owner_uuid=m.owner_uuid AND i.uuid=m.task_uuid LEFT JOIN organizer_items p ON p.uuid=i.project_uuid AND p.domain_uuid=m.domain_uuid AND p.owner_uuid=m.owner_uuid AND p.kind='project' WHERE m.domain_uuid=? AND m.owner_uuid=? AND m.completed_ms>=? AND m.completed_ms<? ORDER BY m.completed_ms,m.task_uuid LIMIT 10001`, domain, actor, start.UnixMilli(), end.UnixMilli())
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
@@ -381,8 +436,8 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
for rows.Next() {
|
||||
var ms int64
|
||||
var estimate, complexity int
|
||||
var id, title, project string
|
||||
if err = rows.Scan(&ms, &estimate, &complexity, &id, &title, &project); err != nil {
|
||||
var id, title, project, projectTitle string
|
||||
if err = rows.Scan(&ms, &estimate, &complexity, &id, &title, &project, &projectTitle); err != nil {
|
||||
rows.Close()
|
||||
return out, err
|
||||
}
|
||||
@@ -397,7 +452,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
d.Estimate += estimate
|
||||
d.Points += 1 + ((estimate+29)/30)*max(1, complexity)
|
||||
byTask[id] = len(out.Tasks)
|
||||
out.Tasks = append(out.Tasks, TaskReportItem{UUID: id, Title: title, ProjectUUID: project, Completed: true, Points: 1 + ((estimate+29)/30)*max(1, complexity)})
|
||||
out.Tasks = append(out.Tasks, TaskReportItem{UUID: id, Title: title, ProjectUUID: project, ProjectTitle: projectTitle, Completed: true, Points: 1 + ((estimate+29)/30)*max(1, complexity)})
|
||||
}
|
||||
}
|
||||
err = rows.Err()
|
||||
@@ -428,15 +483,15 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
}
|
||||
return total
|
||||
}
|
||||
rows, err = s.DB.QueryContext(ctx, `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid AND i.uuid=w.task_uuid WHERE w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?) ORDER BY w.start_ms DESC,w.uuid LIMIT 10001`, domain, actor, end.UnixMilli(), start.UnixMilli())
|
||||
rows, err = s.DB.QueryContext(ctx, `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,COALESCE(i.title,''),COALESCE(i.project_uuid,''),COALESCE(p.title,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid AND i.uuid=w.task_uuid LEFT JOIN organizer_items p ON p.uuid=i.project_uuid AND p.domain_uuid=w.domain_uuid AND p.owner_uuid=w.owner_uuid AND p.kind='project' WHERE w.cancelled_ms=0 AND w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?) ORDER BY w.start_ms DESC,w.uuid LIMIT 10001`, domain, actor, end.UnixMilli(), start.UnixMilli())
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
n = 0
|
||||
for rows.Next() {
|
||||
var v TaskWork
|
||||
var title, project string
|
||||
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &title, &project); err != nil {
|
||||
var title, project, projectTitle string
|
||||
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &title, &project, &projectTitle); err != nil {
|
||||
rows.Close()
|
||||
return out, err
|
||||
}
|
||||
@@ -458,7 +513,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
if !ok {
|
||||
index = len(out.Tasks)
|
||||
byTask[v.TaskUUID] = index
|
||||
out.Tasks = append(out.Tasks, TaskReportItem{UUID: v.TaskUUID, Title: title, ProjectUUID: project})
|
||||
out.Tasks = append(out.Tasks, TaskReportItem{UUID: v.TaskUUID, Title: title, ProjectUUID: project, ProjectTitle: projectTitle})
|
||||
}
|
||||
out.Tasks[index].WorkMS += elapsed
|
||||
}
|
||||
@@ -485,5 +540,39 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
|
||||
}
|
||||
addSpan(a, b, true)
|
||||
}
|
||||
return out, rows.Err()
|
||||
if err = rows.Err(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
groups := map[string]*TaskReportProject{}
|
||||
for _, task := range out.Tasks {
|
||||
p := groups[task.ProjectUUID]
|
||||
if p == nil {
|
||||
p = &TaskReportProject{UUID: task.ProjectUUID, Title: task.ProjectTitle}
|
||||
groups[task.ProjectUUID] = p
|
||||
}
|
||||
if task.Completed {
|
||||
p.Completed++
|
||||
}
|
||||
p.Points += task.Points
|
||||
p.WorkMS += task.WorkMS
|
||||
}
|
||||
out.Projects = []TaskReportProject{}
|
||||
for _, p := range groups {
|
||||
out.Projects = append(out.Projects, *p)
|
||||
}
|
||||
sort.Slice(out.Projects, func(i, j int) bool {
|
||||
if out.Projects[i].Title != out.Projects[j].Title {
|
||||
return out.Projects[i].Title < out.Projects[j].Title
|
||||
}
|
||||
return out.Projects[i].UUID < out.Projects[j].UUID
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type TaskReportProject struct {
|
||||
UUID string `json:"uuid"`
|
||||
Title string `json:"title"`
|
||||
Completed int `json:"completed"`
|
||||
Points int `json:"points"`
|
||||
WorkMS int64 `json:"work_ms"`
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type TaskWorkRevision struct {
|
||||
Item TaskWork `json:"item"`
|
||||
Action string `json:"action"`
|
||||
Reason string `json:"reason"`
|
||||
ChangedMS int64 `json:"changed_ms"`
|
||||
}
|
||||
|
||||
func auditTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v TaskWork, now time.Time) error {
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Legacy sessions acquire a baseline lazily. Never rewrite an earlier audit.
|
||||
query := `INSERT INTO organizer_work_revisions(work_uuid,version,domain_uuid,owner_uuid,action,reason,changed_ms,data) VALUES(?,?,?,?,?,?,?,?)`
|
||||
if action == "baseline" {
|
||||
query += ` ON CONFLICT(work_uuid,version) DO NOTHING`
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, query, v.UUID, v.Version, domain, actor, action, reason, now.UnixMilli(), string(raw))
|
||||
return err
|
||||
}
|
||||
|
||||
func correctTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v, old TaskWork, now time.Time) (TaskWork, error) {
|
||||
reason = strings.TrimSpace(reason)
|
||||
if reason == "" || len(reason) > 500 || !utf8.ValidString(reason) || strings.ContainsAny(reason, "\x00\r\n") {
|
||||
return v, ErrTaskInvalid
|
||||
}
|
||||
if v.Version < 1 || old.EndMS == 0 || old.CancelledMS != 0 {
|
||||
return v, ErrTaskConflict
|
||||
}
|
||||
if old.Version != v.Version {
|
||||
return v, ErrTaskConflict
|
||||
}
|
||||
if v.TaskUUID != "" && v.TaskUUID != old.TaskUUID {
|
||||
return v, ErrTaskInvalid
|
||||
}
|
||||
result := old
|
||||
if action == "correct" {
|
||||
if v.StartMS <= 0 || v.EndMS <= v.StartMS || v.EndMS > now.UnixMilli() || v.EndMS-v.StartMS > int64(31*24*time.Hour/time.Millisecond) {
|
||||
return v, ErrTaskInvalid
|
||||
}
|
||||
// Adjacent spans are valid; double-counted elapsed time is not. Scope across
|
||||
// Domains like the existing one-active-session constraint, without disclosing them.
|
||||
var overlap bool
|
||||
err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_work_sessions WHERE owner_uuid=? AND uuid<>? AND cancelled_ms=0 AND start_ms<? AND (end_ms=0 OR end_ms>?))`, actor, old.UUID, v.EndMS, v.StartMS).Scan(&overlap)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
if overlap {
|
||||
return v, ErrTaskConflict
|
||||
}
|
||||
result.StartMS = v.StartMS
|
||||
result.EndMS = v.EndMS
|
||||
} else {
|
||||
result.CancelledMS = now.UnixMilli()
|
||||
}
|
||||
if err := auditTaskWork(ctx, tx, domain, actor, "baseline", "", old, now); err != nil {
|
||||
return v, err
|
||||
}
|
||||
result.Version++
|
||||
updated, err := tx.ExecContext(ctx, `UPDATE organizer_work_sessions SET start_ms=?,end_ms=?,cancelled_ms=?,version=? WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND version=?`, result.StartMS, result.EndMS, result.CancelledMS, result.Version, old.UUID, domain, actor, old.Version)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
if count, err := updated.RowsAffected(); err != nil {
|
||||
return v, err
|
||||
} else if count != 1 {
|
||||
return v, ErrTaskConflict
|
||||
}
|
||||
if err := auditTaskWork(ctx, tx, domain, actor, action, reason, result, now); err != nil {
|
||||
return v, err
|
||||
}
|
||||
return result, tx.Commit()
|
||||
}
|
||||
|
||||
type TaskWorkEntry struct {
|
||||
TaskWork
|
||||
Title string `json:"title"`
|
||||
ProjectUUID string `json:"project_uuid"`
|
||||
}
|
||||
type TaskWorkPage struct {
|
||||
Items []TaskWorkEntry `json:"items"`
|
||||
Next string `json:"next"`
|
||||
}
|
||||
type workCursor struct {
|
||||
Start int64 `json:"start"`
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
|
||||
func (s *Store) TaskWorkHistory(ctx context.Context, domain, actor string, start, end time.Time, after string, limit int) (TaskWorkPage, error) {
|
||||
out := TaskWorkPage{Items: []TaskWorkEntry{}}
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if !end.After(start) || end.Sub(start) > 367*24*time.Hour || limit < 1 || limit > 100 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
query := `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,w.cancelled_ms,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.uuid=w.task_uuid AND i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid WHERE w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?)`
|
||||
args := []any{domain, actor, end.UnixMilli(), start.UnixMilli()}
|
||||
if after != "" {
|
||||
if len(after) > 256 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(after)
|
||||
var cursor workCursor
|
||||
if err != nil || json.Unmarshal(raw, &cursor) != nil || cursor.Start <= 0 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
if id, err := uuid.Parse(cursor.UUID); err != nil || id.String() != cursor.UUID {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
query += ` AND (w.start_ms,w.uuid)<(?,?)`
|
||||
args = append(args, cursor.Start, cursor.UUID)
|
||||
}
|
||||
query += ` ORDER BY w.start_ms DESC,w.uuid DESC LIMIT ?`
|
||||
args = append(args, limit+1)
|
||||
rows, err := s.DB.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var v TaskWorkEntry
|
||||
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &v.CancelledMS, &v.Title, &v.ProjectUUID); err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.Items = append(out.Items, v)
|
||||
}
|
||||
if len(out.Items) > limit {
|
||||
out.Items = out.Items[:limit]
|
||||
last := out.Items[limit-1]
|
||||
raw, _ := json.Marshal(workCursor{Start: last.StartMS, UUID: last.UUID})
|
||||
out.Next = base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) TaskWorkRevisions(ctx context.Context, domain, actor, id string, before int64) ([]TaskWorkRevision, error) {
|
||||
out := []TaskWorkRevision{}
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if before < 0 {
|
||||
return out, ErrTaskInvalid
|
||||
}
|
||||
current, err := scanTaskWork(s.DB.QueryRowContext(ctx, `SELECT `+taskWorkColumns+` FROM organizer_work_sessions WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, id, domain, actor))
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if before == 0 {
|
||||
before = current.Version + 1
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT action,reason,changed_ms,data FROM organizer_work_revisions WHERE work_uuid=? AND domain_uuid=? AND owner_uuid=? AND version<? ORDER BY version DESC LIMIT 100`, id, domain, actor, before)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var v TaskWorkRevision
|
||||
var raw string
|
||||
if err = rows.Scan(&v.Action, &v.Reason, &v.ChangedMS, &raw); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if err = json.Unmarshal([]byte(raw), &v.Item); err != nil {
|
||||
return out, err
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
if len(out) == 0 && before > current.Version {
|
||||
out = append(out, TaskWorkRevision{Item: current, Action: "baseline"})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
Reference in new issue
Block a user