Add watch history

This commit is contained in:
Omar Roth 2018-07-28 20:40:59 -05:00
parent 44eef9654a
commit b5c92c1a2f
4 changed files with 103 additions and 20 deletions

View file

@ -12,6 +12,7 @@ CREATE TABLE public.users
preferences text COLLATE pg_catalog."default", preferences text COLLATE pg_catalog."default",
password text COLLATE pg_catalog."default", password text COLLATE pg_catalog."default",
token text COLLATE pg_catalog."default", token text COLLATE pg_catalog."default",
watched text[] COLLATE pg_catalog."default",
CONSTRAINT users_email_key UNIQUE (email), CONSTRAINT users_email_key UNIQUE (email),
CONSTRAINT users_id_key UNIQUE (id) CONSTRAINT users_id_key UNIQUE (id)
) )

View file

@ -270,20 +270,24 @@ get "/" do |env|
end end
get "/watch" do |env| get "/watch" do |env|
user = env.get? "user"
if user
user = user.as(User)
preferences = user.preferences
subscriptions = user.subscriptions.as(Array(String))
end
subscriptions ||= [] of String
if env.params.query["v"]? if env.params.query["v"]?
id = env.params.query["v"] id = env.params.query["v"]
else else
next env.redirect "/" next env.redirect "/"
end end
user = env.get? "user"
if user
user = user.as(User)
if user.watched != ["N/A"] && !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.as(Array(String))
end
subscriptions ||= [] of String
autoplay = env.params.query["autoplay"]?.try &.to_i autoplay = env.params.query["autoplay"]?.try &.to_i
video_loop = env.params.query["video_loop"]?.try &.to_i video_loop = env.params.query["video_loop"]?.try &.to_i
@ -1618,8 +1622,12 @@ post "/login" do |env|
sid = Base64.encode(Random::Secure.random_bytes(50)) sid = Base64.encode(Random::Secure.random_bytes(50))
user = create_user(sid, email, password) user = create_user(sid, email, password)
if user.watched = ["N/A"]
user_array = user.to_a[0..-2]
else
user_array = user.to_a user_array = user.to_a
end
user_array[5] = user_array[5].to_json user_array[5] = user_array[5].to_json
args = arg_array(user_array) args = arg_array(user_array)
@ -1719,6 +1727,10 @@ post "/preferences" do |env|
latest_only ||= "off" latest_only ||= "off"
latest_only = latest_only == "on" latest_only = latest_only == "on"
unseen_only = env.params.body["unseen_only"]?.try &.as(String)
unseen_only ||= "off"
unseen_only = unseen_only == "on"
preferences = { preferences = {
"video_loop" => video_loop, "video_loop" => video_loop,
"autoplay" => autoplay, "autoplay" => autoplay,
@ -1732,6 +1744,7 @@ post "/preferences" do |env|
"max_results" => max_results, "max_results" => max_results,
"sort" => sort, "sort" => sort,
"latest_only" => latest_only, "latest_only" => latest_only,
"unseen_only" => unseen_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)
@ -1773,15 +1786,42 @@ get "/feed/subscriptions" do |env|
end end
if preferences.latest_only if preferences.latest_only
if preferences.unseen_only
ucids = arg_array(user.subscriptions)
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
args = arg_array(user.subscriptions) args = arg_array(user.subscriptions)
videos = PG_DB.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE \ 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) 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!
else
if preferences.unseen_only
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 else
args = arg_array(user.subscriptions, 3) args = arg_array(user.subscriptions, 3)
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args}) \ 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
case preferences.sort case preferences.sort
when "alphabetically" when "alphabetically"
@ -2167,6 +2207,20 @@ get "/subscription_ajax" do |env|
env.redirect referer env.redirect referer
end end
get "/clear_watch_history" do |env|
user = env.get? "user"
referer = env.request.headers["referer"]?
referer ||= "/"
if user
user = user.as(User)
PG_DB.exec("UPDATE users SET watched = '{}' WHERE id = $1", user.id)
end
env.redirect referer
end
get "/user/:user" do |env| get "/user/:user" do |env|
user = env.params.url["user"] user = env.params.url["user"]
env.redirect "/channel/#{user}" env.redirect "/channel/#{user}"

