From a8c6141a2c2510d50549f9dcd45d21e3df4b0998 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 9 Jul 2021 21:46:59 +0200 Subject: [PATCH] Add returning favorites --- pkg/migration/20210709211508.go | 1 + pkg/models/favorites.go | 23 +++++++++++++++++++++++ pkg/models/kanban.go | 2 +- pkg/models/tasks.go | 17 ++++++++++++----- pkg/routes/caldav/listStorageProvider.go | 2 +- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/pkg/migration/20210709211508.go b/pkg/migration/20210709211508.go index cf1a0d27d..bfef937cd 100644 --- a/pkg/migration/20210709211508.go +++ b/pkg/migration/20210709211508.go @@ -36,6 +36,7 @@ func init() { ID: "20210709211508", Description: "", Migrate: func(tx *xorm.Engine) error { + // TODO: migrate all existing favorites return tx.Sync2(taskfavorites20210709211508{}) }, Rollback: func(tx *xorm.Engine) error { diff --git a/pkg/models/favorites.go b/pkg/models/favorites.go index 3d12ca787..0c0776d86 100644 --- a/pkg/models/favorites.go +++ b/pkg/models/favorites.go @@ -19,6 +19,7 @@ package models import ( "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" + "xorm.io/builder" "xorm.io/xorm" ) @@ -81,3 +82,25 @@ func isFavorite(s *xorm.Session, entityID int64, a web.Auth, kind FavoriteKind) Where("task_id = ? AND user_id = ? AND kind = ?", entityID, u.ID, kind). Exist(&Favorite{}) } + +func getFavorites(s *xorm.Session, entityIDs []int64, a web.Auth, kind FavoriteKind) (favorites map[int64]bool, err error) { + favorites = make(map[int64]bool) + u, err := user.GetFromAuth(a) + if err != nil { + // Only error GetFromAuth is if it's a link share and we want to ignore that + return + } + + favs := []*Favorite{} + err = s.Where(builder.And( + builder.Eq{"user_id": u.ID}, + builder.Eq{"kind": kind}, + builder.In("entity_id", entityIDs), + )). + Find(&favs) + + for _, fav := range favs { + favorites[fav.EntityID] = true + } + return +} diff --git a/pkg/models/kanban.go b/pkg/models/kanban.go index 825b69f7c..6c0016377 100644 --- a/pkg/models/kanban.go +++ b/pkg/models/kanban.go @@ -209,7 +209,7 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int taskMap[t.ID] = t } - err = addMoreInfoToTasks(s, taskMap) + err = addMoreInfoToTasks(s, taskMap, auth) if err != nil { return nil, 0, 0, err } diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 86e4a41bf..057a8ec20 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -473,7 +473,7 @@ func getTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskOpti taskMap[t.ID] = t } - err = addMoreInfoToTasks(s, taskMap) + err = addMoreInfoToTasks(s, taskMap, a) if err != nil { return nil, 0, 0, err } @@ -521,7 +521,7 @@ func (bt *BulkTask) GetTasksByIDs(s *xorm.Session) (err error) { } // GetTasksByUIDs gets all tasks from a bunch of uids -func GetTasksByUIDs(s *xorm.Session, uids []string) (tasks []*Task, err error) { +func GetTasksByUIDs(s *xorm.Session, uids []string, a web.Auth) (tasks []*Task, err error) { tasks = []*Task{} err = s.In("uid", uids).Find(&tasks) if err != nil { @@ -533,7 +533,7 @@ func GetTasksByUIDs(s *xorm.Session, uids []string) (tasks []*Task, err error) { taskMap[t.ID] = t } - err = addMoreInfoToTasks(s, taskMap) + err = addMoreInfoToTasks(s, taskMap, a) return } @@ -646,7 +646,7 @@ func addRelatedTasksToTasks(s *xorm.Session, taskIDs []int64, taskMap map[int64] // This function takes a map with pointers and returns a slice with pointers to tasks // It adds more stuff like assignees/labels/etc to a bunch of tasks -func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task) (err error) { +func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (err error) { // No need to iterate over users and stuff if the list doesn't have tasks if len(taskMap) == 0 { @@ -688,6 +688,11 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task) (err error) { return err } + taskFavorites, err := getFavorites(s, taskIDs, a, FavoriteKindTask) + if err != nil { + return err + } + // Get all identifiers lists, err := GetListsByIDs(s, listIDs) if err != nil { @@ -708,6 +713,8 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task) (err error) { // Build the task identifier from the list identifier and task index task.setIdentifier(lists[task.ListID]) + + task.IsFavorite = taskFavorites[task.ID] } // Get all related tasks @@ -1340,7 +1347,7 @@ func (t *Task) ReadOne(s *xorm.Session, a web.Auth) (err error) { return } - err = addMoreInfoToTasks(s, taskMap) + err = addMoreInfoToTasks(s, taskMap, a) if err != nil { return } diff --git a/pkg/routes/caldav/listStorageProvider.go b/pkg/routes/caldav/listStorageProvider.go index 64efaf53f..1171aa47d 100644 --- a/pkg/routes/caldav/listStorageProvider.go +++ b/pkg/routes/caldav/listStorageProvider.go @@ -139,7 +139,7 @@ func (vcls *VikunjaCaldavListStorage) GetResourcesByList(rpaths []string) ([]dat // GetTasksByUIDs... // Parse these into ressources... - tasks, err := models.GetTasksByUIDs(s, uids) + tasks, err := models.GetTasksByUIDs(s, uids, vcls.user) if err != nil { _ = s.Rollback() return nil, err