Skip to content

Commit

Permalink
Sync and deliver during subscription (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 authored May 14, 2022
1 parent a222715 commit 6c049ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/bot/commands/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::config::Config;
use crate::db::feeds;
use crate::db::telegram;
use crate::db::telegram::NewTelegramSubscription;
use crate::deliver::DeliverChatUpdatesJob;
use crate::models::telegram_subscription::TelegramSubscription;
use crate::sync::reader;
use crate::sync::SyncFeedJob;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use diesel::Connection;
use diesel::PgConnection;
use fang::Runnable;
use url::Url;

static COMMAND: &str = "/subscribe";
Expand All @@ -25,6 +27,7 @@ enum SubscriptionError {
UrlIsNotFeed,
SubscriptionAlreadyExists,
SubscriptionCountLimit,
SyncError,
}

impl From<diesel::result::Error> for SubscriptionError {
Expand Down Expand Up @@ -52,6 +55,7 @@ impl Subscribe {
Err(SubscriptionError::SubscriptionCountLimit) => {
"You exceeded the number of subscriptions".to_string()
}
Err(SubscriptionError::SyncError) => "Failed to sync your feed".to_string(),
}
}

Expand All @@ -78,7 +82,11 @@ impl Subscribe {
let subscription =
telegram::create_subscription(db_connection, new_telegram_subscription).unwrap();

SyncFeedJob::new(feed.id).enqueue(db_connection).unwrap();
if let Err(_err) = SyncFeedJob::new(feed.id).run(db_connection) {
return Err(SubscriptionError::SyncError);
}

DeliverChatUpdatesJob::new(chat.id).deliver(db_connection);

Ok(subscription)
})
Expand Down
4 changes: 4 additions & 0 deletions src/deliver/deliver_chat_updates_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ pub struct DeliverChatUpdatesJob {
}

impl DeliverChatUpdatesJob {
pub fn new(chat_id: i64) -> Self {
Self { chat_id }
}

pub fn deliver(&self, db_connection: &PgConnection) {
let subscriptions =
telegram::find_unread_subscriptions_for_chat(db_connection, self.chat_id).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/sync/sync_feed_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Runnable for SyncFeedJob {

impl SyncFeedJob {
pub fn new(feed_id: i64) -> Self {
SyncFeedJob { feed_id }
Self { feed_id }
}

pub fn enqueue(&self, connection: &PgConnection) -> Result<fang::Task, diesel::result::Error> {
Expand Down

0 comments on commit 6c049ea

Please sign in to comment.