View file

@ -29,6 +29,7 @@ DEFAULT_USER_PREFERENCES = Preferences.from_json({
"max_results" => 40, "max_results" => 40,
"sort" => "published", "sort" => "published",
"latest_only" => false, "latest_only" => false,
"unseen_only" => false,
}.to_json) }.to_json)
class Config class Config
@ -147,6 +148,10 @@ class User
}, },
password: String?, password: String?,
token: String, token: String,
watched: {
type: Array(String),
default: ["N/A"],
},
}) })
end end
@ -177,6 +182,7 @@ class Preferences
max_results: Int32, max_results: Int32,
sort: String, sort: String,
latest_only: Bool, latest_only: Bool,
unseen_only: Bool,
}) })
end end
@ -817,7 +823,12 @@ def get_user(sid, client, headers, db, refresh = true)
if refresh && Time.now - user.updated > 1.minute if refresh && Time.now - user.updated > 1.minute
user = fetch_user(sid, client, headers, db) user = fetch_user(sid, client, headers, db)
if user.watched = ["N/A"]
user_array = user.to_a[0..-2]
else
user_array = user.to_a user_array = user.to_a
end
user_array[5] = user_array[5].to_json user_array[5] = user_array[5].to_json
args = arg_array(user_array) args = arg_array(user_array)
@ -826,7 +837,12 @@ def get_user(sid, client, headers, db, refresh = true)
end end
else else
user = fetch_user(sid, client, headers, db) user = fetch_user(sid, client, headers, db)
if user.watched = ["N/A"]
user_array = user.to_a[0..-2]
else
user_array = user.to_a user_array = user.to_a
end
user_array[5] = user_array[5].to_json user_array[5] = user_array[5].to_json
args = arg_array(user.to_a) args = arg_array(user.to_a)
@ -864,7 +880,7 @@ def fetch_user(sid, client, headers, db)
token = Base64.encode(Random::Secure.random_bytes(32)) token = Base64.encode(Random::Secure.random_bytes(32))
user = User.new(sid, Time.now, [] of String, channels, email, DEFAULT_USER_PREFERENCES, nil, token) user = User.new(sid, Time.now, [] of String, channels, email, DEFAULT_USER_PREFERENCES, nil, token, [] of String)
return user return user
end end
@ -872,7 +888,7 @@ def create_user(sid, email, password)
password = Crypto::Bcrypt::Password.create(password, cost: 10) password = Crypto::Bcrypt::Password.create(password, cost: 10)
token = Base64.encode(Random::Secure.random_bytes(32)) token = Base64.encode(Random::Secure.random_bytes(32))
user = User.new(sid, Time.now, [] of String, [] of String, email, DEFAULT_USER_PREFERENCES, password.to_s, token) user = User.new(sid, Time.now, [] of String, [] of String, email, DEFAULT_USER_PREFERENCES, password.to_s, token, [] of String)
return user return user
end end

View file

@ -88,10 +88,22 @@ function update_value(element) {
</div> </div>
<div class="pure-control-group"> <div class="pure-control-group">
<label for="latest_only">Only show latest video from channel: </label> <label for="latest_only">Only show latest <% if user.preferences.unseen_only %>unseen<% end %> video from channel: </label>
<input name="latest_only" id="latest_only" type="checkbox" <% if user.preferences.latest_only %>checked<% end %>> <input name="latest_only" id="latest_only" type="checkbox" <% if user.preferences.latest_only %>checked<% end %>>
</div> </div>
<div class="pure-control-group">
<label for="unseen_only">Only show unseen: </label>
<input name="unseen_only" id="unseen_only" type="checkbox" <% if user.preferences.unseen_only %>checked<% end %>>
</div>
<legend>Data preferences</legend>
<div class="pure-control-group">
<label>
<a href="/clear_watch_history">Clear watch history</a>
</labe>
</div>
<div class="pure-controls"> <div class="pure-controls">
<button type="submit" class="pure-button pure-button-primary">Save preferences</button> <button type="submit" class="pure-button pure-button-primary">Save preferences</button>
</div> </div>