2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
package caldav
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
"code.vikunja.io/api/pkg/models"
|
2020-01-26 18:08:06 +01:00
|
|
|
user2 "code.vikunja.io/api/pkg/user"
|
2019-05-22 19:48:48 +02:00
|
|
|
"github.com/samedi/caldav-go/data"
|
|
|
|
"github.com/samedi/caldav-go/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DavBasePath is the base url path
|
|
|
|
const DavBasePath = `/dav/`
|
|
|
|
|
|
|
|
// ListBasePath is the base path for all lists resources
|
|
|
|
const ListBasePath = DavBasePath + `lists`
|
|
|
|
|
|
|
|
// VikunjaCaldavListStorage represents a list storage
|
|
|
|
type VikunjaCaldavListStorage struct {
|
|
|
|
// Used when handling a list
|
|
|
|
list *models.List
|
|
|
|
// Used when handling a single task, like updating
|
2019-08-14 22:19:04 +02:00
|
|
|
task *models.Task
|
2019-05-22 19:48:48 +02:00
|
|
|
// The current user
|
2020-01-26 18:08:06 +01:00
|
|
|
user *user2.User
|
2019-05-22 19:48:48 +02:00
|
|
|
isPrincipal bool
|
|
|
|
isEntry bool // Entry level handling should only return a link to the principal url
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResources returns either all lists, links to the principal, or only one list, depending on the request
|
|
|
|
func (vcls *VikunjaCaldavListStorage) GetResources(rpath string, withChildren bool) ([]data.Resource, error) {
|
|
|
|
|
|
|
|
// It looks like we need to have the same handler for returning both the calendar home set and the user principal
|
|
|
|
// Since the client seems to ignore the whatever is being returned in the first request and just makes a second one
|
|
|
|
// to the same url but requesting the calendar home instead
|
|
|
|
// The problem with this is caldav-go just return whatever ressource it gets and making that the requested path
|
|
|
|
// And for us here, there is no easy (I can think of at least one hacky way) to figure out if the client is requesting
|
|
|
|
// the home or principal url. Ough.
|
|
|
|
|
|
|
|
// Ok, maybe the problem is more the client making a request to /dav/ and getting a response which says
|
|
|
|
// something like "hey, for /dav/lists, the calendar home is /dav/lists", but the client expects a
|
|
|
|
// response to go something like "hey, for /dav/, the calendar home is /dav/lists" since it requested /dav/
|
|
|
|
// and not /dav/lists. I'm not sure if thats a bug in the client or in caldav-go.
|
|
|
|
|
|
|
|
if vcls.isEntry {
|
|
|
|
r := data.NewResource(rpath, &VikunjaListResourceAdapter{
|
|
|
|
isPrincipal: true,
|
|
|
|
isCollection: true,
|
|
|
|
})
|
|
|
|
return []data.Resource{r}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the request wants the principal url, we'll return that and nothing else
|
|
|
|
if vcls.isPrincipal {
|
|
|
|
r := data.NewResource(DavBasePath+`/lists/`, &VikunjaListResourceAdapter{
|
|
|
|
isPrincipal: true,
|
|
|
|
isCollection: true,
|
|
|
|
})
|
|
|
|
return []data.Resource{r}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If vcls.list.ID is != 0, this means the user is doing a PROPFIND request to /lists/:list
|
|
|
|
// Which means we need to get only one list
|
|
|
|
if vcls.list != nil && vcls.list.ID != 0 {
|
|
|
|
rr, err := vcls.getListRessource(true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
r.Name = vcls.list.Title
|
|
|
|
return []data.Resource{r}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// Otherwise get all lists
|
2020-12-23 16:32:28 +01:00
|
|
|
thelists, _, _, err := vcls.list.ReadAll(s, vcls.user, "", -1, 50)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.Commit(); err != nil {
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lists := thelists.([]*models.List)
|
|
|
|
|
|
|
|
var resources []data.Resource
|
|
|
|
for _, l := range lists {
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
list: l,
|
|
|
|
isCollection: true,
|
|
|
|
}
|
|
|
|
r := data.NewResource(ListBasePath+"/"+strconv.FormatInt(l.ID, 10), &rr)
|
|
|
|
r.Name = l.Title
|
|
|
|
resources = append(resources, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResourcesByList fetches a list of resources from a slice of paths
|
|
|
|
func (vcls *VikunjaCaldavListStorage) GetResourcesByList(rpaths []string) ([]data.Resource, error) {
|
|
|
|
|
|
|
|
// Parse the set of resourcepaths into usable uids
|
|
|
|
// A path looks like this: /dav/lists/10/a6eb526d5748a5c499da202fe74f36ed1aea2aef.ics
|
|
|
|
// So we split the url in parts, take the last one and strip the ".ics" at the end
|
|
|
|
var uids []string
|
|
|
|
for _, path := range rpaths {
|
|
|
|
parts := strings.Split(path, "/")
|
2020-10-11 22:10:03 +02:00
|
|
|
uid := []rune(parts[4]) // The 4th part is the id with ".ics" suffix
|
|
|
|
endlen := len(uid) - len(".ics") // ".ics" are 4 bytes
|
2019-05-22 19:48:48 +02:00
|
|
|
uids = append(uids, string(uid[:endlen]))
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// GetTasksByUIDs...
|
|
|
|
// Parse these into ressources...
|
2020-12-23 16:32:28 +01:00
|
|
|
tasks, err := models.GetTasksByUIDs(s, uids)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.Commit(); err != nil {
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resources []data.Resource
|
|
|
|
for _, t := range tasks {
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
task: t,
|
|
|
|
}
|
|
|
|
r := data.NewResource(getTaskURL(t), &rr)
|
2020-05-16 12:17:44 +02:00
|
|
|
r.Name = t.Title
|
2019-05-22 19:48:48 +02:00
|
|
|
resources = append(resources, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResourcesByFilters fetches a list of resources with a filter
|
|
|
|
func (vcls *VikunjaCaldavListStorage) GetResourcesByFilters(rpath string, filters *data.ResourceFilter) ([]data.Resource, error) {
|
|
|
|
|
|
|
|
// If we already have a list saved, that means the user is making a REPORT request to find out if
|
|
|
|
// anything changed, in that case we need to return all tasks.
|
|
|
|
// That list is coming from a previous "getListRessource" in L177
|
|
|
|
if vcls.list.Tasks != nil {
|
|
|
|
var resources []data.Resource
|
|
|
|
for _, t := range vcls.list.Tasks {
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
list: vcls.list,
|
|
|
|
task: t,
|
|
|
|
isCollection: false,
|
|
|
|
}
|
|
|
|
r := data.NewResource(getTaskURL(t), &rr)
|
2020-05-16 12:17:44 +02:00
|
|
|
r.Name = t.Title
|
2019-05-22 19:48:48 +02:00
|
|
|
resources = append(resources, r)
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is used to get all
|
|
|
|
rr, err := vcls.getListRessource(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
r.Name = vcls.list.Title
|
|
|
|
return []data.Resource{r}, nil
|
|
|
|
// For now, filtering is disabled.
|
2020-10-11 22:10:03 +02:00
|
|
|
// return vcls.GetResources(rpath, false)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 22:19:04 +02:00
|
|
|
func getTaskURL(task *models.Task) string {
|
2019-05-22 19:48:48 +02:00
|
|
|
return ListBasePath + "/" + strconv.FormatInt(task.ListID, 10) + `/` + task.UID + `.ics`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResource fetches a single resource
|
|
|
|
func (vcls *VikunjaCaldavListStorage) GetResource(rpath string) (*data.Resource, bool, error) {
|
|
|
|
|
|
|
|
// If the task is not nil, we need to get the task and not the list
|
|
|
|
if vcls.task != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// save and override the updated unix date to not break any later etag checks
|
|
|
|
updated := vcls.task.Updated
|
2020-12-23 16:32:28 +01:00
|
|
|
task, err := models.GetTaskSimple(s, &models.Task{ID: vcls.task.ID, UID: vcls.task.UID})
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-08-14 22:19:04 +02:00
|
|
|
if models.IsErrTaskDoesNotExist(err) {
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, false, errs.ResourceNotFoundError
|
|
|
|
}
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
if err := s.Commit(); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
vcls.task = &task
|
2020-06-27 19:04:01 +02:00
|
|
|
if updated.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
vcls.task.Updated = updated
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
list: vcls.list,
|
|
|
|
task: &task,
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
return &r, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise get the list with all tasks
|
|
|
|
rr, err := vcls.getListRessource(true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
return &r, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetShallowResource gets a ressource without childs
|
|
|
|
// Since Vikunja has no children, this is the same as GetResource
|
|
|
|
func (vcls *VikunjaCaldavListStorage) GetShallowResource(rpath string) (*data.Resource, bool, error) {
|
|
|
|
// Since Vikunja has no childs, this just returns the same as GetResource()
|
|
|
|
// FIXME: This should just get the list with no tasks whatsoever, nothing else
|
|
|
|
return vcls.GetResource(rpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateResource creates a new resource
|
|
|
|
func (vcls *VikunjaCaldavListStorage) CreateResource(rpath, content string) (*data.Resource, error) {
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
vTask, err := parseTaskFromVTODO(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vTask.ListID = vcls.list.ID
|
|
|
|
|
|
|
|
// Check the rights
|
2020-12-23 16:32:28 +01:00
|
|
|
canCreate, err := vTask.CanCreate(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !canCreate {
|
|
|
|
return nil, errs.ForbiddenError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the task
|
2020-12-23 16:32:28 +01:00
|
|
|
err = vTask.Create(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Commit(); err != nil {
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the proper response
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
list: vcls.list,
|
|
|
|
task: vTask,
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateResource updates a resource
|
|
|
|
func (vcls *VikunjaCaldavListStorage) UpdateResource(rpath, content string) (*data.Resource, error) {
|
|
|
|
|
|
|
|
vTask, err := parseTaskFromVTODO(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we already have the right task in vcls.task, so we can use that ID directly
|
|
|
|
vTask.ID = vcls.task.ID
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// Check the rights
|
2020-12-23 16:32:28 +01:00
|
|
|
canUpdate, err := vTask.CanUpdate(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !canUpdate {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, errs.ForbiddenError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the task
|
2021-02-02 23:48:37 +01:00
|
|
|
err = vTask.Update(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Commit(); err != nil {
|
2019-05-22 19:48:48 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the proper response
|
|
|
|
rr := VikunjaListResourceAdapter{
|
|
|
|
list: vcls.list,
|
|
|
|
task: vTask,
|
|
|
|
}
|
|
|
|
r := data.NewResource(rpath, &rr)
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteResource deletes a resource
|
|
|
|
func (vcls *VikunjaCaldavListStorage) DeleteResource(rpath string) error {
|
|
|
|
if vcls.task != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// Check the rights
|
2020-12-23 16:32:28 +01:00
|
|
|
canDelete, err := vcls.task.CanDelete(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-05-22 19:48:48 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !canDelete {
|
|
|
|
return errs.ForbiddenError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
2021-02-02 23:48:37 +01:00
|
|
|
err = vcls.task.Delete(s, vcls.user)
|
2020-12-23 16:32:28 +01:00
|
|
|
if err != nil {
|
|
|
|
_ = s.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Commit()
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VikunjaListResourceAdapter holds the actual resource
|
|
|
|
type VikunjaListResourceAdapter struct {
|
2020-10-18 12:40:50 +02:00
|
|
|
list *models.List
|
|
|
|
listTasks []*models.Task
|
|
|
|
task *models.Task
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
isPrincipal bool
|
|
|
|
isCollection bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCollection checks if the resoure in the adapter is a collection
|
|
|
|
func (vlra *VikunjaListResourceAdapter) IsCollection() bool {
|
|
|
|
// If the discovery does not work, setting this to true makes it work again.
|
|
|
|
return vlra.isCollection
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateEtag returns the etag of a resource
|
|
|
|
func (vlra *VikunjaListResourceAdapter) CalculateEtag() string {
|
|
|
|
|
|
|
|
// If we're updating a task, the client sends the etag of the list instead of the one from the task.
|
|
|
|
// And therefore, updating the task fails since these etags don't match.
|
|
|
|
// To fix that, we use this extra field to determine if we're currently updating a task and return the
|
|
|
|
// etag of the list instead.
|
2020-10-11 22:10:03 +02:00
|
|
|
// if vlra.list != nil {
|
|
|
|
// return `"` + strconv.FormatInt(vlra.list.ID, 10) + `-` + strconv.FormatInt(vlra.list.Updated, 10) + `"`
|
|
|
|
// }
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
// Return the etag of a task if we have one
|
|
|
|
if vlra.task != nil {
|
2020-06-27 19:04:01 +02:00
|
|
|
return `"` + strconv.FormatInt(vlra.task.ID, 10) + `-` + strconv.FormatInt(vlra.task.Updated.Unix(), 10) + `"`
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
2021-01-16 15:37:35 +01:00
|
|
|
|
|
|
|
if vlra.list == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// This also returns the etag of the list, and not of the task,
|
|
|
|
// which becomes problematic because the client uses this etag (= the one from the list) to make
|
|
|
|
// Requests to update a task. These do not match and thus updating a task fails.
|
2020-06-27 19:04:01 +02:00
|
|
|
return `"` + strconv.FormatInt(vlra.list.ID, 10) + `-` + strconv.FormatInt(vlra.list.Updated.Unix(), 10) + `"`
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContent returns the content string of a resource (a task in our case)
|
|
|
|
func (vlra *VikunjaListResourceAdapter) GetContent() string {
|
|
|
|
if vlra.list != nil && vlra.list.Tasks != nil {
|
2020-10-18 12:40:50 +02:00
|
|
|
return getCaldavTodosForTasks(vlra.list, vlra.listTasks)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if vlra.task != nil {
|
2019-08-14 22:19:04 +02:00
|
|
|
list := models.List{Tasks: []*models.Task{vlra.task}}
|
2020-10-18 12:40:50 +02:00
|
|
|
return getCaldavTodosForTasks(&list, list.Tasks)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContentSize is the size of a caldav content
|
|
|
|
func (vlra *VikunjaListResourceAdapter) GetContentSize() int64 {
|
|
|
|
return int64(len(vlra.GetContent()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetModTime returns when the resource was last modified
|
|
|
|
func (vlra *VikunjaListResourceAdapter) GetModTime() time.Time {
|
|
|
|
if vlra.task != nil {
|
2020-06-27 19:04:01 +02:00
|
|
|
return vlra.task.Updated
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if vlra.list != nil {
|
2020-06-27 19:04:01 +02:00
|
|
|
return vlra.list.Updated
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vcls *VikunjaCaldavListStorage) getListRessource(isCollection bool) (rr VikunjaListResourceAdapter, err error) {
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2021-01-16 15:37:35 +01:00
|
|
|
if vcls.list == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
can, _, err := vcls.list.CanRead(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-05-22 19:48:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !can {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Errorf("User %v tried to access a caldav resource (List %v) which they are not allowed to access", vcls.user.Username, vcls.list.ID)
|
2019-05-22 19:48:48 +02:00
|
|
|
return rr, models.ErrUserDoesNotHaveAccessToList{ListID: vcls.list.ID}
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err = vcls.list.ReadOne(s, vcls.user)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2019-05-22 19:48:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:40:50 +02:00
|
|
|
listTasks := vcls.list.Tasks
|
|
|
|
if listTasks == nil {
|
|
|
|
tk := models.TaskCollection{
|
|
|
|
ListID: vcls.list.ID,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
iface, _, _, err := tk.ReadAll(s, vcls.user, "", 1, 1000)
|
2020-10-18 12:40:50 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:32:28 +01:00
|
|
|
_ = s.Rollback()
|
2020-10-18 12:40:50 +02:00
|
|
|
return rr, err
|
|
|
|
}
|
|
|
|
tasks, ok := iface.([]*models.Task)
|
|
|
|
if !ok {
|
|
|
|
panic("Tasks returned from TaskCollection.ReadAll are not []*models.Task!")
|
|
|
|
}
|
|
|
|
|
|
|
|
listTasks = tasks
|
|
|
|
vcls.list.Tasks = tasks
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if err := s.Commit(); err != nil {
|
|
|
|
return rr, err
|
|
|
|
}
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
rr = VikunjaListResourceAdapter{
|
|
|
|
list: vcls.list,
|
2020-10-18 12:40:50 +02:00
|
|
|
listTasks: listTasks,
|
2019-05-22 19:48:48 +02:00
|
|
|
isCollection: isCollection,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|