2 次代码提交 c0aab7dce5 ... f81b6e98b8

作者 SHA1 备注 提交日期
  Slava Barinov f81b6e98b8 [Telegram] Prevent empty account from adding 2 年之前
  Slava Barinov cffd470839 [Telegram] Sort version in keyboard for SubCategory 2 年之前
共有 1 个文件被更改,包括 22 次插入13 次删除
  1. 22 13
      src/telegram.rs

+ 22 - 13
src/telegram.rs

@@ -132,21 +132,28 @@ async fn command_handler(
                 .await?
         }
         Command::NewAccount { account } => {
-            let (response_tx, response_rx) = oneshot::channel();
-
-            tx.send(TgManagerCommand::Get {
-                user_id: msg.chat.id.0,
-                reply_to: response_tx,
-            })
-            .await?;
+            let acc_to_add = account.trim();
 
-            if let Ok(mut user) = response_rx.await {
-                user.new_account(account);
-                bot.send_message(msg.chat.id, "Account added".to_string())
+            if acc_to_add.is_empty() {
+                bot.send_message(msg.chat.id, "Please enter new account name".to_string())
                     .await?
             } else {
-                bot.send_message(msg.chat.id, "Can't find the requested user".to_string())
-                    .await?
+                let (response_tx, response_rx) = oneshot::channel();
+
+                tx.send(TgManagerCommand::Get {
+                    user_id: msg.chat.id.0,
+                    reply_to: response_tx,
+                })
+                .await?;
+
+                if let Ok(mut user) = response_rx.await {
+                    user.new_account(String::from(acc_to_add));
+                    bot.send_message(msg.chat.id, "Account added".to_string())
+                        .await?
+                } else {
+                    bot.send_message(msg.chat.id, "Can't find the requested user".to_string())
+                        .await?
+                }
             }
         }
         Command::Accounts => {
@@ -441,7 +448,7 @@ async fn handle_category(
         ))
     })?;
 
-    let accounts = user
+    let mut accounts = user
         .accounts
         .iter()
         .filter(|&e| {
@@ -449,6 +456,8 @@ async fn handle_category(
         })
         .collect::<Vec<_>>();
 
+    accounts.sort_unstable();
+
     if accounts.is_empty() {
         bot.send_message(msg.chat.id, format!("Input subcategory for {}", item))
             .await?;