1
0

feat: emoji reactions for tasks and comments (#2196)

This PR adds reactions for tasks and comments, similar to what you can do on Gitea, GitHub, Slack and plenty of other tools.

Reviewed-on: https://kolaente.dev/vikunja/vikunja/pulls/2196
Co-authored-by: kolaente <k@knt.li>
Co-committed-by: kolaente <k@knt.li>
This commit is contained in:
kolaente
2024-03-12 19:25:58 +00:00
committed by konrad
parent b9c513f681
commit a5c51d4b1e
43 changed files with 1653 additions and 37 deletions

View File

@ -1060,6 +1060,33 @@ func (err ErrInvalidFilterExpression) HTTPError() web.HTTPError {
}
}
// ErrInvalidReactionEntityKind represents an error where the reaction kind is invalid
type ErrInvalidReactionEntityKind struct {
Kind string
}
// IsErrInvalidReactionEntityKind checks if an error is ErrInvalidReactionEntityKind.
func IsErrInvalidReactionEntityKind(err error) bool {
_, ok := err.(ErrInvalidReactionEntityKind)
return ok
}
func (err ErrInvalidReactionEntityKind) Error() string {
return fmt.Sprintf("Reaction kind %s is invalid", err.Kind)
}
// ErrCodeInvalidReactionEntityKind holds the unique world-error code of this error
const ErrCodeInvalidReactionEntityKind = 4025
// HTTPError holds the http error description
func (err ErrInvalidReactionEntityKind) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeInvalidReactionEntityKind,
Message: fmt.Sprintf("The reaction kind '%s' is invalid.", err.Kind),
}
}
// ============
// Team errors
// ============

View File

@ -61,6 +61,7 @@ func GetTables() []interface{} {
&APIToken{},
&TypesenseSync{},
&Webhook{},
&Reaction{},
}
}

View File

