228 lines
6.1 KiB
Go
228 lines
6.1 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 contactDAVRoute(p) || 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 == "export.vcf" && (r.Method == "GET" || r.Method == "HEAD") {
|
|
raw, e := s.store.ExportContacts(r.Context(), domain, actor)
|
|
if e != nil {
|
|
taskError(w, e)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/vcard; charset=utf-8")
|
|
w.Header().Set("Content-Disposition", `attachment; filename="contacts.vcf"`)
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(raw)))
|
|
if r.Method == "GET" {
|
|
io.WriteString(w, raw)
|
|
}
|
|
return
|
|
}
|
|
parts := strings.Split(key, "/")
|
|
if len(parts) >= 3 && parts[0] == "items" && (parts[2] == "history" || parts[2] == "restore") {
|
|
id, e := uuid.Parse(parts[1])
|
|
if e != nil || id.String() != parts[1] {
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
if parts[2] == "history" && r.Method == "GET" {
|
|
if len(parts) == 4 {
|
|
revision, e := strconv.ParseInt(parts[3], 10, 64)
|
|
if e != nil || revision < 1 {
|
|
w.WriteHeader(400)
|
|
return
|
|
}
|
|
c, e := s.store.ContactAtRevision(r.Context(), domain, actor, parts[1], revision)
|
|
if e != nil {
|
|
taskError(w, e)
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, map[string]any{"item": c})
|
|
return
|
|
}
|
|
if len(parts) != 3 {
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
before := int64(0)
|
|
if v := r.URL.Query().Get("before"); v != "" {
|
|
before, e = strconv.ParseInt(v, 10, 64)
|
|
if e != nil || before < 1 {
|
|
w.WriteHeader(400)
|
|
return
|
|
}
|
|
}
|
|
items, e := s.store.ContactHistory(r.Context(), domain, actor, parts[1], before)
|
|
if e != nil {
|
|
taskError(w, e)
|
|
return
|
|
}
|
|
next := int64(0)
|
|
if len(items) == 100 {
|
|
next = items[99].Version
|
|
}
|
|
writeEventJSON(w, 200, map[string]any{"items": items, "next": next})
|
|
return
|
|
}
|
|
if parts[2] == "restore" && len(parts) == 3 && r.Method == "POST" {
|
|
var input struct {
|
|
Version int64 `json:"version"`
|
|
Revision int64 `json:"revision"`
|
|
}
|
|
if !taskJSON(w, r, &input) {
|
|
return
|
|
}
|
|
if input.Version < 1 || input.Revision < 1 {
|
|
w.WriteHeader(400)
|
|
return
|
|
}
|
|
if r.Header.Get("If-Match") == "" {
|
|
w.WriteHeader(428)
|
|
return
|
|
}
|
|
if r.Header.Get("If-Match") != `"`+strconv.FormatInt(input.Version, 10)+`"` {
|
|
w.WriteHeader(412)
|
|
return
|
|
}
|
|
c, e := s.store.RestoreContactRevision(r.Context(), domain, actor, parts[1], input.Revision, input.Version)
|
|
if e != nil {
|
|
taskError(w, e)
|
|
return
|
|
}
|
|
w.Header().Set("ETag", store.ContactETag(c))
|
|
writeEventJSON(w, 200, map[string]any{"item": c})
|
|
return
|
|
}
|
|
w.WriteHeader(405)
|
|
return
|
|
}
|
|
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)
|
|
}
|