Return a pseudo namespace and list for favorites
continuous-integration/drone/pr Build was killed Details

This commit is contained in:
kolaente 2020-09-05 17:11:48 +02:00
parent 530f07ce4d
commit 1bfc959bdf
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 54 additions and 20 deletions

View File

@ -74,11 +74,15 @@ type ListBackgroundType struct {
// ListBackgroundUpload represents the list upload background type
const ListBackgroundUpload string = "upload"
// FavoriteListID is the id of the list which holds the favorites
const FavoriteListID int64 = -1
// FavoriteListNamespaceID is the id of the pseudo namespace containing the favorite list
const FavoriteListNamespaceID int64 = -2
// FavoritesPseudoList holds all tasks marked as favorites
var FavoritesPseudoList = List{
ID: -1,
Title: "Favorites",
Description: "This list has all tasks marked as favorites.",
NamespaceID: FavoritesPseudoNamespace.ID,
Created: time.Now(),
Updated: time.Now(),
}
// GetListsByNamespaceID gets all lists in a namespace
func GetListsByNamespaceID(nID int64, doer *user.User) (lists []*List, err error) {
@ -172,7 +176,7 @@ func (l *List) ReadAll(a web.Auth, search string, page int, perPage int) (result
// @Router /lists/{id} [get]
func (l *List) ReadOne() (err error) {
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
// Already "built" the list in CanRead
return nil
}

View File

@ -26,7 +26,7 @@ import (
func (l *List) CanWrite(a web.Auth) (bool, error) {
// The favorite list can't be edited
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
return false, nil
}
@ -70,20 +70,14 @@ func (l *List) CanWrite(a web.Auth) (bool, error) {
func (l *List) CanRead(a web.Auth) (bool, int, error) {
// The favorite list needs a special treatment
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
owner, err := user.GetFromAuth(a)
if err != nil {
return false, 0, err
}
*l = List{
ID: FavoriteListID,
Title: "Favorites",
Description: "This list has all tasks marked as favorites.",
OwnerID: a.GetID(),
Owner: owner,
NamespaceID: FavoriteListNamespaceID,
}
*l = FavoritesPseudoList
l.Owner = owner
return true, int(RightRead), nil
}
@ -108,7 +102,7 @@ func (l *List) CanRead(a web.Auth) (bool, int, error) {
// CanUpdate checks if the user can update a list
func (l *List) CanUpdate(a web.Auth) (canUpdate bool, err error) {
// The favorite list can't be edited
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
return false, nil
}
canUpdate, err = l.CanWrite(a)
@ -134,7 +128,7 @@ func (l *List) CanCreate(a web.Auth) (bool, error) {
// IsAdmin returns whether the user has admin rights on the list or not
func (l *List) IsAdmin(a web.Auth) (bool, error) {
// The favorite list can't be edited
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
return false, nil
}

View File

@ -62,6 +62,15 @@ var PseudoNamespace = Namespace{
Updated: time.Now(),
}
// 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(),
}
// TableName makes beautiful table names
func (Namespace) TableName() string {
return "namespaces"
@ -79,6 +88,11 @@ func (n *Namespace) GetSimpleByID() (err error) {
return
}
if n.ID == FavoritesPseudoNamespace.ID {
*n = FavoritesPseudoNamespace
return
}
namespaceFromDB := &Namespace{}
exists, err := x.Where("id = ?", n.ID).Get(namespaceFromDB)
if err != nil {
@ -180,8 +194,17 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
)
}
// Create our pseudo-namespace to hold the shared lists
// Create our pseudo namespace with favorite lists
// We want this one at the beginning, which is why we create it here
pseudoFavoriteNamespace := FavoritesPseudoNamespace
pseudoFavoriteNamespace.Owner = doer
all = append(all, &NamespaceWithLists{
Namespace: pseudoFavoriteNamespace,
Lists: []*List{{}},
})
*all[0].Lists[0] = FavoritesPseudoList // Copying the list to be able to modify it later
// Create our pseudo namespace to hold the shared lists
pseudonamespace := PseudoNamespace
pseudonamespace.Owner = doer
all = append(all, &NamespaceWithLists{
@ -266,6 +289,19 @@ func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (r
// Remove the pseudonamespace if we don't have any shared lists
if len(individualLists) == 0 {
all = append(all[:1], all[2:]...)
}
// Check if we have any favorites and remove the favorites namespace from the list if not
favoriteCount, err := x.
Join("INNER", "list", "tasks.list_id = list.id").
Join("INNER", "namespaces", "list.namespace_id = namespaces.id").
Where(builder.And(builder.Eq{"is_favorite": true}, builder.In("namespaces.id", namespaceids))).
Count(&Task{})
if err != nil {
return nil, 0, 0, err
}
if favoriteCount == 0 {
all = append(all[:0], all[1:]...)
}

View File

@ -184,7 +184,7 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks []
var listIDs []int64
var hasFavoriteLists bool
for _, l := range lists {
if l.ID == FavoriteListID {
if l.ID == FavoritesPseudoList.ID {
hasFavoriteLists = true
}
listIDs = append(listIDs, l.ID)