@ -23,7 +23,6 @@ import (
"strings"
"time"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/events"
"code.vikunja.io/api/pkg/files"
@ -370,10 +369,7 @@ type projectOptions struct {
}
func getUserProjectsStatement(parentProjectIDs []int64, userID int64, search string, getArchived bool) *builder.Builder {
dialect := config.DatabaseType.GetString()
if dialect == "sqlite" {
dialect = builder.SQLITE
}
dialect := db.GetDialect()
// Adding a 1=1 condition by default here because xorm always needs a condition and cannot handle nil conditions
var getArchivedCond builder.Cond = builder.Eq{"1": 1}

191
pkg/models/reaction.go Normal file
View File

@ -0,0 +1,191 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"time"
"code.vikunja.io/web"
"xorm.io/builder"
"xorm.io/xorm"
"code.vikunja.io/api/pkg/user"
)
type ReactionKind int
const (
ReactionKindTask = iota
ReactionKindComment
)
type Reaction struct {
// The unique numeric id of this reaction
ID int64 `xorm:"autoincr not null unique pk" json:"-" param:"reaction"`
// The user who reacted
User *user.User `xorm:"-" json:"user" valid:"-"`
UserID int64 `xorm:"bigint not null INDEX" json:"-"`
// The id of the entity you're reacting to
EntityID int64 `xorm:"bigint not null INDEX" json:"-" param:"entityid"`
// The entity kind which you're reacting to. Can be 0 for task, 1 for comment.
EntityKind ReactionKind `xorm:"bigint not null INDEX" json:"-"`
EntityKindString string `xorm:"-" json:"-" param:"entitykind"`
// The actual reaction. This can be any valid utf character or text, up to a length of 20.
Value string `xorm:"varchar(20) not null INDEX" json:"value" valid:"required"`
// A timestamp when this reaction was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
web.CRUDable `xorm:"-" json:"-"`
web.Rights `xorm:"-" json:"-"`
}
func (*Reaction) TableName() string {
return "reactions"
}
type ReactionMap map[string][]*user.User
// ReadAll gets all reactions for an entity
// @Summary Get all reactions for an entity
// @Description Returns all reactions for an entity
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Entity ID"
// @Param kind path int true "The kind of the entity. Can be either `tasks` or `comments` for task comments"
// @Success 200 {array} models.ReactionMap "The reactions"
// @Failure 403 {object} web.HTTPError "The user does not have access to the entity"
// @Failure 500 {object} models.Message "Internal error"
// @Router /{kind}/{id}/reactions [get]
func (r *Reaction) ReadAll(s *xorm.Session, a web.Auth, _ string, _ int, _ int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
can, _, err := r.CanRead(s, a)
if err != nil {
return nil, 0, 0, err
}
if !can {
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(where).Find(&reactions)
if err != nil {
return
}
if len(reactions) == 0 {
return
}
cond := builder.
Select("user_id").
From("reactions").
Where(where)
users, err := user.GetUsersByCond(s, builder.In("id", cond))
if err != nil {
return
}
reactionsWithTasks = make(map[int64]ReactionMap)
for _, reaction := range reactions {
if _, taskExists := reactionsWithTasks[reaction.EntityID]; !taskExists {
reactionsWithTasks[reaction.EntityID] = make(ReactionMap)
}
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
}
// Delete removes the user's own reaction
// @Summary Removes the user's reaction
// @Description Removes the reaction of that user on that entity.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Entity ID"
// @Param kind path int true "The kind of the entity. Can be either `tasks` or `comments` for task comments"
// @Param project body models.Reaction true "The reaction you want to add to the entity."
// @Success 200 {object} models.Message "The reaction was successfully removed."
// @Failure 403 {object} web.HTTPError "The user does not have access to the entity"
// @Failure 500 {object} models.Message "Internal error"
// @Router /{kind}/{id}/reactions/delete [post]
func (r *Reaction) Delete(s *xorm.Session, a web.Auth) (err error) {
r.UserID = a.GetID()
_, err = s.Where("user_id = ? AND entity_id = ? AND entity_kind = ? AND value = ?", r.UserID, r.EntityID, r.EntityKind, r.Value).
Delete(&Reaction{})
return
}
// Create adds a new reaction to an entity
// @Summary Add a reaction to an entity
// @Description Add a reaction to an entity. Will do nothing if the reaction already exists.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Entity ID"
// @Param kind path int true "The kind of the entity. Can be either `tasks` or `comments` for task comments"
// @Param project body models.Reaction true "The reaction you want to add to the entity."
// @Success 200 {object} models.Reaction "The created reaction"
// @Failure 403 {object} web.HTTPError "The user does not have access to the entity"
// @Failure 500 {object} models.Message "Internal error"
// @Router /{kind}/{id}/reactions [put]
func (r *Reaction) Create(s *xorm.Session, a web.Auth) (err error) {
r.UserID = a.GetID()
exists, err := s.Where("user_id = ? AND entity_id = ? AND entity_kind = ? AND value = ?", r.UserID, r.EntityID, r.EntityKind, r.Value).
Exist(&Reaction{})
if err != nil {
return err
}
if exists {
return
}
_, err = s.Insert(r)
return
}

View File

@ -0,0 +1,81 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/web"
"xorm.io/xorm"
)
func (r *Reaction) setEntityKindFromString() (err error) {
switch r.EntityKindString {
case "tasks":
r.EntityKind = ReactionKindTask
return
case "comments":
r.EntityKind = ReactionKindComment
return
}
return ErrInvalidReactionEntityKind{
Kind: r.EntityKindString,
}
}
func (r *Reaction) CanRead(s *xorm.Session, a web.Auth) (bool, int, error) {
t, err := r.getTask(s)
if err != nil {
return false, 0, err
}
return t.CanRead(s, a)
}
func (r *Reaction) CanDelete(s *xorm.Session, a web.Auth) (bool, error) {
t, err := r.getTask(s)
if err != nil {
return false, err
}
return t.CanUpdate(s, a)
}
func (r *Reaction) CanCreate(s *xorm.Session, a web.Auth) (bool, error) {
t, err := r.getTask(s)
if err != nil {
return false, err
}
return t.CanUpdate(s, a)
}
func (r *Reaction) getTask(s *xorm.Session) (t *Task, err error) {
err = r.setEntityKindFromString()
if err != nil {
return
}
t = &Task{ID: r.EntityID}
if r.EntityKind == ReactionKindComment {
tc := &TaskComment{ID: r.EntityID}
err = getTaskCommentSimple(s, tc)
if err != nil {
return
}
t.ID = tc.TaskID
}
return
}

217
pkg/models/reaction_test.go Normal file
View File

@ -0,0 +1,217 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"testing"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReaction_ReadAll(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
}
reactions, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.NoError(t, err)
assert.IsType(t, ReactionMap{}, reactions)
reactionMap := reactions.(ReactionMap)
assert.Len(t, reactionMap["👋"], 1)
assert.Equal(t, int64(1), reactionMap["👋"][0].ID)
})
t.Run("invalid entity", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "loremipsum",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidReactionEntityKind{Kind: "loremipsum"})
})
t.Run("no access to task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 34,
EntityKindString: "tasks",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
})
t.Run("nonexistant task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 9999999,
EntityKindString: "tasks",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrTaskDoesNotExist{ID: r.EntityID})
})
t.Run("no access to comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 18,
EntityKindString: "comments",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
})
t.Run("nonexistant comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 9999999,
EntityKindString: "comments",
}
_, _, _, err := r.ReadAll(s, u, "", 0, 0)
require.Error(t, err)
assert.ErrorIs(t, err, ErrTaskCommentDoesNotExist{ID: r.EntityID})
})
}
func TestReaction_Create(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.True(t, can)
err = r.Create(s, u)
require.NoError(t, err)
err = s.Commit()
require.NoError(t, err)
db.AssertExists(t, "reactions", map[string]interface{}{
"entity_id": r.EntityID,
"entity_kind": ReactionKindTask,
"user_id": u.ID,
"value": r.Value,
}, false)
})
t.Run("no permission to access task", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 34,
EntityKindString: "tasks",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.False(t, can)
})
t.Run("no permission to access comment", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 18,
EntityKindString: "comments",
Value: "🦙",
}
can, err := r.CanCreate(s, u)
require.NoError(t, err)
assert.False(t, can)
})
}
func TestReaction_Delete(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
r := &Reaction{
EntityID: 1,
EntityKindString: "tasks",
Value: "👋",
}
can, err := r.CanDelete(s, u)
require.NoError(t, err)
assert.True(t, can)
err = r.Delete(s, u)
require.NoError(t, err)
db.AssertMissing(t, "reactions", map[string]interface{}{
"entity_id": r.EntityID,
"entity_kind": ReactionKindTask,
"value": "👋",
})
})
}

