Skip to content

Commit

Permalink
Merge pull request #19 from tofubert/pat/review-backend
Browse files Browse the repository at this point in the history
Pat/review backend and UI
  • Loading branch information
fightling authored Aug 29, 2024
2 parents 3132f05 + 103163c commit 383dcd4
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 231 deletions.
77 changes: 40 additions & 37 deletions src/backend/nc_message.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,88 @@
use crate::backend::nc_request::NCReqDataMessage;
use chrono::prelude::*;

/// `NextCloud` message interface
#[derive(Debug)]
pub struct NCMessage {
data: NCReqDataMessage,
message: String,
}
pub struct NCMessage(NCReqDataMessage);

impl From<NCReqDataMessage> for NCMessage {
fn from(data: NCReqDataMessage) -> Self {
NCMessage {
message: data.message.clone(),
data,
}
NCMessage(data)
}
}

impl NCMessage {
/// return message time stamp as string
pub fn get_time_str(&self) -> String {
let time: DateTime<Local> =
DateTime::from(DateTime::<Utc>::from_timestamp(self.data.timestamp, 0).unwrap());
let time: DateTime<Local> = DateTime::from(
DateTime::<Utc>::from_timestamp(self.0.timestamp, 0)
.expect("cannot convert UTC time stamp"),
);
time.format("%H:%M").to_string()
}

pub fn get_name(&self) -> String {
self.data.actorDisplayName.clone()
/// return opponent display name
pub fn get_name(&self) -> &str {
&self.0.actorDisplayName
}

pub fn get_message(&self) -> String {
self.message.clone()
/// return the message itself
pub fn get_message(&self) -> &str {
&self.0.message
}

/// get list of reactions as comma separated string
pub fn get_reactions_str(&self) -> String {
let mut reactions = String::new();
for (icon, number) in &self.data.reactions {
reactions = reactions + "('" + icon + "' times " + &number.to_string() + "), ";
}
reactions
self.0
.reactions
.iter()
.map(|(icon, number)| format!("('{icon}' times {}), ", &number.to_string()))
.collect::<Vec<_>>()
.join(", ")
}

/// get message identifier
pub fn get_id(&self) -> i32 {
self.data.id
self.0.id
}

pub fn to_data(&self) -> NCReqDataMessage {
self.data.clone()
/// return inner data message
pub fn data(&self) -> &NCReqDataMessage {
&self.0
}

/// return `true` if message is a comment
pub fn is_comment(&self) -> bool {
self.data.messageType == "comment"
self.0.messageType == "comment"
}

/// return `true` if message is a deleted comment
pub fn is_comment_deleted(&self) -> bool {
self.data.messageType == "comment_deleted"
self.0.messageType == "comment_deleted"
}

/// return `true` if message is a system message
pub fn is_system(&self) -> bool {
self.data.messageType == "system"
self.0.messageType == "system"
}

/// return `true` if message is an edited message
pub fn is_edit_note(&self) -> bool {
if self.is_system() {
self.data.systemMessage == "message_edited"
} else {
false
}
self.is_system() && self.0.systemMessage == "message_edited"
}

/// return `true` if message is a reaction
pub fn is_reaction(&self) -> bool {
if self.is_system() {
self.data.systemMessage == "reaction"
} else {
false
}
self.is_system() && self.0.systemMessage == "reaction"
}

/// return `true` if message is a command
pub fn is_command(&self) -> bool {
self.data.messageType == "command"
self.0.messageType == "command"
}

/// return `true` if message has any reactions
pub fn has_reactions(&self) -> bool {
!self.data.reactions.is_empty()
!self.0.reactions.is_empty()
}
}
58 changes: 28 additions & 30 deletions src/backend/nc_notify.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
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,
persistent: bool,
timeout: Timeout,
silent: bool,
}

impl NCNotify {
pub fn new() -> Self {
let data = &config::get().data;
NCNotify {
app_name: config::get().data.general.chat_server_name.clone(),
timeout_ms: config::get().data.notifications.timeout_ms,
persistent: config::get().data.notifications.persistent,
silent: config::get().data.notifications.silent,
app_name: data.general.chat_server_name.clone(),
timeout: if data.notifications.persistent {
Timeout::Never
} else {
Timeout::Milliseconds(data.notifications.timeout_ms)
},
silent: data.notifications.silent,
}
}

Expand All @@ -25,19 +28,19 @@ impl NCNotify {
number_of_unread: usize,
) -> Result<(), Box<dyn std::error::Error>> {
let mut notification = Notification::new()
.summary(format!("Unread: {room_name}").as_str())
.body(format!("You have {number_of_unread} new Messages in {room_name}").as_str())
.summary(&format!("Unread: {room_name}"))
.body(&format!(
"You have {number_of_unread} new Messages in {room_name}"
))
.icon("dialog-information")
.appname(self.app_name.as_str())
.appname(&self.app_name)
.to_owned();
if self.persistent {
if self.is_persistent() {
log::debug!("Persistent Message!");
notification
.hint(Hint::Resident(true)) // this is not supported by all implementations
.timeout(Timeout::Never); // this however is
} else {
notification.timeout(Timeout::Milliseconds(self.timeout_ms));
}
notification
.hint(Hint::Resident(self.is_persistent())) // this is not supported by all implementations
.timeout(self.timeout);
notification.hint(Hint::SuppressSound(self.silent));

notification.show()?;
Expand All @@ -46,27 +49,22 @@ impl NCNotify {

pub fn new_room(&self, room_name: &String) -> Result<(), Box<dyn std::error::Error>> {
let mut notification = Notification::new()
.summary(format!("New Room: {room_name}").as_str())
.body(format!("You have been added to a new Room {room_name}").as_str())
.summary(&format!("New Room: {room_name}"))
.body(&format!("You have been added to a new Room {room_name}"))
.icon("dialog-information")
.appname(self.app_name.as_str())
.appname(&self.app_name)
.to_owned();
if self.persistent {
notification
.hint(Hint::Resident(true)) // this is not supported by all implementations
.timeout(Timeout::Never); // this however is
} else {
notification.timeout(Timeout::Milliseconds(self.timeout_ms));
}
notification
.hint(Hint::Resident(self.is_persistent())) // this is not supported by all implementations
.timeout(self.timeout); // this however is
notification.hint(Hint::SuppressSound(self.silent));

notification.show()?;
Ok(())
}
}

impl Default for NCNotify {
fn default() -> Self {
Self::new()
/// return `true` if notification is persistent (has infinite display timeout)
pub fn is_persistent(&self) -> bool {
self.timeout == Timeout::Never
}
}
Loading

0 comments on commit 383dcd4

Please sign in to comment.