Better handling of events.
This commit is contained in:
parent
9a27ee8d5b
commit
ed2e5159df
@ -1,3 +1,5 @@
|
|||||||
|
use crate::json_util::get_json_string;
|
||||||
|
use std::collections::HashSet;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use super::github_endpoint_watcher::GithubEndpointWatcher;
|
use super::github_endpoint_watcher::GithubEndpointWatcher;
|
||||||
@ -75,9 +77,10 @@ impl<'a> GithubCtl<'a> {
|
|||||||
let event_stream = self.event_writer.clone();
|
let event_stream = self.event_writer.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
let mut already_seen_event_ids = HashSet::new();
|
||||||
let mut endpoint_watcher = GithubEndpointWatcher::new(username, token, url)
|
let mut endpoint_watcher = GithubEndpointWatcher::new(username, token, url)
|
||||||
.expect("Failed to create endpoint watcher.");
|
.expect("Failed to create endpoint watcher.");
|
||||||
endpoint_watcher.skip_results_before_now().await.expect("Failed to fetch initial historical results.");
|
let mut should_notify: bool = false; // Skip the first page of results because theses events already occurred in the past
|
||||||
loop {
|
loop {
|
||||||
let api_result = match endpoint_watcher.get_results().await {
|
let api_result = match endpoint_watcher.get_results().await {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
@ -88,9 +91,17 @@ impl<'a> GithubCtl<'a> {
|
|||||||
};
|
};
|
||||||
if let Some(serde_json::Value::Array(events)) = api_result {
|
if let Some(serde_json::Value::Array(events)) = api_result {
|
||||||
for event in events {
|
for event in events {
|
||||||
if let Err(_) = event_stream.send(event).await {
|
let event_id = event
|
||||||
error!("Receiver dropped.");
|
.get("id")
|
||||||
return;
|
.map(get_json_string)
|
||||||
|
.expect("Ran into an event without a id.")
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
if should_notify && already_seen_event_ids.insert(event_id) {
|
||||||
|
if let Err(_) = event_stream.send(event).await {
|
||||||
|
error!("Receiver dropped.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let None = api_result {
|
} else if let None = api_result {
|
||||||
@ -98,6 +109,7 @@ impl<'a> GithubCtl<'a> {
|
|||||||
} else {
|
} else {
|
||||||
error!("Unsupported JSON type.");
|
error!("Unsupported JSON type.");
|
||||||
}
|
}
|
||||||
|
should_notify = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -25,11 +25,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_event(event: serde_json::Value) -> Result<(), Box<dyn std::error::Error>> {
|
fn handle_event(json_event: serde_json::Value) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
if PullRequestEvent::is_a(&event)? {
|
if PullRequestEvent::is_a(&json_event)? {
|
||||||
let event = PullRequestEvent::new(&event)?;
|
let event = PullRequestEvent::new(&json_event)?;
|
||||||
if event.action == "opened" {
|
if event.action == "opened" {
|
||||||
println!("PullRequestEvent action {}", event.action);
|
// println!("PullRequestEvent action {}", event.action);
|
||||||
|
println!("{}", serde_json::to_string(&json_event)?);
|
||||||
let notification_body = format!("Pull Request opened: {}", event.title);
|
let notification_body = format!("Pull Request opened: {}", event.title);
|
||||||
Notification::new()
|
Notification::new()
|
||||||
.summary("Pull request opened.")
|
.summary("Pull request opened.")
|
||||||
@ -39,7 +40,14 @@ fn handle_event(event: serde_json::Value) -> Result<(), Box<dyn std::error::Erro
|
|||||||
.hint(Hint::Category("im.received".to_owned()))
|
.hint(Hint::Category("im.received".to_owned()))
|
||||||
.hint(Hint::Resident(true))
|
.hint(Hint::Resident(true))
|
||||||
.timeout(0)
|
.timeout(0)
|
||||||
.show()?;
|
.show()?
|
||||||
|
.wait_for_action(|action| match action {
|
||||||
|
"default" => info!("you clicked \"default\""),
|
||||||
|
"clicked" => info!("that was correct"),
|
||||||
|
// here "__closed" is a hard coded keyword
|
||||||
|
"__closed" => info!("the notification was closed"),
|
||||||
|
_ => info!("Unrecognized action {}", action),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user