Skip to content

Commit 9c9f0ee

Browse files
committed
API: GET/SET User Settings
1 parent b8e4ce7 commit 9c9f0ee

File tree

6 files changed

+273
-1
lines changed

6 files changed

+273
-1
lines changed

modules/structs/user.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,29 @@ func (u User) MarshalJSON() ([]byte, error) {
5555
CompatUserName string `json:"username"`
5656
}{shadow(u), u.UserName})
5757
}
58+
59+
type UserSettings struct {
60+
FullName string `json:"full_name"`
61+
Website string `json:"website"`
62+
Description string `json:"description"`
63+
Location string `json:"location"`
64+
Language string `json:"language"`
65+
Theme string `json:"theme"`
66+
DiffViewStyle string `json:"diff_view_style"`
67+
// Piracy
68+
HideEmail bool `json:"hide_email"`
69+
HideActivity bool `json:"hide_activity"`
70+
}
71+
72+
type UserSettingsOptions struct {
73+
FullName *string `json:"full_name" binding:"MaxSize(100)"`
74+
Website *string `json:"website" binding:"OmitEmpty;ValidUrl;MaxSize(255)"`
75+
Description *string `json:"description" binding:"MaxSize(255)"`
76+
Location *string `json:"location" binding:"MaxSize(50)"`
77+
Language *string `json:"language"`
78+
Theme *string `json:"theme"`
79+
DiffViewStyle *string `json:"diff_view_style"`
80+
// Piracy
81+
HideEmail *bool `json:"hide_email"`
82+
HideActivity *bool `json:"hide_activity"`
83+
}

routers/api/v1/api.go

Lines changed: 4 additions & 0 deletions
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("/preferences", 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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,7 @@ type swaggerParameterBodies struct {
158158

159159
// in:body
160160
PullReviewRequestOptions api.PullReviewRequestOptions
161+
162+
// in:body
163+
UserSettingsOptions api.UserSettingsOptions
161164
}

routers/api/v1/swagger/user.go

Lines changed: 7 additions & 0 deletions
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/preferences.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
api "code.gitea.io/gitea/modules/structs"
13+
"code.gitea.io/gitea/modules/web"
14+
)
15+
16+
func user2UserSettings(user *models.User) api.UserSettings {
17+
return api.UserSettings{
18+
FullName: user.FullName,
19+
Website: user.Website,
20+
Location: user.Location,
21+
Language: user.Language,
22+
Description: user.Description,
23+
Theme: user.Theme,
24+
HideEmail: user.KeepEmailPrivate,
25+
HideActivity: user.KeepActivityPrivate,
26+
DiffViewStyle: user.DiffViewStyle,
27+
}
28+
}
29+
30+
// GetUserSettings returns user settings
31+
func GetUserSettings(ctx *context.APIContext) {
32+
// swagger:operation GET /user/preferences user getUserSettings
33+
// ---
34+
// summary: Get user settings
35+
// produces:
36+
// - application/json
37+
// responses:
38+
// "200":
39+
// "$ref": "#/responses/UserSettings"
40+
ctx.JSON(http.StatusOK, user2UserSettings(ctx.User))
41+
}
42+
43+
// UpdateUserSettings returns user settings
44+
func UpdateUserSettings(ctx *context.APIContext) {
45+
// swagger:operation PATCH /user/preferences user getUserSettings
46+
// ---
47+
// summary: Update user settings
48+
// parameters:
49+
// - name: body
50+
// in: body
51+
// schema:
52+
// "$ref": "#/definitions/UserSettingsOptions"
53+
// produces:
54+
// - application/json
55+
// responses:
56+
// "200":
57+
// "$ref": "#/responses/UserSettings"
58+
59+
form := web.GetForm(ctx).(*api.UserSettingsOptions)
60+
61+
if form.FullName != nil {
62+
ctx.User.FullName = *form.FullName
63+
}
64+
if form.Description != nil {
65+
ctx.User.Description = *form.Description
66+
}
67+
if form.Website != nil {
68+
ctx.User.Website = *form.Website
69+
}
70+
if form.Location != nil {
71+
ctx.User.Location = *form.Location
72+
}
73+
if form.Language != nil {
74+
ctx.User.Language = *form.Language
75+
}
76+
if form.Theme != nil {
77+
ctx.User.Theme = *form.Theme
78+
}
79+
if form.DiffViewStyle != nil {
80+
ctx.User.DiffViewStyle = *form.DiffViewStyle
81+
}
82+
83+
if form.HideEmail != nil {
84+
ctx.User.KeepEmailPrivate = *form.HideEmail
85+
}
86+
if form.HideActivity != nil {
87+
ctx.User.KeepActivityPrivate = *form.HideActivity
88+
}
89+
90+
if err := models.UpdateUser(ctx.User); err != nil {
91+
ctx.InternalServerError(err)
92+
return
93+
}
94+
95+
ctx.JSON(http.StatusOK, user2UserSettings(ctx.User))
96+
}

