From 6b12f11e105eac98c40765b01490d28d2c0669fe Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 19 Nov 2018 22:06:59 -0600 Subject: [PATCH] Add ability to mark videos as watched in subscription feed --- assets/css/default.css | 13 +++ src/invidious.cr | 102 +++++++++++++++++++----- src/invidious/comments.cr | 2 +- src/invidious/views/channel.ecr | 2 +- src/invidious/views/components/item.ecr | 15 ++++ src/invidious/views/history.ecr | 73 +++++++++++++++++ src/invidious/views/subscriptions.ecr | 22 +++++ src/invidious/views/watch.ecr | 2 +- 8 files changed, 206 insertions(+), 25 deletions(-) create mode 100644 src/invidious/views/history.ecr diff --git a/assets/css/default.css b/assets/css/default.css index aebd563c2..4ec94a682 100644 --- a/assets/css/default.css +++ b/assets/css/default.css @@ -55,6 +55,19 @@ img.thumbnail { bottom: -0.5em; } +.watched { + z-index: 100; + position: absolute; + background-color: rgba(35, 35, 35, 0.75); + color: #fff; + border-radius: 2px; + padding: 4px 8px 4px 8px; + font-size: 25px; + font-family: sans-serif; + left: 0.25em; + top: -0.75em; +} + /* * Navbar */ diff --git a/src/invidious.cr b/src/invidious.cr index 0d84081f5..e7f289273 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -246,12 +246,10 @@ get "/watch" do |env| user = env.get? "user" if user user = user.as(User) - if !user.watched.includes? id - PG_DB.exec("UPDATE users SET watched = watched || $1 WHERE id = $2", [id], user.id) - end preferences = user.preferences subscriptions = user.subscriptions + watched = user.watched end subscriptions ||= [] of String @@ -268,6 +266,10 @@ get "/watch" do |env| next templated "error" end + if watched && !watched.includes? id + PG_DB.exec("UPDATE users SET watched = watched || $1 WHERE $2 = id", [id], user.as(User).id) + end + if nojs if preferences source = preferences.comments[0] @@ -963,7 +965,6 @@ end get "/preferences" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -976,7 +977,6 @@ end post "/preferences" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1079,7 +1079,6 @@ end get "/toggle_theme" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1098,13 +1097,66 @@ get "/toggle_theme" do |env| env.redirect referer end +get "/mark_watched" do |env| + user = env.get? "user" + referer = get_referer(env, "/feed/subscriptions") + + id = env.params.query["id"]? + if !id + halt env, status_code: 400 + end + + redirect = env.params.query["redirect"]? + redirect ||= "false" + redirect = redirect == "true" + + if user + user = user.as(User) + if !user.watched.includes? id + PG_DB.exec("UPDATE users SET watched = watched || $1 WHERE $2 = id", [id], user.id) + end + end + + if redirect + env.redirect referer + else + env.response.content_type = "application/json" + "{}" + end +end + +get "/mark_unwatched" do |env| + user = env.get? "user" + referer = get_referer(env, "/feed/history") + + id = env.params.query["id"]? + if !id + halt env, status_code: 400 + end + + redirect = env.params.query["redirect"]? + redirect ||= "false" + redirect = redirect == "true" + + if user + user = user.as(User) + PG_DB.exec("UPDATE users SET watched = array_remove(watched, $1) WHERE id = $2", id, user.id) + end + + if redirect + env.redirect referer + else + env.response.content_type = "application/json" + "{}" + end +end + # /modify_notifications # will "ding" all subscriptions. # /modify_notifications?receive_all_updates=false&receive_no_updates=false # will "unding" all subscriptions. get "/modify_notifications" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1150,7 +1202,6 @@ end get "/subscription_manager" do |env| user = env.get? "user" - referer = get_referer(env, "/") if !user @@ -1235,7 +1286,6 @@ end get "/data_control" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1249,7 +1299,6 @@ end post "/data_control" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1385,7 +1434,6 @@ end get "/subscription_ajax" do |env| user = env.get? "user" - referer = get_referer(env) if user @@ -1547,6 +1595,7 @@ get "/feed/subscriptions" do |env| if user user = user.as(User) preferences = user.preferences + env.set "show_watched", true # Refresh account headers = HTTP::Headers.new @@ -1670,19 +1719,28 @@ get "/feed/subscriptions" do |env| end end -# get "/feed/history" do |env| -# user = env.get? "user" -# referer = get_referer(env) +get "/feed/history" do |env| + user = env.get? "user" + referer = get_referer(env) -# if user -# user = user.as(User) -# watched = user.watched.reverse + page = env.params.query["page"]?.try &.to_i? + page ||= 1 -# templated "history" -# else -# env.redirect referer -# end -# end + if user + user = user.as(User) + + limit = user.preferences.max_results + if user.watched[(page - 1)*limit]? + watched = user.watched.reverse[(page - 1)*limit, limit] + else + watched = [] of String + end + + templated "history" + else + env.redirect referer + end +end get "/feed/channel/:ucid" do |env| env.response.content_type = "text/xml" diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 372e58edb..70d365a19 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -507,7 +507,7 @@ def content_to_comment_html(content) length_seconds = watch_endpoint["startTimeSeconds"].as_i video_id = watch_endpoint["videoId"].as_s - text = %(#{text}) + text = %(#{text}) elsif url = run["navigationEndpoint"]["commandMetadata"]?.try &.["webCommandMetadata"]["url"].as_s text = %(#{text}) end diff --git a/src/invidious/views/channel.ecr b/src/invidious/views/channel.ecr index 98899228a..25cf191d1 100644 --- a/src/invidious/views/channel.ecr +++ b/src/invidious/views/channel.ecr @@ -90,7 +90,7 @@ + +
+
+ <% if page >= 2 %> + Previous page + <% end %> +
+
+
+ <% if watched.size >= limit %> + Next page + <% end %> +
+
diff --git a/src/invidious/views/subscriptions.ecr b/src/invidious/views/subscriptions.ecr index 6aa060ff4..a5c0c924e 100644 --- a/src/invidious/views/subscriptions.ecr +++ b/src/invidious/views/subscriptions.ecr @@ -43,6 +43,28 @@ <% end %> + +
<% if page >= 2 %> diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 31437760f..2f29b5b2f 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -225,7 +225,7 @@ function number_with_separator(val) { subscribe_button = document.getElementById("subscribe"); if (subscribe_button.getAttribute('onclick')) { - subscribe_button["href"] = "javascript:void(0);"; + subscribe_button["href"] = "javascript:void(0)"; } function subscribe() {