Fix warnings in latest version of Crystal

This commit is contained in:
Omar Roth 2020-04-09 12:18:09 -05:00
parent be655ee328
commit ca1185d0be
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2
11 changed files with 119 additions and 79 deletions

View File

@ -385,6 +385,8 @@ get "/" do |env|
else else
templated "popular" templated "popular"
end end
else
templated "empty"
end end
end end
@ -722,6 +724,7 @@ get "/embed/:id" do |env|
end end
next env.redirect url next env.redirect url
else nil # Continue
end end
params = process_video_params(env.params.query, preferences) params = process_video_params(env.params.query, preferences)
@ -1213,6 +1216,10 @@ post "/playlist_ajax" do |env|
error_message = {"error" => "Playlist cannot have more than 500 videos"}.to_json error_message = {"error" => "Playlist cannot have more than 500 videos"}.to_json
next error_message next error_message
end end
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
next error_message
end end
video_id = env.params.query["video_id"] video_id = env.params.query["video_id"]
@ -1253,6 +1260,10 @@ post "/playlist_ajax" do |env|
PG_DB.exec("UPDATE playlists SET index = array_remove(index, $1), video_count = cardinality(index), updated = $2 WHERE id = $3", index, Time.utc, playlist_id) PG_DB.exec("UPDATE playlists SET index = array_remove(index, $1), video_count = cardinality(index), updated = $2 WHERE id = $3", index, Time.utc, playlist_id)
when "action_move_video_before" when "action_move_video_before"
# TODO: Playlist stub # TODO: Playlist stub
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
next error_message
end end
if redirect if redirect
@ -1547,7 +1558,7 @@ post "/login" do |env|
case prompt_type case prompt_type
when "TWO_STEP_VERIFICATION" when "TWO_STEP_VERIFICATION"
prompt_type = 2 prompt_type = 2
when "LOGIN_CHALLENGE" else # "LOGIN_CHALLENGE"
prompt_type = 4 prompt_type = 4
end end
@ -1840,7 +1851,7 @@ post "/login" do |env|
env.response.status_code = 400 env.response.status_code = 400
next templated "error" next templated "error"
end end
when "text" else # "text"
answer = Digest::MD5.hexdigest(answer.downcase.strip) answer = Digest::MD5.hexdigest(answer.downcase.strip)
found_valid_captcha = false found_valid_captcha = false
@ -2251,6 +2262,10 @@ post "/watch_ajax" do |env|
end end
when "action_mark_unwatched" when "action_mark_unwatched"
PG_DB.exec("UPDATE users SET watched = array_remove(watched, $1) WHERE email = $2", id, user.email) PG_DB.exec("UPDATE users SET watched = array_remove(watched, $1) WHERE email = $2", id, user.email)
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
next error_message
end end
if redirect if redirect
@ -2405,6 +2420,10 @@ post "/subscription_ajax" do |env|
end end
when "action_remove_subscriptions" when "action_remove_subscriptions"
PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = array_remove(subscriptions, $1) WHERE email = $2", channel_id, email) PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = array_remove(subscriptions, $1) WHERE email = $2", channel_id, email)
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
next error_message
end end
if redirect if redirect
@ -2559,6 +2578,7 @@ post "/data_control" do |env|
next next
end end
# TODO: Unify into single import based on content-type
case part.name case part.name
when "import_invidious" when "import_invidious"
body = JSON.parse(body) body = JSON.parse(body)
@ -2645,6 +2665,7 @@ post "/data_control" do |env|
end end
end end
end end
else nil # Ignore
end end
end end
end end
@ -2986,6 +3007,10 @@ post "/token_ajax" do |env|
case action case action
when .starts_with? "action_revoke_token" when .starts_with? "action_revoke_token"
PG_DB.exec("DELETE FROM session_ids * WHERE id = $1 AND email = $2", session, user.email) PG_DB.exec("DELETE FROM session_ids * WHERE id = $1 AND email = $2", session, user.email)
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
next error_message
end end
if redirect if redirect
@ -3280,6 +3305,7 @@ get "/feed/playlist/:plid" do |env|
full_path = URI.parse(node[attribute.name]).full_path full_path = URI.parse(node[attribute.name]).full_path
query_string_opt = full_path.starts_with?("/watch?v=") ? "&#{params}" : "" query_string_opt = full_path.starts_with?("/watch?v=") ? "&#{params}" : ""
node[attribute.name] = "#{host_url}#{full_path}#{query_string_opt}" node[attribute.name] = "#{host_url}#{full_path}#{query_string_opt}"
else nil # Skip
end end
end end
end end
@ -4037,7 +4063,7 @@ get "/api/v1/annotations/:id" do |env|
cache_annotation(PG_DB, id, annotations) cache_annotation(PG_DB, id, annotations)
end end
when "youtube" else # "youtube"
response = YT_POOL.client &.get("/annotations_invideo?video_id=#{id}") response = YT_POOL.client &.get("/annotations_invideo?video_id=#{id}")
if response.status_code != 200 if response.status_code != 200

