vikunja-api/pkg/models/namespace.go

251 lines
7.2 KiB
Go
Raw Normal View History

2018-11-26 21:17:33 +01:00
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
2018-12-01 00:26:56 +01:00
import (
"code.vikunja.io/web"
"time"
)
// Namespace holds informations about a namespace
type Namespace struct {
2019-01-03 23:22:06 +01:00
// The unique, numeric id of this namespace.
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
// The name of this namespace.
Name string `xorm:"varchar(250)" json:"name" valid:"required,runelength(5|250)" minLength:"5" maxLength:"250"`
// The description of the namespace
Description string `xorm:"varchar(1000)" json:"description" valid:"runelength(0|250)" maxLength:"250"`
2018-10-05 18:46:01 +02:00
OwnerID int64 `xorm:"int(11) not null INDEX" json:"-"`
2019-01-03 23:22:06 +01:00
// The user who owns this namespace
2018-11-25 22:38:50 +01:00
Owner User `xorm:"-" json:"owner" valid:"-"`
2018-06-14 17:43:00 +02:00
2019-01-03 23:22:06 +01:00
// A unix timestamp when this namespace was created. You cannot change this value.
2018-11-24 13:44:40 +01:00
Created int64 `xorm:"created" json:"created"`
2019-01-03 23:22:06 +01:00
// A unix timestamp when this namespace was last updated. You cannot change this value.
2018-11-24 13:44:40 +01:00
Updated int64 `xorm:"updated" json:"updated"`
2018-12-01 00:26:56 +01:00
web.CRUDable `xorm:"-" json:"-"`
web.Rights `xorm:"-" json:"-"`
}
// PseudoNamespace is a pseudo namespace used to hold shared lists
var PseudoNamespace = Namespace{
ID: -1,
Name: "Shared Lists",
Description: "Lists of other users shared with you via teams or directly.",
Created: time.Now().Unix(),
Updated: time.Now().Unix(),
}
// TableName makes beautiful table names
func (Namespace) TableName() string {
return "namespaces"
}
// GetSimpleByID gets a namespace without things like the owner, it more or less only checks if it exists.
func (n *Namespace) GetSimpleByID() (err error) {
if n.ID == 0 {
return ErrNamespaceDoesNotExist{ID: n.ID}
}
// Get the namesapce with shared lists
if n.ID == -1 {
*n = PseudoNamespace
return
}
exists, err := x.Get(n)
if err != nil {
return
}
if !exists {
return ErrNamespaceDoesNotExist{ID: n.ID}
}
return
}
// GetNamespaceByID returns a namespace object by its ID
func GetNamespaceByID(id int64) (namespace Namespace, err error) {
namespace = Namespace{ID: id}
err = namespace.GetSimpleByID()
if err != nil {
return
}
// Get the namespace Owner
2018-08-30 19:14:02 +02:00
namespace.Owner, err = GetUserByID(namespace.OwnerID)
if err != nil {
return
}
return
}
// ReadOne gets one namespace
// @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
// @Param id path int true "Namespace ID"
// @Success 200 {object} models.Namespace "The Namespace"
2018-12-01 02:59:17 +01:00
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to that namespace."
// @Failure 500 {object} models.Message "Internal error"
// @Router /namespaces/{id} [get]
2018-07-21 15:08:46 +02:00
func (n *Namespace) ReadOne() (err error) {
2018-10-05 19:17:39 +02:00
*n, err = GetNamespaceByID(n.ID)
return
}
// NamespaceWithLists represents a namespace with list meta informations
type NamespaceWithLists struct {
Namespace `xorm:"extends"`
Lists []*List `xorm:"-" json:"lists"`
}
// 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 p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param s query string false "Search namespaces by name."
2019-01-03 23:22:06 +01:00
// @Security JWTKeyAuth
// @Success 200 {array} models.NamespaceWithLists "The Namespaces."
// @Failure 500 {object} models.Message "Internal error"
// @Router /namespaces [get]
2018-12-01 00:26:56 +01:00
func (n *Namespace) ReadAll(search string, a web.Auth, page int) (interface{}, error) {
doer, err := getUserWithError(a)
if err != nil {
return nil, err
}
all := []*NamespaceWithLists{}
// Create our pseudo-namespace to hold the shared lists
// We want this one at the beginning, which is why we create it here
pseudonamespace := PseudoNamespace
pseudonamespace.Owner = *doer
all = append(all, &NamespaceWithLists{
pseudonamespace,
[]*List{},
})
2018-12-01 00:26:56 +01:00
err = x.Select("namespaces.*").
Table("namespaces").
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
2018-09-06 08:46:34 +02:00
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
Where("team_members.user_id = ?", doer.ID).
Or("namespaces.owner_id = ?", doer.ID).
2018-09-06 08:46:34 +02:00
Or("users_namespace.user_id = ?", doer.ID).
GroupBy("namespaces.id").
2018-11-09 11:30:17 +01:00
Limit(getLimitFromPageIndex(page)).
Where("namespaces.name LIKE ?", "%"+search+"%").
Find(&all)
if err != nil {
return all, err
}
// Get all users
users := []*User{}
err = x.Select("users.*").
Table("namespaces").
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
Join("INNER", "users", "users.id = namespaces.owner_id").
Where("team_members.user_id = ?", doer.ID).
Or("namespaces.owner_id = ?", doer.ID).
GroupBy("users.id").
Find(&users)
if err != nil {
return all, err
}
2018-09-17 07:27:49 +02:00
// Make a list of namespace ids
var namespaceids []int64
for _, nsp := range all {
namespaceids = append(namespaceids, nsp.ID)
}
// Get all lists
lists := []*List{}
err = x.Table(&lists).
In("namespace_id", namespaceids).
Find(&lists)
if err != nil {
return all, err
}
// Get all lists individually shared with our user (not via a namespace)
individualLists := []*List{}
err = x.Select("l.*").
Table("list").
Alias("l").
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tl.team_id").
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
Where("tm.user_id = ?", doer.ID).
Or("ul.user_id = ?", doer.ID).
GroupBy("l.id").
Find(&individualLists)
if err != nil {
return nil, err
}
// Make the namespace -1 so we now later which one it was
// + Append it to all lists we already have
for _, l := range individualLists {
l.NamespaceID = -1
lists = append(lists, l)
}
// Remove the pseudonamespace if we don't have any shared lists
if len(individualLists) == 0 {
all = append(all[:0], all[1:]...)
}
// More details for the lists
2018-10-31 13:42:38 +01:00
AddListDetails(lists)
2018-09-17 07:27:49 +02:00
// Put objects in our namespace list
for i, n := range all {
2018-09-17 07:27:49 +02:00
// Users
for _, u := range users {
if n.OwnerID == u.ID {
all[i].Owner = *u
break
}
}
2018-09-17 07:27:49 +02:00
// List infos
for _, l := range lists {
if n.ID == l.NamespaceID {
all[i].Lists = append(all[i].Lists, l)
}
}
}
return all, nil
}