feat(reactions): return reactions when fetching tasks
This commit is contained in:
parent
49b174e19f
commit
c4e0e08bbc
|
@ -86,8 +86,23 @@ func (r *Reaction) ReadAll(s *xorm.Session, a web.Auth, _ string, _ int, _ int)
|
|||
return nil, 0, 0, ErrGenericForbidden{}
|
||||
}
|
||||
|
||||
reactions, err := getReactionsForEntityIDs(s, r.EntityKind, []int64{r.EntityID})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return reactions[r.EntityID], len(reactions[r.EntityID]), int64(len(reactions[r.EntityID])), nil
|
||||
}
|
||||
|
||||
func getReactionsForEntityIDs(s *xorm.Session, entityKind ReactionKind, entityIDs []int64) (reactionsWithTasks map[int64]ReactionMap, err error) {
|
||||
|
||||
where := builder.And(
|
||||
builder.Eq{"entity_kind": entityKind},
|
||||
builder.In("entity_id", entityIDs),
|
||||
)
|
||||
|
||||
reactions := []*Reaction{}
|
||||
err = s.Where("entity_id = ? AND entity_kind = ?", r.EntityID, r.EntityKind).Find(&reactions)
|
||||
err = s.Where(where).Find(&reactions)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -95,24 +110,27 @@ func (r *Reaction) ReadAll(s *xorm.Session, a web.Auth, _ string, _ int, _ int)
|
|||
cond := builder.
|
||||
Select("user_id").
|
||||
From("reactions").
|
||||
And(builder.Eq{"entity_id": r.EntityID}).
|
||||
And(builder.Eq{"entity_kind": r.EntityKind})
|
||||
Where(where)
|
||||
|
||||
users, err := user.GetUsersByCond(s, cond)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
reactionMap := make(ReactionMap)
|
||||
reactionsWithTasks = make(map[int64]ReactionMap)
|
||||
for _, reaction := range reactions {
|
||||
if _, has := reactionMap[reaction.Value]; !has {
|
||||
reactionMap[reaction.Value] = []*user.User{}
|
||||
if _, taskExists := reactionsWithTasks[reaction.EntityID]; !taskExists {
|
||||
reactionsWithTasks[reaction.EntityID] = make(ReactionMap)
|
||||
}
|
||||
|
||||
reactionMap[reaction.Value] = append(reactionMap[reaction.Value], users[reaction.UserID])
|
||||
if _, has := reactionsWithTasks[reaction.EntityID][reaction.Value]; !has {
|
||||
reactionsWithTasks[reaction.EntityID][reaction.Value] = []*user.User{}
|
||||
}
|
||||
|
||||
reactionsWithTasks[reaction.EntityID][reaction.Value] = append(reactionsWithTasks[reaction.EntityID][reaction.Value], users[reaction.UserID])
|
||||
}
|
||||
|
||||
return reactionMap, len(reactionMap), int64(len(reactions)), nil
|
||||
return
|
||||
}
|
||||
|
||||
// Delete removes the user's own reaction
|
||||
|
|
|
@ -125,6 +125,9 @@ type Task struct {
|
|||
// The position of tasks in the kanban board. See the docs for the `position` property on how to use this.
|
||||
KanbanPosition float64 `xorm:"double null" json:"kanban_position"`
|
||||
|
||||
// Reactions on that task.
|
||||
Reactions ReactionMap `xorm:"-" json:"reactions"`
|
||||
|
||||
// The user who initially created the task.
|
||||
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`
|
||||
CreatedByID int64 `xorm:"bigint not null" json:"-"` // ID of the user who put that task on the project
|
||||
|
@ -529,6 +532,23 @@ func addRelatedTasksToTasks(s *xorm.Session, taskIDs []int64, taskMap map[int64]
|
|||
return
|
||||
}
|
||||
|
||||
func addReactionsToTasks(s *xorm.Session, taskIDs []int64, taskMap map[int64]*Task) (err error) {
|
||||
|
||||
reactions, err := getReactionsForEntityIDs(s, ReactionKindTask, taskIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for taskID, r := range reactions {
|
||||
t, has := taskMap[taskID]
|
||||
if has {
|
||||
t.Reactions = r
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 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, a web.Auth) (err error) {
|
||||
|
@ -584,6 +604,11 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (e
|
|||
return err
|
||||
}
|
||||
|
||||
err = addReactionsToTasks(s, taskIDs, taskMap)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Add all objects to their tasks
|
||||
for _, task := range taskMap {
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
"code.vikunja.io/api/pkg/models"
|
||||
"code.vikunja.io/api/pkg/modules/migration"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
|
||||
"github.com/adlio/trello"
|
||||
"github.com/yuin/goldmark"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue