81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"html"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func ValidTaskWikiPrefix(p string) bool {
|
|
if p == "" {
|
|
return true
|
|
}
|
|
if len(p) > 500 || !utf8.ValidString(p) || !strings.HasPrefix(p, "/") || path.Clean(p) != p || strings.ContainsAny(p, "\\?#%:") || strings.IndexFunc(p, unicode.IsControl) >= 0 {
|
|
return false
|
|
}
|
|
for _, part := range strings.Split(p, "/") {
|
|
if part == "_" || part == ".." || part == "." {
|
|
return false
|
|
}
|
|
}
|
|
first := strings.Split(strings.TrimPrefix(p, "/"), "/")[0]
|
|
switch first {
|
|
case "-", "v", "e", "o", "r", "static", "raw":
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// A request UUID suffix separates equal subjects and makes the target stable.
|
|
func TaskCaptureWikiPath(prefix, title, request string) string {
|
|
item := TaskRecord{Title: title, UUID: request}
|
|
name := path.Base(TaskNotePath("", TaskRecord{}, item))
|
|
return strings.TrimRight(prefix, "/") + "/" + name + "--" + request
|
|
}
|
|
|
|
func (s *Store) createCapturedTaskWiki(ctx context.Context, tx *sql.Tx, domain, actor string, item *TaskRecord, target, body string) error {
|
|
if target == "" || !ValidTaskWikiPrefix(path.Dir(target)) || path.Clean(target) != target || item.PageUUID != "" || len(body) > 256<<10 || !utf8.ValidString(body) || strings.IndexByte(body, 0) >= 0 {
|
|
return ErrTaskInvalid
|
|
}
|
|
var exists bool
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM paths WHERE domain_uuid=? AND path=?)`, domain, target).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return ErrTaskNoteExists
|
|
}
|
|
item.PageUUID = uuid.NewString()
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',strftime('%s','now'))`, item.PageUUID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO paths(domain_uuid,path,object_uuid,created_at) VALUES(?,?,?,strftime('%s','now'))`, domain, target, item.PageUUID); err != nil {
|
|
return err
|
|
}
|
|
// Mail is data, never HTML or executable wiki macros (including {{...}}).
|
|
escape := func(v string) string {
|
|
return strings.NewReplacer("{", "{", "}", "}").Replace(html.EscapeString(v))
|
|
}
|
|
content := "<h1>" + escape(item.Title) + "</h1>\n<pre>" + escape(body) + "</pre>\n"
|
|
now := time.Now()
|
|
rev := &Revision{UUID: uuid.NewString(), ObjectUUID: item.PageUUID, DocumentHash: revisionHash(content), Language: "und", ContentType: "text/html; charset=utf-8", Body: content, Message: "Capture mail as task Wiki", CreatedAt: now.Unix(), CreatedAtMS: now.UnixMilli()}
|
|
if err := s.persistDocument(rev.DocumentHash, rev.Body); err != nil {
|
|
return err
|
|
}
|
|
if err := insertRevision(tx, rev, nil); err != nil {
|
|
return err
|
|
}
|
|
if err := s.appendRevisionActivityOutboxIfAvailable(tx, rev); err != nil {
|
|
return err
|
|
}
|
|
payload, _ := json.Marshal(map[string]string{"path": target, "object_uuid": item.PageUUID})
|
|
return s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{DomainUUID: domain, ActorUUID: actor, EventType: "page.created", TargetKind: "page", TargetUUID: item.PageUUID, Payload: string(payload), IdempotencyKey: "page.created:" + item.PageUUID})
|
|
}
|