Skip to content
Closed
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
1 change: 1 addition & 0 deletions locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"preferences_listen_label": "Listen by default: ",
"preferences_local_label": "Proxy videos: ",
"preferences_watch_history_label": "Enable watch history: ",
"preferences_notifications_label": "Enable notifications: ",
"preferences_speed_label": "Default speed: ",
"preferences_quality_label": "Preferred video quality: ",
"preferences_quality_option_dash": "DASH (adaptative quality)",
Expand Down
6 changes: 4 additions & 2 deletions src/invidious/channels/channels.cr
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def fetch_channel(ucid, pull_all_videos : Bool)
# meaning the above timestamp is always null
was_insert = Invidious::Database::ChannelVideos.insert(video)

if was_insert
if preferences.notifications && was_insert
LOGGER.trace("fetch_channel: #{ucid} : video #{video_id} : Inserted, updating subscriptions")
Invidious::Database::Users.add_notification(video)
else
Expand Down Expand Up @@ -264,7 +264,9 @@ 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 preferences.notifications && was_insert
Invidious::Database::Users.add_notification(video)
Comment on lines +267 to +268
Copy link
Member

@SamantazFox SamantazFox Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you can't check the preferences in here: this function is called by the refresh job and hence lacks any user context. We need to add a column to the postgres table users so we can do the following:

UPDATE ... 
SET array_append(subscriptions, '<video_id>')
WHERE notifications = true AND '<ucid>' = ANY(subscriptions)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do that until we have #2878 merged then?

Copy link
Member Author

@unixfox unixfox Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh the query you are talking about, is it this one: https://github.com/iv-org/invidious/blob/master/src/invidious/database/users.cr#L100?

We could just temporarily convert the preferences column to a json object then check in a where clause which user has notifications enabled.

Copy link
Member

@SamantazFox SamantazFox Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's better to wait for 2878 to be merged, so we can start some work on the DB.
And no, the function that updates notifications is this one:

def add_notification(video : ChannelVideo)
request = <<-SQL
UPDATE users
SET notifications = array_append(notifications, $1),
feed_needs_update = true
WHERE $2 = ANY(subscriptions)
SQL
PG_DB.exec(request, video.id, video.ucid)
end
. the one you gave is the function used when a user subscribes to a channel.

As for the temporary solution, Imo it would be preferrable to not clutter the code now.

end
end
end

Expand Down
1 change: 1 addition & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct ConfigPreferences
property local : Bool = false
property locale : String = "en-US"
property watch_history : Bool = true
property notifications : Bool = true
property max_results : Int32 = 40
property notifications_only : Bool = false
property player_style : String = "invidious"
Expand Down
4 changes: 3 additions & 1 deletion src/invidious/routes/embed.cr
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ module Invidious::Routes::Embed
# end

if notifications && notifications.includes? id
Invidious::Database::Users.remove_notification(user.as(User), id)
if preferences.notifications
Invidious::Database::Users.remove_notification(user.as(User), id)
end
env.get("user").as(User).notifications.delete(id)
notifications.delete(id)
end
Expand Down
8 changes: 6 additions & 2 deletions src/invidious/routes/feeds.cr
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ module Invidious::Routes::Feeds
# 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)
if preferences.notifications
Invidious::Database::Users.clear_notifications(user)
end
user.notifications = [] of String
env.set "user", user

Expand Down Expand Up @@ -417,7 +419,9 @@ 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 preferences.notifications && was_insert
Invidious::Database::Users.add_notification(video)
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions src/invidious/routes/preferences.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ module Invidious::Routes::PreferencesRoute
watch_history ||= "off"
watch_history = watch_history == "on"

notifications = env.params.body["notifications"]?.try &.as(String)
notifications ||= "off"
notifications = notifications == "on"

speed = env.params.body["speed"]?.try &.as(String).to_f32?
speed ||= CONFIG.default_user_preferences.speed

Expand Down Expand Up @@ -154,6 +158,7 @@ module Invidious::Routes::PreferencesRoute
listen: listen,
local: local,
watch_history: watch_history,
notifications: notifications,
locale: locale,
max_results: max_results,
notifications_only: notifications_only,
Expand Down
4 changes: 3 additions & 1 deletion src/invidious/routes/watch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ module Invidious::Routes::Watch
end

if notifications && notifications.includes? id
Invidious::Database::Users.remove_notification(user.as(User), id)
if preferences.notifications
Invidious::Database::Users.remove_notification(user.as(User), id)
end
env.get("user").as(User).notifications.delete(id)
notifications.delete(id)
end
Expand Down
1 change: 1 addition & 0 deletions src/invidious/user/preferences.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Preferences
property listen : Bool = CONFIG.default_user_preferences.listen
property local : Bool = CONFIG.default_user_preferences.local
property watch_history : Bool = CONFIG.default_user_preferences.watch_history
property notifications : Bool = CONFIG.default_user_preferences.notifications
property vr_mode : Bool = CONFIG.default_user_preferences.vr_mode
property show_nick : Bool = CONFIG.default_user_preferences.show_nick

Expand Down
5 changes: 5 additions & 0 deletions src/invidious/views/user/preferences.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@
<% if env.get? "user" %>
<legend><%= translate(locale, "preferences_category_subscription") %></legend>

<div class="pure-control-group">
<label for="notifications"><%= translate(locale, "preferences_notifications_label") %></label>
<input name="notifications" id="notifications" type="checkbox" <% if preferences.notifications %>checked<% end %>>
</div>

<div class="pure-control-group">
<label for="watch_history"><%= translate(locale, "preferences_watch_history_label") %></label>
<input name="watch_history" id="watch_history" type="checkbox" <% if preferences.watch_history %>checked<% end %>>
Expand Down