gitforge/routers/api/v1/user/settings.go
forgejo-backport-action a5a2cae7ee [v16.0/forgejo] chore(refactor): add getter/setter to APIContext data members (#13213)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/13143

The details of the refactor are in [a separate branch](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) which details how it was done. This pull request contains a squash of those commits rebased on top of the current Forgejo branch, which involved conflict resolution.

Given the magnitude of the change, there is a high chance that a large number of future pull requests merged in Forgejo will create conflicts. They will be dealt with in the same way  as chore: refactor orgAssignment to two separate middlewares (#13155).

## Review

The only risk I can think of is a getter/setter implementation being incorrectly implemented. This is probably where a detailed review is most needed, in the `services/context/api.go` file.

To not increase the size of the pull request, the `GetX()` accessors were preserved although they are redundant with `X()`. They will be removed in a followup pull request.

## Backporting strategy.

Cherry-picking the commit to v15 has a significant amount of conflicts in 8 files out of 111.

```
unmerged   routers/api/v1/activitypub/repository.go
unmerged   routers/api/v1/admin/user.go
unmerged   routers/api/v1/api.go
unmerged   routers/api/v1/org/team.go
unmerged   routers/api/v1/repo/action.go
unmerged   routers/api/v1/repo/pull.go
unmerged   routers/api/v1/user/follower.go
unmerged   services/context/api.go
```

Instead of resolving them, I think it is easier and safer to repeat the [refactor steps](https://codeberg.org/limiting-factor/forgejo/commits/branch/wip-api-context-accessors-save) in v15.

I also think it is worth the effort since it will avoid trivial backport conflicts on over 100 files.

## Context

This is a followup pull request discussed originally at  https://codeberg.org/forgejo/forgejo/pulls/12512

Co-authored-by: limiting-factor <limiting-factor@posteo.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13213
Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
2026-06-27 18:27:57 +02:00

76 lines
2.3 KiB
Go

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package user
import (
"net/http"
"forgejo.org/modules/optional"
api "forgejo.org/modules/structs"
"forgejo.org/modules/web"
"forgejo.org/services/context"
"forgejo.org/services/convert"
user_service "forgejo.org/services/user"
)
// GetUserSettings returns doer's account settings
func GetUserSettings(ctx *context.APIContext) {
// swagger:operation GET /user/settings user getUserSettings
// ---
// summary: Get current user's account settings
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/UserSettings"
// "401":
// "$ref": "#/responses/unauthorized"
// "403":
// "$ref": "#/responses/forbidden"
ctx.JSON(http.StatusOK, convert.User2UserSettings(ctx.Doer()))
}
// UpdateUserSettings updates settings in doer's account
func UpdateUserSettings(ctx *context.APIContext) {
// swagger:operation PATCH /user/settings user updateUserSettings
// ---
// summary: Update settings in current user's account
// parameters:
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/UserSettingsOptions"
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/UserSettings"
// "401":
// "$ref": "#/responses/unauthorized"
// "403":
// "$ref": "#/responses/forbidden"
form := web.GetForm(ctx).(*api.UserSettingsOptions)
opts := &user_service.UpdateOptions{
FullName: optional.FromPtr(form.FullName),
Description: optional.FromPtr(form.Description),
Pronouns: optional.FromPtr(form.Pronouns),
Website: optional.FromPtr(form.Website),
Location: optional.FromPtr(form.Location),
Language: optional.FromPtr(form.Language),
Theme: optional.FromPtr(form.Theme),
DiffViewStyle: optional.FromPtr(form.DiffViewStyle),
KeepEmailPrivate: optional.FromPtr(form.HideEmail),
KeepPronounsPrivate: optional.FromPtr(form.HidePronouns),
KeepActivityPrivate: optional.FromPtr(form.HideActivity),
EnableRepoUnitHints: optional.FromPtr(form.EnableRepoUnitHints),
}
if err := user_service.UpdateUser(ctx, ctx.Doer(), opts); err != nil {
ctx.InternalServerError(err)
return
}
ctx.JSON(http.StatusOK, convert.User2UserSettings(ctx.Doer()))
}