From 71b581250ae614f67c7d02796de57142c68de1b2 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:15:38 +0200
Subject: [PATCH 01/28] make NcMessage a tuple struct
---
src/backend/nc_message.rs | 38 ++++++++++++++++----------------------
1 file changed, 16 insertions(+), 22 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index b7ef6a5..8fc99f7 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -2,66 +2,60 @@ use crate::backend::nc_request::NCReqDataMessage;
use chrono::prelude::*;
#[derive(Debug)]
-pub struct NCMessage {
- data: NCReqDataMessage,
- message: String,
-}
+pub struct NCMessage(NCReqDataMessage);
impl From for NCMessage {
fn from(data: NCReqDataMessage) -> Self {
- NCMessage {
- message: data.message.clone(),
- data,
- }
+ NCMessage(data)
}
}
impl NCMessage {
pub fn get_time_str(&self) -> String {
let time: DateTime =
- DateTime::from(DateTime::::from_timestamp(self.data.timestamp, 0).unwrap());
+ DateTime::from(DateTime::::from_timestamp(self.0.timestamp, 0).unwrap());
time.format("%H:%M").to_string()
}
pub fn get_name(&self) -> String {
- self.data.actorDisplayName.clone()
+ self.0.actorDisplayName.clone()
}
- pub fn get_message(&self) -> String {
- self.message.clone()
+ pub fn get_message(&self) -> &str {
+ &self.0.message
}
pub fn get_reactions_str(&self) -> String {
let mut reactions = String::new();
- for (icon, number) in &self.data.reactions {
+ for (icon, number) in &self.0.reactions {
reactions = reactions + "('" + icon + "' times " + &number.to_string() + "), ";
}
reactions
}
pub fn get_id(&self) -> i32 {
- self.data.id
+ self.0.id
}
pub fn to_data(&self) -> NCReqDataMessage {
- self.data.clone()
+ self.0.clone()
}
pub fn is_comment(&self) -> bool {
- self.data.messageType == "comment"
+ self.0.messageType == "comment"
}
pub fn is_comment_deleted(&self) -> bool {
- self.data.messageType == "comment_deleted"
+ self.0.messageType == "comment_deleted"
}
pub fn is_system(&self) -> bool {
- self.data.messageType == "system"
+ self.0.messageType == "system"
}
pub fn is_edit_note(&self) -> bool {
if self.is_system() {
- self.data.systemMessage == "message_edited"
+ self.0.systemMessage == "message_edited"
} else {
false
}
@@ -69,17 +63,17 @@ impl NCMessage {
pub fn is_reaction(&self) -> bool {
if self.is_system() {
- self.data.systemMessage == "reaction"
+ self.0.systemMessage == "reaction"
} else {
false
}
}
pub fn is_command(&self) -> bool {
- self.data.messageType == "command"
+ self.0.messageType == "command"
}
pub fn has_reactions(&self) -> bool {
- !self.data.reactions.is_empty()
+ !self.0.reactions.is_empty()
}
}
From 528dde9b27b77d8c1cdf7f6803277600783fed68 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:20:36 +0200
Subject: [PATCH 02/28] remove mut and for
---
src/backend/nc_message.rs | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index 8fc99f7..b06d2ea 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -26,11 +26,14 @@ impl NCMessage {
}
pub fn get_reactions_str(&self) -> String {
- let mut reactions = String::new();
- for (icon, number) in &self.0.reactions {
- reactions = reactions + "('" + icon + "' times " + &number.to_string() + "), ";
- }
- reactions
+ self.0
+ .reactions
+ .iter()
+ .map(|(icon, number)| format!("('{icon}' times {}), ", &number.to_string()))
+ .collect::>()
+ .join(", ")
+ // TODO: this was in previous code! is this comma at the end needed?
+ + ", "
}
pub fn get_id(&self) -> i32 {
From 2029decabd589c0aec08f5909beca6f1c73c856f Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:22:11 +0200
Subject: [PATCH 03/28] remove some redundant ifs
---
src/backend/nc_message.rs | 12 ++----------
1 file changed, 2 insertions(+), 10 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index b06d2ea..b449968 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -57,19 +57,11 @@ impl NCMessage {
}
pub fn is_edit_note(&self) -> bool {
- if self.is_system() {
- self.0.systemMessage == "message_edited"
- } else {
- false
- }
+ self.is_system() && self.0.systemMessage == "message_edited"
}
pub fn is_reaction(&self) -> bool {
- if self.is_system() {
- self.0.systemMessage == "reaction"
- } else {
- false
- }
+ self.is_system() && self.0.systemMessage == "reaction"
}
pub fn is_command(&self) -> bool {
From bc3b7bed72e79ab84d3d327bb4fe9949021e44ad Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:23:49 +0200
Subject: [PATCH 04/28] remove unwrap with expect to give a hint in case
---
src/backend/nc_message.rs | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index b449968..fb3db91 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -12,8 +12,10 @@ impl From for NCMessage {
impl NCMessage {
pub fn get_time_str(&self) -> String {
- let time: DateTime =
- DateTime::from(DateTime::::from_timestamp(self.0.timestamp, 0).unwrap());
+ let time: DateTime = DateTime::from(
+ DateTime::::from_timestamp(self.0.timestamp, 0)
+ .expect("cannot convert UTC time stamp"),
+ );
time.format("%H:%M").to_string()
}
From 9bdd7e92b3bb992caa05f8aff7edc8d6f6f2e7c5 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:35:10 +0200
Subject: [PATCH 05/28] remove mut and for and rename to_data()
---
src/backend/nc_message.rs | 2 +-
src/backend/nc_room.rs | 5 +----
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index fb3db91..27e46a2 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -42,7 +42,7 @@ impl NCMessage {
self.0.id
}
- pub fn to_data(&self) -> NCReqDataMessage {
+ pub fn clone_data(&self) -> NCReqDataMessage {
self.0.clone()
}
diff --git a/src/backend/nc_room.rs b/src/backend/nc_room.rs
index a781e7b..438185f 100644
--- a/src/backend/nc_room.rs
+++ b/src/backend/nc_room.rs
@@ -230,11 +230,8 @@ impl NCRoom {
}
pub fn write_to_log(&mut self) -> Result<(), std::io::Error> {
- let mut data = Vec::::new();
+ let data: Vec<_> = self.messages.iter().map(NCMessage::clone_data).collect();
let path = self.path_to_log.as_path();
- for message in &mut self.messages {
- data.push(message.to_data());
- }
// Open a file in write-only mode, returns `io::Result`
let mut file = match File::create(path) {
Err(why) => {
From 0ab02c5105a11168c81feee1a044342be0a9d83e Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:37:10 +0200
Subject: [PATCH 06/28] return reference
---
src/backend/nc_message.rs | 4 ++--
src/ui/chat_box.rs | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/backend/nc_message.rs b/src/backend/nc_message.rs
index 27e46a2..a0d89c7 100644
--- a/src/backend/nc_message.rs
+++ b/src/backend/nc_message.rs
@@ -19,8 +19,8 @@ impl NCMessage {
time.format("%H:%M").to_string()
}
- pub fn get_name(&self) -> String {
- self.0.actorDisplayName.clone()
+ pub fn get_name(&self) -> &str {
+ &self.0.actorDisplayName
}
pub fn get_message(&self) -> &str {
diff --git a/src/ui/chat_box.rs b/src/ui/chat_box.rs
index 45250c1..d95ea33 100644
--- a/src/ui/chat_box.rs
+++ b/src/ui/chat_box.rs
@@ -46,7 +46,7 @@ impl<'a> ChatBox<'a> {
})
{
let name = textwrap::wrap(
- &message_data.get_name(),
+ message_data.get_name(),
Options::new(NAME_WIDTH.into()).break_words(true),
)
.into_iter()
From eafce25bfc8391859f46fe80987ccbacc5d73310 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:38:18 +0200
Subject: [PATCH 07/28] use derive Default
---
src/backend/nc_notify.rs | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/src/backend/nc_notify.rs b/src/backend/nc_notify.rs
index e8c878f..29611d4 100644
--- a/src/backend/nc_notify.rs
+++ b/src/backend/nc_notify.rs
@@ -1,7 +1,7 @@
use crate::config::{self};
use notify_rust::{Hint, Notification, Timeout};
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Default)]
pub struct NCNotify {
app_name: String,
timeout_ms: u32,
@@ -64,9 +64,3 @@ impl NCNotify {
Ok(())
}
}
-
-impl Default for NCNotify {
- fn default() -> Self {
- Self::new()
- }
-}
From 37333ab95941d341fd61b293b29d0fc1e4d6d0e1 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:43:48 +0200
Subject: [PATCH 08/28] improve readability
---
src/backend/nc_request.rs | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/src/backend/nc_request.rs b/src/backend/nc_request.rs
index 73a2861..a0e77c7 100644
--- a/src/backend/nc_request.rs
+++ b/src/backend/nc_request.rs
@@ -245,10 +245,14 @@ pub struct NCReqOCS {
impl NCRequest {
pub fn new() -> Result> {
- let username = config::get().data.general.user.clone();
- let password = Some(config::get().data.general.app_pw.clone());
- let base_url = config::get().data.general.url.clone();
- let json_dump_path = config::get().get_http_dump_dir();
+ let config = &config::get();
+ let general = &config.data.general;
+
+ let username = general.user.clone();
+ let password = Some(general.app_pw.clone());
+ let base_url = general.url.clone();
+
+ let json_dump_path = config.get_http_dump_dir();
let mut headers = header::HeaderMap::new();
headers.insert("OCS-APIRequest", header::HeaderValue::from_static("true"));
headers.insert(
@@ -259,9 +263,9 @@ impl NCRequest {
let mut buf = b"Basic ".to_vec();
{
let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD);
- let _ = write!(encoder, "{username}:");
+ write!(encoder, "{username}:").expect("i/o error");
if let Some(password) = password {
- let _ = write!(encoder, "{password}");
+ write!(encoder, "{password}").expect("i/o error");
}
}
let mut auth_value =
From 11fda13b565decd037fa74f6d2c704758cfe8927 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 16:56:04 +0200
Subject: [PATCH 09/28] unify reqwest usage
---
src/backend/nc_request.rs | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/src/backend/nc_request.rs b/src/backend/nc_request.rs
index a0e77c7..96548c7 100644
--- a/src/backend/nc_request.rs
+++ b/src/backend/nc_request.rs
@@ -4,8 +4,10 @@
use base64::{prelude::BASE64_STANDARD, write::EncoderWriter};
use jzon;
-use reqwest::header::HeaderMap;
-use reqwest::{header, Client, Response, Url};
+use reqwest::{
+ header::{HeaderMap, HeaderValue, AUTHORIZATION},
+ Client, Response, Url,
+};
use serde::{Deserialize, Deserializer, Serialize};
use std::path::PathBuf;
use std::{collections::HashMap, error::Error, fs::File, io::Write};
@@ -253,12 +255,9 @@ impl NCRequest {
let base_url = general.url.clone();
let json_dump_path = config.get_http_dump_dir();
- let mut headers = header::HeaderMap::new();
- headers.insert("OCS-APIRequest", header::HeaderValue::from_static("true"));
- headers.insert(
- "Accept",
- header::HeaderValue::from_static("application/json"),
- );
+ let mut headers = HeaderMap::new();
+ headers.insert("OCS-APIRequest", HeaderValue::from_static("true"));
+ headers.insert("Accept", HeaderValue::from_static("application/json"));
let mut buf = b"Basic ".to_vec();
{
@@ -269,9 +268,9 @@ impl NCRequest {
}
}
let mut auth_value =
- header::HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue");
+ HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue");
auth_value.set_sensitive(true);
- headers.insert(header::AUTHORIZATION, auth_value);
+ headers.insert(AUTHORIZATION, auth_value);
// get a client builder
let client = reqwest::Client::builder()
From 69d92237ba9b7814b2e029b15f3fc87f0e7da039 Mon Sep 17 00:00:00 2001
From: Patrick Hoffmann
Date: Wed, 28 Aug 2024 17:13:39 +0200
Subject: [PATCH 10/28] remove further mutables
---
src/backend/nc_request.rs | 53 +++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 25 deletions(-)
diff --git a/src/backend/nc_request.rs b/src/backend/nc_request.rs
index 96548c7..d7611de 100644
--- a/src/backend/nc_request.rs
+++ b/src/backend/nc_request.rs
@@ -291,8 +291,7 @@ impl NCRequest {
token: &str,
) -> Result> {
let url_string = self.base_url.clone() + "/ocs/v2.php/apps/spreed/api/v1/chat/" + token;
- let mut params = HashMap::new();
- params.insert("message".to_owned(), message.clone());
+ let params = HashMap::from([("message", message)]);
let url = Url::parse_with_params(&url_string, params)?;
let response = self.request_post(url).await?;
@@ -316,13 +315,10 @@ impl NCRequest {
name: &str,
) -> Result, Box> {
let url_string = self.base_url.clone() + "/ocs/v2.php/core/autocomplete/get";
- let mut params = HashMap::new();
- params.insert("limit".to_owned(), "200".to_string());
- params.insert("search".to_owned(), name.to_string());
-
+ let params = HashMap::from([("limit", "200"), ("search", name)]);
let url = Url::parse_with_params(&url_string, params)?;
-
let response = self.request(url).await?;
+
match response.status() {
reqwest::StatusCode::OK => {
let text = response.text().await?;
@@ -352,8 +348,7 @@ impl NCRequest {
+ "/ocs/v2.php/apps/spreed/api/v4/room/"
+ token
+ "/participants";
- let mut params = HashMap::new();
- params.insert("includeStatus".to_owned(), "true".to_string());
+ let params = HashMap::from([("includeStatus", "true")]);
let url = Url::parse_with_params(&url_string, params)?;
let response = self.request(url).await?;
@@ -394,10 +389,11 @@ impl NCRequest {
last_timestamp: Option,
) -> Result<(Vec, i64), Box> {
let url_string = self.base_url.clone() + "/ocs/v2.php/apps/spreed/api/v4/room";
- let mut params = HashMap::new();
- if let Some(timestamp) = last_timestamp {
- params.insert("modifiedSince".to_owned(), timestamp.to_string());
- }
+ let params = if let Some(timestamp) = last_timestamp {
+ HashMap::from([("modifiedSince", timestamp.to_string())])
+ } else {
+ HashMap::new()
+ };
let url = Url::parse_with_params(&url_string, ¶ms)?;
let response = self.request(url).await?;
match response.status() {
@@ -466,18 +462,23 @@ impl NCRequest {
last_message: Option,
) -> Result