Skip to content

Commit 58501a2

Browse files
6543techknowlogick
andauthored
[API] GET / SET User Settings (#16169)
* API: GET/SET User Settings * linter * Apply suggestions from code review * Update modules/structs/user.go * lint * fix swagger * move User2UserSettings to convert * as per @zeripath "preferences" -> "settings" Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 8640717 commit 58501a2

File tree

7 files changed

+281
-1
lines changed

7 files changed

+281
-1
lines changed

modules/convert/user.go

+15
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ func toUser(user *models.User, signed, authed bool) *api.User {
7676
}
7777
return result
7878
}
79+
80+
// User2UserSettings return UserSettings based on a user
81+
func User2UserSettings(user *models.User) api.UserSettings {
82+
return api.UserSettings{
83+
FullName: user.FullName,
84+
Website: user.Website,
85+
Location: user.Location,
86+
Language: user.Language,
87+
Description: user.Description,
88+
Theme: user.Theme,
89+
HideEmail: user.KeepEmailPrivate,
90+
HideActivity: user.KeepActivityPrivate,
91+
DiffViewStyle: user.DiffViewStyle,
92+
}
93+
}

modules/structs/user.go

+30
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,33 @@ func (u User) MarshalJSON() ([]byte, error) {
6060
CompatUserName string `json:"username"`
6161
}{shadow(u), u.UserName})
6262
}
63+
64+
// UserSettings represents user settings
65+
// swagger:model
66+
type UserSettings struct {
67+
FullName string `json:"full_name"`
68+
Website string `json:"website"`
69+
Description string `json:"description"`
70+
Location string `json:"location"`
71+
Language string `json:"language"`
72+
Theme string `json:"theme"`
73+
DiffViewStyle string `json:"diff_view_style"`
74+
// Privacy
75+
HideEmail bool `json:"hide_email"`
76+
HideActivity bool `json:"hide_activity"`
77+
}
78+
79+
// UserSettingsOptions represents options to change user settings
80+
// swagger:model
81+
type UserSettingsOptions struct {
82+
FullName *string `json:"full_name" binding:"MaxSize(100)"`
83+
Website *string `json:"website" binding:"OmitEmpty;ValidUrl;MaxSize(255)"`
84+
Description *string `json:"description" binding:"MaxSize(255)"`
85+
Location *string `json:"location" binding:"MaxSize(50)"`
86+
Language *string `json:"language"`
87+
Theme *string `json:"theme"`
88+
DiffViewStyle *string `json:"diff_view_style"`
89+
// Privacy
90+
HideEmail *bool `json:"hide_email"`
91+
HideActivity *bool `json:"hide_activity"`
92+
}

