151 lines
5.0 KiB
Go
151 lines
5.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var ErrTaskDAVCollectionExists = errors.New("DAV collection already exists")
|
|
|
|
// Native projects derive their stable collection URI from their canonical UUID.
|
|
// Explicit MKCALENDAR paths keep their original identity. The reserved tasks/
|
|
// namespace cannot shadow a VEVENT calendar (including an external source).
|
|
func (v TaskRecord) TaskDAVCollectionPath() string {
|
|
if v.DAVCollection != "" {
|
|
return v.DAVCollection
|
|
}
|
|
return "tasks/projects/" + v.UUID
|
|
}
|
|
|
|
// Collection metadata lives in a normal, revisioned project document. This
|
|
// table is only its routing/uniqueness index, rebuilt from canonical heads.
|
|
func (s *Store) CreateTaskDAVCollection(ctx context.Context, domain, actor, path, title, credential string) (TaskRecord, error) {
|
|
id, err := uuid.Parse(path)
|
|
if err != nil || id.String() != path {
|
|
return TaskRecord{}, ErrTaskInvalid
|
|
}
|
|
item := TaskRecord{UUID: uuid.NewString(), Kind: "project", Title: title, Status: "todo", Bucket: "inbox"}
|
|
return s.saveTask(ctx, domain, actor, item, 0, func(tx *sql.Tx, v *TaskRecord, old TaskRecord) error {
|
|
if credential != "" {
|
|
if err := s.taskDAVCredentialAllowed(ctx, tx, domain, actor, credential, true); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
var exists bool
|
|
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_dav_collections WHERE domain_uuid=`+s.sqlParam(1)+` AND path=`+s.sqlParam(2)+`)`, domain, path).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return ErrTaskDAVCollectionExists
|
|
}
|
|
// Never shadow even a private calendar owned by another principal. Only
|
|
// inspect directory existence; do not read calendar metadata or events.
|
|
if s.DocumentRoot != "" {
|
|
_, err := os.Stat(filepath.Join(s.DocumentRoot, "calendars", domain, path))
|
|
if err == nil {
|
|
return ErrTaskDAVCollectionExists
|
|
}
|
|
if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
}
|
|
var count int
|
|
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM organizer_dav_collections WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+``, domain, actor).Scan(&count); err != nil {
|
|
return err
|
|
}
|
|
if count >= 256 {
|
|
return ErrTaskDAVLimit
|
|
}
|
|
v.DAVCollection = path
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *Store) IsTaskDAVCollection(ctx context.Context, domain, path string) (bool, error) {
|
|
var found bool
|
|
err := s.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_dav_collections WHERE domain_uuid=`+s.sqlParam(1)+` AND path=`+s.sqlParam(2)+`)`, domain, path).Scan(&found)
|
|
return found, err
|
|
}
|
|
|
|
func (s *Store) TaskDAVCollection(ctx context.Context, domain, actor, path string) (TaskRecord, error) {
|
|
var project string
|
|
if err := s.DB.QueryRowContext(ctx, `SELECT project_uuid FROM organizer_dav_collections WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND path=`+s.sqlParam(3)+``, domain, actor, path).Scan(&project); err != nil {
|
|
return TaskRecord{}, err
|
|
}
|
|
v, err := s.GetTask(ctx, domain, actor, project)
|
|
if err == nil && v.Archived {
|
|
return TaskRecord{}, sql.ErrNoRows
|
|
}
|
|
return v, err
|
|
}
|
|
|
|
func (s *Store) TaskDAVCollections(ctx context.Context, domain, actor string) ([]TaskRecord, error) {
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT i.uuid FROM organizer_dav_collections c JOIN organizer_items i ON i.domain_uuid=c.domain_uuid AND i.uuid=c.project_uuid AND i.owner_uuid=c.owner_uuid WHERE c.domain_uuid=`+s.sqlParam(1)+` AND c.owner_uuid=`+s.sqlParam(2)+` AND i.archived=0 ORDER BY c.path LIMIT 257`, domain, actor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var ids []string
|
|
for rows.Next() {
|
|
var id string
|
|
if err = rows.Scan(&id); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(ids) > 256 {
|
|
return nil, ErrTaskDAVLimit
|
|
}
|
|
items := make([]TaskRecord, 0, len(ids))
|
|
for _, id := range ids {
|
|
v, err := s.GetTask(ctx, domain, actor, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !v.Archived {
|
|
items = append(items, v)
|
|
}
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (s *Store) taskDAVIndexKey(project, value string) string {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return nativeTaskDAVIndexKey(project, value)
|
|
}
|
|
if project == "" {
|
|
return value
|
|
}
|
|
// A NUL prefix cannot occur in an accepted UID/name, avoiding collisions
|
|
// between default-list UIDs and collection-qualified index keys.
|
|
return "\x00" + project + "\x00" + value
|
|
}
|
|
|
|
func (s *Store) taskDAVProjectTx(ctx context.Context, tx *sql.Tx, domain, actor, project string) (string, error) {
|
|
if project == "" {
|
|
return "", nil
|
|
}
|
|
var found bool
|
|
err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_dav_collections WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND project_uuid=`+s.sqlParam(3)+`)`, domain, actor, project).Scan(&found)
|
|
if found {
|
|
return project, err
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "", sql.ErrNoRows
|
|
}
|