mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
Remove newlines from group names, chat names and the displayname (#1845)
This commit is contained in:
14
src/chat.rs
14
src/chat.rs
@@ -1952,7 +1952,8 @@ pub async fn create_group_chat(
|
||||
verified: VerifiedStatus,
|
||||
chat_name: impl AsRef<str>,
|
||||
) -> Result<ChatId, Error> {
|
||||
ensure!(!chat_name.as_ref().is_empty(), "Invalid chat name");
|
||||
let chat_name = improve_single_line_input(chat_name);
|
||||
ensure!(!chat_name.is_empty(), "Invalid chat name");
|
||||
|
||||
let draft_txt = context
|
||||
.stock_string_repl_str(StockMessage::NewGroupDraft, &chat_name)
|
||||
@@ -1967,7 +1968,7 @@ pub async fn create_group_chat(
|
||||
} else {
|
||||
Chattype::Group
|
||||
},
|
||||
chat_name.as_ref().to_string(),
|
||||
chat_name,
|
||||
grpid,
|
||||
time(),
|
||||
],
|
||||
@@ -2432,17 +2433,18 @@ pub async fn set_chat_name(
|
||||
chat_id: ChatId,
|
||||
new_name: impl AsRef<str>,
|
||||
) -> Result<(), Error> {
|
||||
let new_name = improve_single_line_input(new_name);
|
||||
/* the function only sets the names of group chats; normal chats get their names from the contacts */
|
||||
let mut success = false;
|
||||
|
||||
ensure!(!new_name.as_ref().is_empty(), "Invalid name");
|
||||
ensure!(!new_name.is_empty(), "Invalid name");
|
||||
ensure!(!chat_id.is_special(), "Invalid chat ID");
|
||||
|
||||
let chat = Chat::load_from_db(context, chat_id).await?;
|
||||
let mut msg = Message::default();
|
||||
|
||||
if real_group_exists(context, chat_id).await {
|
||||
if chat.name == new_name.as_ref() {
|
||||
if chat.name == new_name {
|
||||
success = true;
|
||||
} else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
|
||||
emit_event!(
|
||||
@@ -2455,7 +2457,7 @@ pub async fn set_chat_name(
|
||||
.sql
|
||||
.execute(
|
||||
"UPDATE chats SET name=? WHERE id=?;",
|
||||
paramsv![new_name.as_ref().to_string(), chat_id],
|
||||
paramsv![new_name.to_string(), chat_id],
|
||||
)
|
||||
.await
|
||||
.is_ok()
|
||||
@@ -2467,7 +2469,7 @@ pub async fn set_chat_name(
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgGrpName,
|
||||
&chat.name,
|
||||
new_name.as_ref(),
|
||||
&new_name,
|
||||
DC_CONTACT_ID_SELF,
|
||||
)
|
||||
.await,
|
||||
|
||||
@@ -232,6 +232,10 @@ impl Context {
|
||||
});
|
||||
ret
|
||||
}
|
||||
Config::Displayname => {
|
||||
let value = value.map(improve_single_line_input);
|
||||
self.sql.set_raw_config(self, key, value.as_deref()).await
|
||||
}
|
||||
_ => self.sql.set_raw_config(self, key, value).await,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,6 +235,7 @@ impl Contact {
|
||||
name: impl AsRef<str>,
|
||||
addr: impl AsRef<str>,
|
||||
) -> Result<u32> {
|
||||
let name = improve_single_line_input(name);
|
||||
ensure!(
|
||||
!addr.as_ref().is_empty(),
|
||||
"Cannot create contact with empty address"
|
||||
|
||||
@@ -612,6 +612,16 @@ pub(crate) fn listflags_has(listflags: u32, bitindex: usize) -> bool {
|
||||
(listflags & bitindex) == bitindex
|
||||
}
|
||||
|
||||
/// Makes sure that a user input that is not supposed to contain newlines does not contain newlines.
|
||||
pub(crate) fn improve_single_line_input(input: impl AsRef<str>) -> String {
|
||||
input
|
||||
.as_ref()
|
||||
.replace("\n", " ")
|
||||
.replace("\r", " ")
|
||||
.trim()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -960,4 +970,10 @@ mod tests {
|
||||
assert_eq!(w, 100);
|
||||
assert_eq!(h, 50);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_improve_single_line_input() {
|
||||
assert_eq!(improve_single_line_input("Hi\naiae "), "Hi aiae");
|
||||
assert_eq!(improve_single_line_input("\r\nahte\n\r"), "ahte");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user