Files
pages/internal/server/contacts.go
T
Anya Tokugawa b4792bb175
Build and publish container images / images (push) Successful in 16m37s
Add task capture, work reports, daily digests and private contacts
2026-11-01 22:28:16 +09:00

134 lines
3.7 KiB
Go

package server
import (
"encoding/json"
"github.com/google/uuid"
"io"
"net/http"
"pageswiki/internal/store"
"strconv"
"strings"
)
const contactsAPI = "/_/api/v1/extension/organizer.contacts/"
func contactRoute(p string) bool { return p == "/v/plan/contacts" || strings.HasPrefix(p, contactsAPI) }
func (s *Server) handleContacts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-store")
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "organizer.contacts") {
http.NotFound(w, r)
return
}
actor, active := s.calendarActor(r)
if actor == "" {
s.authRequired(w, r)
return
}
if r.Method != "GET" && r.Method != "HEAD" && !active {
taskError(w, store.ErrAccessDenied)
return
}
if r.URL.Path == "/v/plan/contacts" {
if r.Method != "GET" && r.Method != "HEAD" {
w.WriteHeader(405)
return
}
if _, _, e := s.store.Contacts(r.Context(), domain, actor, "", ""); e != nil {
taskError(w, e)
return
}
s.renderWithContext(w, r, "contacts.gohtml", map[string]any{"Title": "Contacts", "ContactWritable": active, "Path": r.URL.Path})
return
}
key := strings.TrimPrefix(r.URL.Path, contactsAPI)
if key == "items" && r.Method == "GET" {
items, next, e := s.store.ListContacts(r.Context(), domain, actor, r.URL.Query().Get("q"), r.URL.Query().Get("after"), r.URL.Query().Get("deleted") == "1")
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"items": items, "next": next})
return
}
if key == "import" && r.Method == "POST" {
raw, e := io.ReadAll(http.MaxBytesReader(w, r.Body, 65536))
if e != nil {
taskError(w, store.ErrTaskInvalid)
return
}
c, e := s.store.SaveContact(r.Context(), domain, actor, store.Contact{UUID: uuid.NewString(), VCard: string(raw)}, true)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 201, map[string]any{"item": c})
return
}
id := strings.TrimPrefix(key, "items/")
export := strings.HasSuffix(id, ".vcf")
if export {
id = strings.TrimSuffix(id, ".vcf")
}
if key != "items" {
if u, e := uuid.Parse(id); e != nil || u.String() != id {
http.NotFound(w, r)
return
}
}
if key != "items" && (r.Method == "GET" || r.Method == "HEAD") {
c, e := s.store.Contact(r.Context(), domain, actor, id)
if e != nil {
taskError(w, e)
return
}
w.Header().Set("ETag", `"`+strconv.FormatInt(c.Version, 10)+`"`)
if export {
w.Header().Set("Content-Type", "text/vcard; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="contact.vcf"`)
w.Write([]byte(c.VCard))
} else {
writeEventJSON(w, 200, map[string]any{"item": c})
}
return
}
if !export && (key == "items" && r.Method == "POST" || key != "items" && r.Method == "PUT") {
var c store.Contact
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 128<<10))
decoder.DisallowUnknownFields()
if decoder.Decode(&c) != nil || decoder.Decode(&struct{}{}) != io.EOF {
taskError(w, store.ErrTaskInvalid)
return
}
if key == "items" {
if c.Version != 0 {
taskError(w, store.ErrTaskInvalid)
return
}
} else {
if c.UUID != id {
taskError(w, store.ErrTaskInvalid)
return
}
if r.Header.Get("If-Match") == "" {
w.WriteHeader(428)
return
}
if r.Header.Get("If-Match") != `"`+strconv.FormatInt(c.Version, 10)+`"` {
taskError(w, store.ErrTaskConflict)
return
}
}
c, e := s.store.SaveContact(r.Context(), domain, actor, c, false)
if e != nil {
taskError(w, e)
return
}
w.Header().Set("ETag", `"`+strconv.FormatInt(c.Version, 10)+`"`)
writeEventJSON(w, 200, map[string]any{"item": c})
return
}
w.Header().Set("Allow", "GET, HEAD, POST, PUT")
w.WriteHeader(405)
}