@@ -136,6 +136,36 @@ lazy_static! {
136
136
} ;
137
137
}
138
138
139
+ // todo: clean up duplication here
140
+
141
+ fn send_data_user < T : IntoIterator < Item =U > , U : AsRef < Session > > ( json : & JsonValue , target : & UserId , everyone : T ) {
142
+ let receivers = everyone. into_iter ( ) . filter ( |s| {
143
+ let subscription_state = s. as_ref ( ) . subscription . get ( ) ;
144
+ let join_state = s. as_ref ( ) . join_state . get ( ) ;
145
+ match ( subscription_state, join_state) {
146
+ ( Some ( subscription) , Some ( joined) ) => {
147
+ subscription. data && & joined. user_id == target
148
+ }
149
+ _ => false
150
+ }
151
+ } ) ;
152
+ send_message ( json, receivers)
153
+ }
154
+
155
+ fn send_data_except < T : IntoIterator < Item =U > , U : AsRef < Session > > ( json : & JsonValue , myself : & UserId , everyone : T ) {
156
+ let receivers = everyone. into_iter ( ) . filter ( |s| {
157
+ let subscription_state = s. as_ref ( ) . subscription . get ( ) ;
158
+ let join_state = s. as_ref ( ) . join_state . get ( ) ;
159
+ match ( subscription_state, join_state) {
160
+ ( Some ( subscription) , Some ( joined) ) => {
161
+ subscription. data && & joined. user_id != myself
162
+ }
163
+ _ => false
164
+ }
165
+ } ) ;
166
+ send_message ( json, receivers)
167
+ }
168
+
139
169
fn notify_user < T : IntoIterator < Item =U > , U : AsRef < Session > > ( json : & JsonValue , target : & UserId , everyone : T ) {
140
170
let notifiees = everyone. into_iter ( ) . filter ( |s| {
141
171
let subscription_state = s. as_ref ( ) . subscription . get ( ) ;
@@ -465,12 +495,29 @@ fn process_subscribe(from: &Arc<Session>, what: Subscription) -> MessageResult {
465
495
Ok ( MessageResponse :: msg ( json ! ( { } ) ) )
466
496
}
467
497
498
+ fn process_data ( from : & Arc < Session > , whom : Option < UserId > , body : String ) -> MessageResult {
499
+ let payload = json ! ( { "event" : "data" , "body" : body } ) ;
500
+ let switchboard = STATE . switchboard . write ( ) ?;
501
+ if let Some ( joined) = from. join_state . get ( ) {
502
+ let occupants = switchboard. occupants_of ( & joined. room_id ) ;
503
+ if let Some ( user_id) = whom {
504
+ send_data_user ( & payload, & user_id, occupants) ;
505
+ } else {
506
+ send_data_except ( & payload, & joined. user_id , occupants) ;
507
+ }
508
+ Ok ( MessageResponse :: msg ( json ! ( { } ) ) )
509
+ } else {
510
+ Err ( From :: from ( "Cannot send data when not in a room." ) )
511
+ }
512
+ }
513
+
468
514
fn process_message ( from : & Arc < Session > , msg : MessageKind ) -> MessageResult {
469
515
match msg {
516
+ MessageKind :: Join { room_id, user_id, subscribe } => process_join ( from, room_id, user_id, subscribe) ,
470
517
MessageKind :: Subscribe { what } => process_subscribe ( from, what) ,
471
518
MessageKind :: Block { whom } => process_block ( from, whom) ,
472
519
MessageKind :: Unblock { whom } => process_unblock ( from, whom) ,
473
- MessageKind :: Join { room_id , user_id , subscribe } => process_join ( from, room_id , user_id , subscribe ) ,
520
+ MessageKind :: Data { whom , body } => process_data ( from, whom , body ) ,
474
521
}
475
522
}
476
523
0 commit comments