Skip to content

Commit fc5f84a

Browse files
authored
Merge pull request #2827 from SamantazFox/more-code-cleanup
More code cleanup
2 parents 2289f98 + 519c227 commit fc5f84a

30 files changed

+193
-305
lines changed

scripts/git/pre-commit

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,3 @@ if [ ! -z "$changed_cr_files" ]; then
1515

1616
git add $changed_cr_files
1717
fi
18-
19-
# Locale equalizer
20-
if [ ! -z $(git diff --name-only --cached -- locales/) ]; then
21-
crystal run scripts/propagate-new-locale-keys.cr
22-
git add locales > /dev/null
23-
fi

scripts/propagate-new-locale-keys.cr

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/invidious/channels/about.cr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get_about_info(ucid, locale) : AboutChannel
9696
total_views = channel_about_meta["viewCountText"]?.try &.["simpleText"]?.try &.as_s.gsub(/\D/, "").to_i64? || 0_i64
9797

9898
# The joined text is split to several sub strings. The reduce joins those strings before parsing the date.
99-
joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, node| acc + node["text"].as_s }
99+
joined = channel_about_meta["joinedDateText"]?.try &.["runs"]?.try &.as_a.reduce("") { |acc, nd| acc + nd["text"].as_s }
100100
.try { |text| Time.parse(text, "Joined %b %-d, %Y", Time::Location.local) } || Time.unix(0)
101101

