diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/incoming_webhook.go | 11 | ||||
-rw-r--r-- | model/post.go | 46 | ||||
-rw-r--r-- | model/search_params.go | 30 | ||||
-rw-r--r-- | model/search_params_test.go | 142 | ||||
-rw-r--r-- | model/user.go | 7 | ||||
-rw-r--r-- | model/utils.go | 24 |
6 files changed, 228 insertions, 32 deletions
diff --git a/model/incoming_webhook.go b/model/incoming_webhook.go index be1984244..8ead0da9f 100644 --- a/model/incoming_webhook.go +++ b/model/incoming_webhook.go @@ -24,10 +24,13 @@ type IncomingWebhook struct { } type IncomingWebhookRequest struct { - Text string `json:"text"` - Username string `json:"username"` - IconURL string `json:"icon_url"` - ChannelName string `json:"channel"` + Text string `json:"text"` + Username string `json:"username"` + IconURL string `json:"icon_url"` + ChannelName string `json:"channel"` + Props StringInterface `json:"props"` + Attachments interface{} `json:"attachments"` + Type string `json:"type"` } func (o *IncomingWebhook) ToJson() string { diff --git a/model/post.go b/model/post.go index e0074b348..248d40321 100644 --- a/model/post.go +++ b/model/post.go @@ -10,27 +10,28 @@ import ( ) const ( - POST_DEFAULT = "" - POST_JOIN_LEAVE = "join_leave" + POST_DEFAULT = "" + POST_SLACK_ATTACHMENT = "slack_attachment" + POST_JOIN_LEAVE = "join_leave" ) type Post struct { - Id string `json:"id"` - CreateAt int64 `json:"create_at"` - UpdateAt int64 `json:"update_at"` - DeleteAt int64 `json:"delete_at"` - UserId string `json:"user_id"` - ChannelId string `json:"channel_id"` - RootId string `json:"root_id"` - ParentId string `json:"parent_id"` - OriginalId string `json:"original_id"` - Message string `json:"message"` - ImgCount int64 `json:"img_count"` - Type string `json:"type"` - Props StringMap `json:"props"` - Hashtags string `json:"hashtags"` - Filenames StringArray `json:"filenames"` - PendingPostId string `json:"pending_post_id" db:"-"` + Id string `json:"id"` + CreateAt int64 `json:"create_at"` + UpdateAt int64 `json:"update_at"` + DeleteAt int64 `json:"delete_at"` + UserId string `json:"user_id"` + ChannelId string `json:"channel_id"` + RootId string `json:"root_id"` + ParentId string `json:"parent_id"` + OriginalId string `json:"original_id"` + Message string `json:"message"` + ImgCount int64 `json:"img_count"` + Type string `json:"type"` + Props StringInterface `json:"props"` + Hashtags string `json:"hashtags"` + Filenames StringArray `json:"filenames"` + PendingPostId string `json:"pending_post_id" db:"-"` } func (o *Post) ToJson() string { @@ -103,7 +104,8 @@ func (o *Post) IsValid() *AppError { return NewAppError("Post.IsValid", "Invalid hashtags", "id="+o.Id) } - if !(o.Type == POST_DEFAULT || o.Type == POST_JOIN_LEAVE) { + // should be removed once more message types are supported + if !(o.Type == POST_DEFAULT || o.Type == POST_JOIN_LEAVE || o.Type == POST_SLACK_ATTACHMENT) { return NewAppError("Post.IsValid", "Invalid type", "id="+o.Type) } @@ -128,7 +130,7 @@ func (o *Post) PreSave() { o.UpdateAt = o.CreateAt if o.Props == nil { - o.Props = make(map[string]string) + o.Props = make(map[string]interface{}) } if o.Filenames == nil { @@ -138,14 +140,14 @@ func (o *Post) PreSave() { func (o *Post) MakeNonNil() { if o.Props == nil { - o.Props = make(map[string]string) + o.Props = make(map[string]interface{}) } if o.Filenames == nil { o.Filenames = []string{} } } -func (o *Post) AddProp(key string, value string) { +func (o *Post) AddProp(key string, value interface{}) { o.MakeNonNil() diff --git a/model/search_params.go b/model/search_params.go index 144e8e461..17a64d980 100644 --- a/model/search_params.go +++ b/model/search_params.go @@ -16,7 +16,7 @@ type SearchParams struct { var searchFlags = [...]string{"from", "channel", "in"} -func splitWords(text string) []string { +func splitWordsNoQuotes(text string) []string { words := []string{} for _, word := range strings.Fields(text) { @@ -31,6 +31,32 @@ func splitWords(text string) []string { return words } +func splitWords(text string) []string { + words := []string{} + + foundQuote := false + location := 0 + for i, char := range text { + if char == '"' { + if foundQuote { + // Grab the quoted section + word := text[location : i+1] + words = append(words, word) + foundQuote = false + location = i + 1 + } else { + words = append(words, splitWordsNoQuotes(text[location:i])...) + foundQuote = true + location = i + } + } + } + + words = append(words, splitWordsNoQuotes(text[location:])...) + + return words +} + func parseSearchFlags(input []string) ([]string, [][2]string) { words := []string{} flags := [][2]string{} @@ -127,7 +153,7 @@ func ParseSearchParams(text string) []*SearchParams { } // special case for when no terms are specified but we still have a filter - if len(plainTerms) == 0 && len(hashtagTerms) == 0 { + if len(plainTerms) == 0 && len(hashtagTerms) == 0 && (len(inChannels) != 0 || len(fromUsers) != 0) { paramsList = append(paramsList, &SearchParams{ Terms: "", IsHashtag: true, diff --git a/model/search_params_test.go b/model/search_params_test.go index e03e82c5a..af4cbe595 100644 --- a/model/search_params_test.go +++ b/model/search_params_test.go @@ -7,11 +7,73 @@ import ( "testing" ) +func TestSplitWords(t *testing.T) { + if words := splitWords(""); len(words) != 0 { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords(" "); len(words) != 0 { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("word"); len(words) != 1 || words[0] != "word" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("wo\"rd"); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("wo\"rd\""); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("word1 word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("word1 \"word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "\"word2" || words[2] != "word3" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("\"word1 word2 word3"); len(words) != 3 || words[0] != "\"word1" || words[1] != "word2" || words[2] != "word3" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("word1 word2 word3\""); len(words) != 4 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" || words[3] != "\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("word1 #word2 ##word3"); len(words) != 3 || words[0] != "word1" || words[1] != "#word2" || words[2] != "##word3" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords(" word1 word2 word3 "); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("\"quoted\""); len(words) != 1 || words[0] != "\"quoted\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("\"quoted multiple words\""); len(words) != 1 || words[0] != "\"quoted multiple words\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("some stuff \"quoted multiple words\" more stuff"); len(words) != 5 || words[0] != "some" || words[1] != "stuff" || words[2] != "\"quoted multiple words\"" || words[3] != "more" || words[4] != "stuff" { + t.Fatalf("Incorrect output splitWords: %v", words) + } + + if words := splitWords("some \"stuff\" \"quoted multiple words\" #some \"more stuff\""); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "#some" || words[4] != "\"more stuff\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } +} + func TestParseSearchFlags(t *testing.T) { if words, flags := parseSearchFlags(splitWords("")); len(words) != 0 { - t.Fatal("got words from empty input") + t.Fatalf("got words from empty input") } else if len(flags) != 0 { - t.Fatal("got flags from empty input") + t.Fatalf("got flags from empty input") } if words, flags := parseSearchFlags(splitWords("word")); len(words) != 1 || words[0] != "word" { @@ -32,6 +94,12 @@ func TestParseSearchFlags(t *testing.T) { t.Fatalf("got incorrect flags %v", flags) } + if words, flags := parseSearchFlags(splitWords("#apple #banana from:chan")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { + t.Fatalf("got incorrect words %v", words) + } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { + t.Fatalf("got incorrect flags %v", flags) + } + if words, flags := parseSearchFlags(splitWords("apple banana from: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { t.Fatalf("got incorrect words %v", words) } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { @@ -74,4 +142,74 @@ func TestParseSearchFlags(t *testing.T) { flags[2][0] != "from" || flags[2][1] != "third" || flags[3][0] != "from" || flags[3][1] != "fourth" { t.Fatalf("got incorrect flags %v", flags) } + + if words, flags := parseSearchFlags(splitWords("\"quoted\"")); len(words) != 1 || words[0] != "\"quoted\"" { + t.Fatalf("got incorrect words %v", words) + } else if len(flags) != 0 { + t.Fatalf("got incorrect flags %v", flags) + } + + if words, flags := parseSearchFlags(splitWords("\"quoted multiple words\"")); len(words) != 1 || words[0] != "\"quoted multiple words\"" { + t.Fatalf("got incorrect words %v", words) + } else if len(flags) != 0 { + t.Fatalf("got incorrect flags %v", flags) + } + + if words, flags := parseSearchFlags(splitWords("some \"stuff\" \"quoted multiple words\" some \"more stuff\"")); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "some" || words[4] != "\"more stuff\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } else if len(flags) != 0 { + t.Fatalf("got incorrect flags %v", flags) + } + + if words, flags := parseSearchFlags(splitWords("some in:here \"stuff\" \"quoted multiple words\" from:someone \"more stuff\"")); len(words) != 4 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "\"more stuff\"" { + t.Fatalf("Incorrect output splitWords: %v", words) + } else if len(flags) != 2 || flags[0][0] != "in" || flags[0][1] != "here" || flags[1][0] != "from" || flags[1][1] != "someone" { + t.Fatalf("got incorrect flags %v", flags) + } +} + +func TestParseSearchParams(t *testing.T) { + if sp := ParseSearchParams(""); len(sp) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams(" "); len(sp) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("words words"); len(sp) != 1 || sp[0].Terms != "words words" || sp[0].IsHashtag != false || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("\"my stuff\""); len(sp) != 1 || sp[0].Terms != "\"my stuff\"" || sp[0].IsHashtag != false || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("#words #words"); len(sp) != 1 || sp[0].Terms != "#words #words" || sp[0].IsHashtag != true || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("#words words"); len(sp) != 2 || sp[1].Terms != "#words" || sp[1].IsHashtag != true || len(sp[1].InChannels) != 0 || len(sp[1].FromUsers) != 0 || sp[0].Terms != "words" || sp[0].IsHashtag != false || len(sp[0].InChannels) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("in:channel"); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("testing in:channel"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("in:channel testing"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("in:channel in:otherchannel"); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 2 || sp[0].InChannels[0] != "channel" || sp[0].InChannels[1] != "otherchannel" || len(sp[0].FromUsers) != 0 { + t.Fatalf("Incorrect output from parse search params: %v", sp) + } + + if sp := ParseSearchParams("testing in:channel from:someone"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 1 || sp[0].FromUsers[0] != "someone" { + t.Fatalf("Incorrect output from parse search params: %v", sp[0]) + } } diff --git a/model/user.go b/model/user.go index 871d1bf2d..4365f47d2 100644 --- a/model/user.go +++ b/model/user.go @@ -326,6 +326,13 @@ func IsInRole(userRoles string, inRole string) bool { return false } +func (u *User) IsSSOUser() bool { + if len(u.AuthData) != 0 && len(u.AuthService) != 0 { + return true + } + return false +} + func (u *User) PreExport() { u.Password = "" u.AuthData = "" diff --git a/model/utils.go b/model/utils.go index 681ade870..6d6eb452d 100644 --- a/model/utils.go +++ b/model/utils.go @@ -17,6 +17,7 @@ import ( "time" ) +type StringInterface map[string]interface{} type StringMap map[string]string type StringArray []string type EncryptStringMap map[string]string @@ -125,6 +126,25 @@ func ArrayFromJson(data io.Reader) []string { } } +func StringInterfaceToJson(objmap map[string]interface{}) string { + if b, err := json.Marshal(objmap); err != nil { + return "" + } else { + return string(b) + } +} + +func StringInterfaceFromJson(data io.Reader) map[string]interface{} { + decoder := json.NewDecoder(data) + + var objmap map[string]interface{} + if err := decoder.Decode(&objmap); err != nil { + return make(map[string]interface{}) + } else { + return objmap + } +} + func IsLower(s string) bool { if strings.ToLower(s) == s { return true @@ -242,8 +262,8 @@ func Etag(parts ...interface{}) string { } var validHashtag = regexp.MustCompile(`^(#[A-Za-z]+[A-Za-z0-9_\-]*[A-Za-z0-9])$`) -var puncStart = regexp.MustCompile(`^[.,()&$!\[\]{}"':;\\]+`) -var puncEnd = regexp.MustCompile(`[.,()&$#!\[\]{}"';\\]+$`) +var puncStart = regexp.MustCompile(`^[.,()&$!\[\]{}':;\\]+`) +var puncEnd = regexp.MustCompile(`[.,()&$#!\[\]{}';\\]+$`) func ParseHashtags(text string) (string, string) { words := strings.Fields(text) |