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,104 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func sendTaskDigestSMTP(ctx context.Context, config store.AuthProviderConfig, recipient, day, body string) error {
|
||||
// Reuse envelope validation and bounded TLS-aware SMTP transport, without
|
||||
// exposing SMTP credentials, recipient or task content to job logs.
|
||||
host, from, to, _, err := smtpMessage(config, recipient, "https://pages.invalid/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sender, _ := smtpMailbox(config.SMTPFrom)
|
||||
addressee, _ := smtpMailbox(recipient)
|
||||
if len(body) > 128<<10 || len(day) != 10 || strings.ContainsAny(day, "\r\n\x00") {
|
||||
return store.ErrTaskInvalid
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(body))
|
||||
var lines strings.Builder
|
||||
for len(encoded) > 76 {
|
||||
lines.WriteString(encoded[:76] + "\r\n")
|
||||
encoded = encoded[76:]
|
||||
}
|
||||
lines.WriteString(encoded + "\r\n")
|
||||
message := []byte(fmt.Sprintf("To: %s\r\nFrom: %s\r\nSubject: PagesWiki tasks %s\r\nDate: %s\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n%s", addressee.String(), sender.String(), day, time.Now().Format(time.RFC1123Z), lines.String()))
|
||||
return deliverSMTPMessage(ctx, config, host, from, to, message, nil)
|
||||
}
|
||||
|
||||
func (s *Server) deliverTaskDigests(ctx context.Context, job *store.EventJob) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
due, err := s.store.DueTaskDigests(ctx, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sent, uncertain := 0, 0
|
||||
for _, candidate := range due {
|
||||
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))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user