HTTP API: 404 for a chat id the list of chats leaves out (closes #5)
check / check (push) Successful in 1m19s
check / check (push) Successful in 1m19s
The chat client keeps contact records that GET /api/v1/chats does not list, such as the bot's own profile (1 on a new profile) and a contact it creates itself (2), and reads or sends in them when asked. The message endpoints now look the id up, through chatID in internal/api/chats.go, in the same list the chats endpoint answers with, and answer 404 for any id not in it. The webhook endpoints can call chatID too. Model: opus-5-5
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"sneak.berlin/go/simplexcalc/internal/simplex"
|
||||
)
|
||||
|
||||
@@ -18,9 +17,6 @@ const (
|
||||
// request for its messages reads.
|
||||
defaultCount = 20
|
||||
maxCount = 100
|
||||
|
||||
// noSuchChat answers a request naming a chat the bot does not have.
|
||||
noSuchChat = "no such chat"
|
||||
)
|
||||
|
||||
// message is how the API shows a message, wherever it shows one.
|
||||
@@ -69,10 +65,8 @@ func (h *handlers) handleMessages() http.HandlerFunc {
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
contactID, ok := chatID(r)
|
||||
contactID, ok := h.chatID(w, r)
|
||||
if !ok {
|
||||
h.respondError(w, http.StatusNotFound, noSuchChat)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,10 +110,8 @@ func (h *handlers) handleSend() http.HandlerFunc {
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
contactID, ok := chatID(r)
|
||||
contactID, ok := h.chatID(w, r)
|
||||
if !ok {
|
||||
h.respondError(w, http.StatusNotFound, noSuchChat)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -170,9 +162,9 @@ func (h *handlers) handleSend() http.HandlerFunc {
|
||||
}
|
||||
|
||||
// respondChatError answers a request the chat client did not serve: 404
|
||||
// for a chat the bot does not have, 409 for a contact who cannot receive
|
||||
// messages, 413 for a text too long to send, and 500 with sentence for
|
||||
// anything else, which is logged as what.
|
||||
// for a contact removed after chatID found it, 409 for one who cannot
|
||||
// receive messages, 413 for a text too long to send, and 500 with
|
||||
// sentence for anything else, which is logged as what.
|
||||
func (h *handlers) respondChatError(
|
||||
w http.ResponseWriter, err error, what, sentence string,
|
||||
) {
|
||||
@@ -189,14 +181,6 @@ func (h *handlers) respondChatError(
|
||||
}
|
||||
}
|
||||
|
||||
// chatID returns the chat id in the request's path, and false if it is
|
||||
// not one: a chat's id is its contact's, a positive whole number.
|
||||
func chatID(r *http.Request) (int64, bool) {
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
|
||||
return id, err == nil && id > 0
|
||||
}
|
||||
|
||||
// messageCount returns the request's count, defaultCount if it has none,
|
||||
// and false if it is anything but a whole number from 1 to maxCount. The
|
||||
// query is parsed here because r.URL.Query drops a pair it cannot
|
||||
|
||||
Reference in New Issue
Block a user