View File

@ -412,6 +412,7 @@ def fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by)
url += "&sort=da" url += "&sort=da"
when "newest", "newest_created" when "newest", "newest_created"
url += "&sort=dd" url += "&sort=dd"
else nil # Ignore
end end
response = YT_POOL.client &.get(url) response = YT_POOL.client &.get(url)
@ -469,6 +470,7 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil, sort_by = "
object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x01_i64 object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x01_i64
when "oldest" when "oldest"
object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x02_i64 object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 0x02_i64
else nil # Ignore
end end
object["80226972:embedded"]["3:string"] = Base64.urlsafe_encode(Protodec::Any.from_json(Protodec::Any.cast_json(object["80226972:embedded"]["3:base64"]))) object["80226972:embedded"]["3:string"] = Base64.urlsafe_encode(Protodec::Any.from_json(Protodec::Any.cast_json(object["80226972:embedded"]["3:base64"])))
@ -513,6 +515,7 @@ def produce_channel_playlists_url(ucid, cursor, sort = "newest", auto_generated
object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 3_i64 object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 3_i64
when "last", "last_added" when "last", "last_added"
object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 4_i64 object["80226972:embedded"]["3:base64"].as(Hash)["3:varint"] = 4_i64
else nil # Ignore
end end
end end

View File

@ -356,6 +356,7 @@ def template_youtube_comments(comments, locale, thin_mode)
</div> </div>
</div> </div>
END_HTML END_HTML
else nil # Ignore
end end
end end
@ -609,6 +610,8 @@ def produce_comment_continuation(video_id, cursor = "", sort_by = "top")
object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 0_i64 object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 0_i64
when "new", "newest" when "new", "newest"
object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 1_i64 object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 1_i64
else # top
object["6:embedded"].as(Hash)["4:embedded"].as(Hash)["6:varint"] = 0_i64
end end
continuation = object.try { |i| Protodec::Any.cast_json(object) } continuation = object.try { |i| Protodec::Any.cast_json(object) }

View File

@ -173,6 +173,8 @@ struct Config
yaml.scalar "ipv4" yaml.scalar "ipv4"
when Socket::Family::INET6 when Socket::Family::INET6
yaml.scalar "ipv6" yaml.scalar "ipv6"
when Socket::Family::UNIX
raise "Invalid socket family #{value}"
end end
end end
@ -223,6 +225,8 @@ struct Config
else else
return false return false
end end
else
return false
end end
end end
@ -520,9 +524,7 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)
shelf.xpath_nodes(%q(.//ul[contains(@class, "yt-uix-shelfslider-list") or contains(@class, "expanded-shelf-content-list")]/li)).each do |child_node| shelf.xpath_nodes(%q(.//ul[contains(@class, "yt-uix-shelfslider-list") or contains(@class, "expanded-shelf-content-list")]/li)).each do |child_node|
type = child_node.xpath_node(%q(./div)) type = child_node.xpath_node(%q(./div))
if !type next if !type
next
end
case type["class"] case type["class"]
when .includes? "yt-lockup-video" when .includes? "yt-lockup-video"
@ -599,6 +601,8 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)
videos: videos, videos: videos,
thumbnail: playlist_thumbnail thumbnail: playlist_thumbnail
) )
else
next # Skip
end end
end end
@ -763,7 +767,7 @@ def create_notification_stream(env, config, kemal_config, decrypt_function, topi
loop do loop do
time_span = [0, 0, 0, 0] time_span = [0, 0, 0, 0]
time_span[rand(4)] = rand(30) + 5 time_span[rand(4)] = rand(30) + 5
published = Time.utc - Time::Span.new(time_span[0], time_span[1], time_span[2], time_span[3]) published = Time.utc - Time::Span.new(days: time_span[0], hours: time_span[1], minutes: time_span[2], seconds: time_span[3])
video_id = TEST_IDS[rand(TEST_IDS.size)] video_id = TEST_IDS[rand(TEST_IDS.size)]
video = get_video(video_id, PG_DB) video = get_video(video_id, PG_DB)

View File

@ -24,6 +24,8 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text
if !locale[translation].as_s.empty? if !locale[translation].as_s.empty?
translation = locale[translation].as_s translation = locale[translation].as_s
end end
else
raise "Invalid translation #{translation}"
end end
end end

View File

@ -43,15 +43,10 @@ def sign_token(key, hash)
string_to_sign = [] of String string_to_sign = [] of String
hash.each do |key, value| hash.each do |key, value|
if key == "signature" next if key == "signature"
next
end
if value.is_a?(JSON::Any) if value.is_a?(JSON::Any) && value.as_a?
case value value = value.as_a.map { |i| i.as_s }
when .as_a?
value = value.as_a.map { |item| item.as_s }
end
end end
case value case value

View File

@ -100,7 +100,7 @@ end
def decode_length_seconds(string) def decode_length_seconds(string)
length_seconds = string.gsub(/[^0-9:]/, "").split(":").map &.to_i length_seconds = string.gsub(/[^0-9:]/, "").split(":").map &.to_i
length_seconds = [0] * (3 - length_seconds.size) + length_seconds length_seconds = [0] * (3 - length_seconds.size) + length_seconds
length_seconds = Time::Span.new(length_seconds[0], length_seconds[1], length_seconds[2]) length_seconds = Time::Span.new hours: length_seconds[0], minutes: length_seconds[1], seconds: length_seconds[2]
length_seconds = length_seconds.total_seconds.to_i length_seconds = length_seconds.total_seconds.to_i
return length_seconds return length_seconds
@ -162,6 +162,7 @@ def decode_date(string : String)
return Time.utc return Time.utc
when "yesterday" when "yesterday"
return Time.utc - 1.day return Time.utc - 1.day
else nil # Continue
end end
# String matches format "20 hours ago", "4 months ago"... # String matches format "20 hours ago", "4 months ago"...

View File

@ -310,6 +310,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conte
object["2:embedded"].as(Hash)["1:varint"] = 4_i64 object["2:embedded"].as(Hash)["1:varint"] = 4_i64
when "year" when "year"
object["2:embedded"].as(Hash)["1:varint"] = 5_i64 object["2:embedded"].as(Hash)["1:varint"] = 5_i64
else nil # Ignore
end end
case content_type case content_type
@ -334,6 +335,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conte
object["2:embedded"].as(Hash)["3:varint"] = 1_i64 object["2:embedded"].as(Hash)["3:varint"] = 1_i64
when "long" when "long"
object["2:embedded"].as(Hash)["3:varint"] = 2_i64 object["2:embedded"].as(Hash)["3:varint"] = 2_i64
else nil # Ignore
end end
features.each do |feature| features.each do |feature|
@ -358,6 +360,7 @@ def produce_search_params(sort : String = "relevance", date : String = "", conte
object["2:embedded"].as(Hash)["23:varint"] = 1_i64 object["2:embedded"].as(Hash)["23:varint"] = 1_i64
when "hdr" when "hdr"
object["2:embedded"].as(Hash)["25:varint"] = 1_i64 object["2:embedded"].as(Hash)["25:varint"] = 1_i64
else nil # Ignore
end end
end end

View File

@ -350,6 +350,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)
notifications.sort_by! { |video| video.author } notifications.sort_by! { |video| video.author }
when "channel name - reverse" when "channel name - reverse"
notifications.sort_by! { |video| video.author }.reverse! notifications.sort_by! { |video| video.author }.reverse!
else nil # Ignore
end end
else else
if user.preferences.latest_only if user.preferences.latest_only
@ -398,6 +399,7 @@ def get_subscription_feed(db, user, max_results = 40, page = 1)
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!
else nil # Ignore
end end
notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, as: Array(String)) notifications = PG_DB.query_one("SELECT notifications FROM users WHERE email = $1", user.email, as: Array(String))

