From 1bfc959bdfb21a87579886b1cd5d6dd09c308abf Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 5 Sep 2020 17:11:48 +0200 Subject: [PATCH] Return a pseudo namespace and list for favorites --- pkg/models/list.go | 16 ++++++++++------ pkg/models/list_rights.go | 18 ++++++------------ pkg/models/namespace.go | 38 +++++++++++++++++++++++++++++++++++++- pkg/models/tasks.go | 2 +- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/pkg/models/list.go b/pkg/models/list.go index 658467868..8499f308c 100644 --- a/pkg/models/list.go +++ b/pkg/models/list.go @@ -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 } diff --git a/pkg/models/list_rights.go b/pkg/models/list_rights.go index 0441e5e28..875409e8c 100644 --- a/pkg/models/list_rights.go +++ b/pkg/models/list_rights.go @@ -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 } diff --git a/pkg/models/namespace.go b/pkg/models/namespace.go index 1d3a2a74c..83e030a42 100644 --- a/pkg/models/namespace.go +++ b/pkg/models/namespace.go @@ -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:]...) } diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index d5e340b7f..c26ce8ef7 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -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)