Skip to content

Commit

Permalink
feat(term): add github filter conditions (#85)
Browse files Browse the repository at this point in the history
* chore: use rust-overlay

* feat(term): add filter conditions to github notification

* test(term): add gh notifications test case
  • Loading branch information
ymgyt committed Jul 5, 2024
1 parent cbc77e0 commit a1135c7
Show file tree
Hide file tree
Showing 53 changed files with 1,577 additions and 414 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ itertools = { version = "0.13", default-features = false, features = ["
jsonwebtoken = { version = "9.3.0" }
kvsd = { version = "0.1.3", default-features = false }
moka = { version = "0.12.7", features = ["future"] }
octocrab = { version = "0.38.0", features = ["rustls-webpki-tokio"] }
parse_duration = { version = "2.1.1" }
proptest = { version = "1.5.0" }
rand = { version = "0.8.5" }
Expand Down
3 changes: 2 additions & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ allowed-duplicate-crates = [
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",

]

too-many-lines-threshold = 150
2 changes: 1 addition & 1 deletion crates/synd_term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ html2text = { version = "0.12" }
itertools = { workspace = true }
nom = { version = "7.1.3", default-features = false, features = ["std"] }
nucleo = "0.5.0"
octocrab = "0.38.0"
octocrab = { workspace = true }
open = "5.1.4"
parse_duration = { workspace = true }
ratatui = { version = "0.26.3" }
Expand Down
18 changes: 17 additions & 1 deletion crates/synd_term/src/application/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
application::{Application, Authenticator, Cache, Config},
application::{Application, Authenticator, Cache, Clock, Config},
client::{github::GithubClient, Client},
config::Categories,
interact::Interactor,
Expand All @@ -25,6 +25,7 @@ pub struct ApplicationBuilder<
pub(super) authenticator: Option<Authenticator>,
pub(super) interactor: Option<Interactor>,
pub(super) github_client: Option<GithubClient>,
pub(super) clock: Option<Box<dyn Clock>>,
pub(super) dry_run: bool,
}

Expand All @@ -40,6 +41,7 @@ impl Default for ApplicationBuilder {
authenticator: None,
interactor: None,
github_client: None,
clock: None,
dry_run: false,
}
}
Expand All @@ -58,6 +60,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<(), T1, T2, T3, T4, T5> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand All @@ -76,6 +79,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<T1, (), T2, T3, T4, T5> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand All @@ -97,6 +101,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<T1, T2, (), T3, T4, T5> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand All @@ -115,6 +120,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<T1, T2, T3, (), T4, T5> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand All @@ -133,6 +139,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<T1, T2, T3, T4, (), T5> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand All @@ -151,6 +158,7 @@ impl<T1, T2, T3, T4, T5> ApplicationBuilder<T1, T2, T3, T4, T5, ()> {
authenticator: self.authenticator,
interactor: self.interactor,
github_client: self.github_client,
clock: self.clock,
dry_run: self.dry_run,
}
}
Expand Down Expand Up @@ -181,6 +189,14 @@ impl<T1, T2, T3, T4, T5, T6> ApplicationBuilder<T1, T2, T3, T4, T5, T6> {
}
}

#[must_use]
pub fn clock(self, clock: Box<dyn Clock>) -> Self {
Self {
clock: Some(clock),
..self
}
}

