Skip to content

Commit

Permalink
order validation for chat id
Browse files Browse the repository at this point in the history
  • Loading branch information
nocodeleaks committed May 23, 2024
1 parent 2cf3c58 commit 41ec8dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/controllers/api_handlers_untested.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ func SendAny(w http.ResponseWriter, r *http.Request) {
// Declare a new request struct.
request := &models.QpSendAnyRequest{}

// Getting ChatId parameter
err = request.EnsureValidChatId(r)
if err != nil {
metrics.MessageSendErrors.Inc()
response.ParseError(err)
RespondInterface(w, response)
return
}

switch os := r.Method; os {
case http.MethodPost:
// Try to decode the request body into the struct. If there is an error,
Expand All @@ -110,6 +101,15 @@ func SendAny(w http.ResponseWriter, r *http.Request) {
}
}

// Getting ChatId parameter
err = request.EnsureValidChatId(r)
if err != nil {
metrics.MessageSendErrors.Inc()
response.ParseError(err)
RespondInterface(w, response)
return
}

// override trackid if passed throw any other way
trackid := GetTrackId(r)
if len(trackid) > 0 {
Expand Down
7 changes: 5 additions & 2 deletions src/models/qp_send_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ func (source *QpSendRequest) GetLogger() *log.Entry {
}

func (source *QpSendRequest) EnsureChatId(r *http.Request) (err error) {
if len(source.ChatId) == 0 {
source.ChatId = GetChatId(r)

// already set ?
if len(source.ChatId) > 0 {
return
}

source.ChatId = GetChatId(r)
if len(source.ChatId) == 0 {
err = fmt.Errorf("chat id missing")
}
Expand Down

0 comments on commit 41ec8dd

Please sign in to comment.