1
1
use self :: channel_list:: ChannelListQuery ;
2
- use crate :: db:: { get_channel_by_id, insert_channel, list_channels} ;
2
+ use crate :: db:: { get_channel_by_id, insert_channel, list_channels, insert_validator_messages } ;
3
3
use crate :: success_response;
4
4
use crate :: Application ;
5
5
use crate :: ResponseError ;
@@ -12,6 +12,10 @@ use primitives::sentry::{Event, SuccessResponse};
12
12
use primitives:: { Channel , ChannelId } ;
13
13
use slog:: error;
14
14
use std:: collections:: HashMap ;
15
+ use primitives:: channel:: SpecValidator ;
16
+ use primitives:: validator:: MessageTypes ;
17
+ use futures:: future:: try_join_all;
18
+
15
19
16
20
pub async fn channel_status < A : Adapter > (
17
21
req : Request < Body > ,
@@ -138,6 +142,39 @@ pub async fn insert_events<A: Adapter + 'static>(
138
142
. unwrap ( ) )
139
143
}
140
144
145
+ pub async fn validator_messages < A : Adapter + ' static > (
146
+ req : Request < Body > ,
147
+ app : & Application < A >
148
+ ) -> Result < Response < Body > , ResponseError > {
149
+ let session = req
150
+ . extensions ( )
151
+ . get :: < Session > ( )
152
+ . expect ( "request session" )
153
+ . to_owned ( ) ;
154
+
155
+ let channel = req
156
+ . extensions ( )
157
+ . get :: < Channel > ( )
158
+ . expect ( "Request should have Channel" )
159
+ . to_owned ( ) ;
160
+
161
+ let into_body = req. into_body ( ) ;
162
+ let body = hyper:: body:: to_bytes ( into_body) . await ?;
163
+ let messages = serde_json:: from_slice :: < Vec < MessageTypes > > ( & body) ?;
164
+
165
+ match channel. spec . validators . find ( & session. uid ) {
166
+ SpecValidator :: None => Err ( ResponseError :: Unauthorized ) ,
167
+ _ => {
168
+ try_join_all ( messages. iter ( ) . map (
169
+ |message| insert_validator_messages ( & app. pool , & channel, & session. uid , & message)
170
+ ) ) . await ?;
171
+
172
+ Ok ( success_response ( serde_json:: to_string ( & SuccessResponse { success : true } ) ?) )
173
+ }
174
+ }
175
+
176
+ }
177
+
141
178
mod channel_list {
142
179
use chrono:: serde:: ts_seconds:: deserialize as ts_seconds;
143
180
use chrono:: { DateTime , Utc } ;
0 commit comments