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