Skip to content

Commit

Permalink
implement controller for /users/lists route, fix some outdated API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
krustowski committed Jul 24, 2024
1 parent 4e82c08 commit 8293331
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#

APP_NAME=litter-go
APP_VERSION=0.36.4
APP_VERSION=0.37.0
GOLANG_VERSION=1.22
94 changes: 74 additions & 20 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "MIT",
"url": "https://github.com/krustowski/litter-go/blob/master/LICENSE"
},
"version": "0.36.4"
"version": "0.37.0"
},
"host": "littr.eu",
"basePath": "/api/v1",
Expand Down Expand Up @@ -918,6 +918,7 @@
"users"
],
"summary": "Update the user's details",
"deprecated": true,
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -1026,9 +1027,9 @@
}
}
},
"/users/{nickname}/localtime": {
"/users/{nickname}/lists": {
"patch": {
"description": "toggle the local time mode",
"description": "update user's list",
"consumes": [
"application/json"
],
Expand All @@ -1038,7 +1039,7 @@
"tags": [
"users"
],
"summary": "Toggle the local time mode",
"summary": "Update user's list",
"responses": {
"200": {
"description": "OK",
Expand All @@ -1058,6 +1059,12 @@
"$ref": "#/definitions/common.Response"
}
},
"409": {
"description": "Conflict",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
Expand All @@ -1067,9 +1074,9 @@
}
}
},
"/users/{nickname}/posts": {
"get": {
"description": "get user posts",
"/users/{nickname}/options": {
"patch": {
"description": "update user's option",
"consumes": [
"application/json"
],
Expand All @@ -1079,26 +1086,44 @@
"tags": [
"users"
],
"summary": "Get user posts",
"summary": "Update user's option",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"400": {
"description": "Bad Request",
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"409": {
"description": "Conflict",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.Response"
}
}
}
}
},
"/users/{nickname}/private": {
"/users/{nickname}/passphrase": {
"patch": {
"description": "toggle the private mode",
"description": "update user's passphrase",
"consumes": [
"application/json"
],
Expand All @@ -1108,7 +1133,7 @@
"tags": [
"users"
],
"summary": "Toggle the private mode",
"summary": "Update user's passphrase",
"responses": {
"200": {
"description": "OK",
Expand All @@ -1128,6 +1153,12 @@
"$ref": "#/definitions/common.Response"
}
},
"409": {
"description": "Conflict",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
Expand All @@ -1137,6 +1168,35 @@
}
}
},
"/users/{nickname}/posts": {
"get": {
"description": "get user posts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Get user posts",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.Response"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/common.Response"
}
}
}
}
},
"/users/{nickname}/request": {
"post": {
"description": "add to the request list",
Expand Down Expand Up @@ -1468,7 +1528,6 @@
"models.User": {
"type": "object",
"required": [
"id",
"nickname"
],
"properties": {
Expand All @@ -1482,8 +1541,7 @@
},
"app_bg_mode": {
"description": "AppBgMode string defines the colour mode of the app's background (light vs dark).",
"type": "string",
"default": "dark"
"type": "boolean"
},
"avatar_url": {
"description": "AvatarURL is an URL to the user's custom profile picture.",
Expand Down Expand Up @@ -1517,10 +1575,6 @@
"description": "GDPR consent, set to true because it is noted on the registration page so. No user data should\nbe saved if the boolean is false.",
"type": "boolean"
},
"id": {
"description": "ID is an unique identifier.",
"type": "string"
},
"last_active_time": {
"description": "LastLoginTime is an UNIX timestamp of the last action performed by such user.",
"type": "string"
Expand Down
100 changes: 100 additions & 0 deletions pkg/backend/users/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,100 @@ func updateUserPassphrase(w http.ResponseWriter, r *http.Request) {
resp.Write(w)
return
}

resp.Message = "ok, updating user's passphrase"
resp.Code = http.StatusOK

l.Println(resp.Message, resp.Code)
resp.Write(w)
}

// updateUserList is the users handler that allows one to update various lists associated with such one.
//
// @Summary Update user's list
// @Description update user's list
// @Tags users
// @Accept json
// @Produce json
// @Success 200 {object} common.Response
// @Failure 403 {object} common.Response
// @Failure 404 {object} common.Response
// @Failure 409 {object} common.Response
// @Failure 500 {object} common.Response
// @Router /users/{nickname}/lists [patch]
func updateUserList(w http.ResponseWriter, r *http.Request) {
resp := common.Response{}
l := common.NewLogger(r, "users")

//callerID, _ := r.Context().Value("nickname").(string)
nick := chi.URLParam(r, "nickname")

user, found := db.GetOne(db.UserCache, nick, models.User{})
if !found {
resp.Message = "user nout found: " + nick
resp.Code = http.StatusNotFound

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

lists := struct {
FlowList map[string]bool `json:"flow_list"`
RequestList map[string]bool `json:"request_list"`
ShadeList map[string]bool `json:"shade_list"`
}{}

if err := common.UnmarshalRequestData(r, &lists); err != nil {
resp.Message = "input read error: " + err.Error()
resp.Code = http.StatusBadRequest

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

if lists.FlowList != nil {
for key, value := range lists.FlowList {
if user.FlowList == nil {
user.FlowList = make(map[string]bool)
}
user.FlowList[key] = value
}
}

if lists.RequestList != nil {
for key, value := range lists.RequestList {
if user.RequestList == nil {
user.RequestList = make(map[string]bool)
}
user.RequestList[key] = value
}
}

if lists.ShadeList != nil {
for key, value := range lists.ShadeList {
if user.ShadeList == nil {
user.ShadeList = make(map[string]bool)
}
user.ShadeList[key] = value
}
}

if saved := db.SetOne(db.UserCache, nick, user); !saved {
resp.Message = "backend error: cannot update the user"
resp.Code = http.StatusInternalServerError

l.Println(resp.Message, resp.Code)
resp.Write(w)
return
}

resp.Message = "ok, updating user's lists"
resp.Code = http.StatusOK

l.Println(resp.Message, resp.Code)
resp.Write(w)
}

// updateUserOption is the users handler that allows the user to change some attributes of their models.User instance.
Expand Down Expand Up @@ -468,6 +562,12 @@ func updateUserOption(w http.ResponseWriter, r *http.Request) {
resp.Write(w)
return
}

resp.Message = "ok, updating user's options"
resp.Code = http.StatusOK

l.Println(resp.Message, resp.Code)
resp.Write(w)
}

// deleteUser is the users handler that processes and deletes given user (oneself) form the database.
Expand Down
1 change: 1 addition & 0 deletions pkg/backend/users/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Router() chi.Router {
r.Get("/{nickname}/posts", getUserPosts)
r.Post("/{nickname}/request", addToRequestList)
r.Delete("/{nickname}/request", removeFromRequestList)
r.Patch("/{nickname}/lists", updateUserList)
r.Patch("/{nickname}/options", updateUserOption)
r.Patch("/{nickname}/passphrase", updateUserPassphrase)
//r.Patch("/{nickname}/private", togglePrivateMode)
Expand Down
12 changes: 6 additions & 6 deletions pkg/frontend/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ func (c *flowContent) onClickFollow(ctx app.Context, e app.Event) {

toastText := ""

// do not save new flow user to local var until it is saved on backend
//flowRecords := append(c.flowRecords, flowName)

updatedData := c.user
updatedData.FlowList = flowList
payload := struct {
FlowList map[string]bool `json:"flow_list"`
}{
FlowList: flowList,
}

respRaw, ok := litterAPI("PUT", "/api/v1/users/"+c.user.Nickname, updatedData, c.user.Nickname, 0)
respRaw, ok := litterAPI("PATCH", "/api/v1/users/"+c.user.Nickname+"/lists", payload, c.user.Nickname, 0)
if !ok {
toastText = "generic backend error"
return
Expand Down
4 changes: 4 additions & 0 deletions pkg/frontend/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ func (c *usersContent) onClickUserShade(ctx app.Context, e app.Event) {
return
}

if userShaded.FlowList == nil {
userShaded.FlowList = make(map[string]bool)
}

// disable any following of such user
userShaded.FlowList[c.user.Nickname] = false
c.user.FlowList[key] = false
Expand Down

0 comments on commit 8293331

Please sign in to comment.