Add task capture, work reports, daily digests and private contacts
Build and publish container images / images (push) Successful in 16m37s
Build and publish container images / images (push) Successful in 16m37s
This commit is contained in:
1 parent
50b8b0323f
commit
b4792bb175
50 files changed
+2678
-17
No files matched your search
@@ -0,0 +1,180 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
const TaskDigestEventType = "task.digest.deliver"
|
||||
|
||||
type TaskDigestDue struct {
|
||||
Domain string
|
||||
Owner string
|
||||
}
|
||||
type TaskDigestClaim struct {
|
||||
TaskDigestDue
|
||||
Day string
|
||||
Version int64
|
||||
Timezone string
|
||||
}
|
||||
|
||||
func (s *Store) EnsureTaskDigestEvent() error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms) VALUES('builtin.task.digest','Daily task email',?,'local','{}','interval',60,?,1,-2,1,?,?)`, TaskDigestEventType, now, now, now)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) DueTaskDigests(ctx context.Context, now time.Time) ([]TaskDigestDue, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT domain_uuid,owner_uuid FROM organizer_task_preferences WHERE next_digest_ms>0 AND next_digest_ms<=? ORDER BY next_digest_ms,domain_uuid,owner_uuid LIMIT 20`, now.UnixMilli())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []TaskDigestDue{}
|
||||
for rows.Next() {
|
||||
var v TaskDigestDue
|
||||
if err = rows.Scan(&v.Domain, &v.Owner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Claim before SMTP: an ambiguous delivery is never automatically sent twice.
|
||||
// An interrupted claim stays visible as "sending" for operator review.
|
||||
func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time.Time) (TaskDigestClaim, error) {
|
||||
out := TaskDigestClaim{TaskDigestDue: due}
|
||||
if s.ReadOnly {
|
||||
return out, ErrReadOnly
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
res, err := tx.ExecContext(ctx, `UPDATE organizer_task_preferences SET next_digest_ms=next_digest_ms WHERE domain_uuid=? AND owner_uuid=? AND next_digest_ms>0 AND next_digest_ms<=?`, due.Domain, due.Owner, now.UnixMilli())
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n != 1 {
|
||||
return out, sql.ErrNoRows
|
||||
}
|
||||
var raw string
|
||||
if err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, due.Domain, due.Owner).Scan(&raw); err != nil {
|
||||
return out, err
|
||||
}
|
||||
var p TaskPreferences
|
||||
if err = json.Unmarshal([]byte(raw), &p); err != nil {
|
||||
return out, err
|
||||
}
|
||||
loc, err := time.LoadLocation(p.Timezone)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.Day = now.In(loc).Format("2006-01-02")
|
||||
out.Timezone = p.Timezone
|
||||
out.Version = p.Version
|
||||
allowed := taskActor(ctx, tx, due.Domain, due.Owner, true)
|
||||
on, e := taskReminderExtensionEnabled(ctx, tx, due.Domain)
|
||||
if e != nil {
|
||||
return out, e
|
||||
}
|
||||
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
|
||||
return out, allowed
|
||||
}
|
||||
if allowed != nil || !on || !p.DigestEnabled {
|
||||
// An opted-in schedule resumes after a domain or extension is restored.
|
||||
next, e := nextTaskDigest(now, p)
|
||||
if e != nil {
|
||||
return out, e
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE organizer_task_preferences SET next_digest_ms=? WHERE domain_uuid=? AND owner_uuid=?`, next, due.Domain, due.Owner)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
return out, sql.ErrNoRows
|
||||
}
|
||||
// Missed days are not replayed. Run once for today's due time; if the
|
||||
// configured hour has not arrived, move the cursor there without sending.
|
||||
clock, _ := time.Parse("15:04", p.DigestTime)
|
||||
local := now.In(loc)
|
||||
today := time.Date(local.Year(), local.Month(), local.Day(), clock.Hour(), clock.Minute(), 0, 0, loc)
|
||||
next, err := nextTaskDigest(now, p)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE organizer_task_preferences SET next_digest_ms=? WHERE domain_uuid=? AND owner_uuid=?`, next, due.Domain, due.Owner)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if today.After(now) {
|
||||
if err = tx.Commit(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
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())
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
n, _ = res.RowsAffected()
|
||||
if err = tx.Commit(); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if n != 1 {
|
||||
return out, sql.ErrNoRows
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (string, error) {
|
||||
if err := taskActor(ctx, s.DB, 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)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var p TaskPreferences
|
||||
if json.Unmarshal([]byte(raw), &p) != nil || !p.DigestEnabled || version != c.Version {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Store) LastTaskDigest(ctx context.Context, domain, actor string) (map[string]any, error) {
|
||||
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var day, state string
|
||||
var ms int64
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT day,state,updated_ms FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? ORDER BY day DESC LIMIT 1`, domain, actor).Scan(&day, &state, &ms)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"day": day, "state": state, "updated_at_ms": ms}, nil
|
||||
}
|
||||
Reference in new issue
Block a user