feat: granular settings for watched repo units (#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: https://codeberg.org/forgejo/forgejo/pulls/10927
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
christopher-besch 2026-07-09 19:51:10 +02:00 committed by Gusted
commit 8770ffc848
48 changed files with 1241 additions and 259 deletions

View file

@ -1,16 +1,58 @@
<form hx-target="this" hx-post="{{$.RepoLink}}/action/{{if $.IsWatchingRepo}}un{{end}}watch" hx-on::after-settle="this.querySelector('button').focus()" method="post" action="{{$.RepoLink}}/action/{{if $.IsWatchingRepo}}un{{end}}watch">
<div class="ui labeled button" {{if not $.IsSigned}}data-tooltip-content="{{ctx.Locale.Tr "repo.watch_guest_user"}}"{{end}}>
<button type="submit" class="ui compact small basic button"{{if not $.IsSigned}} disabled{{end}} aria-label="{{if $.IsWatchingRepo}}{{ctx.Locale.Tr "repo.unwatch"}}{{else}}{{ctx.Locale.Tr "repo.watch"}}{{end}}">
{{if $.IsWatchingRepo}}
{{svg "octicon-eye-closed" 16}}<span class="text not-mobile">{{ctx.Locale.Tr "repo.unwatch"}}</span>
{{else}}
{{svg "octicon-eye"}}<span class="text not-mobile">{{ctx.Locale.Tr "repo.watch"}}</span>
{{end}}
</button>
<a hx-boost="false" class="ui basic label" href="{{.RepoLink}}/watchers"
{{if $.IsSigned}}
<details class="dropdown" id="watch-button">
<summary class="ui labeled small button">
<div class="ui compact small button">
{{svg "octicon-eye" 16}}
<span class="text not-mobile">{{if $.RepoWatchSelection.IsWatching}}{{ctx.Locale.Tr "repo.watching"}}{{else}}{{ctx.Locale.Tr "repo.watch"}}{{end}}</span>
{{svg "octicon-triangle-down" 14}}
</div>
<a class="ui basic label" href="{{.RepoLink}}/watchers"
aria-label="{{ctx.Locale.TrPluralString .Repository.NumWatches "watch.n_watchers" (printf "%d" .Repository.NumWatches)}}"
>
{{CountFmt .Repository.NumWatches}}
</a>
</summary>
<div class="content">
<form class="ui form tw-p-3" hx-target="#watch-button" method="post" hx-post="{{$.RepoLink}}/action/watch/select" action="{{$.RepoLink}}/action/watch/select" hx-swap="outerHTML">
<fieldset>
<label>
<input type="checkbox" name="watch_issues" value="true" {{if $.RepoWatchSelection.Issues}}checked{{end}}>
{{svg "octicon-issue-opened"}}
{{ctx.Locale.Tr "repo.watch.issues"}}
</label>
<label>
<input type="checkbox" name="watch_pull_requests" value="true" {{if $.RepoWatchSelection.PullRequests}}checked{{end}}>
{{svg "octicon-git-pull-request"}}
{{ctx.Locale.Tr "repo.watch.pull_requests"}}
</label>
<label>
<input type="checkbox" name="watch_releases" value="true" {{if $.RepoWatchSelection.Releases}}checked{{end}}>
{{svg "octicon-tag"}}
{{ctx.Locale.Tr "repo.watch.releases"}}
<span class="help">{{ctx.Locale.Tr "repo.watch.releases.hint"}}</span>
</label>
<div class="small button-sequence tw-mt-3">
<button type="submit" class="primary button">{{ctx.Locale.Tr "save"}}</button>
{{if $.RepoWatchSelection.IsWatching}}
<button type="button" class="secondary button" hx-post="{{$.RepoLink}}/action/unwatch" hx-target="#watch-button" hx-swap="outerHTML">
{{ctx.Locale.Tr "repo.unwatch"}}
</button>
{{end}}
</div>
</fieldset>
</form>
</div>
</details>
{{else}}
{{/* not logged-in user */}}
<div class="ui disabled labeled button" data-tooltip-content="{{ctx.Locale.Tr "repo.watch_guest_user"}}">
<div class="ui compact small basic button">
{{svg "octicon-eye"}}<span class="text not-mobile">{{ctx.Locale.Tr "repo.watch"}}</span>
</div>
<a class="ui basic label" href="{{.RepoLink}}/watchers"
aria-label="{{ctx.Locale.TrPluralString .Repository.NumWatches "watch.n_watchers" (printf "%d" .Repository.NumWatches)}}"
>
{{CountFmt .Repository.NumWatches}}
</a>
</div>
</form>
{{end}}