mirror of
https://github.com/chatmail/core.git
synced 2026-05-22 16:26:31 +03:00
add dc_msg_set_override_sender_name() api
with mailinglists, we already receive and handle per-message-names, this api allows this also eg. for bots based on the deltachat api.
This commit is contained in:
@@ -3816,6 +3816,21 @@ void dc_msg_set_text (dc_msg_t* msg, const char* text);
|
|||||||
void dc_msg_set_html (dc_msg_t* msg, const char* html);
|
void dc_msg_set_html (dc_msg_t* msg, const char* html);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set different sender name for a message.
|
||||||
|
* This overrides the name set by the dc_set_config()-option `displayname`.
|
||||||
|
*
|
||||||
|
* Usually, this function is not needed
|
||||||
|
* when implementing pure messaging functions.
|
||||||
|
* However, it might be useful for bots eg. building bridges to other networks.
|
||||||
|
*
|
||||||
|
* @memberof dc_msg_t
|
||||||
|
* @param msg The message object.
|
||||||
|
* @param name The name to send along with the message.
|
||||||
|
*/
|
||||||
|
void dc_msg_set_override_sender_name(dc_msg_t* msg, const char* name);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the file associated with a message object.
|
* Set the file associated with a message object.
|
||||||
* This does not alter any information in the database
|
* This does not alter any information in the database
|
||||||
|
|||||||
@@ -3028,6 +3028,21 @@ pub unsafe extern "C" fn dc_msg_set_html(msg: *mut dc_msg_t, html: *const libc::
|
|||||||
ffi_msg.message.set_html(to_opt_string_lossy(html))
|
ffi_msg.message.set_html(to_opt_string_lossy(html))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn dc_msg_set_override_sender_name(
|
||||||
|
msg: *mut dc_msg_t,
|
||||||
|
name: *const libc::c_char,
|
||||||
|
) {
|
||||||
|
if msg.is_null() {
|
||||||
|
eprintln!("ignoring careless call to dc_msg_set_override_sender_name()");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let ffi_msg = &mut *msg;
|
||||||
|
ffi_msg
|
||||||
|
.message
|
||||||
|
.set_override_sender_name(to_opt_string_lossy(name))
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_msg_set_file(
|
pub unsafe extern "C" fn dc_msg_set_file(
|
||||||
msg: *mut dc_msg_t,
|
msg: *mut dc_msg_t,
|
||||||
|
|||||||
@@ -806,6 +806,16 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set different sender name for a message.
|
||||||
|
/// This overrides the name set by the `set_config()`-option `displayname`.
|
||||||
|
pub fn set_override_sender_name(&mut self, name: Option<String>) {
|
||||||
|
if let Some(name) = name {
|
||||||
|
self.param.set(Param::OverrideSenderDisplayname, name);
|
||||||
|
} else {
|
||||||
|
self.param.remove(Param::OverrideSenderDisplayname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_dimension(&mut self, width: i32, height: i32) {
|
pub fn set_dimension(&mut self, width: i32, height: i32) {
|
||||||
self.param.set_int(Param::Width, width);
|
self.param.set_int(Param::Width, width);
|
||||||
self.param.set_int(Param::Height, height);
|
self.param.set_int(Param::Height, height);
|
||||||
@@ -2526,4 +2536,45 @@ mod tests {
|
|||||||
assert!(!msg.get_real_chat_id().is_special());
|
assert!(!msg.get_real_chat_id().is_special());
|
||||||
assert_eq!(msg.get_text().unwrap(), "hello".to_string());
|
assert_eq!(msg.get_text().unwrap(), "hello".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn test_set_override_sender_name() {
|
||||||
|
// send message with overridden sender name
|
||||||
|
let alice = TestContext::new_alice().await;
|
||||||
|
let bob = TestContext::new_bob().await;
|
||||||
|
let chat = alice.create_chat(&bob).await;
|
||||||
|
let contact_id = *chat::get_chat_contacts(&alice, chat.id)
|
||||||
|
.await
|
||||||
|
.first()
|
||||||
|
.unwrap();
|
||||||
|
let contact = Contact::load_from_db(&alice, contact_id).await.unwrap();
|
||||||
|
|
||||||
|
let mut msg = Message::new(Viewtype::Text);
|
||||||
|
msg.set_text(Some("bla blubb".to_string()));
|
||||||
|
msg.set_override_sender_name(Some("over ride".to_string()));
|
||||||
|
assert_eq!(
|
||||||
|
msg.get_override_sender_name(),
|
||||||
|
Some("over ride".to_string())
|
||||||
|
);
|
||||||
|
assert_eq!(msg.get_sender_name(&contact), "over ride".to_string());
|
||||||
|
assert_ne!(contact.get_display_name(), "over ride".to_string());
|
||||||
|
chat::send_msg(&alice, chat.id, &mut msg).await.unwrap();
|
||||||
|
|
||||||
|
// bob receives that message
|
||||||
|
let chat = bob.create_chat(&alice).await;
|
||||||
|
let contact_id = *chat::get_chat_contacts(&bob, chat.id)
|
||||||
|
.await
|
||||||
|
.first()
|
||||||
|
.unwrap();
|
||||||
|
let contact = Contact::load_from_db(&bob, contact_id).await.unwrap();
|
||||||
|
bob.recv_msg(&alice.pop_sent_msg().await).await;
|
||||||
|
let msg = bob.get_last_msg_in(chat.id).await;
|
||||||
|
assert_eq!(msg.text, Some("bla blubb".to_string()));
|
||||||
|
assert_eq!(
|
||||||
|
msg.get_override_sender_name(),
|
||||||
|
Some("over ride".to_string())
|
||||||
|
);
|
||||||
|
assert_eq!(msg.get_sender_name(&contact), "over ride".to_string());
|
||||||
|
assert_ne!(contact.get_display_name(), "over ride".to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ pub enum Param {
|
|||||||
File = b'f',
|
File = b'f',
|
||||||
|
|
||||||
/// For messages: This name should be shown instead of contact.get_display_name()
|
/// For messages: This name should be shown instead of contact.get_display_name()
|
||||||
/// (used if this is a mailinglist)
|
/// (used if this is a mailinglist
|
||||||
|
/// or explictly set using set_override_sender_name(), eg. by bots)
|
||||||
OverrideSenderDisplayname = b'O',
|
OverrideSenderDisplayname = b'O',
|
||||||
|
|
||||||
/// For Messages
|
/// For Messages
|
||||||
|
|||||||
Reference in New Issue
Block a user