github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/web/params.go (about)

     1  // Copyright (c) 2015-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  	"github.com/mattermost/mattermost-server/v5/model"
    14  )
    15  
    16  const (
    17  	PageDefault        = 0
    18  	PerPageDefault     = 60
    19  	PerPageMaximum     = 200
    20  	LogsPerPageDefault = 10000
    21  	LogsPerPageMaximum = 10000
    22  	LimitDefault       = 60
    23  	LimitMaximum       = 200
    24  )
    25  
    26  type Params struct {
    27  	UserId                    string
    28  	TeamId                    string
    29  	InviteId                  string
    30  	TokenId                   string
    31  	ThreadId                  string
    32  	Timestamp                 int64
    33  	ChannelId                 string
    34  	PostId                    string
    35  	FileId                    string
    36  	Filename                  string
    37  	UploadId                  string
    38  	PluginId                  string
    39  	CommandId                 string
    40  	HookId                    string
    41  	ReportId                  string
    42  	EmojiId                   string
    43  	AppId                     string
    44  	Email                     string
    45  	Username                  string
    46  	TeamName                  string
    47  	ChannelName               string
    48  	PreferenceName            string
    49  	EmojiName                 string
    50  	Category                  string
    51  	Service                   string
    52  	JobId                     string
    53  	JobType                   string
    54  	ActionId                  string
    55  	RoleId                    string
    56  	RoleName                  string
    57  	SchemeId                  string
    58  	Scope                     string
    59  	GroupId                   string
    60  	Page                      int
    61  	PerPage                   int
    62  	LogsPerPage               int
    63  	Permanent                 bool
    64  	RemoteId                  string
    65  	SyncableId                string
    66  	SyncableType              model.GroupSyncableType
    67  	BotUserId                 string
    68  	Q                         string
    69  	IsLinked                  *bool
    70  	IsConfigured              *bool
    71  	NotAssociatedToTeam       string
    72  	NotAssociatedToChannel    string
    73  	Paginate                  *bool
    74  	IncludeMemberCount        bool
    75  	NotAssociatedToGroup      string
    76  	ExcludeDefaultChannels    bool
    77  	LimitAfter                int
    78  	LimitBefore               int
    79  	GroupIDs                  string
    80  	IncludeTotalCount         bool
    81  	IncludeDeleted            bool
    82  	FilterAllowReference      bool
    83  	FilterParentTeamPermitted bool
    84  	CategoryId                string
    85  	WarnMetricId              string
    86  	ExportName                string
    87  
    88  	// Cloud
    89  	InvoiceId string
    90  }
    91  
    92  func ParamsFromRequest(r *http.Request) *Params {
    93  	params := &Params{}
    94  
    95  	props := mux.Vars(r)
    96  	query := r.URL.Query()
    97  
    98  	if val, ok := props["user_id"]; ok {
    99  		params.UserId = val
   100  	}
   101  
   102  	if val, ok := props["team_id"]; ok {
   103  		params.TeamId = val
   104  	}
   105  
   106  	if val, ok := props["category_id"]; ok {
   107  		params.CategoryId = val
   108  	}
   109  
   110  	if val, ok := props["invite_id"]; ok {
   111  		params.InviteId = val
   112  	}
   113  
   114  	if val, ok := props["token_id"]; ok {
   115  		params.TokenId = val
   116  	}
   117  
   118  	if val, ok := props["thread_id"]; ok {
   119  		params.ThreadId = val
   120  	}
   121  
   122  	if val, ok := props["channel_id"]; ok {
   123  		params.ChannelId = val
   124  	} else {
   125  		params.ChannelId = query.Get("channel_id")
   126  	}
   127  
   128  	if val, ok := props["post_id"]; ok {
   129  		params.PostId = val
   130  	}
   131  
   132  	if val, ok := props["file_id"]; ok {
   133  		params.FileId = val
   134  	}
   135  
   136  	params.Filename = query.Get("filename")
   137  
   138  	if val, ok := props["upload_id"]; ok {
   139  		params.UploadId = val
   140  	}
   141  
   142  	if val, ok := props["plugin_id"]; ok {
   143  		params.PluginId = val
   144  	}
   145  
   146  	if val, ok := props["command_id"]; ok {
   147  		params.CommandId = val
   148  	}
   149  
   150  	if val, ok := props["hook_id"]; ok {
   151  		params.HookId = val
   152  	}
   153  
   154  	if val, ok := props["report_id"]; ok {
   155  		params.ReportId = val
   156  	}
   157  
   158  	if val, ok := props["emoji_id"]; ok {
   159  		params.EmojiId = val
   160  	}
   161  
   162  	if val, ok := props["app_id"]; ok {
   163  		params.AppId = val
   164  	}
   165  
   166  	if val, ok := props["email"]; ok {
   167  		params.Email = val
   168  	}
   169  
   170  	if val, ok := props["username"]; ok {
   171  		params.Username = val
   172  	}
   173  
   174  	if val, ok := props["team_name"]; ok {
   175  		params.TeamName = strings.ToLower(val)
   176  	}
   177  
   178  	if val, ok := props["channel_name"]; ok {
   179  		params.ChannelName = strings.ToLower(val)
   180  	}
   181  
   182  	if val, ok := props["category"]; ok {
   183  		params.Category = val
   184  	}
   185  
   186  	if val, ok := props["service"]; ok {
   187  		params.Service = val
   188  	}
   189  
   190  	if val, ok := props["preference_name"]; ok {
   191  		params.PreferenceName = val
   192  	}
   193  
   194  	if val, ok := props["emoji_name"]; ok {
   195  		params.EmojiName = val
   196  	}
   197  
   198  	if val, ok := props["job_id"]; ok {
   199  		params.JobId = val
   200  	}
   201  
   202  	if val, ok := props["job_type"]; ok {
   203  		params.JobType = val
   204  	}
   205  
   206  	if val, ok := props["action_id"]; ok {
   207  		params.ActionId = val
   208  	}
   209  
   210  	if val, ok := props["role_id"]; ok {
   211  		params.RoleId = val
   212  	}
   213  
   214  	if val, ok := props["role_name"]; ok {
   215  		params.RoleName = val
   216  	}
   217  
   218  	if val, ok := props["scheme_id"]; ok {
   219  		params.SchemeId = val
   220  	}
   221  
   222  	if val, ok := props["group_id"]; ok {
   223  		params.GroupId = val
   224  	}
   225  
   226  	if val, ok := props["remote_id"]; ok {
   227  		params.RemoteId = val
   228  	}
   229  
   230  	if val, ok := props["invoice_id"]; ok {
   231  		params.InvoiceId = val
   232  	}
   233  
   234  	params.Scope = query.Get("scope")
   235  
   236  	if val, err := strconv.Atoi(query.Get("page")); err != nil || val < 0 {
   237  		params.Page = PageDefault
   238  	} else {
   239  		params.Page = val
   240  	}
   241  
   242  	if val, err := strconv.ParseInt(props["timestamp"], 10, 64); err != nil || val < 0 {
   243  		params.Timestamp = 0
   244  	} else {
   245  		params.Timestamp = val
   246  	}
   247  
   248  	if val, err := strconv.ParseBool(query.Get("permanent")); err == nil {
   249  		params.Permanent = val
   250  	}
   251  
   252  	if val, err := strconv.Atoi(query.Get("per_page")); err != nil || val < 0 {
   253  		params.PerPage = PerPageDefault
   254  	} else if val > PerPageMaximum {
   255  		params.PerPage = PerPageMaximum
   256  	} else {
   257  		params.PerPage = val
   258  	}
   259  
   260  	if val, err := strconv.Atoi(query.Get("logs_per_page")); err != nil || val < 0 {
   261  		params.LogsPerPage = LogsPerPageDefault
   262  	} else if val > LogsPerPageMaximum {
   263  		params.LogsPerPage = LogsPerPageMaximum
   264  	} else {
   265  		params.LogsPerPage = val
   266  	}
   267  
   268  	if val, err := strconv.Atoi(query.Get("limit_after")); err != nil || val < 0 {
   269  		params.LimitAfter = LimitDefault
   270  	} else if val > LimitMaximum {
   271  		params.LimitAfter = LimitMaximum
   272  	} else {
   273  		params.LimitAfter = val
   274  	}
   275  
   276  	if val, err := strconv.Atoi(query.Get("limit_before")); err != nil || val < 0 {
   277  		params.LimitBefore = LimitDefault
   278  	} else if val > LimitMaximum {
   279  		params.LimitBefore = LimitMaximum
   280  	} else {
   281  		params.LimitBefore = val
   282  	}
   283  
   284  	if val, ok := props["syncable_id"]; ok {
   285  		params.SyncableId = val
   286  	}
   287  
   288  	if val, ok := props["syncable_type"]; ok {
   289  		switch val {
   290  		case "teams":
   291  			params.SyncableType = model.GroupSyncableTypeTeam
   292  		case "channels":
   293  			params.SyncableType = model.GroupSyncableTypeChannel
   294  		}
   295  	}
   296  
   297  	if val, ok := props["bot_user_id"]; ok {
   298  		params.BotUserId = val
   299  	}
   300  
   301  	params.Q = query.Get("q")
   302  
   303  	if val, err := strconv.ParseBool(query.Get("is_linked")); err == nil {
   304  		params.IsLinked = &val
   305  	}
   306  
   307  	if val, err := strconv.ParseBool(query.Get("is_configured")); err == nil {
   308  		params.IsConfigured = &val
   309  	}
   310  
   311  	params.NotAssociatedToTeam = query.Get("not_associated_to_team")
   312  	params.NotAssociatedToChannel = query.Get("not_associated_to_channel")
   313  
   314  	if val, err := strconv.ParseBool(query.Get("filter_allow_reference")); err == nil {
   315  		params.FilterAllowReference = val
   316  	}
   317  
   318  	if val, err := strconv.ParseBool(query.Get("filter_parent_team_permitted")); err == nil {
   319  		params.FilterParentTeamPermitted = val
   320  	}
   321  
   322  	if val, err := strconv.ParseBool(query.Get("paginate")); err == nil {
   323  		params.Paginate = &val
   324  	}
   325  
   326  	if val, err := strconv.ParseBool(query.Get("include_member_count")); err == nil {
   327  		params.IncludeMemberCount = val
   328  	}
   329  
   330  	params.NotAssociatedToGroup = query.Get("not_associated_to_group")
   331  
   332  	if val, err := strconv.ParseBool(query.Get("exclude_default_channels")); err == nil {
   333  		params.ExcludeDefaultChannels = val
   334  	}
   335  
   336  	params.GroupIDs = query.Get("group_ids")
   337  
   338  	if val, err := strconv.ParseBool(query.Get("include_total_count")); err == nil {
   339  		params.IncludeTotalCount = val
   340  	}
   341  
   342  	if val, err := strconv.ParseBool(query.Get("include_deleted")); err == nil {
   343  		params.IncludeDeleted = val
   344  	}
   345  
   346  	if val, ok := props["warn_metric_id"]; ok {
   347  		params.WarnMetricId = val
   348  	}
   349  
   350  	if val, ok := props["export_name"]; ok {
   351  		params.ExportName = val
   352  	}
   353  
   354  	return params
   355  }