diff options
author | 6543 <6543@obermui.de> | 2020-04-21 15:48:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-21 14:48:53 +0100 |
commit | bb4261a5ed678235fadef279fe1ba1505993a406 (patch) | |
tree | e3134123a4ccd273ddf5cd93701fef6db21d593b /routers/api/v1 | |
parent | 33176e8d27eeb47c6d63fbd24bf11ac9e5c60acd (diff) | |
download | gitea-bb4261a5ed678235fadef279fe1ba1505993a406.tar.gz gitea-bb4261a5ed678235fadef279fe1ba1505993a406.zip |
Add issue subscription check to API (#10967)
close #10962
Adds `GET /api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions/check`
-> return a `WachInfo`
Diffstat (limited to 'routers/api/v1')
-rw-r--r-- | routers/api/v1/api.go | 1 | ||||
-rw-r--r-- | routers/api/v1/repo/issue_subscription.go | 59 | ||||
-rw-r--r-- | routers/api/v1/user/watch.go | 12 |
3 files changed, 63 insertions, 9 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 225f6a5325..4b20c3e7c0 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -735,6 +735,7 @@ func RegisterRoutes(m *macaron.Macaron) { }) m.Group("/subscriptions", func() { m.Get("", repo.GetIssueSubscribers) + m.Get("/check", reqToken(), repo.CheckIssueSubscription) m.Put("/:user", reqToken(), repo.AddIssueSubscription) m.Delete("/:user", reqToken(), repo.DelIssueSubscription) }) diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index 0406edd207..999dda1738 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -133,6 +134,64 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { ctx.Status(http.StatusCreated) } +// CheckIssueSubscription check if user is subscribed to an issue +func CheckIssueSubscription(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions/check issue issueCheckSubscription + // --- + // summary: Check if user is subscribed to an issue + // 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 + // responses: + // "200": + // "$ref": "#/responses/WatchInfo" + // "404": + // "$ref": "#/responses/notFound" + + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrIssueNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err) + } + + return + } + + watching, err := models.CheckIssueWatch(ctx.User, issue) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.JSON(http.StatusOK, api.WatchInfo{ + Subscribed: watching, + Ignored: !watching, + Reason: nil, + CreatedAt: issue.CreatedUnix.AsTime(), + URL: issue.APIURL() + "/subscriptions", + RepositoryURL: ctx.Repo.Repository.APIURL(), + }) +} + // GetIssueSubscribers return subscribers of an issue func GetIssueSubscribers(ctx *context.APIContext) { // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 1fc736deba..1b55863034 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -9,7 +9,6 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -124,7 +123,7 @@ func IsWatching(ctx *context.APIContext) { Reason: nil, CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(), URL: subscriptionURL(ctx.Repo.Repository), - RepositoryURL: repositoryURL(ctx.Repo.Repository), + RepositoryURL: ctx.Repo.Repository.APIURL(), }) } else { ctx.NotFound() @@ -162,7 +161,7 @@ func Watch(ctx *context.APIContext) { Reason: nil, CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(), URL: subscriptionURL(ctx.Repo.Repository), - RepositoryURL: repositoryURL(ctx.Repo.Repository), + RepositoryURL: ctx.Repo.Repository.APIURL(), }) } @@ -197,10 +196,5 @@ func Unwatch(ctx *context.APIContext) { // subscriptionURL returns the URL of the subscription API endpoint of a repo func subscriptionURL(repo *models.Repository) string { - return repositoryURL(repo) + "/subscription" -} - -// repositoryURL returns the URL of the API endpoint of a repo -func repositoryURL(repo *models.Repository) string { - return setting.AppURL + "api/v1/" + repo.FullName() + return repo.APIURL() + "/subscription" } |