Add returning favorites

This commit is contained in:
kolaente 2021-07-09 21:46:59 +02:00
parent f640db5663
commit a8c6141a2c
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 38 additions and 7 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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