@@ -53,6 +53,7 @@ use tokio_stream::Stream;
53
53
use tracing:: { error, info} ;
54
54
use uuid:: Uuid ;
55
55
56
+ use crate :: store:: StoreNotificationsSender ;
56
57
use crate :: {
57
58
clients:: connection_establishment:: { ConnectionEstablishmentPackageTbs , FriendshipPackage } ,
58
59
contacts:: { Contact , ContactAddInfos , PartialContact } ,
@@ -75,7 +76,6 @@ use crate::{
75
76
Asset ,
76
77
} ;
77
78
use crate :: { key_stores:: as_credentials:: AsCredentials , ConversationId } ;
78
- use crate :: { mimi_content:: MimiContent , store:: StoreNotificationsSender } ;
79
79
use crate :: {
80
80
utils:: persistence:: { SqliteConnection , Storable } ,
81
81
Message ,
@@ -87,6 +87,7 @@ pub(crate) mod api_clients;
87
87
pub ( crate ) mod connection_establishment;
88
88
pub mod conversations;
89
89
mod create_user;
90
+ mod message;
90
91
pub ( crate ) mod own_client_info;
91
92
mod persistence;
92
93
pub mod process;
@@ -260,6 +261,20 @@ impl CoreUser {
260
261
Ok ( Some ( self_user) )
261
262
}
262
263
264
+ /// Executes a fallible closure `f` with a transaction.
265
+ ///
266
+ /// Transaction is committed on success and rolled back on failure of the closure `f`.
267
+ pub ( crate ) async fn with_transaction < T > (
268
+ & self ,
269
+ f : impl FnOnce ( & Transaction ) -> anyhow:: Result < T > ,
270
+ ) -> anyhow:: Result < T > {
271
+ let mut connection = self . inner . connection . lock ( ) . await ;
272
+ let transaction = connection. transaction ( ) ?;
273
+ let value = f ( & transaction) ?;
274
+ transaction. commit ( ) ?;
275
+ Ok ( value)
276
+ }
277
+
263
278
pub ( crate ) fn send_store_notification ( & self , notification : StoreNotification ) {
264
279
if !notification. ops . is_empty ( ) {
265
280
self . inner . store_notifications_tx . notify ( notification) ;
@@ -504,79 +519,6 @@ impl CoreUser {
504
519
Ok ( conversation_messages)
505
520
}
506
521
507
- /// Send a message and return it. Note that the message has already been
508
- /// sent to the DS and has internally been stored in the conversation store.
509
- pub async fn send_message (
510
- & self ,
511
- conversation_id : ConversationId ,
512
- content : MimiContent ,
513
- ) -> Result < ConversationMessage > {
514
- // Phase 1: Load the conversation and group
515
- let ( group, params, conversation, mut conversation_message) = {
516
- let mut notifier = self . store_notifier ( ) ;
517
- let mut connection = self . inner . connection . lock ( ) . await ;
518
- let mut transaction = connection. transaction ( ) ?;
519
- let conversation =
520
- Conversation :: load ( & transaction, & conversation_id) ?. ok_or ( anyhow ! (
521
- "Can't find conversation with id {}" ,
522
- conversation_id. as_uuid( )
523
- ) ) ?;
524
- let group_id = conversation. group_id ( ) ;
525
- // Store the message as unsent so that we don't lose it in case
526
- // something goes wrong.
527
- let conversation_message = ConversationMessage :: new_unsent_message (
528
- self . user_name ( ) . to_string ( ) ,
529
- conversation_id,
530
- content. clone ( ) ,
531
- ) ;
532
- conversation_message. store ( & transaction, & mut notifier) ?;
533
-
534
- // Notify as early as possible to react to the not yet sent message
535
- notifier. notify ( ) ;
536
-
537
- let mut group = Group :: load ( & transaction, group_id) ?
538
- . ok_or ( anyhow ! ( "Can't find group with id {group_id:?}" ) ) ?;
539
- let params = group. create_message ( & transaction, content) ?;
540
- // Immediately write the group back. No need to wait for the DS to
541
- // confirm as this is just an application message.
542
- group. store_update ( & transaction) ?;
543
- // Also, mark the message (and all messages preceeding it) as read.
544
- let mut notifier = self . store_notifier ( ) ;
545
- Conversation :: mark_as_read (
546
- & mut transaction,
547
- & mut notifier,
548
- vec ! [ ( conversation. id( ) , conversation_message. timestamp( ) ) ] . into_iter ( ) ,
549
- ) ?;
550
- transaction. commit ( ) ?;
551
- notifier. notify ( ) ;
552
- drop ( connection) ;
553
- ( group, params, conversation, conversation_message)
554
- } ;
555
-
556
- // Phase 2: Send message to DS
557
- let ds_timestamp = self
558
- . inner
559
- . api_clients
560
- . get ( & conversation. owner_domain ( ) ) ?
561
- . ds_send_message ( params, group. leaf_signer ( ) , group. group_state_ear_key ( ) )
562
- . await ?;
563
-
564
- // Phase 3: Mark the message as sent and read (again).
565
- let mut connection = self . inner . connection . lock ( ) . await ;
566
- let mut notifier = self . store_notifier ( ) ;
567
- conversation_message. mark_as_sent ( & connection, & mut notifier, ds_timestamp) ?;
568
- let mut transaction = connection. transaction ( ) ?;
569
- Conversation :: mark_as_read (
570
- & mut transaction,
571
- & mut notifier,
572
- vec ! [ ( conversation. id( ) , conversation_message. timestamp( ) ) ] . into_iter ( ) ,
573
- ) ?;
574
- transaction. commit ( ) ?;
575
- notifier. notify ( ) ;
576
-
577
- Ok ( conversation_message)
578
- }
579
-
580
522
/// Re-try sending a message, where sending previously failed.
581
523
pub async fn re_send_message ( & self , local_message_id : Uuid ) -> Result < ( ) > {
582
524
// Phase 1: Load the unsent message
@@ -598,7 +540,10 @@ impl CoreUser {
598
540
let group_id = conversation. group_id ( ) ;
599
541
let mut group = Group :: load ( & connection, group_id) ?
600
542
. ok_or ( anyhow ! ( "Can't find group with id {:?}" , group_id) ) ?;
601
- let params = group. create_message ( & connection, content) ?;
543
+ let params = {
544
+ let provider = PhnxOpenMlsProvider :: new ( & connection) ;
545
+ group. create_message ( & provider, content) ?
546
+ } ;
602
547
drop ( connection) ;
603
548
604
549
// Phase 2: Send message to DS
@@ -614,9 +559,9 @@ impl CoreUser {
614
559
let mut notifier = self . store_notifier ( ) ;
615
560
unsent_message. mark_as_sent ( & connection, & mut notifier, ds_timestamp) ?;
616
561
group. store_update ( & connection) ?;
617
- let mut transaction = connection. transaction ( ) ?;
562
+ let transaction = connection. transaction ( ) ?;
618
563
Conversation :: mark_as_read (
619
- & mut transaction,
564
+ & transaction,
620
565
& mut notifier,
621
566
vec ! [ ( conversation. id( ) , unsent_message. timestamp( ) ) ] . into_iter ( ) ,
622
567
) ?;
@@ -1161,9 +1106,9 @@ impl CoreUser {
1161
1106
mark_as_read_data : T ,
1162
1107
) -> Result < ( ) , rusqlite:: Error > {
1163
1108
let mut connection = self . inner . connection . lock ( ) . await ;
1164
- let mut transaction = connection. transaction ( ) ?;
1109
+ let transaction = connection. transaction ( ) ?;
1165
1110
let mut notifier = self . store_notifier ( ) ;
1166
- Conversation :: mark_as_read ( & mut transaction, & mut notifier, mark_as_read_data) ?;
1111
+ Conversation :: mark_as_read ( & transaction, & mut notifier, mark_as_read_data) ?;
1167
1112
transaction. commit ( ) ?;
1168
1113
notifier. notify ( ) ;
1169
1114
Ok ( ( ) )
0 commit comments