mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-25 10:57:37 +00:00
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/10927
Continuation of #10598
Closes #7254
Implement repo granular watch selection
I adopted a lot of the frontend from #10598 while redoing the entire back-end. I propose this new model:
- Remove the WatchMode enum.
- Add the WatchSelection struct, which represents the granular selection. Notice that there is no `watching` bool. I tried very hard to keep the structure as simple and redundancy-free as possible. Therefore, people not watching a repo at all either don't have a watch record or one with an entirely unselected WatchSelection struct.
- Add the WatchSource enum. It replaces the WatchMode enum and has a single purpose: determine whether a watch was explicitly or automatically initiated.
Notice that replacing this
```go
And("`watch`.mode<>?", WatchModeDont).
```
with this is correct:
```go
And(
builder.Or(
builder.Eq{"`watch`.watch_selection_issues": true},
builder.Eq{"`watch`.watch_selection_pull_requests": true},
builder.Eq{"`watch`.watch_selection_releases": true},
),
).
```
That's because there are four modes: dont, none, auto and normal. When `<>` with dont, we look for auto and normal, because there are no records with none. Therefore, the old code looks for records that indicate watching. The code I replaced this with does so, too, just more granular.
Also notice that I've prepared a future `user preset` in a few places. See below for a little more info on that.
## Next PR
I plan to continue working on this. I want to implement a `user preset` option. The user sets that `user preset` in her settings and may use them in any repo.
<details>
- rename account settings to account and notifications
- user preset (always use this preset for newly accessible repos (according to AutoWatchOnChanges and AutoWatchNewRepos))
</details>
## Further PRs
- make api able to granular watch
- move (email) notifications to new notifications tab
Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-authored-by: pat-s <patrick.schratz@gmail.com>
Reviewed-on: #10927
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
(cherry picked from commit 8770ffc848)
220 lines
6.4 KiB
Go
220 lines
6.4 KiB
Go
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"forgejo.org/models/db"
|
|
access_model "forgejo.org/models/perm/access"
|
|
repo_model "forgejo.org/models/repo"
|
|
user_model "forgejo.org/models/user"
|
|
api "forgejo.org/modules/structs"
|
|
"forgejo.org/routers/api/v1/utils"
|
|
"forgejo.org/services/context"
|
|
"forgejo.org/services/convert"
|
|
)
|
|
|
|
// getWatchedRepos returns the repos that the user with the specified userID is watching
|
|
func getWatchedRepos(ctx *context.APIContext, user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) {
|
|
watchedRepos, total, err := repo_model.GetWatchedRepos(ctx, user.ID, private, listOptions, ctx.Reducer())
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
repos := make([]*api.Repository, len(watchedRepos))
|
|
for i, watched := range watchedRepos {
|
|
// Resource filtering is implemented above in the call to GetWatchedRepos, and doesn't need to be taken into
|
|
// account here:
|
|
//
|
|
// nosemgrep: forgejo-api-use-resource-GetUserRepoPermission
|
|
permission, err := access_model.GetUserRepoPermission(ctx, watched, user)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
repos[i] = convert.ToRepo(ctx, watched, permission)
|
|
}
|
|
return repos, total, nil
|
|
}
|
|
|
|
// GetWatchedRepos returns the repos that the user specified in ctx is watching
|
|
func GetWatchedRepos(ctx *context.APIContext) {
|
|
// swagger:operation GET /users/{username}/subscriptions user userListSubscriptions
|
|
// ---
|
|
// summary: List the repositories watched by a user
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: username
|
|
// type: string
|
|
// in: path
|
|
// description: username of the user
|
|
// required: true
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/RepositoryList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
private := ctx.User().ID == ctx.Doer().ID
|
|
repos, total, err := getWatchedRepos(ctx, ctx.User(), private, utils.GetListOptions(ctx))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
|
return
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(total)
|
|
ctx.JSON(http.StatusOK, &repos)
|
|
}
|
|
|
|
// GetMyWatchedRepos returns the repos that the authenticated user is watching
|
|
func GetMyWatchedRepos(ctx *context.APIContext) {
|
|
// swagger:operation GET /user/subscriptions user userCurrentListSubscriptions
|
|
// ---
|
|
// summary: List repositories watched by the authenticated user
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/RepositoryList"
|
|
// "401":
|
|
// "$ref": "#/responses/unauthorized"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
repos, total, err := getWatchedRepos(ctx, ctx.Doer(), true, utils.GetListOptions(ctx))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
|
return
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(total)
|
|
ctx.JSON(http.StatusOK, &repos)
|
|
}
|
|
|
|
// IsWatching returns whether the authenticated user is watching the repo
|
|
// specified in ctx
|
|
func IsWatching(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/subscription repository userCurrentCheckSubscription
|
|
// ---
|
|
// summary: Check if the current user is watching a repo
|
|
// 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
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/WatchInfo"
|
|
// "404":
|
|
// description: User is not watching this repo or repo do not exist
|
|
|
|
if repo_model.IsWatcher(ctx, ctx.Doer().ID, ctx.Repo().Repository.ID) {
|
|
ctx.JSON(http.StatusOK, api.WatchInfo{
|
|
Subscribed: true,
|
|
Ignored: false,
|
|
Reason: nil,
|
|
CreatedAt: ctx.Repo().Repository.CreatedUnix.AsTime(),
|
|
URL: subscriptionURL(ctx.Repo().Repository),
|
|
RepositoryURL: ctx.Repo().Repository.APIURL(),
|
|
})
|
|
} else {
|
|
ctx.NotFound()
|
|
}
|
|
}
|
|
|
|
// Watch the repo specified in ctx, as the authenticated user
|
|
func Watch(ctx *context.APIContext) {
|
|
// swagger:operation PUT /repos/{owner}/{repo}/subscription repository userCurrentPutSubscription
|
|
// ---
|
|
// summary: Watch a repo
|
|
// 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
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/WatchInfo"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
err := repo_model.WatchRepoExplicitly(ctx, ctx.Doer().ID, ctx.Repo().Repository.ID, repo_model.WatchAllSelection)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "WatchRepo", err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, api.WatchInfo{
|
|
Subscribed: true,
|
|
Ignored: false,
|
|
Reason: nil,
|
|
CreatedAt: ctx.Repo().Repository.CreatedUnix.AsTime(),
|
|
URL: subscriptionURL(ctx.Repo().Repository),
|
|
RepositoryURL: ctx.Repo().Repository.APIURL(),
|
|
})
|
|
}
|
|
|
|
// Unwatch the repo specified in ctx, as the authenticated user
|
|
func Unwatch(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/subscription repository userCurrentDeleteSubscription
|
|
// ---
|
|
// summary: Unwatch a repo
|
|
// 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
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
err := repo_model.WatchRepoExplicitly(ctx, ctx.Doer().ID, ctx.Repo().Repository.ID, repo_model.WatchNoneSelection)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// subscriptionURL returns the URL of the subscription API endpoint of a repo
|
|
func subscriptionURL(repo *repo_model.Repository) string {
|
|
return repo.APIURL() + "/subscription"
|
|
}
|