11use crate :: prelude:: db:: * ;
22use crate :: prelude:: errors:: * ;
3+ use crate :: prelude:: types:: * ;
34use chrono:: Utc ;
45use serenity:: all:: { ChannelId , GuildChannel , UserId } ;
56use sqlx:: { Error , SqlitePool } ;
@@ -116,17 +117,17 @@ pub async fn create_thread_for_user(
116117 let channel_id = channel. id . to_string ( ) ;
117118 let thread_id = Uuid :: new_v4 ( ) . to_string ( ) ;
118119
119- match sqlx:: query!(
120+ let res = match sqlx:: query!(
120121 "INSERT INTO threads (id, user_id, user_name, channel_id) VALUES (?, ?, ?, ?)" ,
121122 thread_id,
122123 user_id,
123124 user_name,
124125 channel_id
125126 )
126- . execute ( pool)
127+ . execute ( & pool. clone ( ) )
127128 . await
128129 {
129- Ok ( _) => Ok ( thread_id) ,
130+ Ok ( _) => Ok ( thread_id. clone ( ) ) ,
130131 Err ( Error :: Database ( db_err) )
131132 if db_err. code ( ) == Some ( std:: borrow:: Cow :: Borrowed ( "2067" ) ) =>
132133 {
@@ -142,7 +143,43 @@ pub async fn create_thread_for_user(
142143 }
143144 }
144145 Err ( e) => Err ( e) ,
145- }
146+ } ;
147+
148+ let channel_id = channel. id . get ( ) as i64 ;
149+ let user_id_str = user_id. to_string ( ) ;
150+ let timestamp = Utc :: now ( ) . timestamp ( ) ;
151+
152+ let _ = match sqlx:: query!(
153+ "INSERT INTO thread_status (thread_id, channel_id, owner_id, taken_by, last_message_by, last_message_at) VALUES (?, ?, ?, ?, ?, ?)" ,
154+ thread_id,
155+ channel_id,
156+ user_id_str,
157+ None :: <String >,
158+ "user" ,
159+ timestamp
160+ )
161+ . execute ( & pool. clone ( ) )
162+ . await
163+ {
164+ Ok ( _) => Ok ( thread_id) ,
165+ Err ( Error :: Database ( db_err) )
166+ if db_err. code ( ) == Some ( std:: borrow:: Cow :: Borrowed ( "2067" ) ) =>
167+ {
168+ if let Some ( existing_thread_id) =
169+ sqlx:: query_scalar ( "SELECT id FROM threads WHERE user_id = ? AND status = 1" )
170+ . bind ( user_id)
171+ . fetch_optional ( pool)
172+ . await ?
173+ {
174+ Ok ( existing_thread_id)
175+ } else {
176+ Err ( Error :: Database ( db_err) )
177+ }
178+ }
179+ Err ( e) => Err ( e) ,
180+ } ;
181+
182+ res
146183}
147184
148185pub async fn close_thread (
@@ -379,3 +416,97 @@ pub async fn is_orphaned_thread_channel(
379416
380417 Ok ( exists)
381418}
419+
420+ pub async fn get_all_thread_status ( pool : & SqlitePool ) -> Vec < TicketState > {
421+ match sqlx:: query!(
422+ r#"
423+ SELECT channel_id, owner_id, taken_by, last_message_by, last_message_at
424+ FROM thread_status
425+ "#
426+ )
427+ . fetch_all ( pool)
428+ . await
429+ {
430+ Ok ( rows) => rows
431+ . into_iter ( )
432+ . map ( |r| TicketState {
433+ channel_id : r. channel_id ,
434+ owner_id : r. owner_id ,
435+ taken_by : r. taken_by ,
436+ last_message_by : TicketAuthor :: from_str ( & r. last_message_by ) ,
437+ last_message_at : r. last_message_at ,
438+ } )
439+ . collect ( ) ,
440+ Err ( e) => {
441+ eprintln ! ( "Database error getting thread statuses: {:?}" , e) ;
442+ vec ! [ ]
443+ }
444+ }
445+ }
446+
447+ pub async fn get_thread_status ( thread_id : & str , pool : & SqlitePool ) -> Option < TicketState > {
448+ match sqlx:: query!(
449+ r#"
450+ SELECT
451+ channel_id,
452+ owner_id,
453+ taken_by,
454+ last_message_by,
455+ last_message_at
456+ FROM thread_status
457+ WHERE thread_id = ?
458+ "# ,
459+ thread_id
460+ )
461+ . fetch_optional ( pool)
462+ . await
463+ {
464+ Ok ( Some ( row) ) => Some ( TicketState {
465+ channel_id : row. channel_id ,
466+ owner_id : row. owner_id ,
467+ taken_by : row. taken_by ,
468+ last_message_by : TicketAuthor :: from_str ( & row. last_message_by ) ,
469+ last_message_at : row. last_message_at ,
470+ } ) ,
471+ Ok ( None ) => None ,
472+ Err ( e) => {
473+ eprintln ! (
474+ "⚠️ Database error getting thread status for id {}: {:?}" ,
475+ thread_id, e
476+ ) ;
477+ None
478+ }
479+ }
480+ }
481+
482+ pub async fn update_thread_status (
483+ thread_id : & str ,
484+ ticket : & TicketState ,
485+ pool : & SqlitePool ,
486+ ) -> ModmailResult < ( ) > {
487+ let last_message_by = format ! ( "{:?}" , ticket. last_message_by) . to_lowercase ( ) ;
488+
489+ println ! (
490+ "Updating thread status for thread_id {}: taken_by={:?}, last_message_by={}, last_message_at={}" ,
491+ thread_id, ticket. taken_by, last_message_by, ticket. last_message_at
492+ ) ;
493+
494+ sqlx:: query!(
495+ r#"
496+ UPDATE thread_status
497+ SET
498+ taken_by = ?,
499+ last_message_by = ?,
500+ last_message_at = ?
501+ WHERE thread_id = ?
502+ "# ,
503+ ticket. taken_by,
504+ last_message_by,
505+ ticket. last_message_at,
506+ thread_id
507+ )
508+ . execute ( pool)
509+ . await ?;
510+
511+ Ok ( ( ) )
512+ }
0 commit comments