diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/client.go | 27 | ||||
-rw-r--r-- | model/command.go | 2 | ||||
-rw-r--r-- | model/gitlab.go | 2 | ||||
-rw-r--r-- | model/google.go | 60 | ||||
-rw-r--r-- | model/post.go | 4 | ||||
-rw-r--r-- | model/team.go | 59 | ||||
-rw-r--r-- | model/team_test.go | 59 | ||||
-rw-r--r-- | model/user.go | 60 | ||||
-rw-r--r-- | model/user_test.go | 42 | ||||
-rw-r--r-- | model/utils.go | 25 | ||||
-rw-r--r-- | model/utils_test.go | 44 |
11 files changed, 237 insertions, 147 deletions
diff --git a/model/client.go b/model/client.go index 17e2466df..c355b90f5 100644 --- a/model/client.go +++ b/model/client.go @@ -522,6 +522,15 @@ func (c *Client) GetPosts(channelId string, offset int, limit int, etag string) } } +func (c *Client) GetPostsSince(channelId string, time int64) (*Result, *AppError) { + if r, err := c.DoGet(fmt.Sprintf("/channels/%v/posts/%v", channelId, time), "", ""); err != nil { + return nil, err + } else { + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), PostListFromJson(r.Body)}, nil + } +} + func (c *Client) GetPost(channelId string, postId string, etag string) (*Result, *AppError) { if r, err := c.DoGet(fmt.Sprintf("/channels/%v/post/%v", channelId, postId), "", etag); err != nil { return nil, err @@ -589,6 +598,24 @@ func (c *Client) GetFile(url string, isFullUrl bool) (*Result, *AppError) { } } +func (c *Client) GetFileInfo(url string) (*Result, *AppError) { + var rq *http.Request + rq, _ = http.NewRequest("GET", c.Url+"/files/get_info"+url, nil) + + if len(c.AuthToken) > 0 { + rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken) + } + + if rp, err := c.HttpClient.Do(rq); err != nil { + return nil, NewAppError(url, "We encountered an error while connecting to the server", err.Error()) + } else if rp.StatusCode >= 300 { + return nil, AppErrorFromJson(rp.Body) + } else { + return &Result{rp.Header.Get(HEADER_REQUEST_ID), + rp.Header.Get(HEADER_ETAG_SERVER), MapFromJson(rp.Body)}, nil + } +} + func (c *Client) GetPublicLink(data map[string]string) (*Result, *AppError) { if r, err := c.DoPost("/files/get_public_link", MapToJson(data)); err != nil { return nil, err diff --git a/model/command.go b/model/command.go index 23573205e..83243cc98 100644 --- a/model/command.go +++ b/model/command.go @@ -14,7 +14,7 @@ const ( type Command struct { Command string `json:"command"` - Response string `json:"reponse"` + Response string `json:"response"` GotoLocation string `json:"goto_location"` ChannelId string `json:"channel_id"` Suggest bool `json:"-"` diff --git a/model/gitlab.go b/model/gitlab.go index 9adcac189..d281b6ea0 100644 --- a/model/gitlab.go +++ b/model/gitlab.go @@ -23,7 +23,7 @@ type GitLabUser struct { func UserFromGitLabUser(glu *GitLabUser) *User { user := &User{} - user.Username = glu.Username + user.Username = CleanUsername(glu.Username) splitName := strings.Split(glu.Name, " ") if len(splitName) == 2 { user.FirstName = splitName[0] diff --git a/model/google.go b/model/google.go deleted file mode 100644 index 2a1eb3caa..000000000 --- a/model/google.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. -// See License.txt for license information. - -package model - -import ( - "encoding/json" - "io" - "strings" -) - -const ( - USER_AUTH_SERVICE_GOOGLE = "google" -) - -type GoogleUser struct { - Id string `json:"id"` - Nickname string `json:"nickname"` - DisplayName string `json:"displayName"` - Emails []map[string]string `json:"emails"` - Names map[string]string `json:"name"` -} - -func UserFromGoogleUser(gu *GoogleUser) *User { - user := &User{} - if len(gu.Nickname) > 0 { - user.Username = gu.Nickname - } else { - user.Username = strings.ToLower(strings.Replace(gu.DisplayName, " ", "", -1)) - } - user.FirstName = gu.Names["givenName"] - user.LastName = gu.Names["familyName"] - user.Nickname = gu.Nickname - - for _, e := range gu.Emails { - if e["type"] == "account" { - user.Email = e["value"] - } - } - - user.AuthData = gu.Id - user.AuthService = USER_AUTH_SERVICE_GOOGLE - - return user -} - -func GoogleUserFromJson(data io.Reader) *GoogleUser { - decoder := json.NewDecoder(data) - var gu GoogleUser - err := decoder.Decode(&gu) - if err == nil { - return &gu - } else { - return nil - } -} - -func (gu *GoogleUser) GetAuthData() string { - return gu.Id -} diff --git a/model/post.go b/model/post.go index f8a3032a3..0c035d4e7 100644 --- a/model/post.go +++ b/model/post.go @@ -120,9 +120,7 @@ func (o *Post) PreSave() { o.OriginalId = "" - if o.CreateAt <= 0 { - o.CreateAt = GetMillis() - } + o.CreateAt = GetMillis() o.UpdateAt = o.CreateAt if o.Props == nil { diff --git a/model/team.go b/model/team.go index e7005625b..95e2757c8 100644 --- a/model/team.go +++ b/model/team.go @@ -5,7 +5,10 @@ package model import ( "encoding/json" + "fmt" "io" + "regexp" + "strings" ) const ( @@ -93,7 +96,7 @@ func (o *Team) IsValid() *AppError { return NewAppError("Team.IsValid", "Invalid email", "id="+o.Id) } - if !IsValidEmail(o.Email) { + if len(o.Email) > 0 && !IsValidEmail(o.Email) { return NewAppError("Team.IsValid", "Invalid email", "id="+o.Id) } @@ -140,3 +143,57 @@ func (o *Team) PreSave() { func (o *Team) PreUpdate() { o.UpdateAt = GetMillis() } + +func IsReservedTeamName(s string) bool { + s = strings.ToLower(s) + + for _, value := range reservedName { + if strings.Index(s, value) == 0 { + return true + } + } + + return false +} + +func IsValidTeamName(s string) bool { + + if !IsValidAlphaNum(s) { + return false + } + + if len(s) <= 3 { + return false + } + + return true +} + +var validTeamNameCharacter = regexp.MustCompile(`^[a-z0-9-]$`) + +func CleanTeamName(s string) string { + s = strings.ToLower(strings.Replace(s, " ", "-", -1)) + + for _, value := range reservedName { + if strings.Index(s, value) == 0 { + s = strings.Replace(s, value, "", -1) + } + } + + s = strings.TrimSpace(s) + + for _, c := range s { + char := fmt.Sprintf("%c", c) + if !validTeamNameCharacter.MatchString(char) { + s = strings.Replace(s, char, "", -1) + } + } + + s = strings.Trim(s, "-") + + if !IsValidTeamName(s) { + s = NewId() + } + + return s +} diff --git a/model/team_test.go b/model/team_test.go index 071b1a2e9..0dec07559 100644 --- a/model/team_test.go +++ b/model/team_test.go @@ -74,3 +74,62 @@ func TestTeamPreUpdate(t *testing.T) { o := Team{DisplayName: "test"} o.PreUpdate() } + +var domains = []struct { + value string + expected bool +}{ + {"spin-punch", true}, + {"-spin-punch", false}, + {"spin-punch-", false}, + {"spin_punch", false}, + {"a", false}, + {"aa", false}, + {"aaa", false}, + {"aaa-999b", true}, + {"b00b", true}, + {"b))b", false}, + {"test", true}, +} + +func TestValidTeamName(t *testing.T) { + for _, v := range domains { + if IsValidTeamName(v.value) != v.expected { + t.Errorf("expect %v as %v", v.value, v.expected) + } + } +} + +var tReservedDomains = []struct { + value string + expected bool +}{ + {"test-hello", true}, + {"test", true}, + {"admin", true}, + {"Admin-punch", true}, + {"spin-punch-admin", false}, +} + +func TestReservedTeamName(t *testing.T) { + for _, v := range tReservedDomains { + if IsReservedTeamName(v.value) != v.expected { + t.Errorf("expect %v as %v", v.value, v.expected) + } + } +} + +func TestCleanTeamName(t *testing.T) { + if CleanTeamName("Jimbo's Team") != "jimbos-team" { + t.Fatal("didn't clean name properly") + } + if len(CleanTeamName("Test")) != 26 { + t.Fatal("didn't clean name properly") + } + if CleanTeamName("Team Really cool") != "really-cool" { + t.Fatal("didn't clean name properly") + } + if CleanTeamName("super-duper-guys") != "super-duper-guys" { + t.Fatal("didn't clean name properly") + } +} diff --git a/model/user.go b/model/user.go index ebefa4762..7c53593d2 100644 --- a/model/user.go +++ b/model/user.go @@ -6,6 +6,7 @@ package model import ( "code.google.com/p/go.crypto/bcrypt" "encoding/json" + "fmt" "io" "regexp" "strings" @@ -72,13 +73,7 @@ func (u *User) IsValid() *AppError { return NewAppError("User.IsValid", "Invalid team id", "") } - if len(u.Username) == 0 || len(u.Username) > 64 { - return NewAppError("User.IsValid", "Invalid username", "user_id="+u.Id) - } - - validChars, _ := regexp.Compile("^[a-z0-9\\.\\-\\_]+$") - - if !validChars.MatchString(u.Username) { + if !IsValidUsername(u.Username) { return NewAppError("User.IsValid", "Invalid username", "user_id="+u.Id) } @@ -332,17 +327,58 @@ func ComparePassword(hash string, password string) bool { func IsUsernameValid(username string) bool { - var restrictedUsernames = []string{ - BOT_USERNAME, - "all", - "channel", + return true +} + +var validUsernameChars = regexp.MustCompile(`^[a-z0-9\.\-_]+$`) + +var restrictedUsernames = []string{ + BOT_USERNAME, + "all", + "channel", +} + +func IsValidUsername(s string) bool { + if len(s) == 0 || len(s) > 64 { + return false + } + + if !validUsernameChars.MatchString(s) { + return false } for _, restrictedUsername := range restrictedUsernames { - if username == restrictedUsername { + if s == restrictedUsername { return false } } return true } + +func CleanUsername(s string) string { + s = strings.ToLower(strings.Replace(s, " ", "-", -1)) + + for _, value := range reservedName { + if s == value { + s = strings.Replace(s, value, "", -1) + } + } + + s = strings.TrimSpace(s) + + for _, c := range s { + char := fmt.Sprintf("%c", c) + if !validUsernameChars.MatchString(char) { + s = strings.Replace(s, char, "-", -1) + } + } + + s = strings.Trim(s, "-") + + if !IsValidUsername(s) { + s = "a" + NewId() + } + + return s +} diff --git a/model/user_test.go b/model/user_test.go index a48c3f2e7..a3b4be091 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -150,3 +150,45 @@ func TestUserGetDisplayName(t *testing.T) { t.Fatal("Display name should be nickname") } } + +var usernames = []struct { + value string + expected bool +}{ + {"spin-punch", true}, + {"Spin-punch", false}, + {"spin punch-", false}, + {"spin_punch", true}, + {"spin", true}, + {"PUNCH", false}, + {"spin.punch", true}, + {"spin'punch", false}, + {"spin*punch", false}, + {"all", false}, +} + +func TestValidUsername(t *testing.T) { + for _, v := range usernames { + if IsValidUsername(v.value) != v.expected { + t.Errorf("expect %v as %v", v.value, v.expected) + } + } +} + +func TestCleanUsername(t *testing.T) { + if CleanUsername("Spin-punch") != "spin-punch" { + t.Fatal("didn't clean name properly") + } + if CleanUsername("PUNCH") != "punch" { + t.Fatal("didn't clean name properly") + } + if CleanUsername("spin'punch") != "spin-punch" { + t.Fatal("didn't clean name properly") + } + if CleanUsername("spin") != "spin" { + t.Fatal("didn't clean name properly") + } + if len(CleanUsername("all")) != 27 { + t.Fatal("didn't clean name properly") + } +} diff --git a/model/utils.go b/model/utils.go index a8257467b..17d1c6317 100644 --- a/model/utils.go +++ b/model/utils.go @@ -168,31 +168,6 @@ var reservedName = []string{ "api", } -func IsReservedTeamName(s string) bool { - s = strings.ToLower(s) - - for _, value := range reservedName { - if strings.Index(s, value) == 0 { - return true - } - } - - return false -} - -func IsValidTeamName(s string) bool { - - if !IsValidAlphaNum(s) { - return false - } - - if len(s) <= 3 { - return false - } - - return true -} - var wwwStart = regexp.MustCompile(`^www`) var betaStart = regexp.MustCompile(`^beta`) var ciStart = regexp.MustCompile(`^ci`) diff --git a/model/utils_test.go b/model/utils_test.go index dbb448882..0f26526b2 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -66,50 +66,6 @@ func TestValidLower(t *testing.T) { } } -var domains = []struct { - value string - expected bool -}{ - {"spin-punch", true}, - {"-spin-punch", false}, - {"spin-punch-", false}, - {"spin_punch", false}, - {"a", false}, - {"aa", false}, - {"aaa", false}, - {"aaa-999b", true}, - {"b00b", true}, - {"b))b", false}, - {"test", true}, -} - -func TestValidTeamName(t *testing.T) { - for _, v := range domains { - if IsValidTeamName(v.value) != v.expected { - t.Errorf("expect %v as %v", v.value, v.expected) - } - } -} - -var tReservedDomains = []struct { - value string - expected bool -}{ - {"test-hello", true}, - {"test", true}, - {"admin", true}, - {"Admin-punch", true}, - {"spin-punch-admin", false}, -} - -func TestReservedTeamName(t *testing.T) { - for _, v := range tReservedDomains { - if IsReservedTeamName(v.value) != v.expected { - t.Errorf("expect %v as %v", v.value, v.expected) - } - } -} - func TestEtag(t *testing.T) { etag := Etag("hello", 24) if len(etag) <= 0 { |