-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[API] Add Reactions #9220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[API] Add Reactions #9220
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6f941b5
reject reactions wich ar not allowed
6543 d430a76
dont duble check CreateReaction now throw ErrForbiddenIssueReaction
6543 7c5739e
add /repos/{owner}/{repo}/issues/comments/{id}/reactions endpoint
6543 7a041ca
add Find Functions
6543 e8cd6e2
fix some swagger stuff + add issue reaction endpoints + GET ReactionL…
6543 492c7d9
explicite Issue Only Reaction for FindReactionsOptions with "-1" comm…
6543 e3c9dbd
load issue; load user ...
6543 b4650b5
return error again
6543 8954552
swagger def canged after LINT
6543 b278062
check if user has ben loaded
6543 6d6d4ed
add Tests
6543 682c6ff
better way of comparing results
6543 1b42792
add suggestion
6543 5fadb7b
use different issue for test
6543 f4303c2
test dont compare Location on timeCompare
6543 6a25109
TEST: add forbidden dubble add
6543 76c1758
add comments in code to explain
6543 567af84
add settings.UI.ReactionsMap
6543 30a9d78
Merge branch 'master' into add-api-reactions
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models" | ||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIIssuesReactions(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) | ||
_ = issue.LoadRepo() | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) | ||
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s", | ||
owner.Name, issue.Repo.Name, issue.Index, token) | ||
|
||
//Try to add not allowed reaction | ||
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{ | ||
Reaction: "wrong", | ||
}) | ||
resp := session.MakeRequest(t, req, http.StatusForbidden) | ||
|
||
//Delete not allowed reaction | ||
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{ | ||
Reaction: "zzz", | ||
}) | ||
resp = session.MakeRequest(t, req, http.StatusOK) | ||
|
||
//Add allowed reaction | ||
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{ | ||
Reaction: "rocket", | ||
}) | ||
resp = session.MakeRequest(t, req, http.StatusCreated) | ||
var apiNewReaction api.ReactionResponse | ||
DecodeJSON(t, resp, &apiNewReaction) | ||
|
||
//Add existing reaction | ||
resp = session.MakeRequest(t, req, http.StatusForbidden) | ||
|
||
//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 | ||
DecodeJSON(t, resp, &apiReactions) | ||
expectResponse := make(map[int]api.ReactionResponse) | ||
expectResponse[0] = api.ReactionResponse{ | ||
User: user1.APIFormat(), | ||
Reaction: "zzz", | ||
Created: time.Unix(1573248002, 0), | ||
} | ||
expectResponse[1] = api.ReactionResponse{ | ||
User: user2.APIFormat(), | ||
Reaction: "eyes", | ||
Created: time.Unix(1573248003, 0), | ||
} | ||
expectResponse[2] = apiNewReaction | ||
assert.Len(t, apiReactions, 3) | ||
for i, r := range apiReactions { | ||
assert.Equal(t, expectResponse[i].Reaction, r.Reaction) | ||
assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix()) | ||
assert.Equal(t, expectResponse[i].User.ID, r.User.ID) | ||
} | ||
} | ||
|
||
func TestAPICommentReactions(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment) | ||
_ = comment.LoadIssue() | ||
issue := comment.Issue | ||
_ = issue.LoadRepo() | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) | ||
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s", | ||
owner.Name, issue.Repo.Name, comment.ID, token) | ||
|
||
//Try to add not allowed reaction | ||
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{ | ||
Reaction: "wrong", | ||
}) | ||
resp := session.MakeRequest(t, req, http.StatusForbidden) | ||
|
||
//Delete none existing reaction | ||
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{ | ||
Reaction: "eyes", | ||
}) | ||
resp = session.MakeRequest(t, req, http.StatusOK) | ||
|
||
//Add allowed reaction | ||
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{ | ||
Reaction: "+1", | ||
}) | ||
resp = session.MakeRequest(t, req, http.StatusCreated) | ||
var apiNewReaction api.ReactionResponse | ||
DecodeJSON(t, resp, &apiNewReaction) | ||
|
||
//Add existing reaction | ||
resp = session.MakeRequest(t, req, http.StatusForbidden) | ||
|
||
//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 | ||
DecodeJSON(t, resp, &apiReactions) | ||
expectResponse := make(map[int]api.ReactionResponse) | ||
expectResponse[0] = api.ReactionResponse{ | ||
User: user2.APIFormat(), | ||
Reaction: "laugh", | ||
Created: time.Unix(1573248004, 0), | ||
} | ||
expectResponse[1] = api.ReactionResponse{ | ||
User: user1.APIFormat(), | ||
Reaction: "laugh", | ||
Created: time.Unix(1573248005, 0), | ||
} | ||
expectResponse[2] = apiNewReaction | ||
assert.Len(t, apiReactions, 3) | ||
for i, r := range apiReactions { | ||
assert.Equal(t, expectResponse[i].Reaction, r.Reaction) | ||
assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix()) | ||
assert.Equal(t, expectResponse[i].User.ID, r.User.ID) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,39 @@ | ||
[] # empty | ||
- | ||
id: 1 #issue reaction | ||
type: zzz # not allowed reaction (added before allowed reaction list has changed) | ||
issue_id: 1 | ||
comment_id: 0 | ||
user_id: 2 | ||
created_unix: 1573248001 | ||
|
||
- | ||
id: 2 #issue reaction | ||
type: zzz # not allowed reaction (added before allowed reaction list has changed) | ||
issue_id: 1 | ||
comment_id: 0 | ||
user_id: 1 | ||
created_unix: 1573248002 | ||
|
||
- | ||
id: 3 #issue reaction | ||
type: eyes # allowed reaction | ||
issue_id: 1 | ||
comment_id: 0 | ||
user_id: 2 | ||
created_unix: 1573248003 | ||
|
||
- | ||
id: 4 #comment reaction | ||
type: laugh # allowed reaction | ||
issue_id: 1 | ||
comment_id: 2 | ||
user_id: 2 | ||
created_unix: 1573248004 | ||
|
||
- | ||
id: 5 #comment reaction | ||
type: laugh # allowed reaction | ||
issue_id: 1 | ||
comment_id: 2 | ||
user_id: 1 | ||
created_unix: 1573248005 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package structs | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// EditReactionOption contain the reaction type | ||
type EditReactionOption struct { | ||
Reaction string `json:"content"` | ||
} | ||
|
||
// ReactionResponse contain one reaction | ||
type ReactionResponse struct { | ||
User *User `json:"user"` | ||
Reaction string `json:"content"` | ||
// swagger:strfmt date-time | ||
Created time.Time `json:"created_at"` | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.