mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Implement PUT /users/{user_id}/patch endpoint for APIv4 (#5418)
This commit is contained in:
committed by
Harrison Healey
parent
8f26224159
commit
f87d42916f
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user