View File

@ -1250,6 +1250,7 @@ def fetch_video(id, region)
genre_url = "/channel/UCfFyYRYslvuhwMDnx6KjUvw" genre_url = "/channel/UCfFyYRYslvuhwMDnx6KjUvw"
when "Trailers" when "Trailers"
genre_url = "/channel/UClgRkhTL3_hImCAmdLfDE4g" genre_url = "/channel/UClgRkhTL3_hImCAmdLfDE4g"
else nil # Ignore
end end
license = html.xpath_node(%q(//h4[contains(text(),"License")]/parent::*/ul/li)).try &.content || "" license = html.xpath_node(%q(//h4[contains(text(),"License")]/parent::*/ul/li)).try &.content || ""

View File

@ -22,69 +22,6 @@
<hr> <hr>
<% case account_type when %> <% case account_type when %>
<% when "invidious" %>
<form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=invidious" method="post">
<fieldset>
<% if email %>
<input name="email" type="hidden" value="<%= email %>">
<% else %>
<label for="email"><%= translate(locale, "User ID") %> :</label>
<input required class="pure-input-1" name="email" type="text" placeholder="<%= translate(locale, "User ID") %>">
<% end %>
<% if password %>
<input name="password" type="hidden" value="<%= HTML.escape(password) %>">
<% else %>
<label for="password"><%= translate(locale, "Password") %> :</label>
<input required class="pure-input-1" name="password" type="password" placeholder="<%= translate(locale, "Password") %>">
<% end %>
<% if captcha %>
<% case captcha_type when %>
<% when "image" %>
<% captcha = captcha.not_nil! %>
<img style="width:50%" src='<%= captcha[:question] %>'/>
<% captcha[:tokens].each_with_index do |token, i| %>
<input type="hidden" name="token[<%= i %>]" value="<%= URI.encode_www_form(token) %>">
<% end %>
<input type="hidden" name="captcha_type" value="image">
<label for="answer"><%= translate(locale, "Time (h:mm:ss):") %></label>
<input type="text" name="answer" type="text" placeholder="h:mm:ss">
<% when "text" %>
<% captcha = captcha.not_nil! %>
<% captcha[:tokens].each_with_index do |token, i| %>
<input type="hidden" name="token[<%= i %>]" value="<%= URI.encode_www_form(token) %>">
<% end %>
<input type="hidden" name="captcha_type" value="text">
<label for="answer"><%= captcha[:question] %></label>
<input type="text" name="answer" type="text" placeholder="<%= translate(locale, "Answer") %>">
<% end %>
<button type="submit" name="action" value="signin" class="pure-button pure-button-primary">
<%= translate(locale, "Register") %>
</button>
<% case captcha_type when %>
<% when "image" %>
<label>
<button type="submit" name="change_type" class="pure-button pure-button-primary" value="text">
<%= translate(locale, "Text CAPTCHA") %>
</button>
</label>
<% when "text" %>
<label>
<button type="submit" name="change_type" class="pure-button pure-button-primary" value="image">
<%= translate(locale, "Image CAPTCHA") %>
</button>
</label>
<% end %>
<% else %>
<button type="submit" name="action" value="signin" class="pure-button pure-button-primary">
<%= translate(locale, "Sign In") %>/<%= translate(locale, "Register") %>
</button>
<% end %>
</fieldset>
</form>
<% when "google" %> <% when "google" %>
<form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=google" method="post"> <form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=google" method="post">
<fieldset> <fieldset>
@ -121,6 +58,69 @@
<button type="submit" class="pure-button pure-button-primary"><%= translate(locale, "Sign In") %></button> <button type="submit" class="pure-button pure-button-primary"><%= translate(locale, "Sign In") %></button>
</fieldset> </fieldset>
</form> </form>
<% else # "invidious" %>
<form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.encode_www_form(referer) %>&type=invidious" method="post">
<fieldset>
<% if email %>
<input name="email" type="hidden" value="<%= email %>">
<% else %>
<label for="email"><%= translate(locale, "User ID") %> :</label>
<input required class="pure-input-1" name="email" type="text" placeholder="<%= translate(locale, "User ID") %>">
<% end %>
<% if password %>
<input name="password" type="hidden" value="<%= HTML.escape(password) %>">
<% else %>
<label for="password"><%= translate(locale, "Password") %> :</label>
<input required class="pure-input-1" name="password" type="password" placeholder="<%= translate(locale, "Password") %>">
<% end %>
<% if captcha %>
<% case captcha_type when %>
<% when "image" %>
<% captcha = captcha.not_nil! %>
<img style="width:50%" src='<%= captcha[:question] %>'/>
<% captcha[:tokens].each_with_index do |token, i| %>
<input type="hidden" name="token[<%= i %>]" value="<%= URI.encode_www_form(token) %>">
<% end %>
<input type="hidden" name="captcha_type" value="image">
<label for="answer"><%= translate(locale, "Time (h:mm:ss):") %></label>
<input type="text" name="answer" type="text" placeholder="h:mm:ss">
<% else # "text" %>
<% captcha = captcha.not_nil! %>
<% captcha[:tokens].each_with_index do |token, i| %>
<input type="hidden" name="token[<%= i %>]" value="<%= URI.encode_www_form(token) %>">
<% end %>
<input type="hidden" name="captcha_type" value="text">
<label for="answer"><%= captcha[:question] %></label>
<input type="text" name="answer" type="text" placeholder="<%= translate(locale, "Answer") %>">
<% end %>
<button type="submit" name="action" value="signin" class="pure-button pure-button-primary">
<%= translate(locale, "Register") %>
</button>
<% case captcha_type when %>
<% when "image" %>
<label>
<button type="submit" name="change_type" class="pure-button pure-button-primary" value="text">
<%= translate(locale, "Text CAPTCHA") %>
</button>
</label>
<% else # "text" %>
<label>
<button type="submit" name="change_type" class="pure-button pure-button-primary" value="image">
<%= translate(locale, "Image CAPTCHA") %>
</button>
</label>
<% end %>
<% else %>
<button type="submit" name="action" value="signin" class="pure-button pure-button-primary">
<%= translate(locale, "Sign In") %>/<%= translate(locale, "Register") %>
</button>
<% end %>
</fieldset>
</form>
<% end %> <% end %>
</div> </div>
</div> </div>