From 1a1984637ebede06af9c3ae312efd7c8ab03fa92 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 16:15:05 +0100 Subject: [PATCH 01/12] add ErrReactionAlreadyExist --- models/error.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/models/error.go b/models/error.go index 396d7594c8e93..40702567065ad 100644 --- a/models/error.go +++ b/models/error.go @@ -1201,6 +1201,21 @@ func (err ErrForbiddenIssueReaction) Error() string { return fmt.Sprintf("'%s' is not an allowed reaction", err.Reaction) } +// ErrReactionAlreadyExist is used when a existing reaction was try to created +type ErrReactionAlreadyExist struct { + Reaction string +} + +// IsErrReactionAlreadyExist checks if an error is a ErrReactionAlreadyExist. +func IsErrReactionAlreadyExist(err error) bool { + _, ok := err.(ErrReactionAlreadyExist) + return ok +} + +func (err ErrReactionAlreadyExist) Error() string { + return fmt.Sprintf("reaction '%s' already exist", err.Reaction) +} + // __________ .__ .__ __________ __ // \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_ // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\ From 93067d95ad3248d5ccde8ace719c0f3f06683735 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 16:52:47 +0100 Subject: [PATCH 02/12] extend CreateReaction --- models/issue_reaction.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 6896eeeafc6c4..9ef237f024d05 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -80,9 +80,20 @@ func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { UserID: opts.Doer.ID, IssueID: opts.Issue.ID, } + findOpts := FindReactionsOptions{IssueID: opts.Issue.ID} if opts.Comment != nil { reaction.CommentID = opts.Comment.ID + findOpts.CommentID = opts.Comment.ID } + + existingR, err := findReactions(e, findOpts) + if err != nil { + return nil, err + } + if len(existingR) > 0 { + return existingR[0], ErrReactionAlreadyExist{Reaction: opts.Type} + } + if _, err := e.Insert(reaction); err != nil { return nil, err } @@ -112,13 +123,13 @@ func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) { reaction, err = createReaction(sess, opts) if err != nil { - return nil, err + return } if err = sess.Commit(); err != nil { return nil, err } - return reaction, nil + return } // CreateIssueReaction creates a reaction on issue. From 73935a697cb84f6054e56a18cc791226c916b392 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 16:54:18 +0100 Subject: [PATCH 03/12] reaction already exist = 200 --- routers/api/v1/repo/issue_reaction.go | 34 +++++++++++++++++---------- templates/swagger/v1_json.tmpl | 10 ++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index 4b06bb987c9fc..0314943b7a113 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -114,7 +114,11 @@ func PostIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOpti // schema: // "$ref": "#/definitions/EditReactionOption" // responses: + // "200": + // description: reaction already exist + // "$ref": "#/responses/ReactionResponse" // "201": + // description: reaction created successfully // "$ref": "#/responses/ReactionResponse" // "403": // "$ref": "#/responses/forbidden" @@ -188,19 +192,20 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp if err != nil { if models.IsErrForbiddenIssueReaction(err) { ctx.Error(http.StatusForbidden, err.Error(), err) + } else if models.IsErrReactionAlreadyExist(err) { + ctx.JSON(http.StatusOK, api.ReactionResponse{ + User: ctx.User.APIFormat(), + Reaction: reaction.Type, + Created: reaction.CreatedUnix.AsTime(), + }) } else { ctx.Error(http.StatusInternalServerError, "CreateCommentReaction", err) } return } - _, err = reaction.LoadUser() - if err != nil { - ctx.Error(http.StatusInternalServerError, "Reaction.LoadUser()", err) - return - } ctx.JSON(http.StatusCreated, api.ReactionResponse{ - User: reaction.User.APIFormat(), + User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -317,7 +322,11 @@ func PostIssueReaction(ctx *context.APIContext, form api.EditReactionOption) { // schema: // "$ref": "#/definitions/EditReactionOption" // responses: + // "200": + // description: reaction already exist + // "$ref": "#/responses/ReactionResponse" // "201": + // description: reaction created successfully // "$ref": "#/responses/ReactionResponse" // "403": // "$ref": "#/responses/forbidden" @@ -386,19 +395,20 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i if err != nil { if models.IsErrForbiddenIssueReaction(err) { ctx.Error(http.StatusForbidden, err.Error(), err) + } else if models.IsErrReactionAlreadyExist(err) { + ctx.JSON(http.StatusOK, api.ReactionResponse{ + User: ctx.User.APIFormat(), + Reaction: reaction.Type, + Created: reaction.CreatedUnix.AsTime(), + }) } else { ctx.Error(http.StatusInternalServerError, "CreateCommentReaction", err) } return } - _, err = reaction.LoadUser() - if err != nil { - ctx.Error(http.StatusInternalServerError, "Reaction.LoadUser()", err) - return - } ctx.JSON(http.StatusCreated, api.ReactionResponse{ - User: reaction.User.APIFormat(), + User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7f08b939cc963..835b61b2ad85d 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3181,7 +3181,12 @@ } ], "responses": { + "200": { + "description": "reaction already exist", + "$ref": "#/responses/ReactionResponse" + }, "201": { + "description": "reaction created successfully", "$ref": "#/responses/ReactionResponse" }, "403": { @@ -3947,7 +3952,12 @@ } ], "responses": { + "200": { + "description": "reaction already exist", + "$ref": "#/responses/ReactionResponse" + }, "201": { + "description": "reaction created successfully", "$ref": "#/responses/ReactionResponse" }, "403": { From 82557ceeec65bf2701e76ced3e6417065be0d401 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 17:04:49 +0100 Subject: [PATCH 04/12] extend FindReactionsOptions --- models/issue_reaction.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 9ef237f024d05..d2b0a7d02fbc6 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -30,6 +30,7 @@ type Reaction struct { type FindReactionsOptions struct { IssueID int64 CommentID int64 + Reaction string } func (opts *FindReactionsOptions) toConds() builder.Cond { @@ -46,6 +47,9 @@ func (opts *FindReactionsOptions) toConds() builder.Cond { } else if opts.CommentID == -1 { cond = cond.And(builder.Eq{"reaction.comment_id": 0}) } + if opts.Reaction != "" { + cond = cond.And(builder.Eq{"reaction.type": opts.Reaction}) + } return cond } @@ -80,7 +84,7 @@ func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { UserID: opts.Doer.ID, IssueID: opts.Issue.ID, } - findOpts := FindReactionsOptions{IssueID: opts.Issue.ID} + findOpts := FindReactionsOptions{IssueID: opts.Issue.ID, CommentID: -1, Reaction: opts.Type} if opts.Comment != nil { reaction.CommentID = opts.Comment.ID findOpts.CommentID = opts.Comment.ID From 4ad86e2f90fe3b9aea0644139ab9471634c269a9 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 17:39:14 +0100 Subject: [PATCH 05/12] refactor swagger options/definitions --- modules/structs/issue_reaction.go | 4 ++-- routers/api/v1/repo/issue_reaction.go | 28 +++++++++++----------- routers/api/v1/swagger/issue.go | 23 +++++++----------- routers/api/v1/swagger/options.go | 3 +++ templates/swagger/v1_json.tmpl | 34 +++++++++++---------------- 5 files changed, 41 insertions(+), 51 deletions(-) diff --git a/modules/structs/issue_reaction.go b/modules/structs/issue_reaction.go index 9d71740052001..56408313ee4d3 100644 --- a/modules/structs/issue_reaction.go +++ b/modules/structs/issue_reaction.go @@ -13,8 +13,8 @@ type EditReactionOption struct { Reaction string `json:"content"` } -// ReactionResponse contain one reaction -type ReactionResponse struct { +// Reaction contain one reaction +type Reaction struct { User *User `json:"user"` Reaction string `json:"content"` // swagger:strfmt date-time diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index 0314943b7a113..fee1398786fe0 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -41,7 +41,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) { // required: true // responses: // "200": - // "$ref": "#/responses/ReactionResponseList" + // "$ref": "#/responses/ReactionList" // "403": // "$ref": "#/responses/forbidden" @@ -71,9 +71,9 @@ func GetIssueCommentReactions(ctx *context.APIContext) { return } - var result []api.ReactionResponse + var result []api.Reaction for _, r := range reactions { - result = append(result, api.ReactionResponse{ + result = append(result, api.Reaction{ User: r.User.APIFormat(), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), @@ -116,10 +116,10 @@ func PostIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOpti // responses: // "200": // description: reaction already exist - // "$ref": "#/responses/ReactionResponse" + // "$ref": "#/responses/Reaction" // "201": // description: reaction created successfully - // "$ref": "#/responses/ReactionResponse" + // "$ref": "#/responses/Reaction" // "403": // "$ref": "#/responses/forbidden" @@ -193,7 +193,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp if models.IsErrForbiddenIssueReaction(err) { ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { - ctx.JSON(http.StatusOK, api.ReactionResponse{ + ctx.JSON(http.StatusOK, api.Reaction{ User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), @@ -204,7 +204,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp return } - ctx.JSON(http.StatusCreated, api.ReactionResponse{ + ctx.JSON(http.StatusCreated, api.Reaction{ User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), @@ -249,7 +249,7 @@ func GetIssueReactions(ctx *context.APIContext) { // required: true // responses: // "200": - // "$ref": "#/responses/ReactionResponseList" + // "$ref": "#/responses/ReactionList" // "403": // "$ref": "#/responses/forbidden" @@ -279,9 +279,9 @@ func GetIssueReactions(ctx *context.APIContext) { return } - var result []api.ReactionResponse + var result []api.Reaction for _, r := range reactions { - result = append(result, api.ReactionResponse{ + result = append(result, api.Reaction{ User: r.User.APIFormat(), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), @@ -324,10 +324,10 @@ func PostIssueReaction(ctx *context.APIContext, form api.EditReactionOption) { // responses: // "200": // description: reaction already exist - // "$ref": "#/responses/ReactionResponse" + // "$ref": "#/responses/Reaction" // "201": // description: reaction created successfully - // "$ref": "#/responses/ReactionResponse" + // "$ref": "#/responses/Reaction" // "403": // "$ref": "#/responses/forbidden" @@ -396,7 +396,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i if models.IsErrForbiddenIssueReaction(err) { ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { - ctx.JSON(http.StatusOK, api.ReactionResponse{ + ctx.JSON(http.StatusOK, api.Reaction{ User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), @@ -407,7 +407,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i return } - ctx.JSON(http.StatusCreated, api.ReactionResponse{ + ctx.JSON(http.StatusCreated, api.Reaction{ User: ctx.User.APIFormat(), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), diff --git a/routers/api/v1/swagger/issue.go b/routers/api/v1/swagger/issue.go index 68c0a9a38d694..b12ea0096a411 100644 --- a/routers/api/v1/swagger/issue.go +++ b/routers/api/v1/swagger/issue.go @@ -99,23 +99,16 @@ type swaggerResponseStopWatchList struct { Body []api.StopWatch `json:"body"` } -// EditReactionOption -// swagger:response EditReactionOption -type swaggerEditReactionOption struct { +// Reaction +// swagger:response Reaction +type swaggerReaction struct { // in:body - Body api.EditReactionOption `json:"body"` + Body api.Reaction `json:"body"` } -// ReactionResponse -// swagger:response ReactionResponse -type swaggerReactionResponse struct { +// ReactionList +// swagger:response ReactionList +type swaggerReactionList struct { // in:body - Body api.ReactionResponse `json:"body"` -} - -// ReactionResponseList -// swagger:response ReactionResponseList -type swaggerReactionResponseList struct { - // in:body - Body []api.ReactionResponse `json:"body"` + Body []api.Reaction `json:"body"` } diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index 74a475e275666..83cbb3a74d9dd 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -123,4 +123,7 @@ type swaggerParameterBodies struct { // in:body RepoTopicOptions api.RepoTopicOptions + + // in:body + EditReactionOption api.EditReactionOption } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 835b61b2ad85d..5b5b7f011295a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3130,7 +3130,7 @@ ], "responses": { "200": { - "$ref": "#/responses/ReactionResponseList" + "$ref": "#/responses/ReactionList" }, "403": { "$ref": "#/responses/forbidden" @@ -3183,11 +3183,11 @@ "responses": { "200": { "description": "reaction already exist", - "$ref": "#/responses/ReactionResponse" + "$ref": "#/responses/Reaction" }, "201": { "description": "reaction created successfully", - "$ref": "#/responses/ReactionResponse" + "$ref": "#/responses/Reaction" }, "403": { "$ref": "#/responses/forbidden" @@ -3901,7 +3901,7 @@ ], "responses": { "200": { - "$ref": "#/responses/ReactionResponseList" + "$ref": "#/responses/ReactionList" }, "403": { "$ref": "#/responses/forbidden" @@ -3954,11 +3954,11 @@ "responses": { "200": { "description": "reaction already exist", - "$ref": "#/responses/ReactionResponse" + "$ref": "#/responses/Reaction" }, "201": { "description": "reaction created successfully", - "$ref": "#/responses/ReactionResponse" + "$ref": "#/responses/Reaction" }, "403": { "$ref": "#/responses/forbidden" @@ -10825,7 +10825,7 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, - "ReactionResponse": { + "Reaction": { "description": "ReactionResponse contain one reaction", "type": "object", "properties": { @@ -11738,12 +11738,6 @@ } } }, - "EditReactionOption": { - "description": "EditReactionOption", - "schema": { - "$ref": "#/definitions/EditReactionOption" - } - }, "EmailList": { "description": "EmailList", "schema": { @@ -11930,18 +11924,18 @@ } } }, - "ReactionResponse": { - "description": "ReactionResponse", + "Reaction": { + "description": "Reaction", "schema": { - "$ref": "#/definitions/ReactionResponse" + "$ref": "#/definitions/Reaction" } }, - "ReactionResponseList": { - "description": "ReactionResponseList", + "ReactionList": { + "description": "ReactionList", "schema": { "type": "array", "items": { - "$ref": "#/definitions/ReactionResponse" + "$ref": "#/definitions/Reaction" } } }, @@ -12167,7 +12161,7 @@ "parameterBodies": { "description": "parameterBodies", "schema": { - "$ref": "#/definitions/RepoTopicOptions" + "$ref": "#/definitions/EditReactionOption" } }, "redirect": { From 67767b41fbecd0319727a1fe93463589f5994387 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 17:41:54 +0100 Subject: [PATCH 06/12] fix swagger-validate --- routers/api/v1/repo/issue_reaction.go | 4 ---- templates/swagger/v1_json.tmpl | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index fee1398786fe0..bbc767cc99ad4 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -115,10 +115,8 @@ func PostIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOpti // "$ref": "#/definitions/EditReactionOption" // responses: // "200": - // description: reaction already exist // "$ref": "#/responses/Reaction" // "201": - // description: reaction created successfully // "$ref": "#/responses/Reaction" // "403": // "$ref": "#/responses/forbidden" @@ -323,10 +321,8 @@ func PostIssueReaction(ctx *context.APIContext, form api.EditReactionOption) { // "$ref": "#/definitions/EditReactionOption" // responses: // "200": - // description: reaction already exist // "$ref": "#/responses/Reaction" // "201": - // description: reaction created successfully // "$ref": "#/responses/Reaction" // "403": // "$ref": "#/responses/forbidden" diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 5b5b7f011295a..1f83182031df6 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3182,11 +3182,9 @@ ], "responses": { "200": { - "description": "reaction already exist", "$ref": "#/responses/Reaction" }, "201": { - "description": "reaction created successfully", "$ref": "#/responses/Reaction" }, "403": { @@ -3953,11 +3951,9 @@ ], "responses": { "200": { - "description": "reaction already exist", "$ref": "#/responses/Reaction" }, "201": { - "description": "reaction created successfully", "$ref": "#/responses/Reaction" }, "403": { @@ -10826,7 +10822,7 @@ "x-go-package": "code.gitea.io/gitea/modules/structs" }, "Reaction": { - "description": "ReactionResponse contain one reaction", + "description": "Reaction contain one reaction", "type": "object", "properties": { "content": { From 7148d995cc22d7299f9b9b797376c637e4499ec2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 18:14:33 +0100 Subject: [PATCH 07/12] Update models/error.go Co-Authored-By: zeripath --- models/error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/error.go b/models/error.go index 40702567065ad..f0d5699aad6b8 100644 --- a/models/error.go +++ b/models/error.go @@ -1213,7 +1213,7 @@ func IsErrReactionAlreadyExist(err error) bool { } func (err ErrReactionAlreadyExist) Error() string { - return fmt.Sprintf("reaction '%s' already exist", err.Reaction) + return fmt.Sprintf("reaction '%s' already exists", err.Reaction) } // __________ .__ .__ __________ __ From e7bd9f25b64e6da081e91493c7ddf273c321b23a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 18:15:50 +0100 Subject: [PATCH 08/12] fix test PART1 --- integrations/api_issue_reaction_test.go | 18 +++++++++--------- models/issue_reaction_test.go | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index c474f7bad2541..1906b8d09082c 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -47,7 +47,7 @@ func TestAPIIssuesReactions(t *testing.T) { Reaction: "rocket", }) resp = session.MakeRequest(t, req, http.StatusCreated) - var apiNewReaction api.ReactionResponse + var apiNewReaction api.Reaction DecodeJSON(t, resp, &apiNewReaction) //Add existing reaction @@ -56,10 +56,10 @@ func TestAPIIssuesReactions(t *testing.T) { //Get end result of reaction list of issue #1 req = NewRequestf(t, "GET", urlStr) resp = session.MakeRequest(t, req, http.StatusOK) - var apiReactions []*api.ReactionResponse + var apiReactions []*api.Reaction DecodeJSON(t, resp, &apiReactions) - expectResponse := make(map[int]api.ReactionResponse) - expectResponse[0] = api.ReactionResponse{ + expectResponse := make(map[int]api.Reaction) + expectResponse[0] = api.Reaction{ User: user2.APIFormat(), Reaction: "eyes", Created: time.Unix(1573248003, 0), @@ -107,7 +107,7 @@ func TestAPICommentReactions(t *testing.T) { Reaction: "+1", }) resp = session.MakeRequest(t, req, http.StatusCreated) - var apiNewReaction api.ReactionResponse + var apiNewReaction api.Reaction DecodeJSON(t, resp, &apiNewReaction) //Add existing reaction @@ -116,15 +116,15 @@ func TestAPICommentReactions(t *testing.T) { //Get end result of reaction list of issue #1 req = NewRequestf(t, "GET", urlStr) resp = session.MakeRequest(t, req, http.StatusOK) - var apiReactions []*api.ReactionResponse + var apiReactions []*api.Reaction DecodeJSON(t, resp, &apiReactions) - expectResponse := make(map[int]api.ReactionResponse) - expectResponse[0] = api.ReactionResponse{ + expectResponse := make(map[int]api.Reaction) + expectResponse[0] = api.Reaction{ User: user2.APIFormat(), Reaction: "laugh", Created: time.Unix(1573248004, 0), } - expectResponse[1] = api.ReactionResponse{ + expectResponse[1] = api.Reaction{ User: user1.APIFormat(), Reaction: "laugh", Created: time.Unix(1573248005, 0), diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index 1189b389e9a9c..848c3ebdb8e57 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -50,9 +50,10 @@ func TestIssueAddDuplicateReaction(t *testing.T) { Type: "heart", }) assert.Error(t, err) - assert.Nil(t, reaction) + assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err) - AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) + existingR := AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) + assert.Equal(t, existingR.ID, reaction.ID) } func TestIssueDeleteReaction(t *testing.T) { From fadb79a482434f367104e5401971c246ba8f97a9 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 18:27:33 +0100 Subject: [PATCH 09/12] extend FindReactionsOptions with UserID option --- models/issue_reaction.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/models/issue_reaction.go b/models/issue_reaction.go index d2b0a7d02fbc6..409d058696ed3 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -30,6 +30,7 @@ type Reaction struct { type FindReactionsOptions struct { IssueID int64 CommentID int64 + UserID int64 Reaction string } @@ -47,6 +48,9 @@ func (opts *FindReactionsOptions) toConds() builder.Cond { } else if opts.CommentID == -1 { cond = cond.And(builder.Eq{"reaction.comment_id": 0}) } + if opts.UserID > 0 { + cond = cond.And(builder.Eq{"reaction.user_id": opts.UserID}) + } if opts.Reaction != "" { cond = cond.And(builder.Eq{"reaction.type": opts.Reaction}) } @@ -84,7 +88,12 @@ func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { UserID: opts.Doer.ID, IssueID: opts.Issue.ID, } - findOpts := FindReactionsOptions{IssueID: opts.Issue.ID, CommentID: -1, Reaction: opts.Type} + findOpts := FindReactionsOptions{ + IssueID: opts.Issue.ID, + CommentID: -1, // reaction to issue only + Reaction: opts.Type, + UserID: opts.Doer.ID, + } if opts.Comment != nil { reaction.CommentID = opts.Comment.ID findOpts.CommentID = opts.Comment.ID From 6c331e0a56e82600447cf61c2d7cf1e0355ab655 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 18:27:46 +0100 Subject: [PATCH 10/12] catch error on test --- models/issue_reaction_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index 848c3ebdb8e57..856c3705c5a47 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -161,7 +161,7 @@ func TestIssueCommentReactionCount(t *testing.T) { comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") - DeleteCommentReaction(user1, issue1, comment1, "heart") + assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart")) AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) } From b83bbd164dd78d4adbc6b4476f08c1439f18f33d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 30 Dec 2019 18:36:26 +0100 Subject: [PATCH 11/12] fix test PART2 --- models/issue_reaction_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index 856c3705c5a47..723a6be536848 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -130,7 +130,6 @@ func TestIssueCommentDeleteReaction(t *testing.T) { user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - ghost := NewGhostUser() issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) @@ -140,14 +139,13 @@ func TestIssueCommentDeleteReaction(t *testing.T) { addReaction(t, user2, issue1, comment1, "heart") addReaction(t, user3, issue1, comment1, "heart") addReaction(t, user4, issue1, comment1, "+1") - addReaction(t, ghost, issue1, comment1, "heart") err := comment1.LoadReactions() assert.NoError(t, err) - assert.Len(t, comment1.Reactions, 5) + assert.Len(t, comment1.Reactions, 4) reactions := comment1.Reactions.GroupByType() - assert.Len(t, reactions["heart"], 4) + assert.Len(t, reactions["heart"], 3) assert.Len(t, reactions["+1"], 1) } From 90d8768f1c55bd2f35bde2366b9396d0ba6ef66a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 31 Dec 2019 02:53:59 +0100 Subject: [PATCH 12/12] format ... --- models/issue_reaction.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 409d058696ed3..d421ab44e92d4 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -123,26 +123,26 @@ type ReactionOptions struct { } // CreateReaction creates reaction for issue or comment. -func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) { +func CreateReaction(opts *ReactionOptions) (*Reaction, error) { if !setting.UI.ReactionsMap[opts.Type] { return nil, ErrForbiddenIssueReaction{opts.Type} } sess := x.NewSession() defer sess.Close() - if err = sess.Begin(); err != nil { + if err := sess.Begin(); err != nil { return nil, err } - reaction, err = createReaction(sess, opts) + reaction, err := createReaction(sess, opts) if err != nil { - return + return reaction, err } - if err = sess.Commit(); err != nil { + if err := sess.Commit(); err != nil { return nil, err } - return + return reaction, nil } // CreateIssueReaction creates a reaction on issue.