Skip to content

Commit 119b593

Browse files
authored
feat: implement chat action feedback in messaging service (#78)
* feat: implement chat action feedback in messaging service * fix: simplify send_chat_action call in handle_reply function * feat: add AddSummary struct
1 parent ea69687 commit 119b593

File tree

6 files changed

+298
-199
lines changed

6 files changed

+298
-199
lines changed

src/bot_handler/callbacks/view_labels.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub async fn handle(
1010
repo_id: &str,
1111
page: usize,
1212
from_page: usize,
13+
_query_id: &str,
1314
) -> BotHandlerResult<()> {
1415
let chat_id = ctx.message.chat.id;
1516

src/bot_handler/callbacks/view_repo.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ use crate::{
55
storage::RepoEntity,
66
};
77

8-
pub async fn handle(ctx: Context<'_>, repo_id: &str, from_page: usize) -> BotHandlerResult<()> {
8+
pub async fn handle(
9+
ctx: Context<'_>,
10+
repo_id: &str,
11+
from_page: usize,
12+
_query_id: &str,
13+
) -> BotHandlerResult<()> {
914
let chat_id = ctx.message.chat.id;
1015

1116
// Extract repository name with owner

src/bot_handler/commands/add.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
use std::collections::HashSet;
22

33
use futures::{StreamExt, stream};
4+
use teloxide::types::ChatAction;
45

56
use crate::{
67
bot_handler::{BotHandlerError, BotHandlerResult, CommandState, commands::Context},
78
storage::RepoEntity,
89
};
910

11+
/// A struct to hold the summary of the add operation.
12+
#[derive(Default, Debug, Clone, PartialEq, Eq)]
13+
pub struct AddSummary {
14+
/// Repositories that were successfully added.
15+
pub successfully_added: HashSet<String>,
16+
/// Repositories that were already tracked.
17+
pub already_tracked: HashSet<String>,
18+
/// Repositories that were not found on GitHub.
19+
pub not_found: HashSet<String>,
20+
/// URLs that were invalid.
21+
pub invalid_urls: HashSet<String>,
22+
/// Repositories that failed to be added due to an error.
23+
pub errors: HashSet<(String, String)>,
24+
}
25+
1026
pub async fn handle(ctx: Context<'_>) -> BotHandlerResult<()> {
1127
ctx.handler.messaging_service.prompt_for_repo_input(ctx.message.chat.id).await?;
1228
ctx.dialogue
@@ -25,16 +41,6 @@ enum AddRepoResult {
2541
Error(String, String),
2642
}
2743

28-
// A struct to hold the summary of the add operation.
29-
#[derive(Default)]
30-
struct AddSummary {
31-
successfully_added: HashSet<String>,
32-
already_tracked: HashSet<String>,
33-
not_found: HashSet<String>,
34-
invalid_urls: HashSet<String>,
35-
errors: HashSet<(String, String)>,
36-
}
37-
3844
/// Handle the reply message when we're waiting for repository input.
3945
/// It processes the input, checks each URL, and adds the repositories
4046
/// accordingly.
@@ -54,6 +60,14 @@ pub async fn handle_reply(ctx: Context<'_>, text: &str) -> BotHandlerResult<()>
5460
return Ok(());
5561
}
5662

63+
ctx.handler.messaging_service.send_chat_action(ctx.message.chat.id, ChatAction::Typing).await?;
64+
65+
let status_msg = ctx
66+
.handler
67+
.messaging_service
68+
.send_text_message(ctx.message.chat.id, "Processing... ⏳")
69+
.await?;
70+
5771
let summary = stream::iter(urls)
5872
.map(|url| async move {
5973
let repo = match RepoEntity::from_url(&url) {
@@ -101,14 +115,7 @@ pub async fn handle_reply(ctx: Context<'_>, text: &str) -> BotHandlerResult<()>
101115

102116
ctx.handler
103117
.messaging_service
104-
.send_add_summary_msg(
105-
ctx.message.chat.id,
106-
summary.successfully_added,
107-
summary.already_tracked,
108-
summary.not_found,
109-
summary.invalid_urls,
110-
summary.errors,
111-
)
118+
.edit_add_summary_msg(ctx.message.chat.id, status_msg.id, &summary)
112119
.await?;
113120

114121
Ok(())

src/bot_handler/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ impl BotHandler {
186186

187187
// Answer the callback query to clear the spinner.
188188
self.messaging_service.answer_callback_query(&query_id, &None).await?;
189+
if let Some(message) = query.message.as_ref() {
190+
self.messaging_service
191+
.send_chat_action(message.chat().id, teloxide::types::ChatAction::Typing)
192+
.await?;
193+
}
189194

190195
let ctx = Context {
191196
handler: self,
@@ -198,13 +203,14 @@ impl BotHandler {
198203

199204
match action {
200205
CallbackAction::ViewRepoDetails(repo_id, from_page) => {
201-
callbacks::view_repo::handle(ctx, repo_id, from_page).await?;
206+
callbacks::view_repo::handle(ctx, repo_id, from_page, &query_id).await?;
202207
}
203208
CallbackAction::BackToRepoDetails(repo_id, from_page) => {
204-
callbacks::view_repo::handle(ctx, repo_id, from_page).await?;
209+
callbacks::view_repo::handle(ctx, repo_id, from_page, &query_id).await?;
205210
}
206211
CallbackAction::ViewRepoLabels(repo_id, page, from_page) => {
207-
callbacks::view_labels::handle(ctx, repo_id, page, from_page).await?;
212+
callbacks::view_labels::handle(ctx, repo_id, page, from_page, &query_id)
213+
.await?;
208214
}
209215
CallbackAction::RemoveRepoPrompt(repo_id) => {
210216
callbacks::remove::handle(ctx, repo_id, 1).await?;

0 commit comments

Comments
 (0)