View File

@ -98,6 +98,9 @@ func TestTaskCollection_ReadAll(t *testing.T) {
BucketID: 1,
IsFavorite: true,
Position: 2,
Reactions: ReactionMap{
"👋": []*user.User{user1},
},
Labels: []*Label{
label4,
},

View File

@ -37,6 +37,8 @@ type TaskComment struct {
Author *user.User `xorm:"-" json:"author"`
TaskID int64 `xorm:"not null" json:"-" param:"task"`
Reactions ReactionMap `xorm:"-" json:"reactions"`
Created time.Time `xorm:"created" json:"created"`
Updated time.Time `xorm:"updated" json:"updated"`
@ -167,7 +169,7 @@ func (tc *TaskComment) Update(s *xorm.Session, _ web.Auth) error {
func getTaskCommentSimple(s *xorm.Session, tc *TaskComment) error {
exists, err := s.
Where("id = ? and task_id = ?", tc.ID, tc.TaskID).
Where("id = ?", tc.ID).
NoAutoCondition().
Get(tc)
if err != nil {
@ -263,8 +265,10 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa
}
var authorIDs []int64
var commentIDs []int64
for _, comment := range comments {
authorIDs = append(authorIDs, comment.AuthorID)
commentIDs = append(commentIDs, comment.ID)
}
authors, err := getUsersOrLinkSharesFromIDs(s, authorIDs)
@ -272,8 +276,17 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa
return
}
reactions, err := getReactionsForEntityIDs(s, ReactionKindComment, commentIDs)
if err != nil {
return
}
for _, comment := range comments {
comment.Author = authors[comment.AuthorID]
r, has := reactions[comment.ID]
if has {
comment.Reactions = r
}
}
numberOfTotalItems, err = s.

View File

@ -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
@ -584,6 +587,11 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (e
return err
}
reactions, err := getReactionsForEntityIDs(s, ReactionKindTask, taskIDs)
if err != nil {
return
}
// Add all objects to their tasks
for _, task := range taskMap {
@ -600,6 +608,11 @@ func addMoreInfoToTasks(s *xorm.Session, taskMap map[int64]*Task, a web.Auth) (e
task.setIdentifier(projects[task.ProjectID])
task.IsFavorite = taskFavorites[task.ID]
r, has := reactions[task.ID]
if has {
task.Reactions = r
}
}
// Get all related tasks

View File

@ -65,6 +65,7 @@ func SetupTests() {
"subscriptions",
"favorites",
"api_tokens",
"reactions",
)
if err != nil {
log.Fatal(err)