2019-12-08 03:34:19 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-08 03:34:19 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-03-31 14:50:39 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2019-12-08 03:34:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-01-25 00:30:29 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2019-12-08 03:34:19 +05:30
|
|
|
)
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// GetIssueCommentReactions list reactions of a comment from an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
func GetIssueCommentReactions(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/issues/comments/{id}/reactions issue issueGetCommentReactions
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Get a list of reactions from a comment of an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the comment to edit
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
2019-12-31 13:51:21 +05:30
|
|
|
// "$ref": "#/responses/ReactionList"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err := issues_model.GetCommentByID(ctx, ctx.ParamsInt64(":id"))
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if issues_model.IsErrCommentNotExist(err) {
|
2019-12-08 03:34:19 +05:30
|
|
|
ctx.NotFound(err)
|
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := comment.LoadIssue(ctx); err != nil {
|
2020-10-29 18:18:07 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "comment.LoadIssue", err)
|
2023-11-25 22:51:21 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if comment.Issue.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
2020-10-29 18:18:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "GetIssueCommentReactions", errors.New("no permission to get reactions"))
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:47:37 +05:30
|
|
|
reactions, _, err := issues_model.FindCommentReactions(ctx, comment.IssueID, comment.ID)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2021-12-15 11:09:34 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "FindCommentReactions", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-31 14:50:39 +05:30
|
|
|
_, err = reactions.LoadUsers(ctx, ctx.Repo.Repository)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ReactionList.LoadUsers()", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:51:21 +05:30
|
|
|
var result []api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
for _, r := range reactions {
|
2019-12-31 13:51:21 +05:30
|
|
|
result = append(result, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, r.User, ctx.Doer),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: r.Type,
|
|
|
|
Created: r.CreatedUnix.AsTime(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, result)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// PostIssueCommentReaction add a reaction to a comment of an issue
|
2021-01-26 21:06:53 +05:30
|
|
|
func PostIssueCommentReaction(ctx *context.APIContext) {
|
2019-12-08 03:34:19 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/issues/comments/{id}/reactions issue issuePostCommentReaction
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Add a reaction to a comment of an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the comment to edit
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
|
|
|
// - name: content
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditReactionOption"
|
|
|
|
// responses:
|
2019-12-31 13:51:21 +05:30
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Reaction"
|
2019-12-08 03:34:19 +05:30
|
|
|
// "201":
|
2019-12-31 13:51:21 +05:30
|
|
|
// "$ref": "#/responses/Reaction"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.EditReactionOption)
|
|
|
|
|
|
|
|
changeIssueCommentReaction(ctx, *form, true)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// DeleteIssueCommentReaction remove a reaction from a comment of an issue
|
2021-01-26 21:06:53 +05:30
|
|
|
func DeleteIssueCommentReaction(ctx *context.APIContext) {
|
2019-12-08 03:34:19 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id}/reactions issue issueDeleteCommentReaction
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Remove a reaction from a comment of an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the comment to edit
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
|
|
|
// - name: content
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditReactionOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/empty"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.EditReactionOption)
|
|
|
|
|
|
|
|
changeIssueCommentReaction(ctx, *form, false)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOption, isCreateType bool) {
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err := issues_model.GetCommentByID(ctx, ctx.ParamsInt64(":id"))
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if issues_model.IsErrCommentNotExist(err) {
|
2019-12-08 03:34:19 +05:30
|
|
|
ctx.NotFound(err)
|
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-25 22:51:21 +05:30
|
|
|
if err = comment.LoadIssue(ctx); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "comment.LoadIssue() failed", err)
|
2023-11-25 22:51:21 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if comment.Issue.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Repo.CanReadIssuesOrPulls(comment.Issue.IsPull) {
|
|
|
|
ctx.NotFound()
|
|
|
|
return
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
2020-01-20 17:30:32 +05:30
|
|
|
if comment.Issue.IsLocked && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "ChangeIssueCommentReaction", errors.New("no permission to change reaction"))
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isCreateType {
|
|
|
|
// PostIssueCommentReaction part
|
2023-09-25 18:47:37 +05:30
|
|
|
reaction, err := issues_model.CreateCommentReaction(ctx, ctx.Doer.ID, comment.Issue.ID, comment.ID, form.Reaction)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-03-31 14:50:39 +05:30
|
|
|
if issues_model.IsErrForbiddenIssueReaction(err) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, err.Error(), err)
|
2022-03-31 14:50:39 +05:30
|
|
|
} else if issues_model.IsErrReactionAlreadyExist(err) {
|
2019-12-31 13:51:21 +05:30
|
|
|
ctx.JSON(http.StatusOK, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, ctx.Doer, ctx.Doer),
|
2019-12-31 13:51:21 +05:30
|
|
|
Reaction: reaction.Type,
|
|
|
|
Created: reaction.CreatedUnix.AsTime(),
|
|
|
|
})
|
2019-12-08 03:34:19 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateCommentReaction", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:51:21 +05:30
|
|
|
ctx.JSON(http.StatusCreated, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, ctx.Doer, ctx.Doer),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: reaction.Type,
|
|
|
|
Created: reaction.CreatedUnix.AsTime(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// DeleteIssueCommentReaction part
|
2023-09-25 18:47:37 +05:30
|
|
|
err = issues_model.DeleteCommentReaction(ctx, ctx.Doer.ID, comment.Issue.ID, comment.ID, form.Reaction)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteCommentReaction", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
// ToDo respond 204
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusOK)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// GetIssueReactions list reactions of an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
func GetIssueReactions(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/issues/{index}/reactions issue issueGetIssueReactions
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Get a list reactions of an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
2020-01-25 00:30:29 +05:30
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 10:27:38 +05:30
|
|
|
// description: page size of results
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2019-12-08 03:34:19 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
2019-12-31 13:51:21 +05:30
|
|
|
// "$ref": "#/responses/ReactionList"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2023-07-22 19:44:27 +05:30
|
|
|
issue, err := issues_model.GetIssueWithAttrsByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if issues_model.IsErrIssueNotExist(err) {
|
2019-12-08 03:34:19 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:53:31 +05:30
|
|
|
if !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "GetIssueReactions", errors.New("no permission to get reactions"))
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:47:37 +05:30
|
|
|
reactions, count, err := issues_model.FindIssueReactions(ctx, issue.ID, utils.GetListOptions(ctx))
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "FindIssueReactions", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-31 14:50:39 +05:30
|
|
|
_, err = reactions.LoadUsers(ctx, ctx.Repo.Repository)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ReactionList.LoadUsers()", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:51:21 +05:30
|
|
|
var result []api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
for _, r := range reactions {
|
2019-12-31 13:51:21 +05:30
|
|
|
result = append(result, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, r.User, ctx.Doer),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: r.Type,
|
|
|
|
Created: r.CreatedUnix.AsTime(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-15 11:09:34 +05:30
|
|
|
ctx.SetTotalCountHeader(count)
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, result)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// PostIssueReaction add a reaction to an issue
|
2021-01-26 21:06:53 +05:30
|
|
|
func PostIssueReaction(ctx *context.APIContext) {
|
2019-12-08 03:34:19 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/reactions issue issuePostIssueReaction
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Add a reaction to an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
|
|
|
// - name: content
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditReactionOption"
|
|
|
|
// responses:
|
2019-12-31 13:51:21 +05:30
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Reaction"
|
2019-12-08 03:34:19 +05:30
|
|
|
// "201":
|
2019-12-31 13:51:21 +05:30
|
|
|
// "$ref": "#/responses/Reaction"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.EditReactionOption)
|
|
|
|
changeIssueReaction(ctx, *form, true)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
2020-01-06 10:28:13 +05:30
|
|
|
// DeleteIssueReaction remove a reaction from an issue
|
2021-01-26 21:06:53 +05:30
|
|
|
func DeleteIssueReaction(ctx *context.APIContext) {
|
2019-12-08 03:34:19 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/reactions issue issueDeleteIssueReaction
|
|
|
|
// ---
|
2020-01-06 10:28:13 +05:30
|
|
|
// summary: Remove a reaction from an issue
|
2019-12-08 03:34:19 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// type: integer
|
|
|
|
// format: int64
|
|
|
|
// required: true
|
|
|
|
// - name: content
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditReactionOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/empty"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.EditReactionOption)
|
|
|
|
changeIssueReaction(ctx, *form, false)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, isCreateType bool) {
|
2023-07-22 19:44:27 +05:30
|
|
|
issue, err := issues_model.GetIssueWithAttrsByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if issues_model.IsErrIssueNotExist(err) {
|
2019-12-08 03:34:19 +05:30
|
|
|
ctx.NotFound()
|
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:30:32 +05:30
|
|
|
if issue.IsLocked && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "ChangeIssueCommentReaction", errors.New("no permission to change reaction"))
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isCreateType {
|
|
|
|
// PostIssueReaction part
|
2023-09-25 18:47:37 +05:30
|
|
|
reaction, err := issues_model.CreateIssueReaction(ctx, ctx.Doer.ID, issue.ID, form.Reaction)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2022-03-31 14:50:39 +05:30
|
|
|
if issues_model.IsErrForbiddenIssueReaction(err) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, err.Error(), err)
|
2022-03-31 14:50:39 +05:30
|
|
|
} else if issues_model.IsErrReactionAlreadyExist(err) {
|
2019-12-31 13:51:21 +05:30
|
|
|
ctx.JSON(http.StatusOK, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, ctx.Doer, ctx.Doer),
|
2019-12-31 13:51:21 +05:30
|
|
|
Reaction: reaction.Type,
|
|
|
|
Created: reaction.CreatedUnix.AsTime(),
|
|
|
|
})
|
2019-12-08 03:34:19 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateCommentReaction", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:51:21 +05:30
|
|
|
ctx.JSON(http.StatusCreated, api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
User: convert.ToUser(ctx, ctx.Doer, ctx.Doer),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: reaction.Type,
|
|
|
|
Created: reaction.CreatedUnix.AsTime(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// DeleteIssueReaction part
|
2023-09-25 18:47:37 +05:30
|
|
|
err = issues_model.DeleteIssueReaction(ctx, ctx.Doer.ID, issue.ID, form.Reaction)
|
2019-12-08 03:34:19 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteIssueReaction", err)
|
2019-12-08 03:34:19 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
// ToDo respond 204
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusOK)
|
2019-12-08 03:34:19 +05:30
|
|
|
}
|
|
|
|
}
|