#[must_use]
pub fn dry_run(self, dry_run: bool) -> Self {
Self { dry_run, ..self }
Expand Down
88 changes: 54 additions & 34 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
application::event::KeyEventResult,
auth::{self, AuthenticationProvider, Credential, CredentialError, Verified},
client::{
github::{FetchNotificationInclude, FetchNotificationsParams, GithubClient},
github::{FetchNotificationsParams, GithubClient},
mutation::subscribe_feed::SubscribeFeedInput,
Client, SyndApiError,
},
Expand Down Expand Up @@ -55,7 +55,7 @@ use input_parser::InputParser;
pub use auth::authenticator::{Authenticator, DeviceFlows, JwtService};

mod clock;
pub(crate) use clock::{Clock, SystemClock};
pub use clock::{Clock, SystemClock};

mod cache;
pub use cache::Cache;
Expand Down Expand Up @@ -120,6 +120,7 @@ impl Application {
theme,
authenticator,
interactor,
clock,
dry_run,
} = builder;

Expand All @@ -138,7 +139,7 @@ impl Application {
}

Self {
clock: Box::new(SystemClock),
clock: clock.unwrap_or_else(|| Box::new(SystemClock)),
terminal,
client,
github_client,
Expand Down Expand Up @@ -230,7 +231,7 @@ impl Application {
self.config
.features
.enable_github_notification
.then(|| self.keymaps().enable(KeymapId::Notification));
.then(|| self.keymaps().enable(KeymapId::GhNotification));
}

fn set_credential(&mut self, cred: Verified<Credential>) {
Expand All @@ -248,13 +249,9 @@ impl Application {
.boxed(),
);
if self.config.features.enable_github_notification {
self.jobs.futures.push(
future::ready(Ok(Command::FetchGhNotifications {
page: config::github::INITIAL_PAGE_NUM,
populate: Populate::Replace,
}))
.boxed(),
);
if let Some(fetch) = self.components.gh_notifications.fetch_next_if_needed() {
self.jobs.futures.push(future::ready(Ok(fetch)).boxed());
}
}
}

Expand Down Expand Up @@ -515,7 +512,7 @@ impl Application {
self.keymaps()
.disable(KeymapId::Subscription)
.disable(KeymapId::Entries)
.disable(KeymapId::Notification);
.disable(KeymapId::GhNotification);

match self.components.tabs.move_selection(direction) {
Tab::Feeds => {
Expand All @@ -531,7 +528,7 @@ impl Application {
self.keymaps().enable(KeymapId::Entries);
}
Tab::GitHub => {
self.keymaps().enable(KeymapId::Notification);
self.keymaps().enable(KeymapId::GhNotification);
}
}
self.should_render();
Expand All @@ -558,7 +555,7 @@ impl Application {
}
Command::PromptFeedUnsubscription => {
if self.components.subscription.selected_feed().is_some() {
self.components.subscription.show_unsubscribe_popup(true);
self.components.subscription.toggle_unsubscribe_popup(true);
self.keymaps().enable(KeymapId::UnsubscribePopupSelection);
self.should_render();
}
Expand All @@ -579,7 +576,7 @@ impl Application {
self.should_render();
}
Command::CancelFeedUnsubscriptionPopup => {
self.components.subscription.show_unsubscribe_popup(false);
self.components.subscription.toggle_unsubscribe_popup(false);
self.keymaps().disable(KeymapId::UnsubscribePopupSelection);
self.should_render();
}
Expand Down Expand Up @@ -625,7 +622,9 @@ impl Application {
}
Command::MoveFilterRequirement(direction) => {
let filterer = self.components.filter.move_requirement(direction);
self.apply_filterer(filterer);
self.apply_filterer(filterer)
.into_iter()
.for_each(|command| queue.push_back(command));
self.should_render();
}
Command::ActivateCategoryFilterling => {
Expand All @@ -647,7 +646,9 @@ impl Application {
.components
.filter
.filterer(self.components.tabs.current().into());
self.apply_filterer(filterer);
self.apply_filterer(filterer)
.into_iter()
.for_each(|command| queue.push_back(command));
self.should_render();
}
}
Expand All @@ -669,16 +670,20 @@ impl Application {
}
Command::ActivateAllFilterCategories { lane } => {
let filterer = self.components.filter.activate_all_categories_state(lane);
self.apply_filterer(filterer);
self.apply_filterer(filterer)
.into_iter()
.for_each(|command| queue.push_back(command));
self.should_render();
}
Command::DeactivateAllFilterCategories { lane } => {
let filterer = self.components.filter.deactivate_all_categories_state(lane);
self.apply_filterer(filterer);
self.apply_filterer(filterer)
.into_iter()
.for_each(|command| queue.push_back(command));
self.should_render();
}
Command::FetchGhNotifications { page, populate } => {
self.fetch_gh_notifications(populate, page);
Command::FetchGhNotifications { populate, params } => {
self.fetch_gh_notifications(populate, params);
}
Command::MoveGhNotification(direction) => {
self.components.gh_notifications.move_selection(direction);
Expand All @@ -696,10 +701,8 @@ impl Application {
self.open_notification();
}
Command::ReloadGhNotifications => {
self.fetch_gh_notifications(
Populate::Replace,
config::github::INITIAL_PAGE_NUM,
);
let params = self.components.gh_notifications.reload();
self.fetch_gh_notifications(Populate::Replace, params);
}
Command::FetchGhNotificationDetails { contexts } => {
self.fetch_gh_notification_details(contexts);
Expand All @@ -715,6 +718,28 @@ impl Application {
self.unsubscribe_gh_thread();
self.mark_gh_notification_as_done();
}
Command::OpenGhNotificationFilterPopup => {
self.components.gh_notifications.open_filter_popup();
self.keymaps().enable(KeymapId::GhNotificationFilterPopup);
self.keymaps().disable(KeymapId::GhNotification);
self.should_render();
}
Command::CloseGhNotificationFilterPopup => {
self.components
.gh_notifications
.close_filter_popup()
.into_iter()
.for_each(|command| queue.push_back(command));
self.keymaps().disable(KeymapId::GhNotificationFilterPopup);
self.keymaps().enable(KeymapId::GhNotification);
self.should_render();
}
Command::UpdateGhnotificationFilterPopupOptions(updater) => {
self.components
.gh_notifications
.update_filter_options(&updater);
self.should_render();
}
Command::RotateTheme => {
self.rotate_theme();
self.should_render();
Expand Down Expand Up @@ -1065,22 +1090,16 @@ impl Application {
}

#[tracing::instrument(skip(self))]
fn fetch_gh_notifications(&mut self, populate: Populate, page: u8) {
fn fetch_gh_notifications(&mut self, populate: Populate, params: FetchNotificationsParams) {
let client = self
.github_client
.clone()
.expect("Github client not found, this is a BUG");
let request_seq = self
.in_flight
.add(RequestId::FetchGithubNotifications { page });
.add(RequestId::FetchGithubNotifications { page: params.page });
let fut = async move {
match client
.fetch_notifications(FetchNotificationsParams {
page,
include: FetchNotificationInclude::OnlyUnread,
})
.await
{
match client.fetch_notifications(params).await {
Ok(notifications) => Ok(Command::HandleApiResponse {
request_seq,
response: ApiResponse::FetchGithubNotifications {
Expand Down Expand Up @@ -1252,6 +1271,7 @@ impl Application {
}

impl Application {
#[must_use]
fn apply_filterer(&mut self, filterer: Filterer) -> Option<Command> {
match filterer {
Filterer::Feed(filterer) => {
Expand Down
1 change: 0 additions & 1 deletion crates/synd_term/src/cli/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct ExportCommand {
}

impl ExportCommand {
#[allow(clippy::unused_self)]
pub async fn run(self, endpoint: Url) -> i32 {
let err = if self.print_schema {
Self::print_json_schema()
Expand Down
Loading

0 comments on commit a1135c7

Please sign in to comment.