Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) handleTaskNote(w http.ResponseWriter, r *http.Request, actor string, active bool, id string) {
|
|
if r.Method != "GET" && r.Method != "POST" {
|
|
w.Header().Set("Allow", "GET, POST")
|
|
w.WriteHeader(405)
|
|
return
|
|
}
|
|
domain := s.domainUUIDForRequest(r)
|
|
item, err := s.store.GetTask(r.Context(), domain, actor, id)
|
|
if err != nil {
|
|
taskError(w, err)
|
|
return
|
|
}
|
|
if item.Kind != "task" {
|
|
taskError(w, store.ErrTaskInvalid)
|
|
return
|
|
}
|
|
if r.Method == "POST" {
|
|
if !active || item.Archived {
|
|
taskError(w, store.ErrAccessDenied)
|
|
return
|
|
}
|
|
if r.Header.Get("If-Match") == "" {
|
|
writeEventJSON(w, 428, map[string]string{"error": "precondition"})
|
|
return
|
|
}
|
|
if r.Header.Get("If-Match") != `"`+strconv.FormatInt(item.Version, 10)+`"` {
|
|
taskError(w, store.ErrTaskConflict)
|
|
return
|
|
}
|
|
}
|
|
target := s.taskPagePath(r, item.PageUUID)
|
|
linked := item.PageUUID != ""
|
|
var project store.TaskRecord
|
|
var base string
|
|
if linked && target == "" {
|
|
taskError(w, store.ErrAccessDenied)
|
|
return
|
|
}
|
|
if !linked {
|
|
if item.ProjectUUID != "" {
|
|
project, err = s.store.GetTask(r.Context(), domain, actor, item.ProjectUUID)
|
|
}
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
taskError(w, err)
|
|
return
|
|
}
|
|
base = s.taskPagePath(r, project.PageUUID)
|
|
if project.Kind != "project" || project.Archived || base == "" {
|
|
writeEventJSON(w, 422, map[string]string{"error": "note_setup"})
|
|
return
|
|
}
|
|
target = store.TaskNotePath(base, project, item)
|
|
}
|
|
// Task ownership does not bypass Wiki read/create/write permissions.
|
|
if s.authorizePathRequest(r, "read", target) != nil {
|
|
taskError(w, store.ErrAccessDenied)
|
|
return
|
|
}
|
|
writable := active && !item.Archived && s.authorizePathRequest(r, "write", target) == nil
|
|
if !linked {
|
|
writable = writable && s.authorizePathRequest(r, "create", target) == nil
|
|
}
|
|
exists := linked
|
|
if !linked {
|
|
_, err = s.store.ResolvePathInDomain(domain, target)
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
taskError(w, err)
|
|
return
|
|
}
|
|
exists = err == nil
|
|
}
|
|
if r.Method == "POST" {
|
|
if !writable {
|
|
taskError(w, store.ErrAccessDenied)
|
|
return
|
|
}
|
|
if !linked {
|
|
var input struct {
|
|
Path string `json:"path"`
|
|
}
|
|
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(&input) != nil || decoder.Decode(&struct{}{}) != io.EOF {
|
|
taskError(w, store.ErrTaskInvalid)
|
|
return
|
|
}
|
|
if input.Path != target {
|
|
taskError(w, store.ErrTaskConflict)
|
|
return
|
|
}
|
|
if exists {
|
|
taskError(w, store.ErrTaskNoteExists)
|
|
return
|
|
}
|
|
item, err = s.store.CreateTaskNote(r.Context(), domain, actor, item, item.Version, project, base, target)
|
|
if err != nil {
|
|
taskError(w, err)
|
|
return
|
|
}
|
|
linked, exists = true, true
|
|
}
|
|
}
|
|
escaped := (&url.URL{Path: target}).EscapedPath()
|
|
editURL := "/e/-" + escaped
|
|
if linked && writable {
|
|
if actions, e := s.adapterActionsForPath(r, target); e == nil {
|
|
for _, a := range actions {
|
|
if a.Mode == "editor" && a.Default {
|
|
editURL = a.OpenURL
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
w.Header().Set("ETag", `"`+strconv.FormatInt(item.Version, 10)+`"`)
|
|
writeEventJSON(w, 200, map[string]any{"item": item, "path": target, "url": escaped, "edit_url": editURL, "linked": linked, "exists": exists, "writable": writable})
|
|
}
|