44//! This means, the "Call ID" is a "Message ID" currently - similar to webxdc.
55use crate :: chat:: { Chat , ChatId , send_msg} ;
66use crate :: constants:: Chattype ;
7+ use crate :: contact:: ContactId ;
78use crate :: context:: Context ;
89use crate :: events:: EventType ;
910use crate :: headerdef:: HeaderDef ;
@@ -82,11 +83,10 @@ impl Context {
8283 ensure ! ( chat. typ == Chattype :: Single && !chat. is_self_talk( ) ) ;
8384
8485 let mut call = Message {
85- viewtype : Viewtype :: Text ,
86+ viewtype : Viewtype :: Call ,
8687 text : "Calling..." . into ( ) ,
8788 ..Default :: default ( )
8889 } ;
89- call. param . set_cmd ( SystemMessage :: OutgoingCall ) ;
9090 call. param . set ( Param :: WebrtcRoom , & place_call_info) ;
9191 call. id = send_msg ( self , chat_id, & mut call) . await ?;
9292
@@ -184,60 +184,61 @@ impl Context {
184184 mime_message : & MimeMessage ,
185185 call_id : MsgId ,
186186 ) -> Result < ( ) > {
187- match mime_message. is_system_message {
188- SystemMessage :: IncomingCall => {
189- let call = self . load_call_by_id ( call_id) . await ?;
190- if call. is_incoming {
191- if call. is_stale_call ( ) {
192- call. update_text ( self , "Missed call" ) . await ?;
193- self . emit_incoming_msg ( call. msg . chat_id , call_id) ;
187+ if mime_message. is_call ( ) {
188+ let call = self . load_call_by_id ( call_id) . await ?;
189+ if call. is_incoming {
190+ if call. is_stale_call ( ) {
191+ call. update_text ( self , "Missed call" ) . await ?;
192+ self . emit_incoming_msg ( call. msg . chat_id , call_id) ;
193+ } else {
194+ self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
195+ self . emit_event ( EventType :: IncomingCall {
196+ msg_id : call. msg . id ,
197+ place_call_info : call. place_call_info . to_string ( ) ,
198+ } ) ;
199+ let wait = call. remaining_ring_seconds ( ) ;
200+ task:: spawn ( Context :: emit_end_call_if_unaccepted (
201+ self . clone ( ) ,
202+ wait. try_into ( ) ?,
203+ call. msg . id ,
204+ ) ) ;
205+ }
206+ } else {
207+ self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
208+ }
209+ } else {
210+ match mime_message. is_system_message {
211+ SystemMessage :: CallAccepted => {
212+ let call = self . load_call_by_id ( call_id) . await ?;
213+ self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
214+ if call. is_incoming {
215+ self . emit_event ( EventType :: IncomingCallAccepted {
216+ msg_id : call. msg . id ,
217+ accept_call_info : call. accept_call_info ,
218+ } ) ;
194219 } else {
195- self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
196- self . emit_event ( EventType :: IncomingCall {
220+ let accept_call_info = mime_message
221+ . get_header ( HeaderDef :: ChatWebrtcAccepted )
222+ . unwrap_or_default ( ) ;
223+ call. msg
224+ . clone ( )
225+ . mark_call_as_accepted ( self , accept_call_info. to_string ( ) )
226+ . await ?;
227+ self . emit_event ( EventType :: OutgoingCallAccepted {
197228 msg_id : call. msg . id ,
198- place_call_info : call . place_call_info . to_string ( ) ,
229+ accept_call_info : accept_call_info . to_string ( ) ,
199230 } ) ;
200- let wait = call. remaining_ring_seconds ( ) ;
201- task:: spawn ( Context :: emit_end_call_if_unaccepted (
202- self . clone ( ) ,
203- wait. try_into ( ) ?,
204- call. msg . id ,
205- ) ) ;
206231 }
207- } else {
208- self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
209232 }
210- }
211- SystemMessage :: CallAccepted => {
212- let call = self . load_call_by_id ( call_id) . await ?;
213- self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
214- if call. is_incoming {
215- self . emit_event ( EventType :: IncomingCallAccepted {
216- msg_id : call. msg . id ,
217- accept_call_info : call. accept_call_info ,
218- } ) ;
219- } else {
220- let accept_call_info = mime_message
221- . get_header ( HeaderDef :: ChatWebrtcAccepted )
222- . unwrap_or_default ( ) ;
223- call. msg
224- . clone ( )
225- . mark_call_as_accepted ( self , accept_call_info. to_string ( ) )
226- . await ?;
227- self . emit_event ( EventType :: OutgoingCallAccepted {
233+ SystemMessage :: CallEnded => {
234+ let call = self . load_call_by_id ( call_id) . await ?;
235+ self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
236+ self . emit_event ( EventType :: CallEnded {
228237 msg_id : call. msg . id ,
229- accept_call_info : accept_call_info. to_string ( ) ,
230238 } ) ;
231239 }
240+ _ => { }
232241 }
233- SystemMessage :: CallEnded => {
234- let call = self . load_call_by_id ( call_id) . await ?;
235- self . emit_msgs_changed ( call. msg . chat_id , call_id) ;
236- self . emit_event ( EventType :: CallEnded {
237- msg_id : call. msg . id ,
238- } ) ;
239- }
240- _ => { }
241242 }
242243 Ok ( ( ) )
243244 }
@@ -255,13 +256,10 @@ impl Context {
255256 }
256257
257258 fn load_call_by_message ( & self , call : Message ) -> Result < CallInfo > {
258- ensure ! (
259- call. get_info_type( ) == SystemMessage :: IncomingCall
260- || call. get_info_type( ) == SystemMessage :: OutgoingCall
261- ) ;
259+ ensure ! ( call. viewtype == Viewtype :: Call ) ;
262260
263261 Ok ( CallInfo {
264- is_incoming : call. get_info_type ( ) == SystemMessage :: IncomingCall ,
262+ is_incoming : call. get_from_id ( ) != ContactId :: SELF ,
265263 is_accepted : call. is_call_accepted ( ) ?,
266264 place_call_info : call
267265 . param
@@ -284,21 +282,15 @@ impl Message {
284282 context : & Context ,
285283 accept_call_info : String ,
286284 ) -> Result < ( ) > {
287- ensure ! (
288- self . get_info_type( ) == SystemMessage :: IncomingCall
289- || self . get_info_type( ) == SystemMessage :: OutgoingCall
290- ) ;
285+ ensure ! ( self . viewtype == Viewtype :: Call ) ;
291286 self . param . set_int ( Param :: Arg , 1 ) ;
292287 self . param . set ( Param :: WebrtcAccepted , accept_call_info) ;
293288 self . update_param ( context) . await ?;
294289 Ok ( ( ) )
295290 }
296291
297292 fn is_call_accepted ( & self ) -> Result < bool > {
298- ensure ! (
299- self . get_info_type( ) == SystemMessage :: IncomingCall
300- || self . get_info_type( ) == SystemMessage :: OutgoingCall
301- ) ;
293+ ensure ! ( self . viewtype == Viewtype :: Call ) ;
302294 Ok ( self . param . get_int ( Param :: Arg ) == Some ( 1 ) )
303295 }
304296}
0 commit comments