|
| 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