mirror of
https://git.nadeko.net/Fijxu/invidious.git
synced 2026-01-30 10:42:53 +00:00
Set current companion using preferences and not a separate cookie
This commit is contained in:
@@ -1163,15 +1163,6 @@ video_cache:
|
|||||||
##
|
##
|
||||||
#max_dash_resolution: null
|
#max_dash_resolution: null
|
||||||
|
|
||||||
##
|
|
||||||
## The name of the cookie to hold the backend id that
|
|
||||||
## the client is using.
|
|
||||||
##
|
|
||||||
## Accepted values: a string
|
|
||||||
## Default: "COMPANION_ID"
|
|
||||||
##
|
|
||||||
#server_id_cookie_name: "COMPANION_ID"
|
|
||||||
|
|
||||||
##
|
##
|
||||||
## Checks if the companions in the `invidious_companion` list
|
## Checks if the companions in the `invidious_companion` list
|
||||||
## are alive using their `/healthz` endpoint.
|
## are alive using their `/healthz` endpoint.
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ struct ConfigPreferences
|
|||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
property default_trending_type : Invidious::Routes::Feeds::TrendingTypes = Invidious::Routes::Feeds::TrendingTypes::Default
|
property default_trending_type : Invidious::Routes::Feeds::TrendingTypes = Invidious::Routes::Feeds::TrendingTypes::Default
|
||||||
property show_community_backends : Bool = false
|
property show_community_backends : Bool = false
|
||||||
|
@[YAML::Field(ignore: true)]
|
||||||
|
property current_companion : Int32? = nil
|
||||||
|
|
||||||
def to_tuple
|
def to_tuple
|
||||||
{% begin %}
|
{% begin %}
|
||||||
@@ -223,8 +225,6 @@ class Config
|
|||||||
|
|
||||||
property pubsub_domain : String = ""
|
property pubsub_domain : String = ""
|
||||||
|
|
||||||
property server_id_cookie_name : String = "COMPANION_ID"
|
|
||||||
|
|
||||||
property video_cache : VideoCacheConfig = VideoCacheConfig.from_yaml("")
|
property video_cache : VideoCacheConfig = VideoCacheConfig.from_yaml("")
|
||||||
|
|
||||||
class VideoCacheConfig
|
class VideoCacheConfig
|
||||||
|
|||||||
@@ -3,13 +3,22 @@
|
|||||||
module Invidious::Routes::BackendSwitcher
|
module Invidious::Routes::BackendSwitcher
|
||||||
def self.switch(env)
|
def self.switch(env)
|
||||||
referer = get_referer(env, unroll: false)
|
referer = get_referer(env, unroll: false)
|
||||||
backend_id = env.params.query["backend_id"]?.try &.to_i
|
companion_id = env.params.query["companion_id"]?.try &.to_i
|
||||||
|
preferences = env.get("preferences").as(Preferences)
|
||||||
|
user = env.get? "user"
|
||||||
|
|
||||||
if backend_id.nil?
|
if companion_id.nil?
|
||||||
return error_template(400, "Backend ID is required")
|
return error_template(400, "Companion ID is required")
|
||||||
end
|
end
|
||||||
|
|
||||||
env.response.cookies[CONFIG.server_id_cookie_name] = Invidious::User::Cookies.server_id(env.request.headers["Host"], backend_id)
|
if user
|
||||||
|
user = user.as(User)
|
||||||
|
user.preferences.current_companion = companion_id
|
||||||
|
Invidious::Database::Users.update_preferences(user)
|
||||||
|
else
|
||||||
|
preferences.current_companion = companion_id
|
||||||
|
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(env.request.headers["Host"], preferences)
|
||||||
|
end
|
||||||
|
|
||||||
env.redirect referer
|
env.redirect referer
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -169,7 +169,6 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
companion_status : CompanionStatus,
|
companion_status : CompanionStatus,
|
||||||
preferences : Preferences,
|
preferences : Preferences,
|
||||||
)
|
)
|
||||||
cookie_name = CONFIG.server_id_cookie_name
|
|
||||||
c_size = CONFIG.invidious_companion.size
|
c_size = CONFIG.invidious_companion.size
|
||||||
current_companion = 0
|
current_companion = 0
|
||||||
|
|
||||||
@@ -181,20 +180,20 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
current_companion = index
|
current_companion = index
|
||||||
else
|
else
|
||||||
# Set cookie if there is no cookie
|
# Set cookie if there is no cookie
|
||||||
if !env.request.cookies.has_key?(cookie_name)
|
if !env.request.cookies.has_key?("PREFS")
|
||||||
current_companion = self.find_available_companion(env, host, nil, companion_status, preferences)
|
current_companion = self.find_available_companion(env, host, nil, companion_status, preferences)
|
||||||
if current_companion
|
if current_companion
|
||||||
self.set_cookie(env, host, current_companion)
|
self.set_companion(env, preferences, host, current_companion)
|
||||||
else
|
else
|
||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
current_companion = get_cookie(env)
|
current_companion = get_companion(preferences)
|
||||||
current_companion = self.find_available_companion(env, host, current_companion, companion_status, preferences)
|
current_companion = self.find_available_companion(env, host, current_companion, companion_status, preferences)
|
||||||
rescue
|
rescue
|
||||||
current_companion = rand(c_size)
|
current_companion = rand(c_size)
|
||||||
self.set_cookie(env, host, current_companion)
|
self.set_companion(env, preferences, host, current_companion)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -219,18 +218,29 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
return companion_csp
|
return companion_csp
|
||||||
end
|
end
|
||||||
|
|
||||||
private def set_cookie(
|
private def set_companion(
|
||||||
env : HTTP::Server::Context,
|
env : HTTP::Server::Context,
|
||||||
|
preferences : Preferences,
|
||||||
host : String,
|
host : String,
|
||||||
current_companion : Int32,
|
current_companion : Int32,
|
||||||
)
|
)
|
||||||
cookie_name = CONFIG.server_id_cookie_name
|
user = env.get? "user"
|
||||||
env.response.cookies[cookie_name] = Invidious::User::Cookies.server_id(host, current_companion)
|
|
||||||
|
if user
|
||||||
|
user = user.as(User)
|
||||||
|
user.preferences.current_companion = current_companion
|
||||||
|
Invidious::Database::Users.update_preferences(user)
|
||||||
|
else
|
||||||
|
preferences.current_companion = current_companion
|
||||||
|
env.set "preferences", preferences
|
||||||
|
env.response.cookies["PREFS"] = Invidious::User::Cookies.prefs(env.request.headers["Host"], preferences)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def get_cookie(env : HTTP::Server::Context)
|
private def get_companion(
|
||||||
cookie_name = CONFIG.server_id_cookie_name
|
preferences : Preferences,
|
||||||
return env.request.cookies[cookie_name].value.try &.to_i
|
)
|
||||||
|
return preferences.current_companion
|
||||||
end
|
end
|
||||||
|
|
||||||
private def find_available_companion(
|
private def find_available_companion(
|
||||||
@@ -260,7 +270,7 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
current_companion = self.wrap_current_companion(env, host, current_companion, c_size, working_companions)
|
current_companion = self.wrap_current_companion(env, host, current_companion, c_size, working_companions, preferences)
|
||||||
if current_companion.nil?
|
if current_companion.nil?
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@@ -271,7 +281,7 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
if alive_companion
|
if alive_companion
|
||||||
current_companion = alive_companion
|
current_companion = alive_companion
|
||||||
env.set "companion_switched", true
|
env.set "companion_switched", true
|
||||||
self.set_cookie(env, host, current_companion)
|
self.set_companion(env, preferences, host, current_companion)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -294,11 +304,12 @@ module Invidious::Routes::BeforeAll::Companion
|
|||||||
current_companion : Int32,
|
current_companion : Int32,
|
||||||
invidious_companion_size : Int32,
|
invidious_companion_size : Int32,
|
||||||
working_companions : Array(Int32),
|
working_companions : Array(Int32),
|
||||||
|
preferences : Preferences,
|
||||||
)
|
)
|
||||||
if (current_companion < 0) || current_companion >= invidious_companion_size
|
if (current_companion < 0) || current_companion >= invidious_companion_size
|
||||||
current_companion = self.get_available_companion(invidious_companion_size, working_companions)
|
current_companion = self.get_available_companion(invidious_companion_size, working_companions)
|
||||||
if current_companion
|
if current_companion
|
||||||
self.set_cookie(env, host, current_companion)
|
self.set_companion(env, preferences, host, current_companion)
|
||||||
else
|
else
|
||||||
current_companion = rand(invidious_companion_size)
|
current_companion = rand(invidious_companion_size)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ module Invidious::Routes::PreferencesRoute
|
|||||||
|
|
||||||
show_community_backends = env.params.body["show_community_backends"]?.try &.as(String)
|
show_community_backends = env.params.body["show_community_backends"]?.try &.as(String)
|
||||||
show_community_backends ||= "off"
|
show_community_backends ||= "off"
|
||||||
show_community_backends= show_community_backends == "on"
|
show_community_backends = show_community_backends == "on"
|
||||||
|
|
||||||
# Convert to JSON and back again to take advantage of converters used for compatibility
|
# Convert to JSON and back again to take advantage of converters used for compatibility
|
||||||
preferences = Preferences.from_json({
|
preferences = Preferences.from_json({
|
||||||
|
|||||||
@@ -50,30 +50,8 @@ struct Invidious::User
|
|||||||
expires: Time.utc + 2.years,
|
expires: Time.utc + 2.years,
|
||||||
secure: @@secure,
|
secure: @@secure,
|
||||||
http_only: false,
|
http_only: false,
|
||||||
samesite: HTTP::Cookie::SameSite::Lax
|
samesite: HTTP::Cookie::SameSite::Lax,
|
||||||
)
|
path: "/"
|
||||||
end
|
|
||||||
|
|
||||||
# Backend (CONFIG.server_id_cookie_name) cookie
|
|
||||||
# Parameter "domain" comes from the global config
|
|
||||||
def server_id(domain : String?, server_id : Int32) : HTTP::Cookie
|
|
||||||
# Strip the port from the domain if it's being accessed from another port
|
|
||||||
# Browsers will reject the cookie if it contains the port number. This is
|
|
||||||
# because `example.com:3000` is not the same as `example.com` on a cookie.
|
|
||||||
domain = domain.split(":")[0]
|
|
||||||
# Not secure if it's being accessed from I2P
|
|
||||||
# Browsers expect the domain to include https. On I2P there is no HTTPS
|
|
||||||
if domain.not_nil!.split(".").last == "i2p"
|
|
||||||
@@secure = false
|
|
||||||
end
|
|
||||||
return HTTP::Cookie.new(
|
|
||||||
name: CONFIG.server_id_cookie_name,
|
|
||||||
domain: domain,
|
|
||||||
path: "/",
|
|
||||||
value: server_id.to_s,
|
|
||||||
secure: @@secure,
|
|
||||||
http_only: true,
|
|
||||||
samesite: HTTP::Cookie::SameSite::Lax
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ struct Preferences
|
|||||||
property default_trending_type : Invidious::Routes::Feeds::TrendingTypes = Invidious::Routes::Feeds::TrendingTypes::Default
|
property default_trending_type : Invidious::Routes::Feeds::TrendingTypes = Invidious::Routes::Feeds::TrendingTypes::Default
|
||||||
property default_playlist : String? = nil
|
property default_playlist : String? = nil
|
||||||
property show_community_backends : Bool = false
|
property show_community_backends : Bool = false
|
||||||
|
property current_companion : Int32? = nil
|
||||||
|
|
||||||
module BoolToString
|
module BoolToString
|
||||||
def self.to_json(value : String, json : JSON::Builder)
|
def self.to_json(value : String, json : JSON::Builder)
|
||||||
|
|||||||
@@ -146,7 +146,7 @@
|
|||||||
next if companion.i2p_public_url.host.nil? && using_i2p
|
next if companion.i2p_public_url.host.nil? && using_i2p
|
||||||
backend_name_prefix = CONFIG.backend_name_prefix + (index + 1).to_s
|
backend_name_prefix = CONFIG.backend_name_prefix + (index + 1).to_s
|
||||||
%>
|
%>
|
||||||
<a href="/switchbackend?backend_id=<%= index.to_s %>&referer=<%= current_page %>" style="<%= current_backend == index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
|
<a href="/switchbackend?companion_id=<%= index.to_s %>&referer=<%= current_page %>" style="<%= current_backend == index ? "text-decoration-line: underline;" : "" %> display: inline-block;">
|
||||||
<%= HTML.escape(backend_name_prefix) %> <%= HTML.escape(companion.note) %>
|
<%= HTML.escape(backend_name_prefix) %> <%= HTML.escape(companion.note) %>
|
||||||
<span style="color:
|
<span style="color:
|
||||||
<% if companions[index].status == CompanionStatus::Status::Down %> #fd4848; <% end %>
|
<% if companions[index].status == CompanionStatus::Status::Down %> #fd4848; <% end %>
|
||||||
|
|||||||
Reference in New Issue
Block a user