fix: message list now returns most recent 50, not oldest 50
Query was ORDER BY ASC LIMIT 50 — returning the 50 oldest messages. On page refresh users saw ancient messages instead of recent ones. Changed to ORDER BY DESC LIMIT then reverse in Go so the API still returns chronological order but from the bottom of the history.
This commit is contained in:
@@ -486,7 +486,7 @@ func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
FROM messages m
|
FROM messages m
|
||||||
JOIN users u ON m.author_id = u.id
|
JOIN users u ON m.author_id = u.id
|
||||||
WHERE m.channel_id = $1 AND m.created_at < (SELECT created_at FROM messages WHERE id = $2)
|
WHERE m.channel_id = $1 AND m.created_at < (SELECT created_at FROM messages WHERE id = $2)
|
||||||
ORDER BY m.created_at ASC
|
ORDER BY m.created_at DESC
|
||||||
LIMIT $3
|
LIMIT $3
|
||||||
`, channelID, before, limit)
|
`, channelID, before, limit)
|
||||||
} else {
|
} else {
|
||||||
@@ -495,7 +495,7 @@ func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
FROM messages m
|
FROM messages m
|
||||||
JOIN users u ON m.author_id = u.id
|
JOIN users u ON m.author_id = u.id
|
||||||
WHERE m.channel_id = $1
|
WHERE m.channel_id = $1
|
||||||
ORDER BY m.created_at ASC
|
ORDER BY m.created_at DESC
|
||||||
LIMIT $2
|
LIMIT $2
|
||||||
`, channelID, limit)
|
`, channelID, limit)
|
||||||
}
|
}
|
||||||
@@ -527,6 +527,11 @@ func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
messages = h.attachEmbeds(r.Context(), messages)
|
messages = h.attachEmbeds(r.Context(), messages)
|
||||||
|
|
||||||
|
// Reverse: query returns DESC (newest first), client expects ASC (oldest first).
|
||||||
|
for i, j := 0, len(messages)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
messages[i], messages[j] = messages[j], messages[i]
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(messages)
|
json.NewEncoder(w).Encode(messages)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user