Fix Diesel subquery compilation errors - use exists() and raw SQL for message filtering

Co-Authored-By: Dallin Bentley <dallinbentley98@gmail.com>
This commit is contained in:
Devin AI 2025-07-17 16:21:13 +00:00
parent 9e461f9948
commit ea083784bc
2 changed files with 20 additions and 26 deletions

View File

@ -101,25 +101,22 @@ 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))
diesel::dsl::exists(
messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::request_message.is_not_null())
.filter(messages::deleted_at.is_null())
).or(
diesel::dsl::sql::<diesel::sql_types::Bool>(
"(SELECT COUNT(*) FROM messages WHERE messages.chat_id = chats.id AND messages.deleted_at IS NULL) > 1"
)
)
)
.into_boxed();

View File

@ -73,17 +73,6 @@ pub async fn list_logs_handler(
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)))
@ -92,8 +81,16 @@ pub async fn list_logs_handler(
.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))
diesel::dsl::exists(
messages::table
.filter(messages::chat_id.eq(chats::id))
.filter(messages::request_message.is_not_null())
.filter(messages::deleted_at.is_null())
).or(
diesel::dsl::sql::<diesel::sql_types::Bool>(
"(SELECT COUNT(*) FROM messages WHERE messages.chat_id = chats.id AND messages.deleted_at IS NULL) > 1"
)
)
)
.into_boxed();