From f87d42916f1ec4287daad29d6ffd4145dfc4b5cd Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 16 Feb 2017 09:46:55 -0500 Subject: Implement PUT /users/{user_id}/patch endpoint for APIv4 (#5418) --- model/client4.go | 10 ++++++++ model/user.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) (limited to 'model') diff --git a/model/client4.go b/model/client4.go index 5c51026a9..48a88e968 100644 --- a/model/client4.go +++ b/model/client4.go @@ -334,6 +334,16 @@ func (c *Client4) UpdateUser(user *User) (*User, *Response) { } } +// PatchUser partially updates a user in the system. Any missing fields are not updated. +func (c *Client4) PatchUser(userId string, patch *UserPatch) (*User, *Response) { + if r, err := c.DoApiPut(c.GetUserRoute(userId)+"/patch", patch.ToJson()); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return UserFromJson(r.Body), BuildResponse(r) + } +} + // UpdateUserPassword updates a user's password. Must be logged in as the user or be a system administrator. func (c *Client4) UpdateUserPassword(userId, currentPassword, newPassword string) (bool, *Response) { requestBody := map[string]string{"current_password": currentPassword, "new_password": newPassword} diff --git a/model/user.go b/model/user.go index 8e02fe1ab..7057ab101 100644 --- a/model/user.go +++ b/model/user.go @@ -61,6 +61,18 @@ type User struct { LastActivityAt int64 `db:"-" json:"last_activity_at,omitempty"` } +type UserPatch struct { + Username *string `json:"username"` + Nickname *string `json:"nickname"` + FirstName *string `json:"first_name"` + LastName *string `json:"last_name"` + Position *string `json:"position"` + Email *string `json:"email"` + Props *StringMap `json:"props,omitempty"` + NotifyProps *StringMap `json:"notify_props,omitempty"` + Locale *string `json:"locale"` +} + // IsValid validates the user and returns an error if it isn't configured // correctly. func (u *User) IsValid() *AppError { @@ -215,6 +227,44 @@ func (user *User) UpdateMentionKeysFromUsername(oldUsername string) { } } +func (u *User) Patch(patch *UserPatch) { + if patch.Username != nil { + u.Username = *patch.Username + } + + if patch.Nickname != nil { + u.Nickname = *patch.Nickname + } + + if patch.FirstName != nil { + u.FirstName = *patch.FirstName + } + + if patch.LastName != nil { + u.LastName = *patch.LastName + } + + if patch.Position != nil { + u.Position = *patch.Position + } + + if patch.Email != nil { + u.Email = *patch.Email + } + + if patch.Props != nil { + u.Props = *patch.Props + } + + if patch.NotifyProps != nil { + u.NotifyProps = *patch.NotifyProps + } + + if patch.Locale != nil { + u.Locale = *patch.Locale + } +} + // ToJson convert a User to a json string func (u *User) ToJson() string { b, err := json.Marshal(u) @@ -225,6 +275,15 @@ func (u *User) ToJson() string { } } +func (u *UserPatch) ToJson() string { + b, err := json.Marshal(u) + if err != nil { + return "" + } else { + return string(b) + } +} + // Generate a valid strong etag so the browser can cache the results func (u *User) Etag(showFullName, showEmail bool) string { return Etag(u.Id, u.UpdateAt, showFullName, showEmail) @@ -419,6 +478,17 @@ func UserFromJson(data io.Reader) *User { } } +func UserPatchFromJson(data io.Reader) *UserPatch { + decoder := json.NewDecoder(data) + var user UserPatch + err := decoder.Decode(&user) + if err == nil { + return &user + } else { + return nil + } +} + func UserMapToJson(u map[string]*User) string { b, err := json.Marshal(u) if err != nil { -- cgit v1.2.3-1-g7c22