Skip to content

Commit

Permalink
Fix chat session endpoint and order by latest message time (#557)
Browse files Browse the repository at this point in the history
* Fix chat session endpoint and order by latest message time

* Update chat session query to use LEFT JOIN and order by latest message time and session ID.
  • Loading branch information
swuecho authored Nov 27, 2024
1 parent ad9834c commit 1d23e5a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/chat_session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewChatSessionHandler(sqlc_q *sqlc_queries.Queries) *ChatSessionHandler {
}

func (h *ChatSessionHandler) Register(router *mux.Router) {
router.HandleFunc("/chat_sessions/users", h.getSimpleChatSessionsByUserID).Methods(http.MethodGet)
router.HandleFunc("/chat_sessions/user", h.getSimpleChatSessionsByUserID).Methods(http.MethodGet)

router.HandleFunc("/uuid/chat_sessions/max_length/{uuid}", h.updateSessionMaxLength).Methods("PUT")
router.HandleFunc("/uuid/chat_sessions/topic/{uuid}", h.updateChatSessionTopicByUUID).Methods("PUT")
Expand Down
17 changes: 15 additions & 2 deletions api/sqlc/queries/chat_session.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ returning *;
-- name: GetChatSessionsByUserID :many
SELECT cs.*
FROM chat_session cs
WHERE cs.user_id = $1 and cs.active = true
ORDER BY cs.id DESC;
LEFT JOIN (
SELECT chat_session_uuid, MAX(created_at) AS latest_message_time
FROM chat_message
GROUP BY chat_session_uuid
) cm ON cs.uuid = cm.chat_session_uuid
WHERE cs.user_id = $1 AND cs.active = true
ORDER BY
cm.latest_message_time DESC,
cs.id DESC;


-- SELECT cs.*
-- FROM chat_session cs
-- WHERE cs.user_id = $1 and cs.active = true
-- ORDER BY cs.updated_at DESC;

-- name: HasChatSessionPermission :one
SELECT COUNT(*) > 0 as has_permission
Expand Down
16 changes: 14 additions & 2 deletions api/sqlc_queries/chat_session.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/src/api/chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getChatSessionDefault = async (title: string): Promise<Chat.Session

export const getChatSessionsByUser = async () => {
try {
const response = await request.get('/chat_sessions/users')
const response = await request.get('/chat_sessions/user')
return response.data
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/api/use_chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const renameChatSessionQuery = useMutation({
})

const updateChatSessionQuery = useMutation({
mutationFn: (variables: { sessionUuid: string, session_data: Chat.Session }) => updateChatSession(variables.sessionUuid, variables.session_data),
mutationFn: (variables: { sessionUuid: string, sessionData: Chat.Session }) => updateChatSession(variables.sessionUuid, variables.sessionData),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['sessions'] })
}
Expand Down

0 comments on commit 1d23e5a

Please sign in to comment.