Filter out stale asset import chats from GET /chats and /logs endpoints

- Add subqueries to count user messages (non-null request_message) and total messages per chat
- Filter out chats with zero user messages AND only one total message
- This excludes auto-generated asset import chats that users never interacted with
- Applied same filtering logic to both list_chats_handler.rs and list_logs_handler.rs

Fixes BUS-1415

Co-Authored-By: Dallin Bentley <dallinbentley98@gmail.com>
This commit is contained in:
Devin AI 2025-07-17 15:31:36 +00:00
parent bc1fbb0d7e
commit 152825758b
2 changed files with 33 additions and 3 deletions

View File

@ -78,7 +78,7 @@ pub async fn list_chats_handler(
request: ListChatsRequest,
user: &AuthenticatedUser,
) -> Result<Vec<ChatListItem>> {
use database::schema::{asset_permissions, chats, users};
use database::schema::{asset_permissions, chats, messages, users};
let mut conn = get_pg_pool().get().await?;
@ -101,11 +101,26 @@ pub async fn list_chats_handler(
Vec::new() // If admin view, we'll show all chats anyway
};
let user_message_count = messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::request_message.is_not_null())
.filter(messages::deleted_at.is_null())
.count();
let total_message_count = messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::deleted_at.is_null())
.count();
// Start building the query
let mut query = chats::table
.inner_join(users::table.on(chats::created_by.eq(users::id)))
.filter(chats::deleted_at.is_null())
.filter(chats::title.ne("")) // Filter out empty titles
.filter(
user_message_count.gt(0)
.or(total_message_count.gt(1))
)
.into_boxed();
// Add user filter if not admin view
@ -173,4 +188,4 @@ pub async fn list_chats_handler(
};
Ok(items)
}
}

View File

@ -69,10 +69,21 @@ pub async fn list_logs_handler(
request: ListLogsRequest,
organization_id: Uuid,
) -> Result<ListLogsResponse, anyhow::Error> {
use database::schema::{chats, users};
use database::schema::{chats, messages, users};
let mut conn = get_pg_pool().get().await?;
let user_message_count = messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::request_message.is_not_null())
.filter(messages::deleted_at.is_null())
.count();
let total_message_count = messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::deleted_at.is_null())
.count();
// Start building the query
let mut query = chats::table
.inner_join(users::table.on(chats::created_by.eq(users::id)))
@ -80,6 +91,10 @@ pub async fn list_logs_handler(
.filter(chats::organization_id.eq(organization_id))
.filter(chats::title.ne("")) // Filter out empty titles
.filter(chats::title.ne(" ")) // Filter out single space
.filter(
user_message_count.gt(0)
.or(total_message_count.gt(1))
)
.into_boxed();
// Calculate offset based on page number