routers/api/v1/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ func Routes() *web.Route {
649649

650650
m.Group("/user", func() {
651651
m.Get("", user.GetAuthenticatedUser)
652+
m.Group("/settings", func() {
653+
m.Get("", user.GetUserSettings)
654+
m.Patch("", bind(api.UserSettingsOptions{}), user.UpdateUserSettings)
655+
}, reqToken())
652656
m.Combo("/emails").Get(user.ListEmails).
653657
Post(bind(api.CreateEmailOption{}), user.AddEmail).
654658
Delete(bind(api.DeleteEmailOption{}), user.DeleteEmail)

routers/api/v1/swagger/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,7 @@ type swaggerParameterBodies struct {
161161

162162
// in:body
163163
CreateTagOption api.CreateTagOption
164+
165+
// in:body
166+
UserSettingsOptions api.UserSettingsOptions
164167
}

routers/api/v1/swagger/user.go

+7
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ type swaggerResponseUserHeatmapData struct {
4242
// in:body
4343
Body []models.UserHeatmapData `json:"body"`
4444
}
45+
46+
// UserSettings
47+
// swagger:response UserSettings
48+
type swaggerResponseUserSettings struct {
49+
// in:body
50+
Body []api.UserSettings `json:"body"`
51+
}

routers/api/v1/user/settings.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

templates/swagger/v1_json.tmpl

+139-1
Original file line numberDiff line numberDiff line change
@@ -10911,6 +10911,47 @@
1091110911
}
1091210912
}
1091310913
},
10914+
"/user/settings": {
10915+
"get": {
10916+
"produces": [
10917+
"application/json"
10918+
],
10919+
"tags": [
10920+
"user"
10921+
],
10922+
"summary": "Get user settings",
10923+
"operationId": "getUserSettings",
10924+
"responses": {
10925+
"200": {
10926+
"$ref": "#/responses/UserSettings"
10927+
}
10928+
}
10929+
},
10930+
"patch": {
10931+
"produces": [
10932+
"application/json"
10933+
],
10934+
"tags": [
10935+
"user"
10936+
],
10937+
"summary": "Update user settings",
10938+
"operationId": "updateUserSettings",
10939+
"parameters": [
10940+
{
10941+
"name": "body",
10942+
"in": "body",
10943+
"schema": {
10944+
"$ref": "#/definitions/UserSettingsOptions"
10945+
}
10946+
}
10947+
],
10948+
"responses": {
10949+
"200": {
10950+
"$ref": "#/responses/UserSettings"
10951+
}
10952+
}
10953+
}
10954+
},
1091410955
"/user/starred": {
1091510956
"get": {
1091610957
"produces": [
@@ -16578,6 +16619,94 @@
1657816619
},
1657916620
"x-go-package": "code.gitea.io/gitea/models"
1658016621
},
16622+
"UserSettings": {
16623+
"description": "UserSettings represents user settings",
16624+
"type": "object",
16625+
"properties": {
16626+
"description": {
16627+
"type": "string",
16628+
"x-go-name": "Description"
16629+
},
16630+
"diff_view_style": {
16631+
"type": "string",
16632+
"x-go-name": "DiffViewStyle"
16633+
},
16634+
"full_name": {
16635+
"type": "string",
16636+
"x-go-name": "FullName"
16637+
},
16638+
"hide_activity": {
16639+
"type": "boolean",
16640+
"x-go-name": "HideActivity"
16641+
},
16642+
"hide_email": {
16643+
"description": "Privacy",
16644+
"type": "boolean",
16645+
"x-go-name": "HideEmail"
16646+
},
16647+
"language": {
16648+
"type": "string",
16649+
"x-go-name": "Language"
16650+
},
16651+
"location": {
16652+
"type": "string",
16653+
"x-go-name": "Location"
16654+
},
16655+
"theme": {
16656+
"type": "string",
16657+
"x-go-name": "Theme"
16658+
},
16659+
"website": {
16660+
"type": "string",
16661+
"x-go-name": "Website"
16662+
}
16663+
},
16664+
"x-go-package": "code.gitea.io/gitea/modules/structs"
16665+
},
16666+
"UserSettingsOptions": {
16667+
"description": "UserSettingsOptions represents options to change user settings",
16668+
"type": "object",
16669+
"properties": {
16670+
"description": {
16671+
"type": "string",
16672+
"x-go-name": "Description"
16673+
},
16674+
"diff_view_style": {
16675+
"type": "string",
16676+
"x-go-name": "DiffViewStyle"
16677+
},
16678+
"full_name": {
16679+
"type": "string",
16680+
"x-go-name": "FullName"
16681+
},
16682+
"hide_activity": {
16683+
"type": "boolean",
16684+
"x-go-name": "HideActivity"
16685+
},
16686+
"hide_email": {
16687+
"description": "Privacy",
16688+
"type": "boolean",
16689+
"x-go-name": "HideEmail"
16690+
},
16691+
"language": {
16692+
"type": "string",
16693+
"x-go-name": "Language"
16694+
},
16695+
"location": {
16696+
"type": "string",
16697+
"x-go-name": "Location"
16698+
},
16699+
"theme": {
16700+
"type": "string",
16701+
"x-go-name": "Theme"
16702+
},
16703+
"website": {
16704+
"type": "string",
16705+
"x-go-name": "Website"
16706+
}
16707+
},
16708+
"x-go-package": "code.gitea.io/gitea/modules/structs"
16709+
},
1658116710
"WatchInfo": {
1658216711
"description": "WatchInfo represents an API watch status of one repository",
1658316712
"type": "object",
@@ -17281,6 +17410,15 @@
1728117410
}
1728217411
}
1728317412
},
17413+
"UserSettings": {
17414+
"description": "UserSettings",
17415+
"schema": {
17416+
"type": "array",
17417+
"items": {
17418+
"$ref": "#/definitions/UserSettings"
17419+
}
17420+
}
17421+
},
1728417422
"WatchInfo": {
1728517423
"description": "WatchInfo",
1728617424
"schema": {
@@ -17335,7 +17473,7 @@
1733517473
"parameterBodies": {
1733617474
"description": "parameterBodies",
1733717475
"schema": {
17338-
"$ref": "#/definitions/CreateTagOption"
17476+
"$ref": "#/definitions/UserSettingsOptions"
1733917477
}
1734017478
},
1734117479
"redirect": {

0 commit comments

Comments
 (0)