|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package repo |
| 6 | + |
| 7 | +import ( |
| 8 | + "code.gitea.io/gitea/models" |
| 9 | + "code.gitea.io/gitea/modules/context" |
| 10 | + api "code.gitea.io/gitea/modules/structs" |
| 11 | +) |
| 12 | + |
| 13 | +// AddIssueSubscription Subscribe user to issue |
| 14 | +func AddIssueSubscription(ctx *context.APIContext) { |
| 15 | + // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueAddSubscription |
| 16 | + // --- |
| 17 | + // summary: Subscribe user to issue |
| 18 | + // consumes: |
| 19 | + // - application/json |
| 20 | + // produces: |
| 21 | + // - application/json |
| 22 | + // parameters: |
| 23 | + // - name: owner |
| 24 | + // in: path |
| 25 | + // description: owner of the repo |
| 26 | + // type: string |
| 27 | + // required: true |
| 28 | + // - name: repo |
| 29 | + // in: path |
| 30 | + // description: name of the repo |
| 31 | + // type: string |
| 32 | + // required: true |
| 33 | + // - name: index |
| 34 | + // in: path |
| 35 | + // description: index of the issue |
| 36 | + // type: integer |
| 37 | + // format: int64 |
| 38 | + // required: true |
| 39 | + // - name: user |
| 40 | + // in: path |
| 41 | + // description: user to subscribe |
| 42 | + // type: string |
| 43 | + // required: true |
| 44 | + // responses: |
| 45 | + // "201": |
| 46 | + // "$ref": "#/responses/empty" |
| 47 | + // "304": |
| 48 | + // description: User can only subscribe itself if he is no admin |
| 49 | + // "404": |
| 50 | + // description: Issue not found |
| 51 | + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) |
| 52 | + if err != nil { |
| 53 | + if models.IsErrIssueNotExist(err) { |
| 54 | + ctx.NotFound() |
| 55 | + } else { |
| 56 | + ctx.Error(500, "GetIssueByIndex", err) |
| 57 | + } |
| 58 | + |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + user, err := models.GetUserByName(ctx.Params(":user")) |
| 63 | + if err != nil { |
| 64 | + if models.IsErrUserNotExist(err) { |
| 65 | + ctx.NotFound() |
| 66 | + } else { |
| 67 | + ctx.Error(500, "GetUserByName", err) |
| 68 | + } |
| 69 | + |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + //only admin and user for itself can change subscription |
| 74 | + if user.ID != ctx.User.ID && !ctx.User.IsAdmin { |
| 75 | + ctx.Error(403, "User", nil) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, true); err != nil { |
| 80 | + ctx.Error(500, "CreateOrUpdateIssueWatch", err) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + ctx.Status(201) |
| 85 | +} |
| 86 | + |
| 87 | +// DelIssueSubscription Unsubscribe user from issue |
| 88 | +func DelIssueSubscription(ctx *context.APIContext) { |
| 89 | + // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription |
| 90 | + // --- |
| 91 | + // summary: Unsubscribe user from issue |
| 92 | + // consumes: |
| 93 | + // - application/json |
| 94 | + // produces: |
| 95 | + // - application/json |
| 96 | + // parameters: |
| 97 | + // - name: owner |
| 98 | + // in: path |
| 99 | + // description: owner of the repo |
| 100 | + // type: string |
| 101 | + // required: true |
| 102 | + // - name: repo |
| 103 | + // in: path |
| 104 | + // description: name of the repo |
| 105 | + // type: string |
| 106 | + // required: true |
| 107 | + // - name: index |
| 108 | + // in: path |
| 109 | + // description: index of the issue |
| 110 | + // type: integer |
| 111 | + // format: int64 |
| 112 | + // required: true |
| 113 | + // - name: user |
| 114 | + // in: path |
| 115 | + // description: user witch unsubscribe |
| 116 | + // type: string |
| 117 | + // required: true |
| 118 | + // responses: |
| 119 | + // "201": |
| 120 | + // "$ref": "#/responses/empty" |
| 121 | + // "304": |
| 122 | + // description: User can only subscribe itself if he is no admin |
| 123 | + // "404": |
| 124 | + // description: Issue not found |
| 125 | + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) |
| 126 | + if err != nil { |
| 127 | + if models.IsErrIssueNotExist(err) { |
| 128 | + ctx.NotFound() |
| 129 | + } else { |
| 130 | + ctx.Error(500, "GetIssueByIndex", err) |
| 131 | + } |
| 132 | + |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + user, err := models.GetUserByName(ctx.Params(":user")) |
| 137 | + if err != nil { |
| 138 | + if models.IsErrUserNotExist(err) { |
| 139 | + ctx.NotFound() |
| 140 | + } else { |
| 141 | + ctx.Error(500, "GetUserByName", err) |
| 142 | + } |
| 143 | + |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + //only admin and user for itself can change subscription |
| 148 | + if user.ID != ctx.User.ID && !ctx.User.IsAdmin { |
| 149 | + ctx.Error(403, "User", nil) |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, false); err != nil { |
| 154 | + ctx.Error(500, "CreateOrUpdateIssueWatch", err) |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + ctx.Status(201) |
| 159 | +} |
| 160 | + |
| 161 | +// GetIssueSubscribers return subscribers of an issue |
| 162 | +func GetIssueSubscribers(ctx *context.APIContext, form api.User) { |
| 163 | + // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions |
| 164 | + // --- |
| 165 | + // summary: Get users who subscribed on an issue. |
| 166 | + // consumes: |
| 167 | + // - application/json |
| 168 | + // produces: |
| 169 | + // - application/json |
| 170 | + // parameters: |
| 171 | + // - name: owner |
| 172 | + // in: path |
| 173 | + // description: owner of the repo |
| 174 | + // type: string |
| 175 | + // required: true |
| 176 | + // - name: repo |
| 177 | + // in: path |
| 178 | + // description: name of the repo |
| 179 | + // type: string |
| 180 | + // required: true |
| 181 | + // - name: index |
| 182 | + // in: path |
| 183 | + // description: index of the issue |
| 184 | + // type: integer |
| 185 | + // format: int64 |
| 186 | + // required: true |
| 187 | + // responses: |
| 188 | + // "201": |
| 189 | + // "$ref": "#/responses/empty" |
| 190 | + // "404": |
| 191 | + // description: Issue not found |
| 192 | + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) |
| 193 | + if err != nil { |
| 194 | + if models.IsErrIssueNotExist(err) { |
| 195 | + ctx.NotFound() |
| 196 | + } else { |
| 197 | + ctx.Error(500, "GetIssueByIndex", err) |
| 198 | + } |
| 199 | + |
| 200 | + return |
| 201 | + } |
| 202 | + |
| 203 | + iwl, err := models.GetIssueWatchers(issue.ID) |
| 204 | + if err != nil { |
| 205 | + ctx.Error(500, "GetIssueWatchers", err) |
| 206 | + return |
| 207 | + } |
| 208 | + |
| 209 | + users, err := iwl.LoadWatchUsers() |
| 210 | + if err != nil { |
| 211 | + ctx.Error(500, "LoadWatchUsers", err) |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | + ctx.JSON(200, users.APIFormat()) |
| 216 | +} |
0 commit comments