Better handling of events.

master
Tom Alexander 1 year ago
parent 9a27ee8d5b
commit ed2e5159df
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -1,3 +1,5 @@
use crate::json_util::get_json_string;
use std::collections::HashSet;
use tokio::sync::mpsc;
use super::github_endpoint_watcher::GithubEndpointWatcher;
@ -75,9 +77,10 @@ impl<'a> GithubCtl<'a> {
let event_stream = self.event_writer.clone();
tokio::spawn(async move {
let mut already_seen_event_ids = HashSet::new();
let mut endpoint_watcher = GithubEndpointWatcher::new(username, token, url)
.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 {
let api_result = match endpoint_watcher.get_results().await {
Ok(result) => result,
@ -88,9 +91,17 @@ impl<'a> GithubCtl<'a> {
};
if let Some(serde_json::Value::Array(events)) = api_result {
for event in events {
if let Err(_) = event_stream.send(event).await {
error!("Receiver dropped.");
return;
let event_id = event
.get("id")
.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 {
@ -98,6 +109,7 @@ impl<'a> GithubCtl<'a> {
} else {
error!("Unsupported JSON type.");
}
should_notify = true;
}
});
}

@ -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>> {
if PullRequestEvent::is_a(&event)? {
let event = PullRequestEvent::new(&event)?;
fn handle_event(json_event: serde_json::Value) -> Result<(), Box<dyn std::error::Error>> {
if PullRequestEvent::is_a(&json_event)? {
let event = PullRequestEvent::new(&json_event)?;
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);
Notification::new()
.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::Resident(true))
.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(())

Loading…
Cancel
Save