Skip to content

Commit 98baaa2

Browse files
committed
Consolidate reaction web/repo logic
1 parent 940fa35 commit 98baaa2

File tree

3 files changed

+94
-52
lines changed

3 files changed

+94
-52
lines changed

routers/web/repo/conversation.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -911,37 +911,13 @@ func ChangeConversationCommentReaction(ctx *context.Context) {
911911

912912
switch ctx.PathParam(":action") {
913913
case "react":
914-
reaction, err := conversation_service.CreateCommentReaction(ctx, ctx.Doer, comment, form.Content)
915-
if err != nil {
916-
if conversations_model.IsErrForbiddenConversationReaction(err) || errors.Is(err, user_model.ErrBlockedUser) {
917-
ctx.ServerError("ChangeConversationReaction", err)
918-
return
919-
}
920-
log.Info("CreateConversationCommentReaction: %s", err)
921-
break
922-
}
923-
// Reload new reactions
924-
comment.Reactions = nil
925-
if err = comment.LoadReactions(ctx, ctx.Repo.Repository); err != nil {
926-
log.Info("comment.LoadReactions: %s", err)
914+
if err = AddReaction(ctx, form, comment, nil); err != nil {
927915
break
928916
}
929-
930-
log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, comment.Conversation.ID, comment.ID, reaction.ID)
931917
case "unreact":
932-
if err := conversations_model.DeleteCommentReaction(ctx, ctx.Doer.ID, comment.Conversation.ID, comment.ID, form.Content); err != nil {
933-
ctx.ServerError("DeleteConversationCommentReaction", err)
934-
return
935-
}
936-
937-
// Reload new reactions
938-
comment.Reactions = nil
939-
if err = comment.LoadReactions(ctx, ctx.Repo.Repository); err != nil {
940-
log.Info("comment.LoadReactions: %s", err)
918+
if err = RemoveReaction(ctx, form, comment, nil); err != nil {
941919
break
942920
}
943-
944-
log.Trace("Reaction for conversation comment removed: %d/%d/%d", ctx.Repo.Repository.ID, comment.Conversation.ID, comment.ID)
945921
default:
946922
ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.PathParam(":action")), nil)
947923
return

routers/web/repo/issue.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,37 +3261,13 @@ func ChangeIssueReaction(ctx *context.Context) {
32613261

32623262
switch ctx.PathParam(":action") {
32633263
case "react":
3264-
reaction, err := issue_service.CreateIssueReaction(ctx, ctx.Doer, issue, form.Content)
3265-
if err != nil {
3266-
if issues_model.IsErrForbiddenIssueReaction(err) || errors.Is(err, user_model.ErrBlockedUser) {
3267-
ctx.ServerError("ChangeIssueReaction", err)
3268-
return
3269-
}
3270-
log.Info("CreateIssueReaction: %s", err)
3264+
if err := AddReaction(ctx, form, nil, issue); err != nil {
32713265
break
32723266
}
3273-
// Reload new reactions
3274-
issue.Reactions = nil
3275-
if err = issue.LoadAttributes(ctx); err != nil {
3276-
log.Info("issue.LoadAttributes: %s", err)
3277-
break
3278-
}
3279-
3280-
log.Trace("Reaction for issue created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, reaction.ID)
32813267
case "unreact":
3282-
if err := issues_model.DeleteIssueReaction(ctx, ctx.Doer.ID, issue.ID, form.Content); err != nil {
3283-
ctx.ServerError("DeleteIssueReaction", err)
3284-
return
3285-
}
3286-
3287-
// Reload new reactions
3288-
issue.Reactions = nil
3289-
if err := issue.LoadAttributes(ctx); err != nil {
3290-
log.Info("issue.LoadAttributes: %s", err)
3268+
if err := RemoveReaction(ctx, form, nil, issue); err != nil {
32913269
break
32923270
}
3293-
3294-
log.Trace("Reaction for issue removed: %d/%d", ctx.Repo.Repository.ID, issue.ID)
32953271
default:
32963272
ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.PathParam(":action")), nil)
32973273
return

routers/web/repo/reaction.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package repo
2+
3+
import (
4+
"errors"
5+
6+
conversations_model "code.gitea.io/gitea/models/conversations"
7+
issues_model "code.gitea.io/gitea/models/issues"
8+
user_model "code.gitea.io/gitea/models/user"
9+
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/services/context"
11+
conversation_service "code.gitea.io/gitea/services/conversation"
12+
"code.gitea.io/gitea/services/forms"
13+
issue_service "code.gitea.io/gitea/services/issue"
14+
)
15+
16+
func AddReaction(ctx *context.Context, form *forms.ReactionForm, comment *conversations_model.ConversationComment, issue *issues_model.Issue) error {
17+
if issue != nil {
18+
reaction, err := issue_service.CreateIssueReaction(ctx, ctx.Doer, issue, form.Content)
19+
if err != nil {
20+
if issues_model.IsErrForbiddenIssueReaction(err) || errors.Is(err, user_model.ErrBlockedUser) {
21+
ctx.ServerError("ChangeIssueReaction", err)
22+
return err
23+
}
24+
log.Info("CreateIssueReaction: %s", err)
25+
return err
26+
}
27+
// Reload new reactions
28+
issue.Reactions = nil
29+
if err = issue.LoadAttributes(ctx); err != nil {
30+
log.Info("issue.LoadAttributes: %s", err)
31+
return err
32+
}
33+
34+
log.Trace("Reaction for issue created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, reaction.ID)
35+
} else if comment != nil {
36+
37+
reaction, err := conversation_service.CreateCommentReaction(ctx, ctx.Doer, comment, form.Content)
38+
if err != nil {
39+
if conversations_model.IsErrForbiddenConversationReaction(err) || errors.Is(err, user_model.ErrBlockedUser) {
40+
ctx.ServerError("ChangeConversationReaction", err)
41+
return err
42+
}
43+
log.Info("CreateConversationCommentReaction: %s", err)
44+
return err
45+
}
46+
// Reload new reactions
47+
comment.Reactions = nil
48+
if err = comment.LoadReactions(ctx, ctx.Repo.Repository); err != nil {
49+
log.Info("comment.LoadReactions: %s", err)
50+
return err
51+
}
52+
53+
log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, comment.Conversation.ID, comment.ID, reaction.ID)
54+
}
55+
56+
return nil
57+
}
58+
59+
func RemoveReaction(ctx *context.Context, form *forms.ReactionForm, comment *conversations_model.ConversationComment, issue *issues_model.Issue) error {
60+
if issue != nil {
61+
if err := issues_model.DeleteIssueReaction(ctx, ctx.Doer.ID, issue.ID, form.Content); err != nil {
62+
ctx.ServerError("DeleteIssueReaction", err)
63+
return err
64+
}
65+
66+
// Reload new reactions
67+
issue.Reactions = nil
68+
if err := issue.LoadAttributes(ctx); err != nil {
69+
log.Info("issue.LoadAttributes: %s", err)
70+
return err
71+
}
72+
73+
log.Trace("Reaction for issue removed: %d/%d", ctx.Repo.Repository.ID, issue.ID)
74+
} else if comment != nil {
75+
if err := conversations_model.DeleteCommentReaction(ctx, ctx.Doer.ID, comment.Conversation.ID, comment.ID, form.Content); err != nil {
76+
ctx.ServerError("DeleteConversationCommentReaction", err)
77+
return err
78+
}
79+
80+
// Reload new reactions
81+
comment.Reactions = nil
82+
if err := comment.LoadReactions(ctx, ctx.Repo.Repository); err != nil {
83+
log.Info("comment.LoadReactions: %s", err)
84+
return err
85+
}
86+
87+
log.Trace("Reaction for conversation comment removed: %d/%d/%d", ctx.Repo.Repository.ID, comment.Conversation.ID, comment.ID)
88+
}
89+
return nil
90+
}

0 commit comments

Comments
 (0)