github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/web/params.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package web
     5  
     6  import (
     7  	"net/http"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/gorilla/mux"
    12  	"github.com/crspeller/mattermost-server/model"
    13  )
    14  
    15  const (
    16  	PAGE_DEFAULT          = 0
    17  	PER_PAGE_DEFAULT      = 60
    18  	PER_PAGE_MAXIMUM      = 200
    19  	LOGS_PER_PAGE_DEFAULT = 10000
    20  	LOGS_PER_PAGE_MAXIMUM = 10000
    21  )
    22  
    23  type Params struct {
    24  	UserId         string
    25  	TeamId         string
    26  	InviteId       string
    27  	TokenId        string
    28  	ChannelId      string
    29  	PostId         string
    30  	FileId         string
    31  	Filename       string
    32  	PluginId       string
    33  	CommandId      string
    34  	HookId         string
    35  	ReportId       string
    36  	EmojiId        string
    37  	AppId          string
    38  	Email          string
    39  	Username       string
    40  	TeamName       string
    41  	ChannelName    string
    42  	PreferenceName string
    43  	EmojiName      string
    44  	Category       string
    45  	Service        string
    46  	JobId          string
    47  	JobType        string
    48  	ActionId       string
    49  	RoleId         string
    50  	RoleName       string
    51  	SchemeId       string
    52  	Scope          string
    53  	GroupId        string
    54  	Page           int
    55  	PerPage        int
    56  	LogsPerPage    int
    57  	Permanent      bool
    58  	RemoteId       string
    59  	SyncableId     string
    60  	SyncableType   model.GroupSyncableType
    61  	BotUserId      string
    62  	Q              string
    63  	IsLinked       *bool
    64  	IsConfigured   *bool
    65  }
    66  
    67  func ParamsFromRequest(r *http.Request) *Params {
    68  	params := &Params{}
    69  
    70  	props := mux.Vars(r)
    71  	query := r.URL.Query()
    72  
    73  	if val, ok := props["user_id"]; ok {
    74  		params.UserId = val
    75  	}
    76  
    77  	if val, ok := props["team_id"]; ok {
    78  		params.TeamId = val
    79  	}
    80  
    81  	if val, ok := props["invite_id"]; ok {
    82  		params.InviteId = val
    83  	}
    84  
    85  	if val, ok := props["token_id"]; ok {
    86  		params.TokenId = val
    87  	}
    88  
    89  	if val, ok := props["channel_id"]; ok {
    90  		params.ChannelId = val
    91  	} else {
    92  		params.ChannelId = query.Get("channel_id")
    93  	}
    94  
    95  	if val, ok := props["post_id"]; ok {
    96  		params.PostId = val
    97  	}
    98  
    99  	if val, ok := props["file_id"]; ok {
   100  		params.FileId = val
   101  	}
   102  
   103  	params.Filename = query.Get("filename")
   104  
   105  	if val, ok := props["plugin_id"]; ok {
   106  		params.PluginId = val
   107  	}
   108  
   109  	if val, ok := props["command_id"]; ok {
   110  		params.CommandId = val
   111  	}
   112  
   113  	if val, ok := props["hook_id"]; ok {
   114  		params.HookId = val
   115  	}
   116  
   117  	if val, ok := props["report_id"]; ok {
   118  		params.ReportId = val
   119  	}
   120  
   121  	if val, ok := props["emoji_id"]; ok {
   122  		params.EmojiId = val
   123  	}
   124  
   125  	if val, ok := props["app_id"]; ok {
   126  		params.AppId = val
   127  	}
   128  
   129  	if val, ok := props["email"]; ok {
   130  		params.Email = val
   131  	}
   132  
   133  	if val, ok := props["username"]; ok {
   134  		params.Username = val
   135  	}
   136  
   137  	if val, ok := props["team_name"]; ok {
   138  		params.TeamName = strings.ToLower(val)
   139  	}
   140  
   141  	if val, ok := props["channel_name"]; ok {
   142  		params.ChannelName = strings.ToLower(val)
   143  	}
   144  
   145  	if val, ok := props["category"]; ok {
   146  		params.Category = val
   147  	}
   148  
   149  	if val, ok := props["service"]; ok {
   150  		params.Service = val
   151  	}
   152  
   153  	if val, ok := props["preference_name"]; ok {
   154  		params.PreferenceName = val
   155  	}
   156  
   157  	if val, ok := props["emoji_name"]; ok {
   158  		params.EmojiName = val
   159  	}
   160  
   161  	if val, ok := props["job_id"]; ok {
   162  		params.JobId = val
   163  	}
   164  
   165  	if val, ok := props["job_type"]; ok {
   166  		params.JobType = val
   167  	}
   168  
   169  	if val, ok := props["action_id"]; ok {
   170  		params.ActionId = val
   171  	}
   172  
   173  	if val, ok := props["role_id"]; ok {
   174  		params.RoleId = val
   175  	}
   176  
   177  	if val, ok := props["role_name"]; ok {
   178  		params.RoleName = val
   179  	}
   180  
   181  	if val, ok := props["scheme_id"]; ok {
   182  		params.SchemeId = val
   183  	}
   184  
   185  	if val, ok := props["group_id"]; ok {
   186  		params.GroupId = val
   187  	}
   188  
   189  	if val, ok := props["remote_id"]; ok {
   190  		params.RemoteId = val
   191  	}
   192  
   193  	params.Scope = query.Get("scope")
   194  
   195  	if val, err := strconv.Atoi(query.Get("page")); err != nil || val < 0 {
   196  		params.Page = PAGE_DEFAULT
   197  	} else {
   198  		params.Page = val
   199  	}
   200  
   201  	if val, err := strconv.ParseBool(query.Get("permanent")); err == nil {
   202  		params.Permanent = val
   203  	}
   204  
   205  	if val, err := strconv.Atoi(query.Get("per_page")); err != nil || val < 0 {
   206  		params.PerPage = PER_PAGE_DEFAULT
   207  	} else if val > PER_PAGE_MAXIMUM {
   208  		params.PerPage = PER_PAGE_MAXIMUM
   209  	} else {
   210  		params.PerPage = val
   211  	}
   212  
   213  	if val, err := strconv.Atoi(query.Get("logs_per_page")); err != nil || val < 0 {
   214  		params.LogsPerPage = LOGS_PER_PAGE_DEFAULT
   215  	} else if val > LOGS_PER_PAGE_MAXIMUM {
   216  		params.LogsPerPage = LOGS_PER_PAGE_MAXIMUM
   217  	} else {
   218  		params.LogsPerPage = val
   219  	}
   220  
   221  	if val, ok := props["syncable_id"]; ok {
   222  		params.SyncableId = val
   223  	}
   224  
   225  	if val, ok := props["syncable_type"]; ok {
   226  		switch val {
   227  		case "teams":
   228  			params.SyncableType = model.GroupSyncableTypeTeam
   229  		case "channels":
   230  			params.SyncableType = model.GroupSyncableTypeChannel
   231  		}
   232  	}
   233  
   234  	if val, ok := props["bot_user_id"]; ok {
   235  		params.BotUserId = val
   236  	}
   237  
   238  	params.Q = query.Get("q")
   239  
   240  	if val, err := strconv.ParseBool(query.Get("is_linked")); err == nil {
   241  		params.IsLinked = &val
   242  	}
   243  
   244  	if val, err := strconv.ParseBool(query.Get("is_configured")); err == nil {
   245  		params.IsConfigured = &val
   246  	}
   247  
   248  	return params
   249  }