|
| 1 | +// Copyright 2021 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 user |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models" |
| 11 | + "code.gitea.io/gitea/modules/context" |
| 12 | + "code.gitea.io/gitea/modules/convert" |
| 13 | + api "code.gitea.io/gitea/modules/structs" |
| 14 | + "code.gitea.io/gitea/modules/web" |
| 15 | +) |
| 16 | + |
| 17 | +// GetUserSettings returns user settings |
| 18 | +func GetUserSettings(ctx *context.APIContext) { |
| 19 | + // swagger:operation GET /user/settings user getUserSettings |
| 20 | + // --- |
| 21 | + // summary: Get user settings |
| 22 | + // produces: |
| 23 | + // - application/json |
| 24 | + // responses: |
| 25 | + // "200": |
| 26 | + // "$ref": "#/responses/UserSettings" |
| 27 | + ctx.JSON(http.StatusOK, convert.User2UserSettings(ctx.User)) |
| 28 | +} |
| 29 | + |
| 30 | +// UpdateUserSettings returns user settings |
| 31 | +func UpdateUserSettings(ctx *context.APIContext) { |
| 32 | + // swagger:operation PATCH /user/settings user updateUserSettings |
| 33 | + // --- |
| 34 | + // summary: Update user settings |
| 35 | + // parameters: |
| 36 | + // - name: body |
| 37 | + // in: body |
| 38 | + // schema: |
| 39 | + // "$ref": "#/definitions/UserSettingsOptions" |
| 40 | + // produces: |
| 41 | + // - application/json |
| 42 | + // responses: |
| 43 | + // "200": |
| 44 | + // "$ref": "#/responses/UserSettings" |
| 45 | + |
| 46 | + form := web.GetForm(ctx).(*api.UserSettingsOptions) |
| 47 | + |
| 48 | + if form.FullName != nil { |
| 49 | + ctx.User.FullName = *form.FullName |
| 50 | + } |
| 51 | + if form.Description != nil { |
| 52 | + ctx.User.Description = *form.Description |
| 53 | + } |
| 54 | + if form.Website != nil { |
| 55 | + ctx.User.Website = *form.Website |
| 56 | + } |
| 57 | + if form.Location != nil { |
| 58 | + ctx.User.Location = *form.Location |
| 59 | + } |
| 60 | + if form.Language != nil { |
| 61 | + ctx.User.Language = *form.Language |
| 62 | + } |
| 63 | + if form.Theme != nil { |
| 64 | + ctx.User.Theme = *form.Theme |
| 65 | + } |
| 66 | + if form.DiffViewStyle != nil { |
| 67 | + ctx.User.DiffViewStyle = *form.DiffViewStyle |
| 68 | + } |
| 69 | + |
| 70 | + if form.HideEmail != nil { |
| 71 | + ctx.User.KeepEmailPrivate = *form.HideEmail |
| 72 | + } |
| 73 | + if form.HideActivity != nil { |
| 74 | + ctx.User.KeepActivityPrivate = *form.HideActivity |
| 75 | + } |
| 76 | + |
| 77 | + if err := models.UpdateUser(ctx.User); err != nil { |
| 78 | + ctx.InternalServerError(err) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + ctx.JSON(http.StatusOK, convert.User2UserSettings(ctx.User)) |
| 83 | +} |
0 commit comments