From 51c5f2844360dd84b1bc3dc4c0318791b8da6b82 Mon Sep 17 00:00:00 2001
From: Omar Roth <omarroth@protonmail.com>
Date: Sun, 26 May 2019 11:59:23 -0500
Subject: [PATCH] Add config option for updating feeds on event

---
 src/invidious.cr                 | 2 +-
 src/invidious/helpers/helpers.cr | 1 +
 src/invidious/helpers/jobs.cr    | 6 +++---
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/invidious.cr b/src/invidious.cr
index 370aecbc..53cbcef0 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -130,7 +130,7 @@ end
 # Start jobs
 
 refresh_channels(PG_DB, logger, config.channel_threads, config.full_refresh)
-refresh_feeds(PG_DB, logger, config.feed_threads)
+refresh_feeds(PG_DB, logger, config.feed_threads, config.use_feed_events)
 subscribe_to_feeds(PG_DB, logger, HMAC_KEY, config)
 
 statistics = {
diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr
index 476038c7..983d3d1e 100644
--- a/src/invidious/helpers/helpers.cr
+++ b/src/invidious/helpers/helpers.cr
@@ -105,6 +105,7 @@ struct Config
     hmac_key:                 String?,                              # HMAC signing key for CSRF tokens and verifying pubsub subscriptions
     domain:                   String?,                              # Domain to be used for links to resources on the site where an absolute URL is required
     use_pubsub_feeds:         {type: Bool | Int32, default: false}, # Subscribe to channels using PubSubHubbub (requires domain, hmac_key)
+    use_feed_events:          {type: Bool, default: false},         # Update feeds on receiving notifications
     default_home:             {type: String, default: "Top"},
     feed_menu:                {type: Array(String), default: ["Popular", "Top", "Trending", "Subscriptions"]},
     top_enabled:              {type: Bool, default: true},
diff --git a/src/invidious/helpers/jobs.cr b/src/invidious/helpers/jobs.cr
index 05a0a5a4..5383d8ba 100644
--- a/src/invidious/helpers/jobs.cr
+++ b/src/invidious/helpers/jobs.cr
@@ -42,12 +42,12 @@ def refresh_channels(db, logger, max_threads = 1, full_refresh = false)
   max_channel.send(max_threads)
 end
 
-def refresh_feeds(db, logger, max_threads = 1)
+def refresh_feeds(db, logger, max_threads = 1, use_feed_events = false)
   max_channel = Channel(Int32).new
 
-  # TODO: Make this config option, similar to use_pubsub
+  # TODO: Instead of Fiber.yield, use proper queuing to prevent overloading DB
   # Spawn thread to handle feed events
-  if max_threads > 0
+  if use_feed_events
     spawn do
       PG.connect_listen(PG_URL, "feeds") do |event|
         spawn do