102102
# Normal Auto-generated channels
@@ -136,7 +136,8 @@ def fetch_related_channels(about_channel : AboutChannel) : Array(AboutRelatedCha
136136
channels = YoutubeAPI.browse(browse_id: about_channel.ucid, params: "EghjaGFubmVscw%3D%3D")
137137

138138
tabs = channels.dig?("contents", "twoColumnBrowseResultsRenderer", "tabs").try(&.as_a?) || [] of JSON::Any
139-
tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" }
139+
tab = tabs.find(&.dig?("tabRenderer", "title").try(&.as_s?).try(&.== "Channels"))
140+
140141
return [] of AboutRelatedChannel if tab.nil?
141142

142143
items = tab.dig?("tabRenderer", "content", "sectionListRenderer", "contents", 0, "itemSectionRenderer", "contents", 0, "gridRenderer", "items").try(&.as_a?) || [] of JSON::Any

src/invidious/channels/channels.cr

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ struct ChannelVideo
4444
end
4545
end
4646

47-
def to_json(locale, json : JSON::Builder | Nil = nil)
48-
if json
47+
def to_json(locale, _json : Nil = nil)
48+
JSON.build do |json|
4949
to_json(locale, json)
50-
else
51-
JSON.build do |json|
52-
to_json(locale, json)
53-
end
5450
end
5551
end
5652

@@ -88,13 +84,9 @@ struct ChannelVideo
8884
end
8985
end
9086

91-
def to_xml(locale, xml : XML::Builder | Nil = nil)
92-
if xml
87+
def to_xml(locale, _xml : Nil = nil)
88+
XML.build do |xml|
9389
to_xml(locale, xml)
94-
else
95-
XML.build do |xml|
96-
to_xml(locale, xml)
97-
end
9890
end
9991
end
10092

src/invidious/comments.cr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ def fetch_youtube_comments(id, cursor, format, locale, thin_mode, region, sort_b
9393
end
9494
contents = body["contents"]?
9595
header = body["header"]?
96-
if body["continuations"]?
97-
# Removable? Doesn't seem like this is used.
98-
more_replies_continuation = body["continuations"][0]["nextContinuationData"]["continuation"].as_s
99-
end
10096
else
10197
raise InfoException.new("Could not fetch comments")
10298
end

src/invidious/helpers/errors.cr

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
class InfoException < Exception
77
end
88

9+
# -------------------
10+
# Issue template
11+
# -------------------
12+
913
macro error_template(*args)
10-
error_template_helper(env, locale, {{*args}})
14+
error_template_helper(env, {{*args}})
1115
end
1216

1317
def github_details(summary : String, content : String)
@@ -22,11 +26,13 @@ def github_details(summary : String, content : String)
2226
return HTML.escape(details)
2327
end
2428

25-
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
29+
def error_template_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
2630
if exception.is_a?(InfoException)
27-
return error_template_helper(env, locale, status_code, exception.message || "")
31+
return error_template_helper(env, status_code, exception.message || "")
2832
end
2933

34+
locale = env.get("preferences").as(Preferences).locale
35+
3036
env.response.content_type = "text/html"
3137
env.response.status_code = status_code
3238

@@ -77,71 +83,101 @@ def error_template_helper(env : HTTP::Server::Context, locale : String?, status_
7783
return templated "error"
7884
end
7985

80-
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
86+
def error_template_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
8187
env.response.content_type = "text/html"
8288
env.response.status_code = status_code
89+
90+
locale = env.get("preferences").as(Preferences).locale
91+
8392
error_message = translate(locale, message)
84-
next_steps = error_redirect_helper(env, locale)
93+
next_steps = error_redirect_helper(env)
94+
8595
return templated "error"
8696
end
8797

98+
# -------------------
99+
# Atom feeds
100+
# -------------------
101+
88102
macro error_atom(*args)
89-
error_atom_helper(env, locale, {{*args}})
103+
error_atom_helper(env, {{*args}})
90104
end
91105

92-
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
106+
def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
93107
if exception.is_a?(InfoException)
94-
return error_atom_helper(env, locale, status_code, exception.message || "")
108+
return error_atom_helper(env, status_code, exception.message || "")
95109
end
110+
96111
env.response.content_type = "application/atom+xml"
97112
env.response.status_code = status_code
113+
98114
return "<error>#{exception.inspect_with_backtrace}</error>"
99115
end
100116

101-
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
117+
def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
102118
env.response.content_type = "application/atom+xml"
103119
env.response.status_code = status_code
120+
104121
return "<error>#{message}</error>"
105122
end
106123

124+
# -------------------
125+
# JSON
126+
# -------------------
127+
107128
macro error_json(*args)
108-
error_json_helper(env, locale, {{*args}})
129+
error_json_helper(env, {{*args}})
109130
end
110131

111-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
132+
def error_json_helper(
133+
env : HTTP::Server::Context,
134+
status_code : Int32,
135+
exception : Exception,
136+
additional_fields : Hash(String, Object) | Nil = nil
137+
)
112138
if exception.is_a?(InfoException)
113-
return error_json_helper(env, locale, status_code, exception.message || "", additional_fields)
139+
return error_json_helper(env, status_code, exception.message || "", additional_fields)
114140
end
141+
115142
env.response.content_type = "application/json"
116143
env.response.status_code = status_code
144+
117145
error_message = {"error" => exception.message, "errorBacktrace" => exception.inspect_with_backtrace}
146+
118147
if additional_fields
119148
error_message = error_message.merge(additional_fields)
120149
end
121-
return error_message.to_json
122-
end
123150

124-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
125-
return error_json_helper(env, locale, status_code, exception, nil)
151+
return error_message.to_json
126152
end
127153

128-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
154+
def error_json_helper(
155+
env : HTTP::Server::Context,
156+
status_code : Int32,
157+
message : String,
158+
additional_fields : Hash(String, Object) | Nil = nil
159+
)
129160
env.response.content_type = "application/json"
130161
env.response.status_code = status_code
162+
131163
error_message = {"error" => message}
164+
132165
if additional_fields
133166
error_message = error_message.merge(additional_fields)
134167
end
168+
135169
return error_message.to_json
136170
end
137171

138-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
139-
error_json_helper(env, locale, status_code, message, nil)
140-
end
172+
# -------------------
173+
# Redirect
174+
# -------------------
141175

142-
def error_redirect_helper(env : HTTP::Server::Context, locale : String?)
176+
def error_redirect_helper(env : HTTP::Server::Context)
143177
request_path = env.request.path
144178

179+
locale = env.get("preferences").as(Preferences).locale
180+
145181
if request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||
146182
request_path.starts_with?("/channel") || request_path.starts_with?("/playlist?list=PL")
147183
next_steps_text = translate(locale, "next_steps_error_message")

src/invidious/helpers/i18n.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def translate(locale : String?, key : String, text : String | Nil = nil) : Strin
9494
translation = ""
9595
match_length = 0
9696

97-
raw_data.as_h.each do |key, value|
98-
if md = text.try &.match(/#{key}/)
97+
raw_data.as_h.each do |hash_key, value|
98+
if md = text.try &.match(/#{hash_key}/)
9999
if md[0].size >= match_length
100100
translation = value.as_s
101101
match_length = md[0].size

src/invidious/helpers/json_filter.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ module JSONFilter
9898
end
9999
end
100100

101-
group_name.split('/').each do |group_name|
101+
group_name.split('/').each do |name|
102102
nest_stack.push({
103-
group_name: group_name,
103+
group_name: name,
104104
closing_bracket_index: closing_bracket_index,
105105
})
106106
end

src/invidious/helpers/static_file_handler.cr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ module Kemal
175175

176176
if @cached_files.sum(&.[1][:data].bytesize) + (size = File.size(file_path)) < CACHE_LIMIT
177177
data = Bytes.new(size)
178-
File.open(file_path) do |file|
179-
file.read(data)
180-
end
178+
File.open(file_path, &.read(data))
179+
181180
filestat = File.info(file_path)
182181

183182
@cached_files[file_path] = {data: data, filestat: filestat}

src/invidious/helpers/tokens.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ end
4242
def sign_token(key, hash)
4343
string_to_sign = [] of String
4444

45+
# TODO: figure out which "key" variable is used
46+
# Ameba reports a warning for "Lint/ShadowingOuterLocalVar" on this
47+
# variable, but its preferrable to not touch that (works fine atm).
4548
hash.each do |key, value|
4649
next if key == "signature"
4750

0 commit comments

Comments
 (0)