From 001a4448ca5fb0018eeb442915b473b121c04bf3 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 6 Jan 2016 21:09:05 -0600 Subject: PLT-1429 adding sql storage for slash commands --- model/command.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 101 insertions(+), 20 deletions(-) (limited to 'model/command.go') diff --git a/model/command.go b/model/command.go index 5aec5f534..253021896 100644 --- a/model/command.go +++ b/model/command.go @@ -1,4 +1,4 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package model @@ -9,28 +9,27 @@ import ( ) const ( - RESP_EXECUTED = "executed" - RESP_NOT_IMPLEMENTED = "not implemented" + COMMAND_METHOD_POST = "P" + COMMAND_METHOD_GET = "G" ) type Command struct { - Command string `json:"command"` - Response string `json:"response"` - GotoLocation string `json:"goto_location"` - ChannelId string `json:"channel_id"` - Suggest bool `json:"-"` - Suggestions []*SuggestCommand `json:"suggestions"` -} - -func (o *Command) AddSuggestion(suggest *SuggestCommand) { - - if o.Suggest { - if o.Suggestions == nil { - o.Suggestions = make([]*SuggestCommand, 0, 128) - } - - o.Suggestions = append(o.Suggestions, suggest) - } + Id string `json:"id"` + Token string `json:"token"` + CreateAt int64 `json:"create_at"` + UpdateAt int64 `json:"update_at"` + DeleteAt int64 `json:"delete_at"` + CreatorId string `json:"creator_id"` + TeamId string `json:"team_id"` + Trigger string `json:"trigger"` + Method string `json:"method"` + Username string `json:"username"` + IconURL string `json:"icon_url"` + AutoComplete bool `json:"auto_complete"` + AutoCompleteDesc string `json:"auto_complete_desc"` + AutoCompleteHint string `json:"auto_complete_hint"` + DisplayName string `json:"display_name"` + URL string `json:"url"` } func (o *Command) ToJson() string { @@ -52,3 +51,85 @@ func CommandFromJson(data io.Reader) *Command { return nil } } + +func CommandListToJson(l []*Command) string { + b, err := json.Marshal(l) + if err != nil { + return "" + } else { + return string(b) + } +} + +func CommandListFromJson(data io.Reader) []*Command { + decoder := json.NewDecoder(data) + var o []*Command + err := decoder.Decode(&o) + if err == nil { + return o + } else { + return nil + } +} + +func (o *Command) IsValid() *AppError { + + if len(o.Id) != 26 { + return NewAppError("Command.IsValid", "Invalid Id", "") + } + + if len(o.Token) != 26 { + return NewAppError("Command.IsValid", "Invalid token", "") + } + + if o.CreateAt == 0 { + return NewAppError("Command.IsValid", "Create at must be a valid time", "id="+o.Id) + } + + if o.UpdateAt == 0 { + return NewAppError("Command.IsValid", "Update at must be a valid time", "id="+o.Id) + } + + if len(o.CreatorId) != 26 { + return NewAppError("Command.IsValid", "Invalid user id", "") + } + + if len(o.TeamId) != 26 { + return NewAppError("Command.IsValid", "Invalid team id", "") + } + + if len(o.Trigger) > 1024 { + return NewAppError("Command.IsValid", "Invalid trigger", "") + } + + if len(o.URL) == 0 || len(o.URL) > 1024 { + return NewAppError("Command.IsValid", "Invalid url", "") + } + + if !IsValidHttpUrl(o.URL) { + return NewAppError("Command.IsValid", "Invalid URL. Must be a valid URL and start with http:// or https://", "") + } + + if !(o.Method == COMMAND_METHOD_GET || o.Method == COMMAND_METHOD_POST) { + return NewAppError("Command.IsValid", "Invalid Method", "") + } + + return nil +} + +func (o *Command) PreSave() { + if o.Id == "" { + o.Id = NewId() + } + + if o.Token == "" { + o.Token = NewId() + } + + o.CreateAt = GetMillis() + o.UpdateAt = o.CreateAt +} + +func (o *Command) PreUpdate() { + o.UpdateAt = GetMillis() +} -- cgit v1.2.3-1-g7c22 From e1f4cc4bb004a0a0d4bb6d68ff328233f9f72aa0 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Fri, 8 Jan 2016 22:57:38 -0600 Subject: Adding web service methods --- model/command.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'model/command.go') diff --git a/model/command.go b/model/command.go index 253021896..c917a46ea 100644 --- a/model/command.go +++ b/model/command.go @@ -133,3 +133,12 @@ func (o *Command) PreSave() { func (o *Command) PreUpdate() { o.UpdateAt = GetMillis() } + +func (o *Command) Sanatize() { + o.Token = "" + o.CreatorId = "" + o.Method = "" + o.URL = "" + o.Username = "" + o.IconURL = "" +} -- cgit v1.2.3-1-g7c22 From 27586a320add265f3e032d2cb21b27e93b51a2b0 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Mon, 1 Feb 2016 18:52:43 -0800 Subject: Adding loc to new command backend --- model/command.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'model/command.go') diff --git a/model/command.go b/model/command.go index c917a46ea..4a726b4ac 100644 --- a/model/command.go +++ b/model/command.go @@ -75,43 +75,43 @@ func CommandListFromJson(data io.Reader) []*Command { func (o *Command) IsValid() *AppError { if len(o.Id) != 26 { - return NewAppError("Command.IsValid", "Invalid Id", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.id.app_error", nil, "") } if len(o.Token) != 26 { - return NewAppError("Command.IsValid", "Invalid token", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.token.app_error", nil, "") } if o.CreateAt == 0 { - return NewAppError("Command.IsValid", "Create at must be a valid time", "id="+o.Id) + return NewLocAppError("Command.IsValid", "model.command.is_valid.create_at.app_error", nil, "") } if o.UpdateAt == 0 { - return NewAppError("Command.IsValid", "Update at must be a valid time", "id="+o.Id) + return NewLocAppError("Command.IsValid", "model.command.is_valid.update_at.app_error", nil, "") } if len(o.CreatorId) != 26 { - return NewAppError("Command.IsValid", "Invalid user id", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.user_id.app_error", nil, "") } if len(o.TeamId) != 26 { - return NewAppError("Command.IsValid", "Invalid team id", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.team_id.app_error", nil, "") } if len(o.Trigger) > 1024 { - return NewAppError("Command.IsValid", "Invalid trigger", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.trigger.app_error", nil, "") } if len(o.URL) == 0 || len(o.URL) > 1024 { - return NewAppError("Command.IsValid", "Invalid url", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.url.app_error", nil, "") } if !IsValidHttpUrl(o.URL) { - return NewAppError("Command.IsValid", "Invalid URL. Must be a valid URL and start with http:// or https://", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.url_http.app_error", nil, "") } if !(o.Method == COMMAND_METHOD_GET || o.Method == COMMAND_METHOD_POST) { - return NewAppError("Command.IsValid", "Invalid Method", "") + return NewLocAppError("Command.IsValid", "model.command.is_valid.method.app_error", nil, "") } return nil -- cgit v1.2.3-1-g7c22 From 4da7b0ccbd1b393613037f8ee5870d5660b36a84 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 3 Feb 2016 12:16:37 -0800 Subject: Fixing based on feedback --- model/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'model/command.go') diff --git a/model/command.go b/model/command.go index 4a726b4ac..56d88f13c 100644 --- a/model/command.go +++ b/model/command.go @@ -134,7 +134,7 @@ func (o *Command) PreUpdate() { o.UpdateAt = GetMillis() } -func (o *Command) Sanatize() { +func (o *Command) Sanitize() { o.Token = "" o.CreatorId = "" o.Method = "" -- cgit v1.2.3-1-g7c22