@@ -38,7 +38,7 @@ use ruma::{
3838use serde:: { Deserialize , Serialize } ;
3939use serde_json:: { value:: RawValue as RawJsonValue , Value } ;
4040use tokio:: sync:: mpsc:: { unbounded_channel, UnboundedReceiver } ;
41- use tracing:: error;
41+ use tracing:: { error, info } ;
4242
4343use super :: { machine:: SendEventResponse , StateKeySelector } ;
4444use crate :: { event_handler:: EventHandlerDropGuard , room:: MessagesOptions , Error , Result , Room } ;
@@ -305,13 +305,13 @@ impl<T> EventReceiver<T> {
305305// value through the widget driver and only serialize here to allow potimizing
306306// with `serde(borrow)`.
307307#[ derive( Deserialize , Serialize ) ]
308- struct RoomIdEncryptionSerializer < ' a > {
308+ struct RoomIdEncryptionSerializer {
309309 #[ serde( skip_serializing_if = "Option::is_none" ) ]
310310 room_id : Option < OwnedRoomId > ,
311311 #[ serde( skip_serializing_if = "Option::is_none" ) ]
312312 encrypted : Option < bool > ,
313- #[ serde( flatten, borrow ) ]
314- rest : & ' a RawJsonValue ,
313+ #[ serde( flatten) ]
314+ rest : Value ,
315315}
316316
317317/// Attach additional properties to the event.
@@ -328,12 +328,45 @@ fn add_props_to_raw<T>(
328328 room_id : Option < OwnedRoomId > ,
329329 encryption_info : Option < & EncryptionInfo > ,
330330) -> Result < Raw < T > > {
331- match raw. deserialize_as :: < RoomIdEncryptionSerializer < ' _ > > ( ) {
331+ match raw. deserialize_as :: < RoomIdEncryptionSerializer > ( ) {
332332 Ok ( mut event) => {
333333 event. room_id = room_id. or ( event. room_id ) ;
334334 event. encrypted = encryption_info. map ( |_| true ) . or ( event. encrypted ) ;
335+ info ! ( "rest is: {:?}" , event. rest) ;
335336 Ok ( Raw :: new ( & event) ?. cast ( ) )
336337 }
337338 Err ( e) => Err ( Error :: from ( e) ) ,
338339 }
339340}
341+ #[ cfg( test) ]
342+ mod tests {
343+ use ruma:: { room_id, serde:: Raw } ;
344+ use serde_json:: json;
345+
346+ use super :: add_props_to_raw;
347+
348+ #[ test]
349+ fn test_app_props_to_raw ( ) {
350+ let raw = Raw :: new ( & json ! ( {
351+ "encrypted" : true ,
352+ "type" : "m.room.message" ,
353+ "content" : {
354+ "body" : "Hello world"
355+ }
356+ } ) )
357+ . unwrap ( ) ;
358+ let room_id = room_id ! ( "!my_id:example.org" ) ;
359+ let new = add_props_to_raw ( & raw , Some ( room_id. to_owned ( ) ) , None ) . unwrap ( ) ;
360+ assert_eq ! (
361+ serde_json:: to_value( new) . unwrap( ) ,
362+ json!( {
363+ "encrypted" : true ,
364+ "room_id" : "!my_id:example.org" ,
365+ "type" : "m.room.message" ,
366+ "content" : {
367+ "body" : "Hello world"
368+ }
369+ } )
370+ ) ;
371+ }
372+ }
0 commit comments