Add private CardDAV synchronization and contact revision recovery
This commit is contained in:
1 parent
433169c7e7
commit
19e12f96a8
32 files changed
+1855
-47
No files matched your search
@@ -12,7 +12,9 @@ import (
|
||||
|
||||
const contactsAPI = "/_/api/v1/extension/organizer.contacts/"
|
||||
|
||||
func contactRoute(p string) bool { return p == "/v/plan/contacts" || strings.HasPrefix(p, contactsAPI) }
|
||||
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)
|
||||
@@ -42,6 +44,98 @@ func (s *Server) handleContacts(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user