Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync and deliver during subscription #213

Merged
merged 1 commit into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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