18
18
//! issued a push notification on each of their subscriptions.
19
19
//!
20
20
21
+ use foxbox_taxonomy:: api:: User ;
21
22
use super :: Subscription ;
22
23
use libc:: c_int;
23
24
use rusqlite:: { self , Connection } ;
@@ -34,6 +35,13 @@ fn escape_option(opt: &Option<String>) -> Option<String> {
34
35
} ;
35
36
}
36
37
38
+ fn user_to_str ( user : & User ) -> String {
39
+ match * user {
40
+ User :: Id ( ref user) => user. clone ( ) ,
41
+ User :: None => String :: from ( "" )
42
+ }
43
+ }
44
+
37
45
pub struct WebPushDb {
38
46
db : Connection ,
39
47
}
@@ -43,14 +51,14 @@ impl WebPushDb {
43
51
pub fn new ( path : & str ) -> Self {
44
52
let db = Connection :: open ( path) . unwrap ( ) ;
45
53
db. execute ( "CREATE TABLE IF NOT EXISTS subscriptions (
46
- user_id INTEGER ,
54
+ user_id TEXT ,
47
55
push_uri TEXT NOT NULL UNIQUE,
48
56
public_key TEXT NOT NULL,
49
57
auth TEXT
50
58
)" , & [ ] ) . unwrap ( ) ;
51
59
52
60
db. execute ( "CREATE TABLE IF NOT EXISTS resources (
53
- user_id INTEGER ,
61
+ user_id TEXT ,
54
62
resource TEXT NOT NULL
55
63
)" , & [ ] ) . unwrap ( ) ;
56
64
@@ -60,35 +68,36 @@ impl WebPushDb {
60
68
}
61
69
62
70
/// Adds a new push subscription `sub` bound to the user `user_id`.
63
- pub fn subscribe ( & self , user_id : i32 , sub : & Subscription ) -> rusqlite:: Result < c_int > {
71
+ pub fn subscribe ( & self , user_id : & User , sub : & Subscription ) -> rusqlite:: Result < c_int > {
64
72
self . db . execute ( "INSERT INTO subscriptions VALUES ($1, $2, $3, $4)" ,
65
- & [ & user_id, & escape ( & sub. push_uri ) , & escape ( & sub. public_key ) , & escape_option ( & sub. auth ) ]
73
+ & [ & escape ( & user_to_str ( user_id) ) , & escape ( & sub. push_uri ) , & escape ( & sub. public_key ) , & escape_option ( & sub. auth ) ]
66
74
)
67
75
}
68
76
69
77
/// Removes an existing push subscription identified by `push_uri`.
70
- pub fn unsubscribe ( & self , _: i32 , push_uri : & str ) -> rusqlite:: Result < c_int > {
78
+ pub fn unsubscribe ( & self , _: & User , push_uri : & str ) -> rusqlite:: Result < c_int > {
71
79
self . db . execute ( "DELETE FROM subscriptions WHERE push_uri=$1" ,
72
80
& [ & escape ( push_uri) ]
73
81
)
74
82
}
75
83
76
84
/// Sets the resources to subscribe to notifications for the user `user_id`.
77
- pub fn set_resources ( & self , user_id : i32 , resources : & [ String ] ) -> rusqlite:: Result < ( ) > {
78
- try!( self . db . execute ( "DELETE FROM resources WHERE user_id=$1" , & [ & user_id] ) ) ;
85
+ pub fn set_resources ( & self , user_id : & User , resources : & [ String ] ) -> rusqlite:: Result < ( ) > {
86
+ try!( self . db . execute ( "DELETE FROM resources WHERE user_id=$1" ,
87
+ & [ & user_to_str ( user_id) ] ) ) ;
79
88
for resource in resources. iter ( ) {
80
89
try!( self . db . execute ( "INSERT INTO resources VALUES ($1, $2)" ,
81
- & [ & user_id, & escape ( resource) ]
90
+ & [ & escape ( & user_to_str ( user_id) ) , & escape ( resource) ]
82
91
) ) ;
83
92
}
84
93
Ok ( ( ) )
85
94
}
86
95
87
96
/// Gets the resources subscribed to by the user `user_id`.
88
- pub fn get_resources ( & self , user_id : i32 ) -> rusqlite:: Result < Vec < String > > {
97
+ pub fn get_resources ( & self , user_id : & User ) -> rusqlite:: Result < Vec < String > > {
89
98
let mut subs = Vec :: new ( ) ;
90
99
let mut stmt = try!( self . db . prepare ( "SELECT resource FROM resources WHERE user_id=$1" ) ) ;
91
- let rows = try!( stmt. query ( & [ & user_id] ) ) ;
100
+ let rows = try!( stmt. query ( & [ & user_to_str ( user_id) ] ) ) ;
92
101
let ( count, _) = rows. size_hint ( ) ;
93
102
subs. reserve_exact ( count) ;
94
103
for result_row in rows {
@@ -99,10 +108,10 @@ impl WebPushDb {
99
108
}
100
109
101
110
/// Gets the push subscriptions for the user `user_id`.
102
- pub fn get_subscriptions ( & self , user_id : i32 ) -> rusqlite:: Result < Vec < Subscription > > {
111
+ pub fn get_subscriptions ( & self , user_id : & User ) -> rusqlite:: Result < Vec < Subscription > > {
103
112
let mut subs = Vec :: new ( ) ;
104
113
let mut stmt = try!( self . db . prepare ( "SELECT push_uri, public_key, auth FROM subscriptions WHERE user_id=$1" ) ) ;
105
- let rows = try!( stmt. query ( & [ & user_id] ) ) ;
114
+ let rows = try!( stmt. query ( & [ & user_to_str ( user_id) ] ) ) ;
106
115
let ( count, _) = rows. size_hint ( ) ;
107
116
subs. reserve_exact ( count) ;
108
117
for result_row in rows {
@@ -159,64 +168,65 @@ pub fn remove_test_db() {
159
168
#[ cfg( test) ]
160
169
describe ! tests {
161
170
before_each {
171
+ use foxbox_taxonomy:: api:: User ;
162
172
let db = WebPushDb :: new( & get_db_environment( ) ) ;
163
173
}
164
174
165
175
it "should manage subscription correctly" {
166
176
use super :: super :: Subscription ;
167
177
168
- let subs0 = db. get_subscriptions( 1 ) . unwrap( ) ;
178
+ let subs0 = db. get_subscriptions( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
169
179
assert_eq!( subs0. len( ) , 0 ) ;
170
180
171
181
let sub = Subscription {
172
182
push_uri: "test_push_uri" . to_owned( ) ,
173
183
public_key: "test_public_key" . to_owned( ) ,
174
184
auth: Some ( "test_auth" . to_owned( ) )
175
185
} ;
176
- db. subscribe( 1 , & sub) . unwrap( ) ;
186
+ db. subscribe( & User :: Id ( String :: from ( "1" ) ) , & sub) . unwrap( ) ;
177
187
178
- let subs1 = db. get_subscriptions( 1 ) . unwrap( ) ;
188
+ let subs1 = db. get_subscriptions( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
179
189
assert_eq!( subs1. len( ) , 1 ) ;
180
190
assert_eq!( subs1[ 0 ] , sub) ;
181
191
182
- db. unsubscribe( 1 , & sub. push_uri) . unwrap( ) ;
192
+ db. unsubscribe( & User :: Id ( String :: from ( "1" ) ) , & sub. push_uri) . unwrap( ) ;
183
193
184
- let subs2 = db. get_subscriptions( 1 ) . unwrap( ) ;
194
+ let subs2 = db. get_subscriptions( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
185
195
assert_eq!( subs2. len( ) , 0 ) ;
186
196
}
187
197
188
198
it "should manage resources correctly" {
189
- let res0 = db. get_resources( 1 ) . unwrap( ) ;
199
+ let res0 = db. get_resources( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
190
200
assert_eq!( res0. len( ) , 0 ) ;
191
201
192
202
let res = vec![ "resource1" . to_owned( ) , "resource2" . to_owned( ) ] ;
193
- db. set_resources( 1 , & res) . unwrap( ) ;
203
+ db. set_resources( & User :: Id ( String :: from ( "1" ) ) , & res) . unwrap( ) ;
194
204
195
- let res1 = db. get_resources( 1 ) . unwrap( ) ;
205
+ let res1 = db. get_resources( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
196
206
assert_eq!( res1. len( ) , 2 ) ;
197
207
assert_eq!( res1[ 0 ] , "resource1" . to_owned( ) ) ;
198
208
assert_eq!( res1[ 1 ] , "resource2" . to_owned( ) ) ;
199
209
200
- db. set_resources( 1 , & [ ] ) . unwrap( ) ;
210
+ db. set_resources( & User :: Id ( String :: from ( "1" ) ) , & [ ] ) . unwrap( ) ;
201
211
202
- let res2 = db. get_resources( 1 ) . unwrap( ) ;
212
+ let res2 = db. get_resources( & User :: Id ( String :: from ( "1" ) ) ) . unwrap( ) ;
203
213
assert_eq!( res2. len( ) , 0 ) ;
204
214
}
205
215
206
216
it "should yield subscriptions given a resource" {
207
217
use super :: super :: Subscription ;
208
218
209
- db. subscribe( 1 , & Subscription {
219
+ db. subscribe( & User :: Id ( String :: from ( "1" ) ) , & Subscription {
210
220
push_uri: "u1_sub0_puri" . to_owned( ) ,
211
221
public_key: "u1_sub0_pkey" . to_owned( ) ,
212
222
auth: Some ( "u1_sub0_auth" . to_owned( ) )
213
223
} ) . unwrap( ) ;
214
- db. subscribe( 1 , & Subscription {
224
+ db. subscribe( & User :: Id ( String :: from ( "1" ) ) , & Subscription {
215
225
push_uri: "u1_sub1_puri" . to_owned( ) ,
216
226
public_key: "u1_sub1_pkey" . to_owned( ) ,
217
227
auth: None
218
228
} ) . unwrap( ) ;
219
- db. subscribe( 2 , & Subscription {
229
+ db. subscribe( & User :: Id ( String :: from ( "2" ) ) , & Subscription {
220
230
push_uri: "u2_sub0_puri" . to_owned( ) ,
221
231
public_key: "u2_sub0_pkey" . to_owned( ) ,
222
232
auth: Some ( "u2_sub0_auth" . to_owned( ) )
@@ -226,11 +236,11 @@ describe! tests {
226
236
public_key: "u3_sub0_pkey" . to_owned( ) ,
227
237
auth: Some ( "u3_sub0_auth" . to_owned( ) )
228
238
} ;
229
- db. subscribe( 3 , & u3_sub0) . unwrap( ) ;
239
+ db. subscribe( & User :: Id ( String :: from ( "3" ) ) , & u3_sub0) . unwrap( ) ;
230
240
231
- db. set_resources( 1 , & [ "res1" . to_owned( ) ] ) . unwrap( ) ;
232
- db. set_resources( 2 , & [ "res1" . to_owned( ) , "res2" . to_owned( ) ] ) . unwrap( ) ;
233
- db. set_resources( 3 , & [ "res2" . to_owned( ) , "res3" . to_owned( ) ] ) . unwrap( ) ;
241
+ db. set_resources( & User :: Id ( String :: from ( "1" ) ) , & [ "res1" . to_owned( ) ] ) . unwrap( ) ;
242
+ db. set_resources( & User :: Id ( String :: from ( "2" ) ) , & [ "res1" . to_owned( ) , "res2" . to_owned( ) ] ) . unwrap( ) ;
243
+ db. set_resources( & User :: Id ( String :: from ( "3" ) ) , & [ "res2" . to_owned( ) , "res3" . to_owned( ) ] ) . unwrap( ) ;
234
244
235
245
let subs1 = db. get_resource_subscriptions( "res1" ) . unwrap( ) ;
236
246
assert_eq!( subs1. len( ) , 3 ) ;
0 commit comments