summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2015-07-17 14:01:01 -0800
committerCorey Hulen <corey@hulen.com>2015-07-17 14:01:01 -0800
commitb306ec84378b36372b5465c09233094314789bf2 (patch)
tree1995d12ac34a4c6ba459884c84cb28b62d62b296 /model
parent66c22d342fa42068382c9e80427ea953676357db (diff)
parent1dba330146a10718a2fc9eac0ae7d6e1d6bc0d79 (diff)
downloadchat-b306ec84378b36372b5465c09233094314789bf2.tar.gz
chat-b306ec84378b36372b5465c09233094314789bf2.tar.bz2
chat-b306ec84378b36372b5465c09233094314789bf2.zip
Merge pull request #177 from hmhealey/mm825
MM-825 Replace FullName field with separate FirstName and LastName fields and repurpose the existing FullName as Nickname
Diffstat (limited to 'model')
-rw-r--r--model/channel_extra.go5
-rw-r--r--model/user.go43
-rw-r--r--model/user_test.go64
3 files changed, 101 insertions, 11 deletions
diff --git a/model/channel_extra.go b/model/channel_extra.go
index a5c9acf71..3a918b524 100644
--- a/model/channel_extra.go
+++ b/model/channel_extra.go
@@ -10,7 +10,7 @@ import (
type ExtraMember struct {
Id string `json:"id"`
- FullName string `json:"full_name"`
+ Nickname string `json:"nickname"`
Email string `json:"email"`
Roles string `json:"roles"`
Username string `json:"username"`
@@ -20,9 +20,6 @@ func (o *ExtraMember) Sanitize(options map[string]bool) {
if len(options) == 0 || !options["email"] {
o.Email = ""
}
- if len(options) == 0 || !options["fullname"] {
- o.FullName = ""
- }
}
type ChannelExtra struct {
diff --git a/model/user.go b/model/user.go
index c5a4d846d..727165b8c 100644
--- a/model/user.go
+++ b/model/user.go
@@ -37,7 +37,9 @@ type User struct {
AuthData string `json:"auth_data"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
- FullName string `json:"full_name"`
+ Nickname string `json:"nickname"`
+ FirstName string `json:"first_name"`
+ LastName string `json:"last_name"`
Roles string `json:"roles"`
LastActivityAt int64 `json:"last_activity_at"`
LastPingAt int64 `json:"last_ping_at"`
@@ -82,8 +84,16 @@ func (u *User) IsValid() *AppError {
return NewAppError("User.IsValid", "Invalid email", "user_id="+u.Id)
}
- if len(u.FullName) > 64 {
- return NewAppError("User.IsValid", "Invalid full name", "user_id="+u.Id)
+ if len(u.Nickname) > 64 {
+ return NewAppError("User.IsValid", "Invalid nickname", "user_id="+u.Id)
+ }
+
+ if len(u.FirstName) > 64 {
+ return NewAppError("User.IsValid", "Invalid first name", "user_id="+u.Id)
+ }
+
+ if len(u.LastName) > 64 {
+ return NewAppError("User.IsValid", "Invalid last name", "user_id="+u.Id)
}
return nil
@@ -152,7 +162,7 @@ func (u *User) SetDefaultNotifications() {
u.NotifyProps["first_name"] = "false"
u.NotifyProps["all"] = "true"
u.NotifyProps["channel"] = "true"
- splitName := strings.Split(u.FullName, " ")
+ splitName := strings.Split(u.Nickname, " ")
if len(splitName) > 0 && splitName[0] != "" {
u.NotifyProps["first_name"] = "true"
u.NotifyProps["mention_keys"] += "," + splitName[0]
@@ -191,7 +201,8 @@ func (u *User) Sanitize(options map[string]bool) {
u.Email = ""
}
if len(options) != 0 && !options["fullname"] {
- u.FullName = ""
+ u.FirstName = ""
+ u.LastName = ""
}
if len(options) != 0 && !options["skypeid"] {
// TODO - fill in when SkypeId is added to user model
@@ -226,6 +237,28 @@ func (u *User) AddNotifyProp(key string, value string) {
u.NotifyProps[key] = value
}
+func (u *User) GetFullName() string {
+ if u.FirstName != "" && u.LastName != "" {
+ return u.FirstName + " " + u.LastName
+ } else if u.FirstName != "" {
+ return u.FirstName
+ } else if u.LastName != "" {
+ return u.LastName
+ } else {
+ return ""
+ }
+}
+
+func (u *User) GetDisplayName() string {
+ if u.Nickname != "" {
+ return u.Nickname
+ } else if fullName := u.GetFullName(); fullName != "" {
+ return fullName
+ } else {
+ return u.Username
+ }
+}
+
// UserFromJson will decode the input and return a User
func UserFromJson(data io.Reader) *User {
decoder := json.NewDecoder(data)
diff --git a/model/user_test.go b/model/user_test.go
index df9ac19c2..a48c3f2e7 100644
--- a/model/user_test.go
+++ b/model/user_test.go
@@ -80,13 +80,73 @@ func TestUserIsValid(t *testing.T) {
}
user.Email = "test@nowhere.com"
- user.FullName = strings.Repeat("01234567890", 20)
+ user.Nickname = strings.Repeat("01234567890", 20)
if err := user.IsValid(); err == nil {
t.Fatal()
}
- user.FullName = ""
+ user.Nickname = ""
if err := user.IsValid(); err != nil {
t.Fatal(err)
}
+
+ user.FirstName = ""
+ user.LastName = ""
+ if err := user.IsValid(); err != nil {
+ t.Fatal(err)
+ }
+
+ user.FirstName = strings.Repeat("01234567890", 20)
+ if err := user.IsValid(); err == nil {
+ t.Fatal(err)
+ }
+
+ user.FirstName = ""
+ user.LastName = strings.Repeat("01234567890", 20)
+ if err := user.IsValid(); err == nil {
+ t.Fatal(err)
+ }
+}
+
+func TestUserGetFullName(t *testing.T) {
+ user := User{}
+
+ if fullName := user.GetFullName(); fullName != "" {
+ t.Fatal("Full name should be blank")
+ }
+
+ user.FirstName = "first"
+ if fullName := user.GetFullName(); fullName != "first" {
+ t.Fatal("Full name should be first name")
+ }
+
+ user.FirstName = ""
+ user.LastName = "last"
+ if fullName := user.GetFullName(); fullName != "last" {
+ t.Fatal("Full name should be last name")
+ }
+
+ user.FirstName = "first"
+ if fullName := user.GetFullName(); fullName != "first last" {
+ t.Fatal("Full name should be first name and last name")
+ }
+}
+
+func TestUserGetDisplayName(t *testing.T) {
+ user := User{Username: "user"}
+
+ if displayName := user.GetDisplayName(); displayName != "user" {
+ t.Fatal("Display name should be username")
+ }
+
+ user.FirstName = "first"
+ user.LastName = "last"
+ if displayName := user.GetDisplayName(); displayName != "first last" {
+ t.Fatal("Display name should be full name")
+ }
+
+ user.Nickname = "nickname"
+ if displayName := user.GetDisplayName(); displayName != "nickname" {
+ t.Fatal("Display name should be nickname")
+ }
}