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

View file

@ -1,5 +1,5 @@
class Invidious::Routes::Embed < Invidious::Routes::BaseRoute
def redirect(env)
module Invidious::Routes::Embed
def self.redirect(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
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
end
def show(env)
def self.show(env)
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
id = env.params.url["id"]

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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