2023-06-29 14:33:26 +00:00
|
|
|
use crate::prelude::*;
|
2024-02-03 13:21:16 +00:00
|
|
|
use futures::TryFutureExt;
|
2023-06-23 08:45:37 +00:00
|
|
|
use itertools::Itertools;
|
2024-02-10 16:28:16 +00:00
|
|
|
use std::{borrow::Cow, fmt::Write, path::Path};
|
2023-07-20 20:28:26 +00:00
|
|
|
use teloxide::{
|
|
|
|
net::Download,
|
|
|
|
types::{Document, FileMeta},
|
|
|
|
};
|
2023-07-20 20:58:29 +00:00
|
|
|
use tokio::{task::spawn_blocking, try_join};
|
2023-06-23 08:45:37 +00:00
|
|
|
use trim_in_place::TrimInPlace;
|
|
|
|
|
|
|
|
#[inline]
|
2023-07-20 20:28:26 +00:00
|
|
|
fn validate_document(document: Option<&Document>) -> Result<&Document, &'static str> {
|
2023-11-16 18:51:46 +00:00
|
|
|
let Some(document) = document else {
|
|
|
|
return Err("You didn't send a file. Try again");
|
2023-07-20 20:28:26 +00:00
|
|
|
};
|
|
|
|
|
2023-07-20 20:42:40 +00:00
|
|
|
if document.file.size > 1024 * 1024 * 200 {
|
|
|
|
return Err("The file is larger than 200 MiB. Try splitting it into multiple files");
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:28:26 +00:00
|
|
|
let name = match document.file_name.as_deref() {
|
|
|
|
Some(name) => Path::new(name.trim_end()),
|
|
|
|
None => return Err("Couldn't get the name of the file. Try sending it again"),
|
|
|
|
};
|
|
|
|
|
|
|
|
match name.extension() {
|
|
|
|
Some(ext) if ext.eq_ignore_ascii_case("json") => Ok(document),
|
|
|
|
_ => Err("Invalid file name. You need to send a json file. Try again"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-02-22 09:44:02 +00:00
|
|
|
async fn download_file(bot: &Throttle<Bot>, file: &FileMeta) -> crate::Result<Box<[u8]>> {
|
|
|
|
let path = bot.get_file(&file.id).await?.path;
|
2023-07-20 20:28:26 +00:00
|
|
|
let mut data = Vec::with_capacity(file.size as usize);
|
2024-02-22 09:44:02 +00:00
|
|
|
let mut stream = bot.download_file_stream(&path);
|
|
|
|
while let Some(bytes) = stream.try_next().await? {
|
|
|
|
data.extend_from_slice(&bytes);
|
|
|
|
}
|
|
|
|
Ok(data.into_boxed_slice())
|
2023-06-23 08:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-06-23 09:36:36 +00:00
|
|
|
fn process_accounts(
|
|
|
|
accounts: &mut [DecryptedAccount],
|
2023-09-04 18:25:43 +00:00
|
|
|
existing_names: ahash::HashSet<String>,
|
2023-06-23 09:36:36 +00:00
|
|
|
) -> crate::Result<Result<(), String>> {
|
2023-06-23 08:45:37 +00:00
|
|
|
for account in accounts.iter_mut() {
|
|
|
|
account.name.trim_in_place();
|
|
|
|
account.login.trim_in_place();
|
|
|
|
account.password.trim_in_place();
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts.sort_unstable_by(|a, b| a.name.cmp(&b.name));
|
|
|
|
|
|
|
|
let mut duplicates = Vec::new();
|
2023-06-23 09:36:36 +00:00
|
|
|
let mut existing = Vec::new();
|
2023-06-23 08:45:37 +00:00
|
|
|
let mut invalid = Vec::new();
|
|
|
|
|
|
|
|
accounts
|
|
|
|
.iter()
|
|
|
|
.dedup_by_with_count(|a, b| a.name == b.name)
|
|
|
|
.for_each(|(count, account)| {
|
2023-06-23 09:36:36 +00:00
|
|
|
let duplicate = count != 1;
|
|
|
|
let exists = existing_names.contains(&account.name);
|
|
|
|
if duplicate {
|
2023-06-23 08:45:37 +00:00
|
|
|
duplicates.push(account.name.as_str());
|
2023-06-23 09:36:36 +00:00
|
|
|
}
|
|
|
|
if exists {
|
2023-11-16 18:51:46 +00:00
|
|
|
existing.push(account.name.as_str());
|
2023-06-23 09:36:36 +00:00
|
|
|
}
|
|
|
|
// If it already exists or if it is a duplicate there's no need to check the account's validity
|
|
|
|
if !duplicate && !exists && !account.validate() {
|
2023-06-23 08:45:37 +00:00
|
|
|
invalid.push(account.name.as_str());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-23 09:36:36 +00:00
|
|
|
drop(existing_names);
|
|
|
|
|
|
|
|
if duplicates.is_empty() && invalid.is_empty() && existing.is_empty() {
|
2023-06-23 08:45:37 +00:00
|
|
|
return Ok(Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut error_text = "Your accounts have the following problems:".to_owned();
|
|
|
|
|
|
|
|
if !duplicates.is_empty() {
|
|
|
|
write!(
|
|
|
|
error_text,
|
|
|
|
"\n\nDuplicate names:\n{:?}",
|
|
|
|
duplicates.into_iter().format("\n")
|
2023-11-16 18:51:46 +00:00
|
|
|
)?;
|
2023-06-23 08:45:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 09:36:36 +00:00
|
|
|
if !existing.is_empty() {
|
|
|
|
write!(
|
|
|
|
error_text,
|
|
|
|
"\n\nAccounts with these names already exist in the database:\n{:?}",
|
|
|
|
existing.into_iter().format("\n")
|
2023-11-16 18:51:46 +00:00
|
|
|
)?;
|
2023-06-23 09:36:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 08:45:37 +00:00
|
|
|
if !invalid.is_empty() {
|
|
|
|
write!(
|
|
|
|
error_text,
|
|
|
|
"\n\nInvalid account fields:\n{:?}",
|
|
|
|
invalid.into_iter().format("\n")
|
2023-11-16 18:51:46 +00:00
|
|
|
)?;
|
2023-06-23 08:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error_text.push_str("\n\nFix these problems and send the file again");
|
|
|
|
|
|
|
|
Ok(Err(error_text))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-02-22 09:44:02 +00:00
|
|
|
fn user_from_bytes(
|
|
|
|
bytes: impl AsRef<[u8]>,
|
2023-09-04 18:25:43 +00:00
|
|
|
existing_names: ahash::HashSet<String>,
|
2023-06-23 09:36:36 +00:00
|
|
|
) -> crate::Result<Result<User, String>> {
|
2024-02-22 09:44:02 +00:00
|
|
|
let mut user: User = serde_json::from_slice(bytes.as_ref())?;
|
|
|
|
drop(bytes);
|
2023-06-23 09:36:36 +00:00
|
|
|
match process_accounts(&mut user.accounts, existing_names)? {
|
2023-06-23 08:45:37 +00:00
|
|
|
Ok(()) => Ok(Ok(user)),
|
|
|
|
Err(error_text) => Ok(Err(error_text)),
|
|
|
|
}
|
|
|
|
}
|
2023-05-27 23:21:50 +00:00
|
|
|
|
2024-02-10 16:28:16 +00:00
|
|
|
#[inline]
|
|
|
|
async fn user_from_document(
|
|
|
|
bot: &Throttle<Bot>,
|
|
|
|
db: &Pool,
|
|
|
|
document: Option<&Document>,
|
|
|
|
user_id: u64,
|
|
|
|
) -> Result<User, Cow<'static, str>> {
|
|
|
|
let (data, existing_names) = {
|
2024-02-22 09:44:02 +00:00
|
|
|
let file = &validate_document(document)?.file;
|
2024-02-11 09:12:14 +00:00
|
|
|
let data = download_file(bot, file).map_err(|_| "Error downloading the file. Try again");
|
2024-02-10 16:28:16 +00:00
|
|
|
|
|
|
|
let existing_names = Account::get_names(user_id, db)
|
|
|
|
.try_collect()
|
2024-02-11 09:12:14 +00:00
|
|
|
.map_err(|_| "Error getting existing account names. Try again");
|
2024-02-10 16:28:16 +00:00
|
|
|
|
|
|
|
try_join!(data, existing_names)?
|
|
|
|
};
|
|
|
|
|
2024-02-22 09:44:02 +00:00
|
|
|
match spawn_blocking(|| user_from_bytes(data, existing_names)).await {
|
|
|
|
Ok(Ok(user)) => user.map_err(Cow::Owned),
|
2024-02-10 16:28:16 +00:00
|
|
|
_ => Err(Cow::Borrowed("Error parsing the json file. Try again")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:51:46 +00:00
|
|
|
/// Function to handle `GetUser` state. It doesn't actually validate anything
|
2024-02-10 16:28:16 +00:00
|
|
|
#[inline]
|
2023-05-27 23:21:50 +00:00
|
|
|
pub async fn get_user(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
2024-02-03 13:23:39 +00:00
|
|
|
db: Pool,
|
2023-05-27 23:21:50 +00:00
|
|
|
dialogue: MainDialogue,
|
2023-11-16 18:51:46 +00:00
|
|
|
handler: PackagedHandler<User>,
|
2023-05-27 23:21:50 +00:00
|
|
|
) -> crate::Result<()> {
|
2023-06-23 09:36:36 +00:00
|
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
2023-07-14 21:07:59 +00:00
|
|
|
|
2023-11-16 18:51:46 +00:00
|
|
|
let mut handler = handler.lock().await;
|
2023-07-14 21:07:59 +00:00
|
|
|
if handler.func.is_none() {
|
|
|
|
let _ = dialogue.exit().await;
|
|
|
|
return Err(HandlerUsed.into());
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:51:46 +00:00
|
|
|
if msg.text().map(str::trim) == Some("/cancel") {
|
2023-05-27 23:21:50 +00:00
|
|
|
dialogue.exit().await?;
|
2023-07-29 07:19:22 +00:00
|
|
|
handler
|
|
|
|
.previous
|
|
|
|
.alter_message(&bot, "Successfully cancelled", deletion_markup(), None)
|
2023-05-27 23:21:50 +00:00
|
|
|
.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:28:16 +00:00
|
|
|
let user = match user_from_document(&bot, &db, msg.document(), user_id).await {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(error_text) => {
|
2023-07-29 07:19:22 +00:00
|
|
|
handler
|
|
|
|
.previous
|
|
|
|
.alter_message(&bot, error_text, None, None)
|
|
|
|
.await?;
|
2023-06-23 08:45:37 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2023-05-27 23:21:50 +00:00
|
|
|
};
|
|
|
|
|
2023-07-25 21:52:12 +00:00
|
|
|
let previous = handler.previous;
|
2023-05-27 23:21:50 +00:00
|
|
|
let func = handler.func.take().unwrap();
|
|
|
|
drop(handler);
|
|
|
|
|
2023-07-25 21:52:12 +00:00
|
|
|
if let Err(err) = func(bot, msg, db, dialogue.clone(), previous, user).await {
|
2023-05-27 23:21:50 +00:00
|
|
|
let _ = dialogue.exit().await;
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|