From 98301a223750b61915d61ac5221e8b71ea2b40ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milien=20Devos?= Date: Thu, 5 Jan 2023 23:08:05 +0000 Subject: [PATCH] Add ability to disable all user notifications (#3473) --- config/config.example.yml | 11 ++++++ src/invidious/channels/channels.cr | 14 ++++++-- src/invidious/config.cr | 2 ++ src/invidious/database/users.cr | 10 ++++++ src/invidious/routes/embed.cr | 2 +- src/invidious/routes/feeds.cr | 38 +++++++++++++-------- src/invidious/routes/watch.cr | 2 +- src/invidious/routing.cr | 10 ++++-- src/invidious/views/feeds/subscriptions.ecr | 4 +++ src/invidious/views/template.ecr | 4 ++- src/invidious/views/user/preferences.ecr | 2 ++ 11 files changed, 77 insertions(+), 22 deletions(-) diff --git a/config/config.example.yml b/config/config.example.yml index 8794880d..8abe1b9e 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -295,6 +295,17 @@ https_only: false ## #admins: [""] +## +## Enable/Disable the user notifications for all users +## +## Note: On large instances, it is recommended to set this option to 'false' +## in order to reduce the amount of data written to the database, and hence +## improve the overall performance of the instance. +## +## Accepted values: true, false +## Default: true +## +#enable_user_notifications: true # ----------------------------- # Background jobs diff --git a/src/invidious/channels/channels.cr b/src/invidious/channels/channels.cr index e3d3d9ee..9806d1da 100644 --- a/src/invidious/channels/channels.cr +++ b/src/invidious/channels/channels.cr @@ -228,7 +228,11 @@ def fetch_channel(ucid, pull_all_videos : Bool) if was_insert LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Inserted, updating subscriptions") - Invidious::Database::Users.add_notification(video) + if CONFIG.enable_user_notifications + Invidious::Database::Users.add_notification(video) + else + Invidious::Database::Users.feed_needs_update(video) + end else LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Updated") end @@ -264,7 +268,13 @@ def fetch_channel(ucid, pull_all_videos : Bool) # so since they don't provide a published date here we can safely ignore them. if Time.utc - video.published > 1.minute was_insert = Invidious::Database::ChannelVideos.insert(video) - Invidious::Database::Users.add_notification(video) if was_insert + if was_insert + if CONFIG.enable_user_notifications + Invidious::Database::Users.add_notification(video) + else + Invidious::Database::Users.feed_needs_update(video) + end + end end end diff --git a/src/invidious/config.cr b/src/invidious/config.cr index c9bf43a4..9fc58409 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -110,6 +110,8 @@ class Config property hsts : Bool? = true # Disable proxying server-wide: options: 'dash', 'livestreams', 'downloads', 'local' property disable_proxy : Bool? | Array(String)? = false + # Enable the user notifications for all users + property enable_user_notifications : Bool = true # URL to the modified source code to be easily AGPL compliant # Will display in the footer, next to the main source code link diff --git a/src/invidious/database/users.cr b/src/invidious/database/users.cr index f62b43ea..0a4a4fd8 100644 --- a/src/invidious/database/users.cr +++ b/src/invidious/database/users.cr @@ -154,6 +154,16 @@ module Invidious::Database::Users # Update (misc) # ------------------- + def feed_needs_update(video : ChannelVideo) + request = <<-SQL + UPDATE users + SET feed_needs_update = true + WHERE $1 = ANY(subscriptions) + SQL + + PG_DB.exec(request, video.ucid) + end + def update_preferences(user : User) request = <<-SQL UPDATE users diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.cr index 289d87c9..266f7ba4 100644 --- a/src/invidious/routes/embed.cr +++ b/src/invidious/routes/embed.cr @@ -147,7 +147,7 @@ module Invidious::Routes::Embed # PG_DB.exec("UPDATE users SET watched = array_append(watched, $1) WHERE email = $2", id, user.as(User).email) # end - if notifications && notifications.includes? id + if CONFIG.enable_user_notifications && notifications && notifications.includes? id Invidious::Database::Users.remove_notification(user.as(User), id) env.get("user").as(User).notifications.delete(id) notifications.delete(id) diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.cr index b601db94..fb482e33 100644 --- a/src/invidious/routes/feeds.cr +++ b/src/invidious/routes/feeds.cr @@ -96,12 +96,14 @@ module Invidious::Routes::Feeds videos, notifications = get_subscription_feed(user, max_results, page) - # "updated" here is used for delivering new notifications, so if - # we know a user has looked at their feed e.g. in the past 10 minutes, - # they've already seen a video posted 20 minutes ago, and don't need - # to be notified. - Invidious::Database::Users.clear_notifications(user) - user.notifications = [] of String + if CONFIG.enable_user_notifications + # "updated" here is used for delivering new notifications, so if + # we know a user has looked at their feed e.g. in the past 10 minutes, + # they've already seen a video posted 20 minutes ago, and don't need + # to be notified. + Invidious::Database::Users.clear_notifications(user) + user.notifications = [] of String + end env.set "user", user templated "feeds/subscriptions" @@ -404,13 +406,15 @@ module Invidious::Routes::Feeds video = get_video(id, force_refresh: true) - # Deliver notifications to `/api/v1/auth/notifications` - payload = { - "topic" => video.ucid, - "videoId" => video.id, - "published" => published.to_unix, - }.to_json - PG_DB.exec("NOTIFY notifications, E'#{payload}'") + if CONFIG.enable_user_notifications + # Deliver notifications to `/api/v1/auth/notifications` + payload = { + "topic" => video.ucid, + "videoId" => video.id, + "published" => published.to_unix, + }.to_json + PG_DB.exec("NOTIFY notifications, E'#{payload}'") + end video = ChannelVideo.new({ id: id, @@ -426,7 +430,13 @@ module Invidious::Routes::Feeds }) was_insert = Invidious::Database::ChannelVideos.insert(video, with_premiere_timestamp: true) - Invidious::Database::Users.add_notification(video) if was_insert + if was_insert + if CONFIG.enable_user_notifications + Invidious::Database::Users.add_notification(video) + else + Invidious::Database::Users.feed_needs_update(video) + end + end end end diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index 5f481557..5d3845c3 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -80,7 +80,7 @@ module Invidious::Routes::Watch Invidious::Database::Users.mark_watched(user.as(User), id) end - if notifications && notifications.includes? id + if CONFIG.enable_user_notifications && notifications && notifications.includes? id Invidious::Database::Users.remove_notification(user.as(User), id) env.get("user").as(User).notifications.delete(id) notifications.delete(id) diff --git a/src/invidious/routing.cr b/src/invidious/routing.cr index f409f13c..1995677c 100644 --- a/src/invidious/routing.cr +++ b/src/invidious/routing.cr @@ -37,7 +37,9 @@ module Invidious::Routing get "/feed/webhook/:token", Routes::Feeds, :push_notifications_get post "/feed/webhook/:token", Routes::Feeds, :push_notifications_post - get "/modify_notifications", Routes::Notifications, :modify + if CONFIG.enable_user_notifications + get "/modify_notifications", Routes::Notifications, :modify + end {% end %} self.register_image_routes @@ -260,8 +262,10 @@ module Invidious::Routing post "/api/v1/auth/tokens/register", {{namespace}}::Authenticated, :register_token post "/api/v1/auth/tokens/unregister", {{namespace}}::Authenticated, :unregister_token - get "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications - post "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications + if CONFIG.enable_user_notifications + get "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications + post "/api/v1/auth/notifications", {{namespace}}::Authenticated, :notifications + end # Misc get "/api/v1/stats", {{namespace}}::Misc, :stats diff --git a/src/invidious/views/feeds/subscriptions.ecr b/src/invidious/views/feeds/subscriptions.ecr index 8d56ad14..76f2f2bd 100644 --- a/src/invidious/views/feeds/subscriptions.ecr +++ b/src/invidious/views/feeds/subscriptions.ecr @@ -23,6 +23,8 @@ +<% if CONFIG.enable_user_notifications %> +
<%= translate_count(locale, "subscriptions_unseen_notifs_count", notifications.size) %>
@@ -39,6 +41,8 @@ <% end %> +<% end %> +

diff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecr index 98f72eba..77265679 100644 --- a/src/invidious/views/template.ecr +++ b/src/invidious/views/template.ecr @@ -54,7 +54,7 @@
" href="/feed/subscriptions" class="pure-menu-heading"> <% notification_count = env.get("user").as(Invidious::User).notifications.size %> - <% if notification_count > 0 %> + <% if CONFIG.enable_user_notifications && notification_count > 0 %> <%= notification_count %> <% else %> @@ -170,7 +170,9 @@ }.to_pretty_json %> + <% if CONFIG.enable_user_notifications %> + <% end %> <% end %> diff --git a/src/invidious/views/user/preferences.ecr b/src/invidious/views/user/preferences.ecr index d841982c..dfda1434 100644 --- a/src/invidious/views/user/preferences.ecr +++ b/src/invidious/views/user/preferences.ecr @@ -244,6 +244,7 @@ checked<% end %>>
+ <% if CONFIG.enable_user_notifications %>
checked<% end %>> @@ -255,6 +256,7 @@ <%= translate(locale, "Enable web notifications") %>
<% end %> + <% end %> <% end %> <% if env.get?("user") && CONFIG.admins.includes? env.get?("user").as(Invidious::User).email %>