templates/swagger/v1_json.tmpl

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10654,6 +10654,47 @@
1065410654
}
1065510655
}
1065610656
},
10657+
"/user/preferences": {
10658+
"get": {
10659+
"produces": [
10660+
"application/json"
10661+
],
10662+
"tags": [
10663+
"user"
10664+
],
10665+
"summary": "Get user settings",
10666+
"operationId": "getUserSettings",
10667+
"responses": {
10668+
"200": {
10669+
"$ref": "#/responses/UserSettings"
10670+
}
10671+
}
10672+
},
10673+
"patch": {
10674+
"produces": [
10675+
"application/json"
10676+
],
10677+
"tags": [
10678+
"user"
10679+
],
10680+
"summary": "Update user settings",
10681+
"operationId": "getUserSettings",
10682+
"parameters": [
10683+
{
10684+
"name": "body",
10685+
"in": "body",
10686+
"schema": {
10687+
"$ref": "#/definitions/UserSettingsOptions"
10688+
}
10689+
}
10690+
],
10691+
"responses": {
10692+
"200": {
10693+
"$ref": "#/responses/UserSettings"
10694+
}
10695+
}
10696+
}
10697+
},
1065710698
"/user/repos": {
1065810699
"get": {
1065910700
"produces": [
@@ -16344,6 +16385,92 @@
1634416385
},
1634516386
"x-go-package": "code.gitea.io/gitea/models"
1634616387
},
16388+
"UserSettings": {
16389+
"type": "object",
16390+
"properties": {
16391+
"description": {
16392+
"type": "string",
16393+
"x-go-name": "Description"
16394+
},
16395+
"diff_view_style": {
16396+
"type": "string",
16397+
"x-go-name": "DiffViewStyle"
16398+
},
16399+
"full_name": {
16400+
"type": "string",
16401+
"x-go-name": "FullName"
16402+
},
16403+
"hide_activity": {
16404+
"type": "boolean",
16405+
"x-go-name": "HideActivity"
16406+
},
16407+
"hide_email": {
16408+
"description": "Piracy",
16409+
"type": "boolean",
16410+
"x-go-name": "HideEmail"
16411+
},
16412+
"language": {
16413+
"type": "string",
16414+
"x-go-name": "Language"
16415+
},
16416+
"location": {
16417+
"type": "string",
16418+
"x-go-name": "Location"
16419+
},
16420+
"theme": {
16421+
"type": "string",
16422+
"x-go-name": "Theme"
16423+
},
16424+
"website": {
16425+
"type": "string",
16426+
"x-go-name": "Website"
16427+
}
16428+
},
16429+
"x-go-package": "code.gitea.io/gitea/modules/structs"
16430+
},
16431+
"UserSettingsOptions": {
16432+
"type": "object",
16433+
"properties": {
16434+
"description": {
16435+
"type": "string",
16436+
"x-go-name": "Description"
16437+
},
16438+
"diff_view_style": {
16439+
"type": "string",
16440+
"x-go-name": "DiffViewStyle"
16441+
},
16442+
"full_name": {
16443+
"type": "string",
16444+
"x-go-name": "FullName"
16445+
},
16446+
"hide_activity": {
16447+
"type": "boolean",
16448+
"x-go-name": "HideActivity"
16449+
},
16450+
"hide_email": {
16451+
"description": "Piracy",
16452+
"type": "boolean",
16453+
"x-go-name": "HideEmail"
16454+
},
16455+
"language": {
16456+
"type": "string",
16457+
"x-go-name": "Language"
16458+
},
16459+
"location": {
16460+
"type": "string",
16461+
"x-go-name": "Location"
16462+
},
16463+
"theme": {
16464+
"type": "string",
16465+
"x-go-name": "Theme"
16466+
},
16467+
"website": {
16468+
"type": "string",
16469+
"x-go-name": "Website"
16470+
}
16471+
},
16472+
"x-go-package": "code.gitea.io/gitea/modules/structs"
16473+
},
1634716474
"WatchInfo": {
1634816475
"description": "WatchInfo represents an API watch status of one repository",
1634916476
"type": "object",
@@ -17047,6 +17174,15 @@
1704717174
}
1704817175
}
1704917176
},
17177+
"UserSettings": {
17178+
"description": "UserSettings",
17179+
"schema": {
17180+
"type": "array",
17181+
"items": {
17182+
"$ref": "#/definitions/UserSettings"
17183+
}
17184+
}
17185+
},
1705017186
"WatchInfo": {
1705117187
"description": "WatchInfo",
1705217188
"schema": {
@@ -17101,7 +17237,7 @@
1710117237
"parameterBodies": {
1710217238
"description": "parameterBodies",
1710317239
"schema": {
17104-
"$ref": "#/definitions/PullReviewRequestOptions"
17240+
"$ref": "#/definitions/UserSettingsOptions"
1710517241
}
1710617242
},
1710717243
"redirect": {

0 commit comments

Comments
 (0)