github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/model/command.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	COMMAND_METHOD_POST = "P"
    15  	COMMAND_METHOD_GET  = "G"
    16  	MIN_TRIGGER_LENGTH  = 1
    17  	MAX_TRIGGER_LENGTH  = 128
    18  )
    19  
    20  type Command struct {
    21  	Id               string `json:"id"`
    22  	Token            string `json:"token"`
    23  	CreateAt         int64  `json:"create_at"`
    24  	UpdateAt         int64  `json:"update_at"`
    25  	DeleteAt         int64  `json:"delete_at"`
    26  	CreatorId        string `json:"creator_id"`
    27  	BranchId         string `json:"branch_id"`
    28  	Trigger          string `json:"trigger"`
    29  	Method           string `json:"method"`
    30  	Username         string `json:"username"`
    31  	IconURL          string `json:"icon_url"`
    32  	AutoComplete     bool   `json:"auto_complete"`
    33  	AutoCompleteDesc string `json:"auto_complete_desc"`
    34  	AutoCompleteHint string `json:"auto_complete_hint"`
    35  	DisplayName      string `json:"display_name"`
    36  	Description      string `json:"description"`
    37  	URL              string `json:"url"`
    38  }
    39  
    40  func (o *Command) ToJson() string {
    41  	b, _ := json.Marshal(o)
    42  	return string(b)
    43  }
    44  
    45  func CommandFromJson(data io.Reader) *Command {
    46  	var o *Command
    47  	json.NewDecoder(data).Decode(&o)
    48  	return o
    49  }
    50  
    51  func CommandListToJson(l []*Command) string {
    52  	b, _ := json.Marshal(l)
    53  	return string(b)
    54  }
    55  
    56  func CommandListFromJson(data io.Reader) []*Command {
    57  	var o []*Command
    58  	json.NewDecoder(data).Decode(&o)
    59  	return o
    60  }
    61  
    62  func (o *Command) IsValid() *AppError {
    63  
    64  	if len(o.Id) != 26 {
    65  		return NewAppError("Command.IsValid", "model.command.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    66  	}
    67  
    68  	if len(o.Token) != 26 {
    69  		return NewAppError("Command.IsValid", "model.command.is_valid.token.app_error", nil, "", http.StatusBadRequest)
    70  	}
    71  
    72  	if o.CreateAt == 0 {
    73  		return NewAppError("Command.IsValid", "model.command.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    74  	}
    75  
    76  	if o.UpdateAt == 0 {
    77  		return NewAppError("Command.IsValid", "model.command.is_valid.update_at.app_error", nil, "", http.StatusBadRequest)
    78  	}
    79  
    80  	if len(o.CreatorId) != 26 {
    81  		return NewAppError("Command.IsValid", "model.command.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
    82  	}
    83  
    84  	if len(o.BranchId) != 26 {
    85  		return NewAppError("Command.IsValid", "model.command.is_valid.branch_id.app_error", nil, "", http.StatusBadRequest)
    86  	}
    87  
    88  	if len(o.Trigger) < MIN_TRIGGER_LENGTH || len(o.Trigger) > MAX_TRIGGER_LENGTH || strings.Index(o.Trigger, "/") == 0 || strings.Contains(o.Trigger, " ") {
    89  		return NewAppError("Command.IsValid", "model.command.is_valid.trigger.app_error", nil, "", http.StatusBadRequest)
    90  	}
    91  
    92  	if len(o.URL) == 0 || len(o.URL) > 1024 {
    93  		return NewAppError("Command.IsValid", "model.command.is_valid.url.app_error", nil, "", http.StatusBadRequest)
    94  	}
    95  
    96  	if !IsValidHttpUrl(o.URL) {
    97  		return NewAppError("Command.IsValid", "model.command.is_valid.url_http.app_error", nil, "", http.StatusBadRequest)
    98  	}
    99  
   100  	if !(o.Method == COMMAND_METHOD_GET || o.Method == COMMAND_METHOD_POST) {
   101  		return NewAppError("Command.IsValid", "model.command.is_valid.method.app_error", nil, "", http.StatusBadRequest)
   102  	}
   103  
   104  	if len(o.DisplayName) > 64 {
   105  		return NewAppError("Command.IsValid", "model.command.is_valid.display_name.app_error", nil, "", http.StatusBadRequest)
   106  	}
   107  
   108  	if len(o.Description) > 128 {
   109  		return NewAppError("Command.IsValid", "model.command.is_valid.description.app_error", nil, "", http.StatusBadRequest)
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func (o *Command) PreSave() {
   116  	if o.Id == "" {
   117  		o.Id = NewId()
   118  	}
   119  
   120  	if o.Token == "" {
   121  		o.Token = NewId()
   122  	}
   123  
   124  	o.CreateAt = GetMillis()
   125  	o.UpdateAt = o.CreateAt
   126  }
   127  
   128  func (o *Command) PreUpdate() {
   129  	o.UpdateAt = GetMillis()
   130  }
   131  
   132  func (o *Command) Sanitize() {
   133  	o.Token = ""
   134  	o.CreatorId = ""
   135  	o.Method = ""
   136  	o.URL = ""
   137  	o.Username = ""
   138  	o.IconURL = ""
   139  }