@@ -2,7 +2,7 @@ use std::{collections::HashSet, sync::RwLock};
22
33use redis:: AsyncCommands ;
44
5- use crate :: domain:: QueueRepository ;
5+ use crate :: domain:: { QueueEntry , QueueRepository } ;
66
77fn queue_key ( guild_id : & str ) -> String {
88 format ! ( "queue:{guild_id}" )
@@ -40,63 +40,65 @@ impl QueueRepository for RedisQueueRepository {
4040 self . open_guilds . read ( ) . unwrap ( ) . contains ( guild_id)
4141 }
4242
43- async fn index_of ( & self , guild_id : & str , target : String ) -> Option < usize > {
43+ async fn index_of ( & self , guild_id : & str , user_id : & str ) -> Option < usize > {
4444 let key = queue_key ( guild_id) ;
4545 let mut con = self . redis . get_multiplexed_async_connection ( ) . await . ok ( ) ?;
4646 let list: Vec < String > = con. lrange ( & key, 0 , -1 ) . await . ok ( ) ?;
4747
48- list. iter ( ) . position ( |item| item == & target)
48+ list. iter ( ) . position ( |json_str| {
49+ serde_json:: from_str :: < QueueEntry > ( json_str)
50+ . map ( |entry| entry. user_id == user_id)
51+ . unwrap_or ( false )
52+ } )
4953 }
5054
5155 async fn size ( & self , guild_id : & str ) -> usize {
5256 let key = queue_key ( guild_id) ;
53- let mut con = self
54- . redis
55- . get_multiplexed_async_connection ( )
56- . await
57- . ok ( )
58- . unwrap ( ) ;
57+ let mut con = match self . redis . get_multiplexed_async_connection ( ) . await {
58+ Ok ( con) => con,
59+ Err ( _) => return 0 ,
60+ } ;
5961 con. llen ( & key) . await . unwrap_or ( 0 )
6062 }
6163
62- async fn push ( & self , guild_id : & str , value : String ) -> usize {
64+ async fn push ( & self , guild_id : & str , entry : QueueEntry ) -> usize {
6365 let key = queue_key ( guild_id) ;
64- let mut con = self
65- . redis
66- . get_multiplexed_async_connection ( )
67- . await
68- . ok ( )
69- . unwrap ( ) ;
70- con. rpush ( & key, value) . await . unwrap_or_default ( )
66+ let json = serde_json:: to_string ( & entry) . unwrap ( ) ;
67+ let mut con = match self . redis . get_multiplexed_async_connection ( ) . await {
68+ Ok ( con) => con,
69+ Err ( _) => return 0 ,
70+ } ;
71+ con. rpush ( & key, json) . await . unwrap_or_default ( )
7172 }
7273
73- async fn pop ( & self , guild_id : & str ) -> Option < String > {
74+ async fn pop ( & self , guild_id : & str ) -> Option < QueueEntry > {
7475 let key = queue_key ( guild_id) ;
7576 let mut con = self . redis . get_multiplexed_async_connection ( ) . await . ok ( ) ?;
7677 let result: redis:: RedisResult < Vec < String > > = con. lpop ( & key, None ) . await ;
77- result. ok ( ) . and_then ( |mut vec| vec. pop ( ) )
78+ result
79+ . ok ( )
80+ . and_then ( |mut vec| vec. pop ( ) )
81+ . and_then ( |json_str| serde_json:: from_str ( & json_str) . ok ( ) )
7882 }
7983
80- async fn list ( & self , guild_id : & str ) -> Vec < String > {
84+ async fn list ( & self , guild_id : & str ) -> Vec < QueueEntry > {
8185 let key = queue_key ( guild_id) ;
82- let mut con = self
83- . redis
84- . get_multiplexed_async_connection ( )
85- . await
86- . ok ( )
87- . unwrap ( ) ;
88- con. lrange ( & key, 0 , -1 ) . await . unwrap_or_else ( |_| vec ! [ ] )
86+ let mut con = match self . redis . get_multiplexed_async_connection ( ) . await {
87+ Ok ( con) => con,
88+ Err ( _) => return vec ! [ ] ,
89+ } ;
90+ let json_list: Vec < String > = con. lrange ( & key, 0 , -1 ) . await . unwrap_or_else ( |_| vec ! [ ] ) ;
91+ json_list
92+ . into_iter ( )
93+ . filter_map ( |json_str| serde_json:: from_str ( & json_str) . ok ( ) )
94+ . collect ( )
8995 }
9096
9197 async fn clear ( & self , guild_id : & str ) {
9298 let key = queue_key ( guild_id) ;
93- let mut con = self
94- . redis
95- . get_multiplexed_async_connection ( )
96- . await
97- . ok ( )
98- . unwrap ( ) ;
99- let _: ( ) = con. del ( & key) . await . unwrap_or_default ( ) ;
99+ if let Ok ( mut con) = self . redis . get_multiplexed_async_connection ( ) . await {
100+ let _: ( ) = con. del ( & key) . await . unwrap_or_default ( ) ;
101+ }
100102 }
101103}
102104
@@ -157,67 +159,92 @@ mod tests {
157159 #[ tokio:: test]
158160 async fn test_push_and_pop ( ) {
159161 let queue = setup ( ) . await ;
162+ let guild = "test-push-and-pop" ;
163+ queue. clear ( guild) . await ;
160164
161- queue . push ( TEST_GUILD , "foo " . to_string ( ) ) . await ;
162- queue . push ( TEST_GUILD , "bar " . to_string ( ) ) . await ;
165+ let foo = QueueEntry :: new ( "foo" . to_string ( ) , "Foo User " . to_string ( ) ) ;
166+ let bar = QueueEntry :: new ( "bar" . to_string ( ) , "Bar User " . to_string ( ) ) ;
163167
164- assert_eq ! ( queue. size ( TEST_GUILD ) . await , 2 ) ;
165- assert_eq ! ( queue. index_of ( TEST_GUILD , " bar" . to_string ( ) ) . await , Some ( 1 ) ) ;
168+ queue. push ( guild , foo . clone ( ) ) . await ;
169+ queue. push ( guild , bar. clone ( ) ) . await ;
166170
167- let popped = queue. pop ( TEST_GUILD ) . await ;
168- assert_eq ! ( popped, Some ( "foo" . to_string( ) ) ) ;
169- assert_eq ! ( queue. size( TEST_GUILD ) . await , 1 ) ;
171+ assert_eq ! ( queue. size( guild) . await , 2 ) ;
172+ assert_eq ! ( queue. index_of( guild, "bar" ) . await , Some ( 1 ) ) ;
170173
171- let remaining = queue. list ( TEST_GUILD ) . await ;
172- assert_eq ! ( remaining, vec![ "bar" . to_string( ) ] ) ;
174+ let popped = queue. pop ( guild) . await ;
175+ assert_eq ! ( popped, Some ( foo) ) ;
176+ assert_eq ! ( queue. size( guild) . await , 1 ) ;
177+
178+ let remaining = queue. list ( guild) . await ;
179+ assert_eq ! ( remaining, vec![ bar] ) ;
173180 }
174181
175182 #[ tokio:: test]
176183 async fn test_list ( ) {
177184 let queue = setup ( ) . await ;
185+ let guild = "test-list" ;
186+ queue. clear ( guild) . await ;
187+
188+ let foo = QueueEntry :: new ( "foo" . to_string ( ) , "Foo User" . to_string ( ) ) ;
189+ let bar = QueueEntry :: new ( "bar" . to_string ( ) , "Bar User" . to_string ( ) ) ;
178190
179- queue. push ( TEST_GUILD , " foo" . to_string ( ) ) . await ;
180- queue. push ( TEST_GUILD , " bar" . to_string ( ) ) . await ;
191+ queue. push ( guild , foo. clone ( ) ) . await ;
192+ queue. push ( guild , bar. clone ( ) ) . await ;
181193
182- let list = queue. list ( TEST_GUILD ) . await ;
183- assert_eq ! ( list, vec![ " foo" . to_string ( ) , " bar" . to_string ( ) ] ) ;
194+ let list = queue. list ( guild ) . await ;
195+ assert_eq ! ( list, vec![ foo, bar] ) ;
184196 }
185197
186198 #[ tokio:: test]
187199 async fn test_index_of ( ) {
188200 let queue = setup ( ) . await ;
201+ let guild = "test-index-of" ;
202+ queue. clear ( guild) . await ;
189203
190- queue . push ( TEST_GUILD , "foo " . to_string ( ) ) . await ;
191- queue . push ( TEST_GUILD , "bar " . to_string ( ) ) . await ;
204+ let foo = QueueEntry :: new ( "foo" . to_string ( ) , "Foo User " . to_string ( ) ) ;
205+ let bar = QueueEntry :: new ( "bar" . to_string ( ) , "Bar User " . to_string ( ) ) ;
192206
193- assert_eq ! ( queue. index_of( TEST_GUILD , "foo" . to_string( ) ) . await , Some ( 0 ) ) ;
194- assert_eq ! ( queue. index_of( TEST_GUILD , "bar" . to_string( ) ) . await , Some ( 1 ) ) ;
195- assert_eq ! ( queue. index_of( TEST_GUILD , "baz" . to_string( ) ) . await , None ) ;
207+ queue. push ( guild, foo) . await ;
208+ queue. push ( guild, bar) . await ;
209+
210+ assert_eq ! ( queue. index_of( guild, "foo" ) . await , Some ( 0 ) ) ;
211+ assert_eq ! ( queue. index_of( guild, "bar" ) . await , Some ( 1 ) ) ;
212+ assert_eq ! ( queue. index_of( guild, "baz" ) . await , None ) ;
196213 }
197214
198215 #[ tokio:: test]
199216 async fn test_size ( ) {
200217 let queue = setup ( ) . await ;
218+ let guild = "test-size" ;
219+ queue. clear ( guild) . await ;
220+
221+ assert_eq ! ( queue. size( guild) . await , 0 ) ;
201222
202- assert_eq ! ( queue. size( TEST_GUILD ) . await , 0 ) ;
223+ let foo = QueueEntry :: new ( "foo" . to_string ( ) , "Foo User" . to_string ( ) ) ;
224+ let bar = QueueEntry :: new ( "bar" . to_string ( ) , "Bar User" . to_string ( ) ) ;
203225
204- queue. push ( TEST_GUILD , " foo" . to_string ( ) ) . await ;
205- queue. push ( TEST_GUILD , " bar" . to_string ( ) ) . await ;
226+ queue. push ( guild , foo) . await ;
227+ queue. push ( guild , bar) . await ;
206228
207- assert_eq ! ( queue. size( TEST_GUILD ) . await , 2 ) ;
229+ assert_eq ! ( queue. size( guild ) . await , 2 ) ;
208230 }
209231
210232 #[ tokio:: test]
211233 async fn test_clear ( ) {
212234 let queue = setup ( ) . await ;
235+ let guild = "test-clear" ;
236+ queue. clear ( guild) . await ;
213237
214- queue . push ( TEST_GUILD , "foo " . to_string ( ) ) . await ;
215- queue . push ( TEST_GUILD , "bar " . to_string ( ) ) . await ;
238+ let foo = QueueEntry :: new ( "foo" . to_string ( ) , "Foo User " . to_string ( ) ) ;
239+ let bar = QueueEntry :: new ( "bar" . to_string ( ) , "Bar User " . to_string ( ) ) ;
216240
217- assert_eq ! ( queue. size( TEST_GUILD ) . await , 2 ) ;
241+ queue. push ( guild, foo) . await ;
242+ queue. push ( guild, bar) . await ;
218243
219- queue. clear ( TEST_GUILD ) . await ;
244+ assert_eq ! ( queue. size( guild) . await , 2 ) ;
245+
246+ queue. clear ( guild) . await ;
220247
221- assert_eq ! ( queue. size( TEST_GUILD ) . await , 0 ) ;
248+ assert_eq ! ( queue. size( guild ) . await , 0 ) ;
222249 }
223250}
0 commit comments