summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
Diffstat (limited to 'model')
-rw-r--r--model/client.go47
-rw-r--r--model/command.go130
-rw-r--r--model/command_response.go41
-rw-r--r--model/command_response_test.go19
-rw-r--r--model/command_test.go90
-rw-r--r--model/config.go50
-rw-r--r--model/version.go3
7 files changed, 331 insertions, 49 deletions
diff --git a/model/client.go b/model/client.go
index d31ac1592..a271e6162 100644
--- a/model/client.go
+++ b/model/client.go
@@ -372,7 +372,43 @@ func (c *Client) Command(channelId string, command string, suggest bool) (*Resul
m["command"] = command
m["channelId"] = channelId
m["suggest"] = strconv.FormatBool(suggest)
- if r, err := c.DoApiPost("/command", MapToJson(m)); err != nil {
+ if r, err := c.DoApiPost("/commands/execute", MapToJson(m)); err != nil {
+ return nil, err
+ } else {
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), CommandResponseFromJson(r.Body)}, nil
+ }
+}
+
+func (c *Client) ListCommands() (*Result, *AppError) {
+ if r, err := c.DoApiGet("/commands/list", "", ""); err != nil {
+ return nil, err
+ } else {
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), CommandListFromJson(r.Body)}, nil
+ }
+}
+
+func (c *Client) ListTeamCommands() (*Result, *AppError) {
+ if r, err := c.DoApiGet("/commands/list_team_commands", "", ""); err != nil {
+ return nil, err
+ } else {
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), CommandListFromJson(r.Body)}, nil
+ }
+}
+
+func (c *Client) CreateCommand(cmd *Command) (*Result, *AppError) {
+ if r, err := c.DoApiPost("/commands/create", cmd.ToJson()); err != nil {
+ return nil, err
+ } else {
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), CommandFromJson(r.Body)}, nil
+ }
+}
+
+func (c *Client) RegenCommandToken(data map[string]string) (*Result, *AppError) {
+ if r, err := c.DoApiPost("/commands/regen_token", MapToJson(data)); err != nil {
return nil, err
} else {
return &Result{r.Header.Get(HEADER_REQUEST_ID),
@@ -380,6 +416,15 @@ func (c *Client) Command(channelId string, command string, suggest bool) (*Resul
}
}
+func (c *Client) DeleteCommand(data map[string]string) (*Result, *AppError) {
+ if r, err := c.DoApiPost("/commands/delete", MapToJson(data)); err != nil {
+ return nil, err
+ } else {
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), MapFromJson(r.Body)}, nil
+ }
+}
+
func (c *Client) GetAudits(id string, etag string) (*Result, *AppError) {
if r, err := c.DoApiGet("/users/"+id+"/audits", "", etag); err != nil {
return nil, err
diff --git a/model/command.go b/model/command.go
index 5aec5f534..c917a46ea 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,94 @@ 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()
+}
+
+func (o *Command) Sanatize() {
+ o.Token = ""
+ o.CreatorId = ""
+ o.Method = ""
+ o.URL = ""
+ o.Username = ""
+ o.IconURL = ""
+}
diff --git a/model/command_response.go b/model/command_response.go
new file mode 100644
index 000000000..9314f38ef
--- /dev/null
+++ b/model/command_response.go
@@ -0,0 +1,41 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "encoding/json"
+ "io"
+)
+
+const (
+ COMMAND_RESPONSE_TYPE_IN_CHANNEL = "in_channel"
+ COMMAND_RESPONSE_TYPE_EPHEMERAL = "ephemeral"
+)
+
+type CommandResponse struct {
+ ResponseType string `json:"response_type"`
+ Text string `json:"text"`
+ GotoLocation string `json:"goto_location"`
+ Attachments interface{} `json:"attachments"`
+}
+
+func (o *CommandResponse) ToJson() string {
+ b, err := json.Marshal(o)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func CommandResponseFromJson(data io.Reader) *CommandResponse {
+ decoder := json.NewDecoder(data)
+ var o CommandResponse
+ err := decoder.Decode(&o)
+ if err == nil {
+ return &o
+ } else {
+ return nil
+ }
+}
diff --git a/model/command_response_test.go b/model/command_response_test.go
new file mode 100644
index 000000000..7aa3e984b
--- /dev/null
+++ b/model/command_response_test.go
@@ -0,0 +1,19 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestCommandResponseJson(t *testing.T) {
+ o := CommandResponse{Text: "test"}
+ json := o.ToJson()
+ ro := CommandResponseFromJson(strings.NewReader(json))
+
+ if o.Text != ro.Text {
+ t.Fatal("Ids do not match")
+ }
+}
diff --git a/model/command_test.go b/model/command_test.go
index 61302ea10..0581625d9 100644
--- a/model/command_test.go
+++ b/model/command_test.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,17 +9,89 @@ import (
)
func TestCommandJson(t *testing.T) {
+ o := Command{Id: NewId()}
+ json := o.ToJson()
+ ro := CommandFromJson(strings.NewReader(json))
- command := &Command{Command: NewId(), Suggest: true}
- command.AddSuggestion(&SuggestCommand{Suggestion: NewId()})
- json := command.ToJson()
- result := CommandFromJson(strings.NewReader(json))
-
- if command.Command != result.Command {
+ if o.Id != ro.Id {
t.Fatal("Ids do not match")
}
+}
- if command.Suggestions[0].Suggestion != result.Suggestions[0].Suggestion {
- t.Fatal("Ids do not match")
+func TestCommandIsValid(t *testing.T) {
+ o := Command{}
+
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.Id = NewId()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.CreateAt = GetMillis()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.UpdateAt = GetMillis()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.CreatorId = "123"
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.CreatorId = NewId()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.Token = "123"
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
}
+
+ o.Token = NewId()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.TeamId = "123"
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.TeamId = NewId()
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.URL = "nowhere.com/"
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.URL = "http://nowhere.com/"
+ if err := o.IsValid(); err == nil {
+ t.Fatal("should be invalid")
+ }
+
+ o.Method = COMMAND_METHOD_GET
+ if err := o.IsValid(); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestCommandPreSave(t *testing.T) {
+ o := Command{}
+ o.PreSave()
+}
+
+func TestCommandPreUpdate(t *testing.T) {
+ o := Command{}
+ o.PreUpdate()
}
diff --git a/model/config.go b/model/config.go
index 5c8604ff1..f518e8f8d 100644
--- a/model/config.go
+++ b/model/config.go
@@ -24,24 +24,26 @@ const (
)
type ServiceSettings struct {
- ListenAddress string
- MaximumLoginAttempts int
- SegmentDeveloperKey string
- GoogleDeveloperKey string
- EnableOAuthServiceProvider bool
- EnableIncomingWebhooks bool
- EnableOutgoingWebhooks bool
- EnablePostUsernameOverride bool
- EnablePostIconOverride bool
- EnableTesting bool
- EnableDeveloper *bool
- EnableSecurityFixAlert *bool
- SessionLengthWebInDays *int
- SessionLengthMobileInDays *int
- SessionLengthSSOInDays *int
- SessionCacheInMinutes *int
- WebsocketSecurePort *int
- WebsocketPort *int
+ ListenAddress string
+ MaximumLoginAttempts int
+ SegmentDeveloperKey string
+ GoogleDeveloperKey string
+ EnableOAuthServiceProvider bool
+ EnableIncomingWebhooks bool
+ EnableOutgoingWebhooks bool
+ EnableCommands *bool
+ EnableOnlyAdminIntegrations *bool
+ EnablePostUsernameOverride bool
+ EnablePostIconOverride bool
+ EnableTesting bool
+ EnableDeveloper *bool
+ EnableSecurityFixAlert *bool
+ SessionLengthWebInDays *int
+ SessionLengthMobileInDays *int
+ SessionLengthSSOInDays *int
+ SessionCacheInMinutes *int
+ WebsocketSecurePort *int
+ WebsocketPort *int
}
type SSOSettings struct {
@@ -332,10 +334,22 @@ func (o *Config) SetDefaults() {
o.ServiceSettings.SessionCacheInMinutes = new(int)
*o.ServiceSettings.SessionCacheInMinutes = 10
}
+
+ if o.ServiceSettings.EnableCommands == nil {
+ o.ServiceSettings.EnableCommands = new(bool)
+ *o.ServiceSettings.EnableCommands = false
+ }
+
+ if o.ServiceSettings.EnableOnlyAdminIntegrations == nil {
+ o.ServiceSettings.EnableOnlyAdminIntegrations = new(bool)
+ *o.ServiceSettings.EnableOnlyAdminIntegrations = true
+ }
+
if o.ServiceSettings.WebsocketPort == nil {
o.ServiceSettings.WebsocketPort = new(int)
*o.ServiceSettings.WebsocketPort = 80
}
+
if o.ServiceSettings.WebsocketSecurePort == nil {
o.ServiceSettings.WebsocketSecurePort = new(int)
*o.ServiceSettings.WebsocketSecurePort = 443
diff --git a/model/version.go b/model/version.go
index 69529e7a1..a5227f328 100644
--- a/model/version.go
+++ b/model/version.go
@@ -26,6 +26,7 @@ var versions = []string{
}
var CurrentVersion string = versions[0]
+
var BuildNumber = "_BUILD_NUMBER_"
var BuildDate = "_BUILD_DATE_"
var BuildHash = "_BUILD_HASH_"
@@ -83,7 +84,7 @@ func GetPreviousVersion(version string) string {
}
func IsOfficalBuild() bool {
- return BuildNumber != "_BUILD_NUMBER_"
+ return BuildNumber != "dev"
}
func IsCurrentVersion(versionToCheck string) bool {