|
|
@ -214,24 +214,16 @@ spawn do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
before_all do |env|
|
|
|
|
before_all do |env|
|
|
|
|
if env.request.cookies.has_key?("SID")
|
|
|
|
if env.request.cookies.has_key? "SID"
|
|
|
|
env.set "authorized", true
|
|
|
|
headers = HTTP::Headers.new
|
|
|
|
|
|
|
|
headers["Cookie"] = env.request.headers["Cookie"]
|
|
|
|
|
|
|
|
|
|
|
|
sid = env.request.cookies["SID"].value
|
|
|
|
sid = env.request.cookies["SID"].value
|
|
|
|
env.set "sid", sid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subscriptions = PG_DB.query_one?("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String))
|
|
|
|
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
|
|
|
|
env.set "subscriptions", subscriptions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notifications = PG_DB.query_one?("SELECT cardinality(notifications) FROM users WHERE id = $1", sid, as: Int32)
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
notifications ||= 0
|
|
|
|
user = get_user(sid, client, headers, PG_DB, false)
|
|
|
|
|
|
|
|
|
|
|
|
env.set "notifications", notifications
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if env.request.cookies.has_key?("darktheme") && env.request.cookies["darktheme"].value == "true"
|
|
|
|
env.set "user", user
|
|
|
|
env.set "darktheme", true
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -240,9 +232,11 @@ get "/" do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
get "/watch" do |env|
|
|
|
|
get "/watch" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
if authorized
|
|
|
|
if user
|
|
|
|
subscriptions = env.get("subscriptions").as(Array(String))
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
preferences = user.preferences
|
|
|
|
|
|
|
|
subscriptions = user.subscriptions.as(Array(String))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
|
|
|
|
|
|
|
@ -670,11 +664,73 @@ get "/signout" do |env|
|
|
|
|
env.redirect referer
|
|
|
|
env.redirect referer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get "/preferences" do |env|
|
|
|
|
|
|
|
|
user = env.get? "user"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
|
|
|
|
referer ||= "/preferences"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if referer.size > 64
|
|
|
|
|
|
|
|
puts "nope"
|
|
|
|
|
|
|
|
referer = "/preferences"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user
|
|
|
|
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
templated "preferences"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
env.redirect referer
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
post "/preferences" do |env|
|
|
|
|
|
|
|
|
user = env.get? "user"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
referer = env.params.query["referer"]?
|
|
|
|
|
|
|
|
referer ||= "/preferences"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user
|
|
|
|
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
video_loop = env.params.body["video_loop"]?.try &.as(String)
|
|
|
|
|
|
|
|
video_loop ||= "off"
|
|
|
|
|
|
|
|
video_loop = video_loop == "on"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
autoplay = env.params.body["autoplay"]?.try &.as(String)
|
|
|
|
|
|
|
|
autoplay ||= "off"
|
|
|
|
|
|
|
|
autoplay = autoplay == "on"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quality = env.params.body["quality"]?.try &.as(String)
|
|
|
|
|
|
|
|
quality ||= "hd720"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
volume = env.params.body["volume"]?.try &.as(String).to_i
|
|
|
|
|
|
|
|
volume ||= 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dark_mode = env.params.body["dark_mode"]?.try &.as(String)
|
|
|
|
|
|
|
|
dark_mode ||= "off"
|
|
|
|
|
|
|
|
dark_mode = dark_mode == "on"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preferences = {
|
|
|
|
|
|
|
|
"video_loop" => video_loop,
|
|
|
|
|
|
|
|
"autoplay" => autoplay,
|
|
|
|
|
|
|
|
"quality" => quality,
|
|
|
|
|
|
|
|
"volume" => volume,
|
|
|
|
|
|
|
|
"dark_mode" => dark_mode,
|
|
|
|
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PG_DB.exec("UPDATE users SET preferences = $1 WHERE email = $2", preferences, user.email)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env.redirect referer
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Get subscriptions for authorized user
|
|
|
|
# Get subscriptions for authorized user
|
|
|
|
get "/feed/subscriptions" do |env|
|
|
|
|
get "/feed/subscriptions" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user
|
|
|
|
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
|
|
|
|
if authorized
|
|
|
|
|
|
|
|
max_results = env.params.query["maxResults"]?.try &.to_i || 40
|
|
|
|
max_results = env.params.query["maxResults"]?.try &.to_i || 40
|
|
|
|
|
|
|
|
|
|
|
|
page = env.params.query["page"]?.try &.to_i
|
|
|
|
page = env.params.query["page"]?.try &.to_i
|
|
|
@ -688,14 +744,6 @@ get "/feed/subscriptions" do |env|
|
|
|
|
offset = (page - 1) * max_results
|
|
|
|
offset = (page - 1) * max_results
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
headers = HTTP::Headers.new
|
|
|
|
|
|
|
|
headers["Cookie"] = env.request.headers["Cookie"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sid = env.get("sid").as(String)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
|
|
|
|
user = get_user(sid, client, headers, PG_DB)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
@ -709,7 +757,7 @@ get "/feed/subscriptions" do |env|
|
|
|
|
videos = videos[0..max_results]
|
|
|
|
videos = videos[0..max_results]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
PG_DB.exec("UPDATE users SET notifications = $1 WHERE id = $2", [] of String, sid)
|
|
|
|
PG_DB.exec("UPDATE users SET notifications = $1 WHERE id = $2", [] of String, user.id)
|
|
|
|
env.set "notifications", 0
|
|
|
|
env.set "notifications", 0
|
|
|
|
|
|
|
|
|
|
|
|
templated "subscriptions"
|
|
|
|
templated "subscriptions"
|
|
|
@ -726,12 +774,14 @@ end
|
|
|
|
# /modify_notifications?receive_all_updates=false&receive_no_updates=false
|
|
|
|
# /modify_notifications?receive_all_updates=false&receive_no_updates=false
|
|
|
|
# will "unding" all subscriptions.
|
|
|
|
# will "unding" all subscriptions.
|
|
|
|
get "/modify_notifications" do |env|
|
|
|
|
get "/modify_notifications" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
|
|
|
|
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
referer ||= "/"
|
|
|
|
referer ||= "/"
|
|
|
|
|
|
|
|
|
|
|
|
if authorized
|
|
|
|
if user
|
|
|
|
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
|
|
|
|
channel_req = {} of String => String
|
|
|
|
channel_req = {} of String => String
|
|
|
|
|
|
|
|
|
|
|
|
channel_req["receive_all_updates"] = env.params.query["receive_all_updates"]? || "true"
|
|
|
|
channel_req["receive_all_updates"] = env.params.query["receive_all_updates"]? || "true"
|
|
|
@ -770,12 +820,14 @@ get "/modify_notifications" do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
get "/subscription_manager" do |env|
|
|
|
|
get "/subscription_manager" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
if !authorized
|
|
|
|
|
|
|
|
|
|
|
|
if !user
|
|
|
|
next env.redirect "/"
|
|
|
|
next env.redirect "/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
subscriptions = env.get?("subscriptions").as(Array(String))
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
subscriptions = user.subscriptions
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
|
|
|
|
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
client = make_client(YT_URL)
|
|
|
@ -788,11 +840,13 @@ get "/subscription_manager" do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
get "/subscription_ajax" do |env|
|
|
|
|
get "/subscription_ajax" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
referer ||= "/"
|
|
|
|
referer ||= "/"
|
|
|
|
|
|
|
|
|
|
|
|
if authorized
|
|
|
|
if user
|
|
|
|
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
|
|
|
|
if env.params.query["action_create_subscription_to_channel"]?
|
|
|
|
if env.params.query["action_create_subscription_to_channel"]?
|
|
|
|
action = "action_create_subscription_to_channel"
|
|
|
|
action = "action_create_subscription_to_channel"
|
|
|
|
elsif env.params.query["action_remove_subscriptions"]?
|
|
|
|
elsif env.params.query["action_remove_subscriptions"]?
|
|
|
@ -827,7 +881,7 @@ get "/subscription_ajax" do |env|
|
|
|
|
|
|
|
|
|
|
|
|
# Update user
|
|
|
|
# Update user
|
|
|
|
if client.post(post_url, headers, post_req).status_code == 200
|
|
|
|
if client.post(post_url, headers, post_req).status_code == 200
|
|
|
|
sid = env.get("sid").as(String)
|
|
|
|
sid = user.id
|
|
|
|
|
|
|
|
|
|
|
|
case action
|
|
|
|
case action
|
|
|
|
when .starts_with? "action_create"
|
|
|
|
when .starts_with? "action_create"
|
|
|
@ -847,11 +901,10 @@ get "/user/:user" do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
get "/channel/:ucid" do |env|
|
|
|
|
get "/channel/:ucid" do |env|
|
|
|
|
authorized = env.get? "authorized"
|
|
|
|
user = env.get? "user"
|
|
|
|
if authorized
|
|
|
|
if user
|
|
|
|
sid = env.get("sid").as(String)
|
|
|
|
user = user.as(User)
|
|
|
|
|
|
|
|
subscriptions = user.subscriptions
|
|
|
|
subscriptions = PG_DB.query_one?("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String))
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
subscriptions ||= [] of String
|
|
|
|
|
|
|
|
|
|
|
@ -889,20 +942,6 @@ get "/channel/:ucid" do |env|
|
|
|
|
templated "channel"
|
|
|
|
templated "channel"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
get "/modify_theme" do |env|
|
|
|
|
|
|
|
|
referer = env.request.headers["referer"]?
|
|
|
|
|
|
|
|
referer ||= "/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if env.params.query["dark"]?
|
|
|
|
|
|
|
|
env.response.cookies["darktheme"] = "true"
|
|
|
|
|
|
|
|
elsif env.params.query["light"]?
|
|
|
|
|
|
|
|
env.request.cookies["darktheme"].expires = Time.new(1990, 1, 1)
|
|
|
|
|
|
|
|
env.request.cookies.add_response_headers(env.response.headers)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env.redirect referer
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get "/redirect" do |env|
|
|
|
|
get "/redirect" do |env|
|
|
|
|
if env.params.query["q"]?
|
|
|
|
if env.params.query["q"]?
|
|
|
|
env.redirect env.params.query["q"]
|
|
|
|
env.redirect env.params.query["q"]
|
|
|
@ -1158,6 +1197,6 @@ end
|
|
|
|
public_folder "assets"
|
|
|
|
public_folder "assets"
|
|
|
|
|
|
|
|
|
|
|
|
add_handler FilteredCompressHandler.new
|
|
|
|
add_handler FilteredCompressHandler.new
|
|
|
|
add_context_storage_type(Array(String))
|
|
|
|
add_context_storage_type(User)
|
|
|
|
|
|
|
|
|
|
|
|
Kemal.run
|
|
|
|
Kemal.run
|
|
|
|