mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-22 02:54:21 +00:00
Move TelegramWrite
impl for Diario
to its own file
This commit is contained in:
parent
7b81649c56
commit
020cab782b
3 changed files with 43 additions and 41 deletions
|
@ -1,4 +1,3 @@
|
||||||
use std::fmt::{Error, Write};
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
@ -9,11 +8,10 @@ use teloxide::prelude::Requester;
|
||||||
use teloxide::types::{Message, ParseMode, ReplyParameters};
|
use teloxide::types::{Message, ParseMode, ReplyParameters};
|
||||||
use teloxide::Bot;
|
use teloxide::Bot;
|
||||||
|
|
||||||
use crate::interfaces::database::models::Diario;
|
|
||||||
use crate::interfaces::database::models::RoyalnetUser;
|
use crate::interfaces::database::models::RoyalnetUser;
|
||||||
use crate::services::telegram::commands::CommandResult;
|
use crate::services::telegram::commands::CommandResult;
|
||||||
use crate::services::telegram::dependencies::interface_database::DatabaseInterface;
|
use crate::services::telegram::dependencies::interface_database::DatabaseInterface;
|
||||||
use crate::utils::telegram_string::{TelegramEscape, TelegramWrite};
|
use crate::utils::telegram_string::TelegramWrite;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct DiarioArgs {
|
pub struct DiarioArgs {
|
||||||
|
@ -65,43 +63,6 @@ impl FromStr for DiarioArgs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TelegramWrite for Diario {
|
|
||||||
fn write_telegram<T>(&self, f: &mut T) -> Result<(), Error>
|
|
||||||
where
|
|
||||||
T: Write,
|
|
||||||
{
|
|
||||||
// Diario ID
|
|
||||||
write!(f, "<code>#{}</code>", self.id)?;
|
|
||||||
|
|
||||||
// Optional content warning
|
|
||||||
if let Some(warning) = self.to_owned().warning {
|
|
||||||
write!(f, ", <b>{}</b>", warning.escape_telegram_html())?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Newline
|
|
||||||
writeln!(f)?;
|
|
||||||
|
|
||||||
// Quote optionally covered by a spoiler tag
|
|
||||||
match self.warning.to_owned() {
|
|
||||||
None => write!(f, "<blockquote expandable>{}</blockquote>", self.clone().quote.escape_telegram_html())?,
|
|
||||||
Some(_) => write!(f, "<blockquote expandable><tg-spoiler>{}</tg-spoiler></blockquote>", self.clone().quote.escape_telegram_html())?,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Newline
|
|
||||||
writeln!(f)?;
|
|
||||||
|
|
||||||
// Optional citation with optional context
|
|
||||||
match (self.quoted_name.to_owned(), self.context.to_owned()) {
|
|
||||||
(Some(name), Some(context)) => write!(f, "—{}, <i>{}</i>", name.escape_telegram_html(), context.escape_telegram_html())?,
|
|
||||||
(Some(name), None) => write!(f, "—{}", name.escape_telegram_html())?,
|
|
||||||
(None, Some(context)) => write!(f, "...<i>{}</i>", context.escape_telegram_html())?,
|
|
||||||
(None, None) => write!(f, "")?,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handler(bot: &Bot, message: &Message, args: &DiarioArgs, database: &DatabaseInterface) -> CommandResult {
|
pub async fn handler(bot: &Bot, message: &Message, args: &DiarioArgs, database: &DatabaseInterface) -> CommandResult {
|
||||||
let author = message.from.as_ref()
|
let author = message.from.as_ref()
|
||||||
.context("Non è stato possibile determinare chi abbia inviato il comando.")?;
|
.context("Non è stato possibile determinare chi abbia inviato il comando.")?;
|
||||||
|
|
40
src/services/telegram/utils/diario.rs
Normal file
40
src/services/telegram/utils/diario.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use crate::interfaces::database::models::Diario;
|
||||||
|
use crate::utils::telegram_string::{TelegramEscape, TelegramWrite};
|
||||||
|
use std::fmt::{Error, Write};
|
||||||
|
|
||||||
|
impl TelegramWrite for Diario {
|
||||||
|
fn write_telegram<T>(&self, f: &mut T) -> Result<(), Error>
|
||||||
|
where
|
||||||
|
T: Write,
|
||||||
|
{
|
||||||
|
// Diario ID
|
||||||
|
write!(f, "<code>#{}</code>", self.id)?;
|
||||||
|
|
||||||
|
// Optional content warning
|
||||||
|
if let Some(warning) = self.to_owned().warning {
|
||||||
|
write!(f, ", <b>{}</b>", warning.escape_telegram_html())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Newline
|
||||||
|
writeln!(f)?;
|
||||||
|
|
||||||
|
// Quote optionally covered by a spoiler tag
|
||||||
|
match self.warning.to_owned() {
|
||||||
|
None => write!(f, "<blockquote expandable>{}</blockquote>", self.clone().quote.escape_telegram_html())?,
|
||||||
|
Some(_) => write!(f, "<blockquote expandable><tg-spoiler>{}</tg-spoiler></blockquote>", self.clone().quote.escape_telegram_html())?,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Newline
|
||||||
|
writeln!(f)?;
|
||||||
|
|
||||||
|
// Optional citation with optional context
|
||||||
|
match (self.quoted_name.to_owned(), self.context.to_owned()) {
|
||||||
|
(Some(name), Some(context)) => write!(f, "—{}, <i>{}</i>", name.escape_telegram_html(), context.escape_telegram_html())?,
|
||||||
|
(Some(name), None) => write!(f, "—{}", name.escape_telegram_html())?,
|
||||||
|
(None, Some(context)) => write!(f, "...<i>{}</i>", context.escape_telegram_html())?,
|
||||||
|
(None, None) => write!(f, "")?,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod matchmaking;
|
pub mod matchmaking;
|
||||||
|
pub mod diario;
|
Loading…
Reference in a new issue