github.com/jfrerich/mattermost-server@v5.8.0-rc2+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 "github.com/mattermost/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 } 62 63 func ParamsFromRequest(r *http.Request) *Params { 64 params := &Params{} 65 66 props := mux.Vars(r) 67 query := r.URL.Query() 68 69 if val, ok := props["user_id"]; ok { 70 params.UserId = val 71 } 72 73 if val, ok := props["team_id"]; ok { 74 params.TeamId = val 75 } 76 77 if val, ok := props["invite_id"]; ok { 78 params.InviteId = val 79 } 80 81 if val, ok := props["token_id"]; ok { 82 params.TokenId = val 83 } 84 85 if val, ok := props["channel_id"]; ok { 86 params.ChannelId = val 87 } else { 88 params.ChannelId = query.Get("channel_id") 89 } 90 91 if val, ok := props["post_id"]; ok { 92 params.PostId = val 93 } 94 95 if val, ok := props["file_id"]; ok { 96 params.FileId = val 97 } 98 99 params.Filename = query.Get("filename") 100 101 if val, ok := props["plugin_id"]; ok { 102 params.PluginId = val 103 } 104 105 if val, ok := props["command_id"]; ok { 106 params.CommandId = val 107 } 108 109 if val, ok := props["hook_id"]; ok { 110 params.HookId = val 111 } 112 113 if val, ok := props["report_id"]; ok { 114 params.ReportId = val 115 } 116 117 if val, ok := props["emoji_id"]; ok { 118 params.EmojiId = val 119 } 120 121 if val, ok := props["app_id"]; ok { 122 params.AppId = val 123 } 124 125 if val, ok := props["email"]; ok { 126 params.Email = val 127 } 128 129 if val, ok := props["username"]; ok { 130 params.Username = val 131 } 132 133 if val, ok := props["team_name"]; ok { 134 params.TeamName = strings.ToLower(val) 135 } 136 137 if val, ok := props["channel_name"]; ok { 138 params.ChannelName = strings.ToLower(val) 139 } 140 141 if val, ok := props["category"]; ok { 142 params.Category = val 143 } 144 145 if val, ok := props["service"]; ok { 146 params.Service = val 147 } 148 149 if val, ok := props["preference_name"]; ok { 150 params.PreferenceName = val 151 } 152 153 if val, ok := props["emoji_name"]; ok { 154 params.EmojiName = val 155 } 156 157 if val, ok := props["job_id"]; ok { 158 params.JobId = val 159 } 160 161 if val, ok := props["job_type"]; ok { 162 params.JobType = val 163 } 164 165 if val, ok := props["action_id"]; ok { 166 params.ActionId = val 167 } 168 169 if val, ok := props["role_id"]; ok { 170 params.RoleId = val 171 } 172 173 if val, ok := props["role_name"]; ok { 174 params.RoleName = val 175 } 176 177 if val, ok := props["scheme_id"]; ok { 178 params.SchemeId = val 179 } 180 181 if val, ok := props["group_id"]; ok { 182 params.GroupId = val 183 } 184 185 if val, ok := props["remote_id"]; ok { 186 params.RemoteId = val 187 } 188 189 params.Scope = query.Get("scope") 190 191 if val, err := strconv.Atoi(query.Get("page")); err != nil || val < 0 { 192 params.Page = PAGE_DEFAULT 193 } else { 194 params.Page = val 195 } 196 197 if val, err := strconv.ParseBool(query.Get("permanent")); err == nil { 198 params.Permanent = val 199 } 200 201 if val, err := strconv.Atoi(query.Get("per_page")); err != nil || val < 0 { 202 params.PerPage = PER_PAGE_DEFAULT 203 } else if val > PER_PAGE_MAXIMUM { 204 params.PerPage = PER_PAGE_MAXIMUM 205 } else { 206 params.PerPage = val 207 } 208 209 if val, err := strconv.Atoi(query.Get("logs_per_page")); err != nil || val < 0 { 210 params.LogsPerPage = LOGS_PER_PAGE_DEFAULT 211 } else if val > LOGS_PER_PAGE_MAXIMUM { 212 params.LogsPerPage = LOGS_PER_PAGE_MAXIMUM 213 } else { 214 params.LogsPerPage = val 215 } 216 217 if val, ok := props["syncable_id"]; ok { 218 params.SyncableId = val 219 } 220 221 if val, ok := props["syncable_type"]; ok { 222 switch val { 223 case "teams": 224 params.SyncableType = model.GroupSyncableTypeTeam 225 case "channels": 226 params.SyncableType = model.GroupSyncableTypeChannel 227 } 228 } 229 return params 230 }