github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/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  )
    13  
    14  const (
    15  	PAGE_DEFAULT          = 0
    16  	PER_PAGE_DEFAULT      = 60
    17  	PER_PAGE_MAXIMUM      = 200
    18  	LOGS_PER_PAGE_DEFAULT = 10000
    19  	LOGS_PER_PAGE_MAXIMUM = 10000
    20  )
    21  
    22  type Params struct {
    23  	UserId         string
    24  	TeamId         string
    25  	InviteId       string
    26  	TokenId        string
    27  	ChannelId      string
    28  	PostId         string
    29  	FileId         string
    30  	Filename       string
    31  	PluginId       string
    32  	CommandId      string
    33  	HookId         string
    34  	ReportId       string
    35  	EmojiId        string
    36  	AppId          string
    37  	Email          string
    38  	Username       string
    39  	TeamName       string
    40  	ChannelName    string
    41  	PreferenceName string
    42  	EmojiName      string
    43  	Category       string
    44  	Service        string
    45  	JobId          string
    46  	JobType        string
    47  	ActionId       string
    48  	RoleId         string
    49  	RoleName       string
    50  	SchemeId       string
    51  	Scope          string
    52  	Page           int
    53  	PerPage        int
    54  	LogsPerPage    int
    55  	Permanent      bool
    56  }
    57  
    58  func ParamsFromRequest(r *http.Request) *Params {
    59  	params := &Params{}
    60  
    61  	props := mux.Vars(r)
    62  	query := r.URL.Query()
    63  
    64  	if val, ok := props["user_id"]; ok {
    65  		params.UserId = val
    66  	}
    67  
    68  	if val, ok := props["team_id"]; ok {
    69  		params.TeamId = val
    70  	}
    71  
    72  	if val, ok := props["invite_id"]; ok {
    73  		params.InviteId = val
    74  	}
    75  
    76  	if val, ok := props["token_id"]; ok {
    77  		params.TokenId = val
    78  	}
    79  
    80  	if val, ok := props["channel_id"]; ok {
    81  		params.ChannelId = val
    82  	} else {
    83  		params.ChannelId = query.Get("channel_id")
    84  	}
    85  
    86  	if val, ok := props["post_id"]; ok {
    87  		params.PostId = val
    88  	}
    89  
    90  	if val, ok := props["file_id"]; ok {
    91  		params.FileId = val
    92  	}
    93  
    94  	params.Filename = query.Get("filename")
    95  
    96  	if val, ok := props["plugin_id"]; ok {
    97  		params.PluginId = val
    98  	}
    99  
   100  	if val, ok := props["command_id"]; ok {
   101  		params.CommandId = val
   102  	}
   103  
   104  	if val, ok := props["hook_id"]; ok {
   105  		params.HookId = val
   106  	}
   107  
   108  	if val, ok := props["report_id"]; ok {
   109  		params.ReportId = val
   110  	}
   111  
   112  	if val, ok := props["emoji_id"]; ok {
   113  		params.EmojiId = val
   114  	}
   115  
   116  	if val, ok := props["app_id"]; ok {
   117  		params.AppId = val
   118  	}
   119  
   120  	if val, ok := props["email"]; ok {
   121  		params.Email = val
   122  	}
   123  
   124  	if val, ok := props["username"]; ok {
   125  		params.Username = val
   126  	}
   127  
   128  	if val, ok := props["team_name"]; ok {
   129  		params.TeamName = strings.ToLower(val)
   130  	}
   131  
   132  	if val, ok := props["channel_name"]; ok {
   133  		params.ChannelName = strings.ToLower(val)
   134  	}
   135  
   136  	if val, ok := props["category"]; ok {
   137  		params.Category = val
   138  	}
   139  
   140  	if val, ok := props["service"]; ok {
   141  		params.Service = val
   142  	}
   143  
   144  	if val, ok := props["preference_name"]; ok {
   145  		params.PreferenceName = val
   146  	}
   147  
   148  	if val, ok := props["emoji_name"]; ok {
   149  		params.EmojiName = val
   150  	}
   151  
   152  	if val, ok := props["job_id"]; ok {
   153  		params.JobId = val
   154  	}
   155  
   156  	if val, ok := props["job_type"]; ok {
   157  		params.JobType = val
   158  	}
   159  
   160  	if val, ok := props["action_id"]; ok {
   161  		params.ActionId = val
   162  	}
   163  
   164  	if val, ok := props["role_id"]; ok {
   165  		params.RoleId = val
   166  	}
   167  
   168  	if val, ok := props["role_name"]; ok {
   169  		params.RoleName = val
   170  	}
   171  
   172  	if val, ok := props["scheme_id"]; ok {
   173  		params.SchemeId = val
   174  	}
   175  
   176  	params.Scope = query.Get("scope")
   177  
   178  	if val, err := strconv.Atoi(query.Get("page")); err != nil || val < 0 {
   179  		params.Page = PAGE_DEFAULT
   180  	} else {
   181  		params.Page = val
   182  	}
   183  
   184  	if val, err := strconv.ParseBool(query.Get("permanent")); err == nil {
   185  		params.Permanent = val
   186  	}
   187  
   188  	if val, err := strconv.Atoi(query.Get("per_page")); err != nil || val < 0 {
   189  		params.PerPage = PER_PAGE_DEFAULT
   190  	} else if val > PER_PAGE_MAXIMUM {
   191  		params.PerPage = PER_PAGE_MAXIMUM
   192  	} else {
   193  		params.PerPage = val
   194  	}
   195  
   196  	if val, err := strconv.Atoi(query.Get("logs_per_page")); err != nil || val < 0 {
   197  		params.LogsPerPage = LOGS_PER_PAGE_DEFAULT
   198  	} else if val > LOGS_PER_PAGE_MAXIMUM {
   199  		params.LogsPerPage = LOGS_PER_PAGE_MAXIMUM
   200  	} else {
   201  		params.LogsPerPage = val
   202  	}
   203  
   204  	return params
   205  }