github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api4/params.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     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 ApiParams 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  	Page           int
    51  	PerPage        int
    52  	LogsPerPage    int
    53  	Permanent      bool
    54  }
    55  
    56  func ApiParamsFromRequest(r *http.Request) *ApiParams {
    57  	params := &ApiParams{}
    58  
    59  	props := mux.Vars(r)
    60  	query := r.URL.Query()
    61  
    62  	if val, ok := props["user_id"]; ok {
    63  		params.UserId = val
    64  	}
    65  
    66  	if val, ok := props["team_id"]; ok {
    67  		params.TeamId = val
    68  	}
    69  
    70  	if val, ok := props["invite_id"]; ok {
    71  		params.InviteId = val
    72  	}
    73  
    74  	if val, ok := props["token_id"]; ok {
    75  		params.TokenId = val
    76  	}
    77  
    78  	if val, ok := props["channel_id"]; ok {
    79  		params.ChannelId = val
    80  	} else {
    81  		params.ChannelId = query.Get("channel_id")
    82  	}
    83  
    84  	if val, ok := props["post_id"]; ok {
    85  		params.PostId = val
    86  	}
    87  
    88  	if val, ok := props["file_id"]; ok {
    89  		params.FileId = val
    90  	}
    91  
    92  	params.Filename = query.Get("filename")
    93  
    94  	if val, ok := props["plugin_id"]; ok {
    95  		params.PluginId = val
    96  	}
    97  
    98  	if val, ok := props["command_id"]; ok {
    99  		params.CommandId = val
   100  	}
   101  
   102  	if val, ok := props["hook_id"]; ok {
   103  		params.HookId = val
   104  	}
   105  
   106  	if val, ok := props["report_id"]; ok {
   107  		params.ReportId = val
   108  	}
   109  
   110  	if val, ok := props["emoji_id"]; ok {
   111  		params.EmojiId = val
   112  	}
   113  
   114  	if val, ok := props["app_id"]; ok {
   115  		params.AppId = val
   116  	}
   117  
   118  	if val, ok := props["email"]; ok {
   119  		params.Email = val
   120  	}
   121  
   122  	if val, ok := props["username"]; ok {
   123  		params.Username = val
   124  	}
   125  
   126  	if val, ok := props["team_name"]; ok {
   127  		params.TeamName = strings.ToLower(val)
   128  	}
   129  
   130  	if val, ok := props["channel_name"]; ok {
   131  		params.ChannelName = strings.ToLower(val)
   132  	}
   133  
   134  	if val, ok := props["category"]; ok {
   135  		params.Category = val
   136  	}
   137  
   138  	if val, ok := props["service"]; ok {
   139  		params.Service = val
   140  	}
   141  
   142  	if val, ok := props["preference_name"]; ok {
   143  		params.PreferenceName = val
   144  	}
   145  
   146  	if val, ok := props["emoji_name"]; ok {
   147  		params.EmojiName = val
   148  	}
   149  
   150  	if val, ok := props["job_id"]; ok {
   151  		params.JobId = val
   152  	}
   153  
   154  	if val, ok := props["job_type"]; ok {
   155  		params.JobType = val
   156  	}
   157  
   158  	if val, ok := props["action_id"]; ok {
   159  		params.ActionId = val
   160  	}
   161  
   162  	if val, ok := props["role_id"]; ok {
   163  		params.RoleId = val
   164  	}
   165  
   166  	if val, ok := props["role_name"]; ok {
   167  		params.RoleName = val
   168  	}
   169  
   170  	if val, err := strconv.Atoi(query.Get("page")); err != nil || val < 0 {
   171  		params.Page = PAGE_DEFAULT
   172  	} else {
   173  		params.Page = val
   174  	}
   175  
   176  	if val, err := strconv.ParseBool(query.Get("permanent")); err != nil {
   177  		params.Permanent = val
   178  	}
   179  
   180  	if val, err := strconv.Atoi(query.Get("per_page")); err != nil || val < 0 {
   181  		params.PerPage = PER_PAGE_DEFAULT
   182  	} else if val > PER_PAGE_MAXIMUM {
   183  		params.PerPage = PER_PAGE_MAXIMUM
   184  	} else {
   185  		params.PerPage = val
   186  	}
   187  
   188  	if val, err := strconv.Atoi(query.Get("logs_per_page")); err != nil || val < 0 {
   189  		params.LogsPerPage = LOGS_PER_PAGE_DEFAULT
   190  	} else if val > LOGS_PER_PAGE_MAXIMUM {
   191  		params.LogsPerPage = LOGS_PER_PAGE_MAXIMUM
   192  	} else {
   193  		params.LogsPerPage = val
   194  	}
   195  
   196  	return params
   197  }