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.
|
2018-11-26 21:17:33 +01: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.
|
2018-11-26 21:17:33 +01: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.
|
2018-11-26 21:17:33 +01: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/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"sort"
|
2020-12-22 00:13:15 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-10-11 22:10:03 +02:00
|
|
|
"time"
|
|
|
|
|
2021-08-01 23:40:25 +02:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
2021-02-02 23:48:37 +01:00
|
|
|
|
2021-08-01 23:40:25 +02:00
|
|
|
"code.vikunja.io/api/pkg/events"
|
2020-12-22 00:13:15 +01:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2021-08-01 23:40:25 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2020-03-22 18:24:54 +01:00
|
|
|
"xorm.io/builder"
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
2018-12-01 00:26:56 +01:00
|
|
|
)
|
2018-11-19 12:37:59 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// Namespace holds informations about a namespace
|
|
|
|
type Namespace struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this namespace.
|
2020-12-18 17:51:22 +01:00
|
|
|
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"namespace"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The name of this namespace.
|
2020-09-26 23:02:17 +02:00
|
|
|
Title string `xorm:"varchar(250) not null" json:"title" valid:"required,runelength(1|250)" minLength:"1" maxLength:"250"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The description of the namespace
|
2019-07-21 23:57:19 +02:00
|
|
|
Description string `xorm:"longtext null" json:"description"`
|
2020-12-18 17:51:22 +01:00
|
|
|
OwnerID int64 `xorm:"bigint not null INDEX" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2020-03-22 22:09:32 +01:00
|
|
|
// The hex color of this namespace
|
|
|
|
HexColor string `xorm:"varchar(6) null" json:"hex_color" valid:"runelength(0|6)" maxLength:"6"`
|
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// Whether or not a namespace is archived.
|
|
|
|
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`
|
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who owns this namespace
|
2020-01-26 18:08:06 +01:00
|
|
|
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
|
2018-06-14 17:43:00 +02:00
|
|
|
|
2021-02-14 20:18:14 +01:00
|
|
|
// The subscription status for the user reading this namespace. You can only read this property, use the subscription endpoints to modify it.
|
|
|
|
// Will only returned when retreiving one namespace.
|
|
|
|
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
|
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this namespace was created. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time `xorm:"created not null" json:"created"`
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this namespace was last updated. You cannot change this value.
|
2020-06-27 19:04:01 +02:00
|
|
|
Updated time.Time `xorm:"updated not null" json:"updated"`
|
2018-07-09 23:17:19 +02:00
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
// If set to true, will only return the namespaces, not their lists.
|
|
|
|
NamespacesOnly bool `xorm:"-" json:"-" query:"namespaces_only"`
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 23:02:17 +02:00
|
|
|
// SharedListsPseudoNamespace is a pseudo namespace used to hold shared lists
|
|
|
|
var SharedListsPseudoNamespace = Namespace{
|
2018-12-04 11:16:42 +01:00
|
|
|
ID: -1,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Shared Lists",
|
2018-12-04 11:16:42 +01:00
|
|
|
Description: "Lists of other users shared with you via teams or directly.",
|
2020-06-27 19:04:01 +02:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2018-12-04 11:16:42 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:16:02 +02:00
|
|
|
// FavoritesPseudoNamespace is a pseudo namespace used to hold favorited lists and tasks
|
|
|
|
var FavoritesPseudoNamespace = Namespace{
|
|
|
|
ID: -2,
|
|
|
|
Title: "Favorites",
|
|
|
|
Description: "Favorite lists and tasks.",
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2020-09-26 23:02:17 +02:00
|
|
|
// SavedFiltersPseudoNamespace is a pseudo namespace used to hold saved filters
|
|
|
|
var SavedFiltersPseudoNamespace = Namespace{
|
|
|
|
ID: -3,
|
|
|
|
Title: "Filters",
|
|
|
|
Description: "Saved filters.",
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// TableName makes beautiful table names
|
|
|
|
func (Namespace) TableName() string {
|
|
|
|
return "namespaces"
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:31:37 +01:00
|
|
|
// GetSimpleByID gets a namespace without things like the owner, it more or less only checks if it exists.
|
2020-12-23 16:32:28 +01:00
|
|
|
func getNamespaceSimpleByID(s *xorm.Session, id int64) (namespace *Namespace, err error) {
|
|
|
|
if id == 0 {
|
|
|
|
return nil, ErrNamespaceDoesNotExist{ID: id}
|
2018-09-13 20:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 11:16:42 +01:00
|
|
|
// Get the namesapce with shared lists
|
2020-12-23 16:32:28 +01:00
|
|
|
if id == -1 {
|
|
|
|
return &SharedListsPseudoNamespace, nil
|
2018-12-04 11:16:42 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if id == FavoritesPseudoNamespace.ID {
|
|
|
|
return &FavoritesPseudoNamespace, nil
|
2020-09-05 22:16:02 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 21:39:38 +01:00
|
|
|
if id == SavedFiltersPseudoNamespace.ID {
|
|
|
|
return &SavedFiltersPseudoNamespace, nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
namespace = &Namespace{}
|
|
|
|
|
|
|
|
exists, err := s.Where("id = ?", id).Get(namespace)
|
2018-07-02 08:40:24 +02:00
|
|
|
if err != nil {
|
2019-03-08 22:31:37 +01:00
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
|
|
|
if !exists {
|
2020-12-23 16:32:28 +01:00
|
|
|
return nil, ErrNamespaceDoesNotExist{ID: id}
|
2019-03-08 22:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNamespaceByID returns a namespace object by its ID
|
2020-12-23 16:32:28 +01:00
|
|
|
func GetNamespaceByID(s *xorm.Session, id int64) (namespace *Namespace, err error) {
|
|
|
|
namespace, err = getNamespaceSimpleByID(s, id)
|
2019-03-08 22:31:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the namespace Owner
|
2020-12-23 16:32:28 +01:00
|
|
|
namespace.Owner, err = user.GetUserByID(s, namespace.OwnerID)
|
2019-03-08 22:31:37 +01:00
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// CheckIsArchived returns an ErrNamespaceIsArchived if the namepace is archived.
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) CheckIsArchived(s *xorm.Session) error {
|
|
|
|
exists, err := s.
|
2020-03-15 22:50:39 +01:00
|
|
|
Where("id = ? AND is_archived = true", n.ID).
|
|
|
|
Exist(&Namespace{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return ErrNamespaceIsArchived{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 11:54:55 +02:00
|
|
|
// ReadOne gets one namespace
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Gets one namespace
|
|
|
|
// @Description Returns a namespace by its ID.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param id path int true "Namespace ID"
|
|
|
|
// @Success 200 {object} models.Namespace "The Namespace"
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to that namespace."
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id} [get]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (n *Namespace) ReadOne(s *xorm.Session, a web.Auth) (err error) {
|
2020-12-23 16:32:28 +01:00
|
|
|
nn, err := GetNamespaceByID(s, n.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*n = *nn
|
2021-02-14 20:18:14 +01:00
|
|
|
|
|
|
|
n.Subscription, err = GetSubscription(s, SubscriptionEntityNamespace, n.ID, a)
|
2018-07-12 11:54:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:46:35 +01:00
|
|
|
// NamespaceWithLists represents a namespace with list meta informations
|
|
|
|
type NamespaceWithLists struct {
|
|
|
|
Namespace `xorm:"extends"`
|
|
|
|
Lists []*List `xorm:"-" json:"lists"`
|
|
|
|
}
|
|
|
|
|
2021-09-04 21:26:31 +02:00
|
|
|
type NamespaceWithListsAndTasks struct {
|
|
|
|
Namespace
|
|
|
|
Lists []*ListWithTasksAndBuckets `xorm:"-" json:"lists"`
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func makeNamespaceSlice(namespaces map[int64]*NamespaceWithLists, userMap map[int64]*user.User, subscriptions map[int64]*Subscription) []*NamespaceWithLists {
|
2020-12-22 00:13:15 +01:00
|
|
|
all := make([]*NamespaceWithLists, 0, len(namespaces))
|
|
|
|
for _, n := range namespaces {
|
|
|
|
n.Owner = userMap[n.OwnerID]
|
2021-02-14 20:18:14 +01:00
|
|
|
n.Subscription = subscriptions[n.ID]
|
2020-12-22 00:13:15 +01:00
|
|
|
all = append(all, n)
|
|
|
|
}
|
|
|
|
sort.Slice(all, func(i, j int) bool {
|
|
|
|
return all[i].ID < all[j].ID
|
|
|
|
})
|
|
|
|
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func getNamespaceFilterCond(search string) (filterCond builder.Cond) {
|
2021-08-01 23:40:25 +02:00
|
|
|
filterCond = db.ILIKE("namespaces.title", search)
|
2020-09-26 23:02:17 +02:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
if search == "" {
|
|
|
|
return
|
2018-12-01 00:26:56 +01:00
|
|
|
}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
vals := strings.Split(search, ",")
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return
|
2020-03-22 18:24:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:13:15 +01:00
|
|
|
ids := []int64{}
|
|
|
|
for _, val := range vals {
|
|
|
|
v, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Namespace search string part '%s' is not a number: %s", val, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ids = append(ids, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
filterCond = builder.In("namespaces.id", ids)
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNamespaceArchivedCond(archived bool) builder.Cond {
|
|
|
|
// Adding a 1=1 condition by default here because xorm always needs a condition and cannot handle nil conditions
|
|
|
|
var isArchivedCond builder.Cond = builder.Eq{"1": 1}
|
|
|
|
if !archived {
|
|
|
|
isArchivedCond = builder.And(
|
|
|
|
builder.Eq{"namespaces.is_archived": false},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return isArchivedCond
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNamespacesWithLists(s *xorm.Session, namespaces *map[int64]*NamespaceWithLists, search string, isArchived bool, page, perPage int, userID int64) (numberOfTotalItems int64, err error) {
|
|
|
|
isArchivedCond := getNamespaceArchivedCond(isArchived)
|
|
|
|
filterCond := getNamespaceFilterCond(search)
|
|
|
|
|
2020-04-12 19:29:24 +02:00
|
|
|
limit, start := getLimitFromPageIndex(page, perPage)
|
2020-12-23 16:32:28 +01:00
|
|
|
query := s.Select("namespaces.*").
|
2018-07-11 13:00:00 +02:00
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
2021-03-28 20:17:35 +02:00
|
|
|
Join("LEFT", "users_namespaces", "users_namespaces.namespace_id = namespaces.id").
|
2021-02-16 22:35:55 +01:00
|
|
|
Where("team_members.user_id = ?", userID).
|
|
|
|
Or("namespaces.owner_id = ?", userID).
|
2021-03-28 20:17:35 +02:00
|
|
|
Or("users_namespaces.user_id = ?", userID).
|
2018-07-11 13:00:00 +02:00
|
|
|
GroupBy("namespaces.id").
|
2020-12-22 00:13:15 +01:00
|
|
|
Where(filterCond).
|
2020-04-12 19:29:24 +02:00
|
|
|
Where(isArchivedCond)
|
|
|
|
if limit > 0 {
|
|
|
|
query = query.Limit(limit, start)
|
|
|
|
}
|
2021-02-16 22:35:55 +01:00
|
|
|
err = query.Find(namespaces)
|
2018-07-11 13:00:00 +02:00
|
|
|
if err != nil {
|
2021-02-16 22:35:55 +01:00
|
|
|
return 0, err
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
numberOfTotalItems, err = s.
|
2020-12-22 00:13:15 +01:00
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
2021-03-28 20:17:35 +02:00
|
|
|
Join("LEFT", "users_namespaces", "users_namespaces.namespace_id = namespaces.id").
|
2021-02-16 22:35:55 +01:00
|
|
|
Where("team_members.user_id = ?", userID).
|
|
|
|
Or("namespaces.owner_id = ?", userID).
|
2021-03-28 20:17:35 +02:00
|
|
|
Or("users_namespaces.user_id = ?", userID).
|
2020-12-22 00:13:15 +01:00
|
|
|
And("namespaces.is_archived = false").
|
|
|
|
GroupBy("namespaces.id").
|
|
|
|
Where(filterCond).
|
2021-02-16 22:35:55 +01:00
|
|
|
Where(isArchivedCond).
|
2020-12-22 00:13:15 +01:00
|
|
|
Count(&NamespaceWithLists{})
|
2021-02-16 22:35:55 +01:00
|
|
|
return numberOfTotalItems, err
|
|
|
|
}
|
2020-12-22 00:13:15 +01:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func getNamespaceOwnerIDs(namespaces map[int64]*NamespaceWithLists) (namespaceIDs, ownerIDs []int64) {
|
2020-09-26 23:02:17 +02:00
|
|
|
for _, nsp := range namespaces {
|
2021-02-16 22:35:55 +01:00
|
|
|
namespaceIDs = append(namespaceIDs, nsp.ID)
|
|
|
|
ownerIDs = append(ownerIDs, nsp.OwnerID)
|
2020-05-15 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNamespaceSubscriptions(s *xorm.Session, namespaceIDs []int64, userID int64) (map[int64]*Subscription, error) {
|
2021-05-26 11:04:29 +02:00
|
|
|
subscriptionsMap := make(map[int64]*Subscription)
|
|
|
|
if len(namespaceIDs) == 0 {
|
|
|
|
return subscriptionsMap, nil
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:18:14 +01:00
|
|
|
subscriptions := []*Subscription{}
|
2021-02-16 22:35:55 +01:00
|
|
|
err := s.
|
|
|
|
Where("entity_type = ? AND user_id = ?", SubscriptionEntityNamespace, userID).
|
|
|
|
In("entity_id", namespaceIDs).
|
2021-02-14 20:18:14 +01:00
|
|
|
Find(&subscriptions)
|
|
|
|
if err != nil {
|
2021-02-16 22:35:55 +01:00
|
|
|
return nil, err
|
2021-02-14 20:18:14 +01:00
|
|
|
}
|
|
|
|
for _, sub := range subscriptions {
|
|
|
|
sub.Entity = sub.EntityType.String()
|
|
|
|
subscriptionsMap[sub.EntityID] = sub
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
return subscriptionsMap, err
|
|
|
|
}
|
2020-12-22 00:13:15 +01:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func getListsForNamespaces(s *xorm.Session, namespaceIDs []int64, archived bool) ([]*List, error) {
|
2018-09-17 07:27:49 +02:00
|
|
|
lists := []*List{}
|
2020-12-23 16:32:28 +01:00
|
|
|
listQuery := s.
|
2021-07-28 21:06:40 +02:00
|
|
|
OrderBy("position").
|
2021-02-16 22:35:55 +01:00
|
|
|
In("namespace_id", namespaceIDs)
|
2020-03-22 18:24:54 +01:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
if !archived {
|
2020-03-22 18:24:54 +01:00
|
|
|
listQuery.And("is_archived = false")
|
|
|
|
}
|
2021-02-16 22:35:55 +01:00
|
|
|
err := listQuery.Find(&lists)
|
|
|
|
return lists, err
|
|
|
|
}
|
2020-09-26 23:02:17 +02:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func getSharedListsInNamespace(s *xorm.Session, archived bool, doer *user.User) (sharedListsNamespace *NamespaceWithLists, err error) {
|
2020-09-26 23:02:17 +02:00
|
|
|
// Create our pseudo namespace to hold the shared lists
|
|
|
|
sharedListsPseudonamespace := SharedListsPseudoNamespace
|
|
|
|
sharedListsPseudonamespace.Owner = doer
|
2021-02-16 22:35:55 +01:00
|
|
|
sharedListsNamespace = &NamespaceWithLists{
|
2020-09-26 23:02:17 +02:00
|
|
|
sharedListsPseudonamespace,
|
|
|
|
[]*List{},
|
2018-09-17 07:27:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 12:37:59 +01:00
|
|
|
// Get all lists individually shared with our user (not via a namespace)
|
|
|
|
individualLists := []*List{}
|
2020-12-23 16:32:28 +01:00
|
|
|
iListQuery := s.Select("l.*").
|
2021-03-28 20:17:35 +02:00
|
|
|
Table("lists").
|
2018-11-19 12:37:59 +01:00
|
|
|
Alias("l").
|
2021-03-28 20:17:35 +02:00
|
|
|
Join("LEFT", []string{"team_lists", "tl"}, "l.id = tl.list_id").
|
2018-11-19 12:37:59 +01:00
|
|
|
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tl.team_id").
|
2021-03-28 20:17:35 +02:00
|
|
|
Join("LEFT", []string{"users_lists", "ul"}, "ul.list_id = l.id").
|
2021-01-31 13:09:14 +01:00
|
|
|
Where(builder.And(
|
|
|
|
builder.Eq{"tm.user_id": doer.ID},
|
|
|
|
builder.Neq{"l.owner_id": doer.ID},
|
|
|
|
)).
|
|
|
|
Or(builder.And(
|
|
|
|
builder.Eq{"ul.user_id": doer.ID},
|
|
|
|
builder.Neq{"l.owner_id": doer.ID},
|
|
|
|
)).
|
2020-03-22 18:24:54 +01:00
|
|
|
GroupBy("l.id")
|
2021-02-16 22:35:55 +01:00
|
|
|
if !archived {
|
2020-03-22 18:24:54 +01:00
|
|
|
iListQuery.And("l.is_archived = false")
|
|
|
|
}
|
|
|
|
err = iListQuery.Find(&individualLists)
|
2018-11-19 12:37:59 +01:00
|
|
|
if err != nil {
|
2021-02-16 22:35:55 +01:00
|
|
|
return
|
2018-11-19 12:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the namespace -1 so we now later which one it was
|
|
|
|
// + Append it to all lists we already have
|
|
|
|
for _, l := range individualLists {
|
2021-02-16 22:35:55 +01:00
|
|
|
l.NamespaceID = sharedListsNamespace.ID
|
2018-11-19 12:37:59 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
sharedListsNamespace.Lists = individualLists
|
|
|
|
|
2020-09-26 23:02:17 +02:00
|
|
|
// Remove the sharedListsPseudonamespace if we don't have any shared lists
|
2018-11-19 12:37:59 +01:00
|
|
|
if len(individualLists) == 0 {
|
2021-02-16 22:35:55 +01:00
|
|
|
sharedListsNamespace = nil
|
2019-10-23 23:11:40 +02:00
|
|
|
}
|
2018-10-05 19:16:14 +02:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
return
|
|
|
|
}
|
2018-09-17 07:27:49 +02:00
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
func getFavoriteLists(s *xorm.Session, lists []*List, namespaceIDs []int64, doer *user.User) (favoriteNamespace *NamespaceWithLists, err error) {
|
2020-09-26 23:02:17 +02:00
|
|
|
// Create our pseudo namespace with favorite lists
|
|
|
|
pseudoFavoriteNamespace := FavoritesPseudoNamespace
|
|
|
|
pseudoFavoriteNamespace.Owner = doer
|
2021-02-16 22:35:55 +01:00
|
|
|
favoriteNamespace = &NamespaceWithLists{
|
2020-09-26 23:02:17 +02:00
|
|
|
Namespace: pseudoFavoriteNamespace,
|
|
|
|
Lists: []*List{{}},
|
2020-05-15 17:10:22 +02:00
|
|
|
}
|
2021-02-16 22:35:55 +01:00
|
|
|
*favoriteNamespace.Lists[0] = FavoritesPseudoList // Copying the list to be able to modify it later
|
2020-05-15 17:10:22 +02:00
|
|
|
|
|
|
|
for _, list := range lists {
|
2021-02-16 22:35:55 +01:00
|
|
|
if !list.IsFavorite {
|
|
|
|
continue
|
2020-09-06 16:20:16 +02:00
|
|
|
}
|
2021-02-16 22:35:55 +01:00
|
|
|
favoriteNamespace.Lists = append(favoriteNamespace.Lists, list)
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:20:16 +02:00
|
|
|
// Check if we have any favorites or favorited lists and remove the favorites namespace from the list if not
|
2021-07-10 12:21:54 +02:00
|
|
|
cond := builder.
|
|
|
|
Select("tasks.id").
|
|
|
|
From("tasks").
|
2021-03-28 20:17:35 +02:00
|
|
|
Join("INNER", "lists", "tasks.list_id = lists.id").
|
|
|
|
Join("INNER", "namespaces", "lists.namespace_id = namespaces.id").
|
2021-07-10 12:21:54 +02:00
|
|
|
Where(builder.In("namespaces.id", namespaceIDs))
|
|
|
|
|
|
|
|
var favoriteCount int64
|
|
|
|
favoriteCount, err = s.
|
|
|
|
Where(builder.And(
|
|
|
|
builder.Eq{"user_id": doer.ID},
|
|
|
|
builder.Eq{"kind": FavoriteKindTask},
|
|
|
|
builder.In("entity_id", cond),
|
|
|
|
)).
|
|
|
|
Count(&Favorite{})
|
2020-09-06 16:20:16 +02:00
|
|
|
if err != nil {
|
2021-02-16 22:35:55 +01:00
|
|
|
return
|
2020-09-06 16:20:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have any favorites in the favorites pseudo list, remove that pseudo list from the namespace
|
|
|
|
if favoriteCount == 0 {
|
2021-02-16 22:35:55 +01:00
|
|
|
for in, l := range favoriteNamespace.Lists {
|
2020-09-06 16:20:16 +02:00
|
|
|
if l.ID == FavoritesPseudoList.ID {
|
2021-02-16 22:35:55 +01:00
|
|
|
favoriteNamespace.Lists = append(favoriteNamespace.Lists[:in], favoriteNamespace.Lists[in+1:]...)
|
2020-09-06 16:20:16 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have any favorites in the namespace, remove it
|
2021-02-16 22:35:55 +01:00
|
|
|
if len(favoriteNamespace.Lists) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSavedFilters(s *xorm.Session, doer *user.User) (savedFiltersNamespace *NamespaceWithLists, err error) {
|
|
|
|
savedFilters, err := getSavedFiltersForUser(s, doer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(savedFilters) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
savedFiltersPseudoNamespace := SavedFiltersPseudoNamespace
|
|
|
|
savedFiltersPseudoNamespace.Owner = doer
|
|
|
|
savedFiltersNamespace = &NamespaceWithLists{
|
|
|
|
Namespace: savedFiltersPseudoNamespace,
|
|
|
|
Lists: make([]*List, 0, len(savedFilters)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filter := range savedFilters {
|
2021-04-03 16:49:20 +02:00
|
|
|
filterList := filter.toList()
|
|
|
|
filterList.NamespaceID = savedFiltersNamespace.ID
|
|
|
|
filterList.Owner = doer
|
|
|
|
savedFiltersNamespace.Lists = append(savedFiltersNamespace.Lists, filterList)
|
2021-02-16 22:35:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAll gets all namespaces a user has access to
|
|
|
|
// @Summary Get all namespaces a user has access to
|
|
|
|
// @Description Returns all namespaces a user has access to.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
|
|
|
// @Param s query string false "Search namespaces by name."
|
|
|
|
// @Param is_archived query bool false "If true, also returns all archived namespaces."
|
|
|
|
// @Param namespaces_only query bool false "If true, also returns only namespaces without their lists."
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Success 200 {array} models.NamespaceWithLists "The Namespaces."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces [get]
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
|
|
|
|
if _, is := a.(*LinkSharing); is {
|
|
|
|
return nil, 0, 0, ErrGenericForbidden{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This map will hold all namespaces and their lists. The key is usually the id of the namespace.
|
|
|
|
// We're using a map here because it makes a few things like adding lists or removing pseudo namespaces easier.
|
|
|
|
namespaces := make(map[int64]*NamespaceWithLists)
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
// Lists with their namespaces
|
|
|
|
|
|
|
|
doer, err := user.GetFromAuth(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfTotalItems, err = getNamespacesWithLists(s, &namespaces, search, n.IsArchived, page, perPage, doer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaceIDs, ownerIDs := getNamespaceOwnerIDs(namespaces)
|
|
|
|
|
2021-05-26 12:00:55 +02:00
|
|
|
if len(namespaceIDs) == 0 {
|
|
|
|
return nil, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
subscriptionsMap, err := getNamespaceSubscriptions(s, namespaceIDs, doer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerMap, err := user.GetUsersByIDs(s, ownerIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.NamespacesOnly {
|
|
|
|
all := makeNamespaceSlice(namespaces, ownerMap, subscriptionsMap)
|
|
|
|
return all, len(all), numberOfTotalItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all lists
|
|
|
|
lists, err := getListsForNamespaces(s, namespaceIDs, n.IsArchived)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Shared Lists
|
|
|
|
|
|
|
|
sharedListsNamespace, err := getSharedListsInNamespace(s, n.IsArchived, doer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sharedListsNamespace != nil {
|
|
|
|
namespaces[sharedListsNamespace.ID] = sharedListsNamespace
|
|
|
|
lists = append(lists, sharedListsNamespace.Lists...)
|
2020-09-06 16:20:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 23:02:17 +02:00
|
|
|
/////////////////
|
2021-04-03 16:49:20 +02:00
|
|
|
// Saved Filters
|
2020-09-26 23:02:17 +02:00
|
|
|
|
2021-04-03 16:49:20 +02:00
|
|
|
savedFiltersNamespace, err := getSavedFilters(s, doer)
|
2019-10-23 23:11:40 +02:00
|
|
|
if err != nil {
|
2020-09-26 23:02:17 +02:00
|
|
|
return nil, 0, 0, err
|
2019-10-23 23:11:40 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 16:49:20 +02:00
|
|
|
if savedFiltersNamespace != nil {
|
|
|
|
namespaces[savedFiltersNamespace.ID] = savedFiltersNamespace
|
|
|
|
lists = append(lists, savedFiltersNamespace.Lists...)
|
2021-02-16 22:35:55 +01:00
|
|
|
}
|
2020-09-26 23:02:17 +02:00
|
|
|
|
2021-07-10 12:21:54 +02:00
|
|
|
/////////////////
|
2021-07-20 21:32:25 +02:00
|
|
|
// Add list details (favorite state, among other things)
|
2021-07-10 12:21:54 +02:00
|
|
|
err = addListDetails(s, lists, a)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:35:55 +01:00
|
|
|
/////////////////
|
2021-04-03 16:49:20 +02:00
|
|
|
// Favorite lists
|
2021-02-16 22:35:55 +01:00
|
|
|
|
2021-04-03 16:49:20 +02:00
|
|
|
favoritesNamespace, err := getFavoriteLists(s, lists, namespaceIDs, doer)
|
2021-02-16 22:35:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
2021-04-03 16:49:20 +02:00
|
|
|
if favoritesNamespace != nil {
|
|
|
|
namespaces[favoritesNamespace.ID] = favoritesNamespace
|
2020-09-26 23:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////
|
2021-02-16 22:35:55 +01:00
|
|
|
// Put it all together
|
|
|
|
|
|
|
|
for _, list := range lists {
|
2021-04-03 16:49:20 +02:00
|
|
|
if list.NamespaceID == SharedListsPseudoNamespace.ID || list.NamespaceID == SavedFiltersPseudoNamespace.ID {
|
|
|
|
// Shared lists and filtered lists are already in the namespace
|
2021-03-14 21:12:03 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-02-16 22:35:55 +01:00
|
|
|
namespaces[list.NamespaceID].Lists = append(namespaces[list.NamespaceID].Lists, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
all := makeNamespaceSlice(namespaces, ownerMap, subscriptionsMap)
|
|
|
|
return all, len(all), numberOfTotalItems, err
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Create implements the creation method via the interface
|
|
|
|
// @Summary Creates a new namespace
|
|
|
|
// @Description Creates a new namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param namespace body models.Namespace true "The namespace you want to create."
|
2021-05-26 21:56:31 +02:00
|
|
|
// @Success 201 {object} models.Namespace "The created namespace."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid namespace object provided."
|
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the namespace"
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces [put]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (n *Namespace) Create(s *xorm.Session, a web.Auth) (err error) {
|
2022-03-06 17:08:58 +01:00
|
|
|
// Check if we have at least a title
|
2020-05-16 12:17:44 +02:00
|
|
|
if n.Title == "" {
|
2019-07-16 16:15:40 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: 0, UserID: a.GetID()}
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
n.Owner, err = user.GetUserByID(s, a.GetID())
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.OwnerID = n.Owner.ID
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if _, err = s.Insert(n); err != nil {
|
2019-07-16 16:15:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-02 23:48:37 +01:00
|
|
|
err = events.Dispatch(&NamespaceCreatedEvent{
|
|
|
|
Namespace: n,
|
|
|
|
Doer: a,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-21 17:38:58 +01:00
|
|
|
// CreateNewNamespaceForUser creates a new namespace for a user. To prevent import cycles, we can't do that
|
|
|
|
// directly in the user.Create function.
|
2020-12-23 16:32:28 +01:00
|
|
|
func CreateNewNamespaceForUser(s *xorm.Session, user *user.User) (err error) {
|
2020-11-21 17:38:58 +01:00
|
|
|
newN := &Namespace{
|
|
|
|
Title: user.Username,
|
|
|
|
Description: user.Username + "'s namespace.",
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
return newN.Create(s, user)
|
2020-11-21 17:38:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Delete deletes a namespace
|
|
|
|
// @Summary Deletes a namespace
|
|
|
|
// @Description Delets a namespace
|
|
|
|
// @tags namespace
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Namespace ID"
|
|
|
|
// @Success 200 {object} models.Message "The namespace was successfully deleted."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid namespace object provided."
|
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the namespace"
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id} [delete]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (n *Namespace) Delete(s *xorm.Session, a web.Auth) (err error) {
|
2021-08-11 21:08:10 +02:00
|
|
|
return deleteNamespace(s, n, a, true)
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
func deleteNamespace(s *xorm.Session, n *Namespace, a web.Auth, withLists bool) (err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
// Check if the namespace exists
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = GetNamespaceByID(s, n.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the namespace
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.ID(n.ID).Delete(&Namespace{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
namespaceDeleted := &NamespaceDeletedEvent{
|
|
|
|
Namespace: n,
|
|
|
|
Doer: a,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !withLists {
|
|
|
|
return events.Dispatch(namespaceDeleted)
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Delete all lists with their tasks
|
2020-12-23 16:32:28 +01:00
|
|
|
lists, err := GetListsByNamespaceID(s, n.ID, &user.User{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
|
|
|
|
if len(lists) == 0 {
|
2021-08-11 21:08:10 +02:00
|
|
|
return events.Dispatch(namespaceDeleted)
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
// Looping over all lists to let the list handle properly cleaning up the tasks and everything else associated with it.
|
|
|
|
for _, list := range lists {
|
|
|
|
err = list.Delete(s, a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 21:08:10 +02:00
|
|
|
return events.Dispatch(namespaceDeleted)
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements the update method via the interface
|
|
|
|
// @Summary Updates a namespace
|
|
|
|
// @Description Updates a namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Namespace ID"
|
|
|
|
// @Param namespace body models.Namespace true "The namespace with updated values you want to update."
|
|
|
|
// @Success 200 {object} models.Namespace "The updated namespace."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid namespace object provided."
|
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the namespace"
|
2019-07-16 16:15:40 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespace/{id} [post]
|
2021-02-02 23:48:37 +01:00
|
|
|
func (n *Namespace) Update(s *xorm.Session, a web.Auth) (err error) {
|
2019-07-16 16:15:40 +02:00
|
|
|
// Check if we have at least a name
|
2020-05-16 12:17:44 +02:00
|
|
|
if n.Title == "" {
|
2019-07-16 16:15:40 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the namespace exists
|
2020-12-23 16:32:28 +01:00
|
|
|
currentNamespace, err := GetNamespaceByID(s, n.ID)
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// Check if the namespace is archived and the update is not un-archiving it
|
|
|
|
if currentNamespace.IsArchived && n.IsArchived {
|
|
|
|
return ErrNamespaceIsArchived{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Check if the (new) owner exists
|
2020-03-15 22:50:39 +01:00
|
|
|
if n.Owner != nil {
|
|
|
|
n.OwnerID = n.Owner.ID
|
|
|
|
if currentNamespace.OwnerID != n.OwnerID {
|
2020-12-23 16:32:28 +01:00
|
|
|
n.Owner, err = user.GetUserByID(s, n.OwnerID)
|
2020-03-15 22:50:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:02:53 +01:00
|
|
|
// We need to specify the cols we want to update here to be able to un-archive lists
|
|
|
|
colsToUpdate := []string{
|
2020-06-26 00:03:28 +02:00
|
|
|
"title",
|
2020-03-22 19:02:53 +01:00
|
|
|
"is_archived",
|
2020-06-15 11:32:07 +02:00
|
|
|
"hex_color",
|
2020-03-22 19:02:53 +01:00
|
|
|
}
|
|
|
|
if n.Description != "" {
|
|
|
|
colsToUpdate = append(colsToUpdate, "description")
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Do the actual update
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.
|
2020-03-22 19:02:53 +01:00
|
|
|
ID(currentNamespace.ID).
|
|
|
|
Cols(colsToUpdate...).
|
|
|
|
Update(n)
|
2021-02-02 23:48:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return events.Dispatch(&NamespaceUpdatedEvent{
|
|
|
|
Namespace: n,
|
|
|
|
Doer: a,
|
|
|
|
})
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|