Merge pull request #53 from omarroth/notifications-only

Add option to view only notifications
pull/66/head
Omar Roth 6 years ago committed by GitHub
commit 51903462ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1817,20 +1817,25 @@ post "/preferences" do |env|
unseen_only ||= "off" unseen_only ||= "off"
unseen_only = unseen_only == "on" unseen_only = unseen_only == "on"
notifications_only = env.params.body["notifications_only"]?.try &.as(String)
notifications_only ||= "off"
notifications_only = notifications_only == "on"
preferences = { preferences = {
"video_loop" => video_loop, "video_loop" => video_loop,
"autoplay" => autoplay, "autoplay" => autoplay,
"speed" => speed, "speed" => speed,
"quality" => quality, "quality" => quality,
"volume" => volume, "volume" => volume,
"comments" => comments, "comments" => comments,
"redirect_feed" => redirect_feed, "redirect_feed" => redirect_feed,
"dark_mode" => dark_mode, "dark_mode" => dark_mode,
"thin_mode" => thin_mode, "thin_mode" => thin_mode,
"max_results" => max_results, "max_results" => max_results,
"sort" => sort, "sort" => sort,
"latest_only" => latest_only, "latest_only" => latest_only,
"unseen_only" => unseen_only, "unseen_only" => unseen_only,
"notifications_only" => notifications_only,
}.to_json }.to_json
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email) PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email)
@ -1871,61 +1876,84 @@ get "/feed/subscriptions" do |env|
offset = (page - 1) * max_results offset = (page - 1) * max_results
end end
if preferences.latest_only notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email,
if preferences.unseen_only as: Array(String))
ucids = arg_array(user.subscriptions) if preferences.notifications_only && !notifications.empty?
if user.watched.empty? args = arg_array(notifications)
watched = "'{}'"
else
watched = arg_array(user.watched, user.subscriptions.size + 1)
end
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE \ videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE id IN (#{args})
ucid IN (#{ucids}) AND id NOT IN (#{watched}) ORDER BY ucid, published DESC", ORDER BY published DESC", notifications, as: ChannelVideo)
user.subscriptions + user.watched, as: ChannelVideo) notifications = [] of ChannelVideo
else
args = arg_array(user.subscriptions)
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE \
ucid IN (#{args}) ORDER BY ucid, published DESC", user.subscriptions, as: ChannelVideo)
end
videos.sort_by! { |video| video.published }.reverse! videos.sort_by! { |video| video.published }.reverse!
case preferences.sort
when "alphabetically"
videos.sort_by! { |video| video.title }
when "alphabetically - reverse"
videos.sort_by! { |video| video.title }.reverse!
when "channel name"
videos.sort_by! { |video| video.author }
when "channel name - reverse"
videos.sort_by! { |video| video.author }.reverse!
end
else else
if preferences.unseen_only if preferences.latest_only
ucids = arg_array(user.subscriptions, 3) if preferences.unseen_only
if user.watched.empty? ucids = arg_array(user.subscriptions)
watched = "'{}'" if user.watched.empty?
watched = "'{}'"
else
watched = arg_array(user.watched, user.subscriptions.size + 1)
end
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE \
ucid IN (#{ucids}) AND id NOT IN (#{watched}) ORDER BY ucid, published DESC",
user.subscriptions + user.watched, as: ChannelVideo)
else else
watched = arg_array(user.watched, user.subscriptions.size + 3) args = arg_array(user.subscriptions)
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE \
ucid IN (#{args}) ORDER BY ucid, published DESC", user.subscriptions, as: ChannelVideo)
end end
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{ucids}) \ videos.sort_by! { |video| video.published }.reverse!
AND id NOT IN (#{watched}) ORDER BY published DESC LIMIT $1 OFFSET $2",
[limit, offset] + user.subscriptions + user.watched, as: ChannelVideo)
else else
args = arg_array(user.subscriptions, 3) if preferences.unseen_only
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args}) \ ucids = arg_array(user.subscriptions, 3)
if user.watched.empty?
watched = "'{}'"
else
watched = arg_array(user.watched, user.subscriptions.size + 3)
end
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{ucids}) \
AND id NOT IN (#{watched}) ORDER BY published DESC LIMIT $1 OFFSET $2",
[limit, offset] + user.subscriptions + user.watched, as: ChannelVideo)
else
args = arg_array(user.subscriptions, 3)
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args}) \
ORDER BY published DESC LIMIT $1 OFFSET $2", [limit, offset] + user.subscriptions, as: ChannelVideo) ORDER BY published DESC LIMIT $1 OFFSET $2", [limit, offset] + user.subscriptions, as: ChannelVideo)
end
end end
end
case preferences.sort case preferences.sort
when "alphabetically" when "alphabetically"
videos.sort_by! { |video| video.title } videos.sort_by! { |video| video.title }
when "alphabetically - reverse" when "alphabetically - reverse"
videos.sort_by! { |video| video.title }.reverse! videos.sort_by! { |video| video.title }.reverse!
when "channel name" when "channel name"
videos.sort_by! { |video| video.author } videos.sort_by! { |video| video.author }
when "channel name - reverse" when "channel name - reverse"
videos.sort_by! { |video| video.author }.reverse! videos.sort_by! { |video| video.author }.reverse!
end end
# TODO: Add option to disable picking out notifications from regular feed # TODO: Add option to disable picking out notifications from regular feed
notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email,
as: Array(String)) as: Array(String))
notifications = videos.select { |v| notifications.includes? v.id } notifications = videos.select { |v| notifications.includes? v.id }
videos = videos - notifications videos = videos - notifications
end
if !limit if !limit
videos = videos[0..max_results] videos = videos[0..max_results]

@ -176,10 +176,15 @@ class Preferences
nilable: true, nilable: true,
default: false, default: false,
}, },
max_results: Int32, max_results: Int32,
sort: String, sort: String,
latest_only: Bool, latest_only: Bool,
unseen_only: Bool, unseen_only: Bool,
notifications_only: {
type: Bool,
nilable: true,
default: false,
},
}) })
end end

@ -97,6 +97,11 @@ function update_value(element) {
<input name="unseen_only" id="unseen_only" type="checkbox" <% if user.preferences.unseen_only %>checked<% end %>> <input name="unseen_only" id="unseen_only" type="checkbox" <% if user.preferences.unseen_only %>checked<% end %>>
</div> </div>
<div class="pure-control-group">
<label for="notifications_only">Only show notifications: </label>
<input name="notifications_only" id="notifications_only" type="checkbox" <% if user.preferences.notifications_only %>checked<% end %>>
</div>
<legend>Data preferences</legend> <legend>Data preferences</legend>
<div class="pure-control-group"> <div class="pure-control-group">
<label> <label>

Loading…
Cancel
Save