summaryrefslogtreecommitdiffstats
path: root/model/user.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-02-16 09:46:55 -0500
committerHarrison Healey <harrisonmhealey@gmail.com>2017-02-16 09:46:55 -0500
commitf87d42916f1ec4287daad29d6ffd4145dfc4b5cd (patch)
treedd0296f0023efe031dbacbb39b724540bed2d8a5 /model/user.go
parent8f262241595cc9377e4c3adce9c728eaff38c5f3 (diff)
downloadchat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.tar.gz
chat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.tar.bz2
chat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.zip
Implement PUT /users/{user_id}/patch endpoint for APIv4 (#5418)
Diffstat (limited to 'model/user.go')
-rw-r--r--model/user.go70
1 files changed, 70 insertions, 0 deletions
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 {