Skip to content

Commit a3a8f37

Browse files
committed
First draft of GetCommentReactions
1 parent eeacb98 commit a3a8f37

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

models/issue_reaction.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010

1111
"code.gitea.io/gitea/modules/setting"
12+
api "code.gitea.io/gitea/modules/structs"
1213
"code.gitea.io/gitea/modules/timeutil"
1314

1415
"xorm.io/builder"
@@ -26,6 +27,9 @@ type Reaction struct {
2627
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
2728
}
2829

30+
// ReactionList represents list of reactions
31+
type ReactionList []*Reaction
32+
2933
// FindReactionsOptions describes the conditions to Find reactions
3034
type FindReactionsOptions struct {
3135
IssueID int64
@@ -43,8 +47,13 @@ func (opts *FindReactionsOptions) toConds() builder.Cond {
4347
return cond
4448
}
4549

46-
func findReactions(e Engine, opts FindReactionsOptions) ([]*Reaction, error) {
47-
reactions := make([]*Reaction, 0, 10)
50+
//FindReactions returns Reactions based
51+
func FindReactions(opts FindReactionsOptions) (ReactionList, error) {
52+
return findReactions(x, opts)
53+
}
54+
55+
func findReactions(e Engine, opts FindReactionsOptions) (ReactionList, error) {
56+
reactions := make(ReactionList, 0, 10)
4857
sess := e.Where(opts.toConds())
4958
return reactions, sess.
5059
Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id").
@@ -160,9 +169,6 @@ func DeleteCommentReaction(doer *User, issue *Issue, comment *Comment, content s
160169
})
161170
}
162171

163-
// ReactionList represents list of reactions
164-
type ReactionList []*Reaction
165-
166172
// HasUser check if user has reacted
167173
func (list ReactionList) HasUser(userID int64) bool {
168174
if userID == 0 {
@@ -247,3 +253,24 @@ func (list ReactionList) GetMoreUserCount() int {
247253
}
248254
return len(list) - setting.UI.ReactionMaxUserNum
249255
}
256+
257+
// APIFormat returns Raction in api Format
258+
func (list ReactionList) APIFormat() api.CommentReactionList {
259+
result := []*api.CommentReaction{}
260+
users := map[string][]*string{}
261+
counts := map[string]int64{}
262+
263+
for _, r := range list {
264+
users[r.Type] = append(users[r.Type], &r.User.LoginName)
265+
counts[r.Type]++
266+
}
267+
268+
for k, v := range users {
269+
result = append(result, &api.CommentReaction{
270+
Reaction: k,
271+
Users: v,
272+
Count: counts[k],
273+
})
274+
}
275+
return result
276+
}

routers/api/v1/repo/issue_comment.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,63 @@ func deleteIssueComment(ctx *context.APIContext) {
394394
ctx.Status(204)
395395
}
396396

397+
//GetCommentReactions return all reactions of a specific comment
397398
func GetCommentReactions(ctx *context.APIContext, form api.CommentReactionList) {
399+
// swagger:operation GET /repos/{owner}/{repo}/issues/{index}/comments/{id}/reactions issue issueGetCommentReactions
400+
// ---
401+
// summary: Return all reactions of a specific comment
402+
// produces:
403+
// - application/json
404+
// parameters:
405+
// - name: owner
406+
// in: path
407+
// description: owner of the repo
408+
// type: string
409+
// required: true
410+
// - name: repo
411+
// in: path
412+
// description: name of the repo
413+
// type: string
414+
// required: true
415+
// - name: index
416+
// in: path
417+
// description: this parameter is ignored
418+
// type: integer
419+
// required: true
420+
// - name: id
421+
// in: path
422+
// description: id of the comment
423+
// type: integer
424+
// format: int64
425+
// required: true
426+
// responses:
427+
// "200":
428+
// "$ref": "#/responses/CommentReactions"
429+
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
430+
if err != nil {
431+
ctx.Error(500, "GetIssueByIndex", err)
432+
return
433+
}
434+
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
435+
if err != nil {
436+
if models.IsErrCommentNotExist(err) {
437+
ctx.NotFound(err)
438+
} else {
439+
ctx.Error(500, "GetCommentByID", err)
440+
}
441+
return
442+
}
443+
444+
rl, err := models.FindReactions(models.FindReactionsOptions{IssueID: issue.ID, CommentID: comment.ID})
445+
if err != nil {
446+
ctx.Error(500, "FindReactionsOptions", err)
447+
return
448+
} else if rl == nil {
449+
ctx.NotFound("")
450+
return
451+
}
452+
453+
ctx.JSON(200, rl.APIFormat())
398454

399455
}
400456

0 commit comments

Comments
 (0)