Switch routing logic to use modules (#2298)

* Switch routing logic to use modules
* Add more macros for adding routes of different HTTP methods
This commit is contained in:
syeopite 2021-08-11 03:36:25 -07:00 committed by GitHub
parent 637a5cc14f
commit 7afa027b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 52 additions and 58 deletions

View File

@ -1,2 +0,0 @@
abstract class Invidious::Routes::BaseRoute
end

View File

@ -1,9 +1,9 @@
class Invidious::Routes::Channels < Invidious::Routes::BaseRoute module Invidious::Routes::Channels
def home(env) def self.home(env)
self.videos(env) self.videos(env)
end end
def videos(env) def self.videos(env)
data = self.fetch_basic_information(env) data = self.fetch_basic_information(env)
if !data.is_a?(Tuple) if !data.is_a?(Tuple)
return data return data
@ -40,7 +40,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
templated "channel" templated "channel"
end end
def playlists(env) def self.playlists(env)
data = self.fetch_basic_information(env) data = self.fetch_basic_information(env)
if !data.is_a?(Tuple) if !data.is_a?(Tuple)
return data return data
@ -62,7 +62,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
templated "playlists" templated "playlists"
end end
def community(env) def self.community(env)
data = self.fetch_basic_information(env) data = self.fetch_basic_information(env)
if !data.is_a?(Tuple) if !data.is_a?(Tuple)
return data return data
@ -91,7 +91,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
templated "community" templated "community"
end end
def about(env) def self.about(env)
data = self.fetch_basic_information(env) data = self.fetch_basic_information(env)
if !data.is_a?(Tuple) if !data.is_a?(Tuple)
return data return data
@ -102,7 +102,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
end end
# Redirects brand url channels to a normal /channel/:ucid route # Redirects brand url channels to a normal /channel/:ucid route
def brand_redirect(env) def self.brand_redirect(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
# /attribution_link endpoint needs both the `a` and `u` parameter # /attribution_link endpoint needs both the `a` and `u` parameter
@ -131,7 +131,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
end end
# Handles redirects for the /profile endpoint # Handles redirects for the /profile endpoint
def profile(env) def self.profile(env)
# The /profile endpoint is special. If passed into the resolve_url # The /profile endpoint is special. If passed into the resolve_url
# endpoint YouTube would return a sign in page instead of an /channel/:ucid # endpoint YouTube would return a sign in page instead of an /channel/:ucid
# thus we'll add an edge case and handle it here. # thus we'll add an edge case and handle it here.
@ -146,7 +146,7 @@ class Invidious::Routes::Channels < Invidious::Routes::BaseRoute
end end
end end
private def fetch_basic_information(env) private def self.fetch_basic_information(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Embed < Invidious::Routes::BaseRoute module Invidious::Routes::Embed
def redirect(env) def self.redirect(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
if plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") if plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")
@ -23,7 +23,7 @@ class Invidious::Routes::Embed < Invidious::Routes::BaseRoute
env.redirect url env.redirect url
end end
def show(env) def self.show(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
id = env.params.url["id"] id = env.params.url["id"]

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Login < Invidious::Routes::BaseRoute module Invidious::Routes::Login
def login_page(env) def self.login_page(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -28,7 +28,7 @@ class Invidious::Routes::Login < Invidious::Routes::BaseRoute
templated "login" templated "login"
end end
def login(env) def self.login(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
referer = get_referer(env, "/feed/subscriptions") referer = get_referer(env, "/feed/subscriptions")
@ -475,7 +475,7 @@ class Invidious::Routes::Login < Invidious::Routes::BaseRoute
end end
end end
def signout(env) def self.signout(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Misc < Invidious::Routes::BaseRoute module Invidious::Routes::Misc
def home(env) def self.home(env)
preferences = env.get("preferences").as(Preferences) preferences = env.get("preferences").as(Preferences)
locale = LOCALES[preferences.locale]? locale = LOCALES[preferences.locale]?
user = env.get? "user" user = env.get? "user"
@ -26,17 +26,17 @@ class Invidious::Routes::Misc < Invidious::Routes::BaseRoute
end end
end end
def privacy(env) def self.privacy(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
templated "privacy" templated "privacy"
end end
def licenses(env) def self.licenses(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
rendered "licenses" rendered "licenses"
end end
def cross_instance_redirect(env) def self.cross_instance_redirect(env)
referer = get_referer(env) referer = get_referer(env)
if !env.get("preferences").as(Preferences).automatic_instance_redirect if !env.get("preferences").as(Preferences).automatic_instance_redirect

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute module Invidious::Routes::Playlists
def index(env) def self.index(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -24,7 +24,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "view_all_playlists" templated "view_all_playlists"
end end
def new(env) def self.new(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -40,7 +40,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "create_playlist" templated "create_playlist"
end end
def create(env) def self.create(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -78,7 +78,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
env.redirect "/playlist?list=#{playlist.id}" env.redirect "/playlist?list=#{playlist.id}"
end end
def subscribe(env) def self.subscribe(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -95,7 +95,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
env.redirect "/playlist?list=#{playlist.id}" env.redirect "/playlist?list=#{playlist.id}"
end end
def delete_page(env) def self.delete_page(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -118,7 +118,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "delete_playlist" templated "delete_playlist"
end end
def delete(env) def self.delete(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -151,7 +151,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
env.redirect "/view_all_playlists" env.redirect "/view_all_playlists"
end end
def edit(env) def self.edit(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -191,7 +191,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "edit_playlist" templated "edit_playlist"
end end
def update(env) def self.update(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -235,7 +235,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
env.redirect "/playlist?list=#{plid}" env.redirect "/playlist?list=#{plid}"
end end
def add_playlist_items_page(env) def self.add_playlist_items_page(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -282,7 +282,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "add_playlist_items" templated "add_playlist_items"
end end
def playlist_ajax(env) def self.playlist_ajax(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get? "user" user = env.get? "user"
@ -409,7 +409,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
end end
end end
def show(env) def self.show(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
user = env.get?("user").try &.as(User) user = env.get?("user").try &.as(User)
@ -457,7 +457,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
templated "playlist" templated "playlist"
end end
def mix(env) def self.mix(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
rdid = env.params.query["list"]? rdid = env.params.query["list"]?

View File

@ -1,5 +1,5 @@
class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute module Invidious::Routes::PreferencesRoute
def show(env) def self.show(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
referer = get_referer(env) referer = get_referer(env)
@ -9,7 +9,7 @@ class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute
templated "preferences" templated "preferences"
end end
def update(env) def self.update(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
referer = get_referer(env) referer = get_referer(env)
@ -219,7 +219,7 @@ class Invidious::Routes::PreferencesRoute < Invidious::Routes::BaseRoute
env.redirect referer env.redirect referer
end end
def toggle_theme(env) def self.toggle_theme(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
referer = get_referer(env, unroll: false) referer = get_referer(env, unroll: false)

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Search < Invidious::Routes::BaseRoute module Invidious::Routes::Search
def opensearch(env) def self.opensearch(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
env.response.content_type = "application/opensearchdescription+xml" env.response.content_type = "application/opensearchdescription+xml"
@ -15,7 +15,7 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute
end end
end end
def results(env) def self.results(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
query = env.params.query["search_query"]? query = env.params.query["search_query"]?
@ -34,7 +34,7 @@ class Invidious::Routes::Search < Invidious::Routes::BaseRoute
end end
end end
def search(env) def self.search(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
region = env.params.query["region"]? region = env.params.query["region"]?

View File

@ -1,5 +1,5 @@
class Invidious::Routes::Watch < Invidious::Routes::BaseRoute module Invidious::Routes::Watch
def handle(env) def self.handle(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]? locale = LOCALES[env.get("preferences").as(Preferences).locale]?
region = env.params.query["region"]? region = env.params.query["region"]?
@ -190,7 +190,7 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRoute
templated "watch" templated "watch"
end end
def redirect(env) def self.redirect(env)
url = "/watch?v=#{env.params.url["id"]}" url = "/watch?v=#{env.params.url["id"]}"
if env.params.query.size > 0 if env.params.query.size > 0
url += "&#{env.params.query}" url += "&#{env.params.query}"

View File

@ -1,15 +1,11 @@
module Invidious::Routing module Invidious::Routing
macro get(path, controller, method = :handle) {% for http_method in {"get", "post", "delete", "options", "patch", "put", "head"} %}
get {{ path }} do |env|
controller_instance = {{ controller }}.new
controller_instance.{{ method.id }}(env)
end
end
macro post(path, controller, method = :handle) macro {{http_method.id}}(path, controller, method = :handle)
post {{ path }} do |env| {{http_method.id}} \{{ path }} do |env|
controller_instance = {{ controller }}.new \{{ controller }}.\{{ method.id }}(env)
controller_instance.{{ method.id }}(env) end
end end
end
{% end %}
end end