Skip to content

Commit 668eaf9

Browse files
6543techknowlogick
authored andcommitted
[Feature] Custom Reactions (#8886)
* add [ui] Reactions * move contend check from form to go functions * use else if * check if reaction is allowed only on react (so previous custom reaction can be still removed) * use $.AllowedReactions in templates * use ctx.Flash.Error * use it there too * add redirection * back to server error because a wrong reaction is a template issue ... * add emoji list link * add docs entry * small wording nit suggestions from @jolheiser - thx * same reactions as github * fix PR reactions * handle error so template JS could check * Add Integrations Test * add REACTIONS setting to cheat-sheet doc page
1 parent 674bc77 commit 668eaf9

File tree

13 files changed

+76
-18
lines changed

13 files changed

+76
-18
lines changed

custom/conf/app.ini.sample

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ SHOW_USER_EMAIL = true
149149
DEFAULT_THEME = gitea
150150
; All available themes. Allow users select personalized themes regardless of the value of `DEFAULT_THEME`.
151151
THEMES = gitea,arc-green
152+
; All available reactions. Allow users react with different emoji's
153+
: For the whole list look at https://gitea.com/gitea/gitea.com/issues/8
154+
REACTIONS = +1, -1, laugh, hooray, confused, heart, rocket, eyes
152155
; Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
153156
DEFAULT_SHOW_FULL_NAME = false
154157
; Whether to search within description at repository search on explore page.

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
118118
- `DEFAULT_THEME`: **gitea**: \[gitea, arc-green\]: Set the default theme for the Gitea install.
119119
- `THEMES`: **gitea,arc-green**: All available themes. Allow users select personalized themes
120120
regardless of the value of `DEFAULT_THEME`.
121+
- `REACTIONS`: All available reactions. Allow users react with different emoji's.
121122
- `DEFAULT_SHOW_FULL_NAME`: **false**: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
122123
- `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page.
123124
- `USE_SERVICE_WORKER`: **true**: Whether to enable a Service Worker to cache frontend assets.

docs/content/doc/advanced/customizing-gitea.en-us.md

+9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ Locales may change between versions, so keeping track of your customized locales
161161

162162
To add a custom Readme, add a markdown formatted file (without an `.md` extension) to `custom/options/readme`
163163

164+
### Reactions
165+
166+
To change reaction emoji's you can set allowed reactions at app.ini
167+
```
168+
[ui]
169+
REACTIONS = +1, -1, laugh, confused, heart, hooray, eyes
170+
```
171+
A full list of supported emoji's is at [emoji list](https://gitea.com/gitea/gitea.com/issues/8)
172+
164173
## Customizing the look of Gitea
165174

166175
As of version 1.6.0 Gitea has built-in themes. The two built-in themes are, the default theme `gitea`, and a dark theme `arc-green`. To change the look of your Gitea install change the value of `DEFAULT_THEME` in the [ui](https://docs.gitea.io/en-us/config-cheat-sheet/#ui-ui) section of `app.ini` to another one of the available options.

integrations/issue_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,32 @@ func TestIssueCommentClose(t *testing.T) {
194194
assert.Equal(t, "Description", val)
195195
}
196196

197+
func TestIssueReaction(t *testing.T) {
198+
defer prepareTestEnv(t)()
199+
session := loginUser(t, "user2")
200+
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
201+
202+
req := NewRequest(t, "GET", issueURL)
203+
resp := session.MakeRequest(t, req, http.StatusOK)
204+
htmlDoc := NewHTMLParser(t, resp.Body)
205+
206+
req = NewRequestWithValues(t, "POST", path.Join(issueURL, "/reactions/react"), map[string]string{
207+
"_csrf": htmlDoc.GetCSRF(),
208+
"content": "8ball",
209+
})
210+
session.MakeRequest(t, req, http.StatusInternalServerError)
211+
req = NewRequestWithValues(t, "POST", path.Join(issueURL, "/reactions/react"), map[string]string{
212+
"_csrf": htmlDoc.GetCSRF(),
213+
"content": "eyes",
214+
})
215+
session.MakeRequest(t, req, http.StatusOK)
216+
req = NewRequestWithValues(t, "POST", path.Join(issueURL, "/reactions/unreact"), map[string]string{
217+
"_csrf": htmlDoc.GetCSRF(),
218+
"content": "eyes",
219+
})
220+
session.MakeRequest(t, req, http.StatusOK)
221+
}
222+
197223
func TestIssueCrossReference(t *testing.T) {
198224
defer prepareTestEnv(t)()
199225

modules/auth/repo_form.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors)
347347

348348
// ReactionForm form for adding and removing reaction
349349
type ReactionForm struct {
350-
Content string `binding:"Required;In(+1,-1,laugh,confused,heart,hooray)"`
350+
Content string `binding:"Required"`
351351
}
352352

353353
// Validate validates the fields

modules/setting/setting.go

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ var (
169169
DefaultShowFullName bool
170170
DefaultTheme string
171171
Themes []string
172+
Reactions []string
172173
SearchRepoDescription bool
173174
UseServiceWorker bool
174175

@@ -198,6 +199,7 @@ var (
198199
MaxDisplayFileSize: 8388608,
199200
DefaultTheme: `gitea`,
200201
Themes: []string{`gitea`, `arc-green`},
202+
Reactions: []string{`+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes`},
201203
Admin: struct {
202204
UserPagingNum int
203205
RepoPagingNum int

routers/repo/issue.go

+13
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ func ViewIssue(ctx *context.Context) {
673673
}
674674
}
675675
ctx.Data["IssueWatch"] = iw
676+
ctx.Data["AllowedReactions"] = setting.UI.Reactions
676677

677678
issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink,
678679
ctx.Repo.Repository.ComposeMetas()))
@@ -1447,6 +1448,12 @@ func ChangeIssueReaction(ctx *context.Context, form auth.ReactionForm) {
14471448

14481449
switch ctx.Params(":action") {
14491450
case "react":
1451+
if !util.IsStringInSlice(form.Content, setting.UI.Reactions) {
1452+
err := fmt.Errorf("ChangeIssueReaction: '%s' is not an allowed reaction", form.Content)
1453+
ctx.ServerError(err.Error(), err)
1454+
return
1455+
}
1456+
14501457
reaction, err := models.CreateIssueReaction(ctx.User, issue, form.Content)
14511458
if err != nil {
14521459
log.Info("CreateIssueReaction: %s", err)
@@ -1542,6 +1549,12 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
15421549

15431550
switch ctx.Params(":action") {
15441551
case "react":
1552+
if !util.IsStringInSlice(form.Content, setting.UI.Reactions) {
1553+
err := fmt.Errorf("ChangeIssueReaction: '%s' is not an allowed reaction", form.Content)
1554+
ctx.ServerError(err.Error(), err)
1555+
return
1556+
}
1557+
15451558
reaction, err := models.CreateCommentReaction(ctx.User, comment.Issue, comment, form.Content)
15461559
if err != nil {
15471560
log.Info("CreateCommentReaction: %s", err)

routers/repo/pull.go

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
422422

423423
ctx.Data["NumCommits"] = compareInfo.Commits.Len()
424424
ctx.Data["NumFiles"] = compareInfo.NumFiles
425+
ctx.Data["AllowedReactions"] = setting.UI.Reactions
425426
return compareInfo
426427
}
427428

templates/repo/diff/comments.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{{$reactions := .Reactions.GroupByType}}
3939
{{if $reactions}}
4040
<div class="ui attached segment reactions">
41-
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions }}
41+
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions "AllowedReactions" $.AllowedReactions }}
4242
</div>
4343
{{end}}
4444
</div>

templates/repo/issue/view_content.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{{end}}
2929
{{if not $.Repository.IsArchived}}
3030
<div class="ui right actions">
31-
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) }}
31+
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "AllowedReactions" $.AllowedReactions}}
3232
{{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" .Issue "delete" false "diff" false }}
3333
</div>
3434
{{end}}
@@ -47,7 +47,7 @@
4747
{{$reactions := .Issue.Reactions.GroupByType}}
4848
{{if $reactions}}
4949
<div class="ui attached segment reactions">
50-
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions }}
50+
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions "AllowedReactions" $.AllowedReactions}}
5151
</div>
5252
{{end}}
5353
{{if .Issue.Attachments}}

templates/repo/issue/view_content/add_reaction.tmpl

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
<div class="menu has-emoji">
88
<div class="header">{{ .ctx.i18n.Tr "repo.pick_reaction"}}</div>
99
<div class="divider"></div>
10-
<div class="item" data-content="+1">:+1:</div>
11-
<div class="item" data-content="-1">:-1:</div>
12-
<div class="item" data-content="laugh">:laughing:</div>
13-
<div class="item" data-content="confused">:confused:</div>
14-
<div class="item" data-content="heart">:heart:</div>
15-
<div class="item" data-content="hooray">:tada:</div>
10+
{{range $value := .AllowedReactions}}
11+
{{if eq $value "hooray"}}
12+
<div class="item" data-content="hooray">:tada:</div>
13+
{{else if eq $value "laugh"}}
14+
<div class="item" data-content="laugh">:laughing:</div>
15+
{{else}}
16+
<div class="item" data-content="{{$value}}">:{{$value}}:</div>
17+
{{end}}
18+
{{end}}
1619
</div>
1720
</div>
1821
{{end}}

templates/repo/issue/view_content/comments.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
{{end}}
3737
</div>
3838
{{end}}
39-
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) }}
39+
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "AllowedReactions" $.AllowedReactions}}
4040
{{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" . "delete" true "diff" false }}
4141
</div>
4242
{{end}}
@@ -55,7 +55,7 @@
5555
{{$reactions := .Reactions.GroupByType}}
5656
{{if $reactions}}
5757
<div class="ui attached segment reactions">
58-
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions }}
58+
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions "AllowedReactions" $.AllowedReactions}}
5959
</div>
6060
{{end}}
6161
{{if .Attachments}}

templates/repo/issue/view_content/reactions.tmpl

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<a class="ui label basic{{if $value.HasUser $.ctx.SignedUserID}} blue{{end}}{{if not $.ctx.IsSigned}} disabled{{end}} has-emoji" data-title="{{$value.GetFirstUsers}}{{if gt ($value.GetMoreUserCount) 0}} {{ $.ctx.i18n.Tr "repo.reactions_more" $value.GetMoreUserCount}}{{end}}" data-content="{{ $key }}" data-action-url="{{ $.ActionURL }}">
33
{{if eq $key "hooray"}}
44
:tada:
5+
{{else if eq $key "laugh"}}
6+
:laughing:
57
{{else}}
6-
{{if eq $key "laugh"}}
7-
:laughing:
8-
{{else}}
9-
:{{$key}}:
10-
{{end}}
8+
:{{$key}}:
119
{{end}}
1210
{{len $value}}
1311
</a>
1412
{{end}}
15-
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $.ctx "ActionURL" .ActionURL }}
13+
{{if $.AllowedReactions}}
14+
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $.ctx "ActionURL" .ActionURL "AllowedReactions" $.AllowedReactions}}
15+
{{end}}

0 commit comments

Comments
 (0)