@@ -896,19 +896,35 @@ impl CommandApi {
896
896
. to_u32 ( ) )
897
897
}
898
898
899
- // for now only text messages, because we only used text messages in desktop thusfar
899
+ /// Add a message to the device-chat.
900
+ /// Device-messages usually contain update information
901
+ /// and some hints that are added during the program runs, multi-device etc.
902
+ /// The device-message may be defined by a label;
903
+ /// if a message with the same label was added or skipped before,
904
+ /// the message is not added again, even if the message was deleted in between.
905
+ /// If needed, the device-chat is created before.
906
+ ///
907
+ /// Sends the `MsgsChanged` event on success.
908
+ ///
909
+ /// Setting msg to None will prevent the device message with this label from being added in the future.
900
910
async fn add_device_message (
901
911
& self ,
902
912
account_id : u32 ,
903
913
label : String ,
904
- text : String ,
905
- ) -> Result < u32 > {
914
+ msg : Option < MessageData > ,
915
+ ) -> Result < Option < u32 > > {
906
916
let ctx = self . get_context ( account_id) . await ?;
907
- let mut msg = Message :: new ( Viewtype :: Text ) ;
908
- msg. set_text ( text) ;
909
- let message_id =
910
- deltachat:: chat:: add_device_msg ( & ctx, Some ( & label) , Some ( & mut msg) ) . await ?;
911
- Ok ( message_id. to_u32 ( ) )
917
+ if let Some ( msg) = msg {
918
+ let mut message = msg. create_message ( & ctx) . await ?;
919
+ let message_id =
920
+ deltachat:: chat:: add_device_msg ( & ctx, Some ( & label) , Some ( & mut message) ) . await ?;
921
+ if !message_id. is_unset ( ) {
922
+ return Ok ( Some ( message_id. to_u32 ( ) ) ) ;
923
+ }
924
+ } else {
925
+ deltachat:: chat:: add_device_msg ( & ctx, Some ( & label) , None ) . await ?;
926
+ }
927
+ Ok ( None )
912
928
}
913
929
914
930
/// Mark all messages in a chat as _noticed_.
@@ -1808,38 +1824,7 @@ impl CommandApi {
1808
1824
1809
1825
async fn send_msg ( & self , account_id : u32 , chat_id : u32 , data : MessageData ) -> Result < u32 > {
1810
1826
let ctx = self . get_context ( account_id) . await ?;
1811
- let mut message = Message :: new ( if let Some ( viewtype) = data. viewtype {
1812
- viewtype. into ( )
1813
- } else if data. file . is_some ( ) {
1814
- Viewtype :: File
1815
- } else {
1816
- Viewtype :: Text
1817
- } ) ;
1818
- message. set_text ( data. text . unwrap_or_default ( ) ) ;
1819
- if data. html . is_some ( ) {
1820
- message. set_html ( data. html ) ;
1821
- }
1822
- if data. override_sender_name . is_some ( ) {
1823
- message. set_override_sender_name ( data. override_sender_name ) ;
1824
- }
1825
- if let Some ( file) = data. file {
1826
- message. set_file ( file, None ) ;
1827
- }
1828
- if let Some ( ( latitude, longitude) ) = data. location {
1829
- message. set_location ( latitude, longitude) ;
1830
- }
1831
- if let Some ( id) = data. quoted_message_id {
1832
- message
1833
- . set_quote (
1834
- & ctx,
1835
- Some (
1836
- & Message :: load_from_db ( & ctx, MsgId :: new ( id) )
1837
- . await
1838
- . context ( "message to quote could not be loaded" ) ?,
1839
- ) ,
1840
- )
1841
- . await ?;
1842
- }
1827
+ let mut message = data. create_message ( & ctx) . await ?;
1843
1828
let msg_id = chat:: send_msg ( & ctx, ChatId :: new ( chat_id) , & mut message)
1844
1829
. await ?
1845
1830
. to_u32 ( ) ;
0 commit comments