Merge pull request #1399 from matthewmcgarvey/routing-job-patterns
Provide rough draft of better project organizationpull/1406/head
commit
59d966356e
@ -0,0 +1,13 @@
|
||||
module Invidious::Jobs
|
||||
JOBS = [] of BaseJob
|
||||
|
||||
def self.register(job : BaseJob)
|
||||
JOBS << job
|
||||
end
|
||||
|
||||
def self.start_all
|
||||
JOBS.each do |job|
|
||||
spawn { job.begin }
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
abstract class Invidious::Jobs::BaseJob
|
||||
abstract def begin
|
||||
end
|
@ -0,0 +1,27 @@
|
||||
class Invidious::Jobs::PullPopularVideosJob < Invidious::Jobs::BaseJob
|
||||
QUERY = <<-SQL
|
||||
SELECT DISTINCT ON (ucid) *
|
||||
FROM channel_videos
|
||||
WHERE ucid IN (SELECT channel FROM (SELECT UNNEST(subscriptions) AS channel FROM users) AS d
|
||||
GROUP BY channel ORDER BY COUNT(channel) DESC LIMIT 40)
|
||||
ORDER BY ucid, published DESC
|
||||
SQL
|
||||
POPULAR_VIDEOS = Atomic.new([] of ChannelVideo)
|
||||
private getter db : DB::Database
|
||||
|
||||
def initialize(@db)
|
||||
end
|
||||
|
||||
def begin
|
||||
loop do
|
||||
videos = db.query_all(QUERY, as: ChannelVideo)
|
||||
.sort_by(&.published)
|
||||
.reverse
|
||||
|
||||
POPULAR_VIDEOS.set(videos)
|
||||
|
||||
sleep 1.minute
|
||||
Fiber.yield
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,8 @@
|
||||
abstract class Invidious::Routes::BaseRoute
|
||||
private getter config : Config
|
||||
|
||||
def initialize(@config)
|
||||
end
|
||||
|
||||
abstract def handle(env)
|
||||
end
|
@ -0,0 +1,34 @@
|
||||
class Invidious::Routes::Home < Invidious::Routes::BaseRoute
|
||||
def handle(env)
|
||||
preferences = env.get("preferences").as(Preferences)
|
||||
locale = LOCALES[preferences.locale]?
|
||||
user = env.get? "user"
|
||||
|
||||
case preferences.default_home
|
||||
when ""
|
||||
templated "empty"
|
||||
when "Popular"
|
||||
templated "popular"
|
||||
when "Trending"
|
||||
env.redirect "/feed/trending"
|
||||
when "Subscriptions"
|
||||
if user
|
||||
env.redirect "/feed/subscriptions"
|
||||
else
|
||||
templated "popular"
|
||||
end
|
||||
when "Playlists"
|
||||
if user
|
||||
env.redirect "/view_all_playlists"
|
||||
else
|
||||
templated "popular"
|
||||
end
|
||||
else
|
||||
templated "empty"
|
||||
end
|
||||
end
|
||||
|
||||
private def popular_videos
|
||||
Jobs::PullPopularVideosJob::POPULAR_VIDEOS.get
|
||||
end
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
class Invidious::Routes::Licenses < Invidious::Routes::BaseRoute
|
||||
def handle(env)
|
||||
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
||||
rendered "licenses"
|
||||
end
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
class Invidious::Routes::Privacy < Invidious::Routes::BaseRoute
|
||||
def handle(env)
|
||||
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
||||
templated "privacy"
|
||||
end
|
||||
end
|
@ -0,0 +1,8 @@
|
||||
module Invidious::Routing
|
||||
macro get(path, controller)
|
||||
get {{ path }} do |env|
|
||||
controller_instance = {{ controller }}.new(config)
|
||||
controller_instance.handle(env)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue