Files

295 lines
7.2 KiB
Go
Raw Permalink Normal View History

2019-11-02 16:27:49 +01:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2019-11-02 16:27:49 +01:00
package repo
import (
"fmt"
2019-12-20 18:07:12 +01:00
"net/http"
issues_model "code.gitea.io/gitea/models/issues"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
2020-01-24 19:00:29 +00:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
2019-11-02 16:27:49 +01:00
)
// AddIssueSubscription Subscribe user to issue
func AddIssueSubscription(ctx *context.APIContext) {
// swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueAddSubscription
// ---
// summary: Subscribe user to 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
// - name: user
// in: path
// description: username of the user to subscribe the issue to
2019-11-02 16:27:49 +01:00
// type: string
// required: true
// responses:
// "200":
// description: Already subscribed
2019-11-02 16:27:49 +01:00
// "201":
// description: Successfully Subscribed
2019-11-02 16:27:49 +01:00
// "304":
// description: User can only subscribe itself if he is no admin
// "404":
2019-12-20 18:07:12 +01:00
// "$ref": "#/responses/notFound"
2019-11-20 15:50:54 +01:00
setIssueSubscription(ctx, true)
2019-11-02 16:27:49 +01:00
}
// DelIssueSubscription Unsubscribe user from issue
func DelIssueSubscription(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
// ---
// summary: Unsubscribe user from 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
// - name: user
// in: path
// description: username of the user to unsubscribe from an issue
2019-11-02 16:27:49 +01:00
// type: string
// required: true
// responses:
// "200":
// description: Already unsubscribed
2019-11-02 16:27:49 +01:00
// "201":
// description: Successfully Unsubscribed
2019-11-02 16:27:49 +01:00
// "304":
// description: User can only subscribe itself if he is no admin
// "404":
2019-12-20 18:07:12 +01:00
// "$ref": "#/responses/notFound"
2019-11-20 15:50:54 +01:00
setIssueSubscription(ctx, false)
}
func setIssueSubscription(ctx *context.APIContext, watch bool) {
2024-12-24 21:47:45 +08:00
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
2019-11-02 16:27:49 +01:00
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2019-11-02 16:27:49 +01:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
}
return
}
2024-12-24 21:47:45 +08:00
user, err := user_model.GetUserByName(ctx, ctx.PathParam("user"))
2019-11-02 16:27:49 +01:00
if err != nil {
if user_model.IsErrUserNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2019-11-02 16:27:49 +01:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
}
return
}
2022-01-20 18:46:10 +01:00
// only admin and user for itself can change subscription
2022-03-22 08:03:22 +01:00
if user.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusForbidden, fmt.Errorf("%s is not permitted to change subscriptions for %s", ctx.Doer.Name, user.Name))
2019-11-02 16:27:49 +01:00
return
}
current, err := issues_model.CheckIssueWatch(ctx, user, issue)
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
// If watch state won't change
if current == watch {
ctx.Status(http.StatusOK)
return
}
// Update watch state
if err := issues_model.CreateOrUpdateIssueWatch(ctx, user.ID, issue.ID, watch); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
return
}
2019-12-20 18:07:12 +01:00
ctx.Status(http.StatusCreated)
2019-11-02 16:27:49 +01:00
}
// 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"
2024-12-24 21:47:45 +08:00
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
}
return
}
watching, err := issues_model.CheckIssueWatch(ctx, ctx.Doer, issue)
if err != nil {
2025-02-17 14:13:17 +08:00
ctx.APIErrorInternal(err)
return
}
ctx.JSON(http.StatusOK, api.WatchInfo{
Subscribed: watching,
Ignored: !watching,
Reason: nil,
CreatedAt: issue.CreatedUnix.AsTime(),
URL: issue.APIURL(ctx) + "/subscriptions",
RepositoryURL: ctx.Repo.Repository.APIURL(),
})
}
2019-11-02 16:27:49 +01:00
// GetIssueSubscribers return subscribers of an issue
2019-11-20 15:50:54 +01:00
func GetIssueSubscribers(ctx *context.APIContext) {
2019-11-02 16:27:49 +01:00
// swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
// ---
// summary: Get users who subscribed on 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
2020-01-24 19:00:29 +00:00
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
2020-01-24 19:00:29 +00:00
// type: integer
2019-11-02 16:27:49 +01:00
// responses:
2019-11-20 15:50:54 +01:00
// "200":
// "$ref": "#/responses/UserList"
2019-11-02 16:27:49 +01:00
// "404":
2019-12-20 18:07:12 +01:00
// "$ref": "#/responses/notFound"
2024-12-24 21:47:45 +08:00
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
2019-11-02 16:27:49 +01:00
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2019-11-02 16:27:49 +01:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
}
return
}
iwl, err := issues_model.GetIssueWatchers(ctx, issue.ID, utils.GetListOptions(ctx))
2019-11-02 16:27:49 +01:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
return
}
2022-01-20 18:46:10 +01:00
userIDs := make([]int64, 0, len(iwl))
for _, iw := range iwl {
userIDs = append(userIDs, iw.UserID)
}
2023-09-14 19:09:32 +02:00
users, err := user_model.GetUsersByIDs(ctx, userIDs)
2019-11-02 16:27:49 +01:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2019-11-02 16:27:49 +01:00
return
}
apiUsers := make([]*api.User, 0, len(users))
for _, v := range users {
apiUsers = append(apiUsers, convert.ToUser(ctx, v, ctx.Doer))
}
2019-11-02 16:27:49 +01:00
count, err := issues_model.CountIssueWatchers(ctx, issue.ID)
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
return
}
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, apiUsers)
2019-11-02 16:27:49 +01:00
}