Skip to content

Commit

Permalink
Add option to configure default user preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
omarroth committed Apr 3, 2019
1 parent 1fd7ff5 commit bd4f5eb
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 90 deletions.
16 changes: 8 additions & 8 deletions src/invidious.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1198,22 +1198,22 @@ post "/preferences" do |env|
local = local == "on"

speed = env.params.body["speed"]?.try &.as(String).to_f?
speed ||= DEFAULT_USER_PREFERENCES.speed
speed ||= CONFIG.default_user_preferences.speed

quality = env.params.body["quality"]?.try &.as(String)
quality ||= DEFAULT_USER_PREFERENCES.quality
quality ||= CONFIG.default_user_preferences.quality

volume = env.params.body["volume"]?.try &.as(String).to_i?
volume ||= DEFAULT_USER_PREFERENCES.volume
volume ||= CONFIG.default_user_preferences.volume

comments = [] of String
2.times do |i|
comments << (env.params.body["comments[#{i}]"]?.try &.as(String) || DEFAULT_USER_PREFERENCES.comments[i])
comments << (env.params.body["comments[#{i}]"]?.try &.as(String) || CONFIG.default_user_preferences.comments[i])
end

captions = [] of String
3.times do |i|
captions << (env.params.body["captions[#{i}]"]?.try &.as(String) || DEFAULT_USER_PREFERENCES.captions[i])
captions << (env.params.body["captions[#{i}]"]?.try &.as(String) || CONFIG.default_user_preferences.captions[i])
end

related_videos = env.params.body["related_videos"]?.try &.as(String)
Expand All @@ -1225,7 +1225,7 @@ post "/preferences" do |env|
redirect_feed = redirect_feed == "on"

locale = env.params.body["locale"]?.try &.as(String)
locale ||= DEFAULT_USER_PREFERENCES.locale
locale ||= CONFIG.default_user_preferences.locale

dark_mode = env.params.body["dark_mode"]?.try &.as(String)
dark_mode ||= "off"
Expand All @@ -1236,10 +1236,10 @@ post "/preferences" do |env|
thin_mode = thin_mode == "on"

max_results = env.params.body["max_results"]?.try &.as(String).to_i?
max_results ||= DEFAULT_USER_PREFERENCES.max_results
max_results ||= CONFIG.default_user_preferences.max_results

sort = env.params.body["sort"]?.try &.as(String)
sort ||= DEFAULT_USER_PREFERENCES.sort
sort ||= CONFIG.default_user_preferences.sort

latest_only = env.params.body["latest_only"]?.try &.as(String)
latest_only ||= "off"
Expand Down
4 changes: 2 additions & 2 deletions src/invidious/channels.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct InvidiousChannel
add_mapping({
db_mapping({
id: String,
author: String,
updated: Time,
Expand All @@ -9,7 +9,7 @@ struct InvidiousChannel
end

struct ChannelVideo
add_mapping({
db_mapping({
id: String,
title: String,
published: Time,
Expand Down
104 changes: 90 additions & 14 deletions src/invidious/helpers/helpers.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
require "./macros"

struct ConfigPreferences
module StringToArray
def self.to_yaml(value : Array(String), yaml : YAML::Nodes::Builder)
yaml.sequence do
value.each do |element|
yaml.scalar element
end
end
end

def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array(String)
begin
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
end

result = [] of String
node.nodes.each do
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.class}"
end

result << node.value
end
rescue ex
if node.is_a?(YAML::Nodes::Scalar)
result = [node.value, ""]
else
result = ["", ""]
end
end

result
end
end

yaml_mapping({
autoplay: {type: Bool, default: false},
captions: {type: Array(String), default: ["", "", ""], converter: StringToArray},
comments: {type: Array(String), default: ["youtube", ""], converter: StringToArray},
continue: {type: Bool, default: false},
dark_mode: {type: Bool, default: false},
latest_only: {type: Bool, default: false},
listen: {type: Bool, default: false},
local: {type: Bool, default: false},
locale: {type: String, default: "en-US"},
max_results: {type: Int32, default: 40},
notifications_only: {type: Bool, default: false},
quality: {type: String, default: "hd720"},
redirect_feed: {type: Bool, default: false},
related_videos: {type: Bool, default: true},
sort: {type: String, default: "published"},
speed: {type: Float32, default: 1.0_f32},
thin_mode: {type: Bool, default: false},
unseen_only: {type: Bool, default: false},
video_loop: {type: Bool, default: false},
volume: {type: Int32, default: 100},
})
end

struct Config
module ConfigPreferencesConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Preferences
Preferences.new(*ConfigPreferences.new(ctx, node).to_tuple)
end

def self.to_yaml(value : Preferences, yaml : YAML::Nodes::Builder)
value.to_yaml(yaml)
end
end

YAML.mapping({
channel_threads: Int32, # Number of threads to use for crawling videos from channels (for updating subscriptions)
feed_threads: Int32, # Number of threads to use for updating feeds
Expand All @@ -9,20 +81,24 @@ user: String,
port: Int32,
dbname: String,
),
full_refresh: Bool, # Used for crawling channels: threads should check all videos uploaded by a channel
https_only: Bool?, # Used to tell Invidious it is behind a proxy, so links to resources should be https://
hmac_key: String?, # HMAC signing key for CSRF tokens and verifying pubsub subscriptions
domain: String?, # Domain to be used for links to resources on the site where an absolute URL is required
use_pubsub_feeds: {type: Bool, default: false}, # Subscribe to channels using PubSubHubbub (requires domain, hmac_key)
default_home: {type: String, default: "Top"},
feed_menu: {type: Array(String), default: ["Popular", "Top", "Trending", "Subscriptions"]},
top_enabled: {type: Bool, default: true},
captcha_enabled: {type: Bool, default: true},
login_enabled: {type: Bool, default: true},
registration_enabled: {type: Bool, default: true},
statistics_enabled: {type: Bool, default: false},
admins: {type: Array(String), default: [] of String},
external_port: {type: Int32 | Nil, default: nil},
full_refresh: Bool, # Used for crawling channels: threads should check all videos uploaded by a channel
https_only: Bool?, # Used to tell Invidious it is behind a proxy, so links to resources should be https://
hmac_key: String?, # HMAC signing key for CSRF tokens and verifying pubsub subscriptions
domain: String?, # Domain to be used for links to resources on the site where an absolute URL is required
use_pubsub_feeds: {type: Bool, default: false}, # Subscribe to channels using PubSubHubbub (requires domain, hmac_key)
default_home: {type: String, default: "Top"},
feed_menu: {type: Array(String), default: ["Popular", "Top", "Trending", "Subscriptions"]},
top_enabled: {type: Bool, default: true},
captcha_enabled: {type: Bool, default: true},
login_enabled: {type: Bool, default: true},
registration_enabled: {type: Bool, default: true},
statistics_enabled: {type: Bool, default: false},
admins: {type: Array(String), default: [] of String},
external_port: {type: Int32?, default: nil},
default_user_preferences: {type: Preferences,
default: Preferences.new(*ConfigPreferences.from_yaml("").to_tuple),
converter: ConfigPreferencesConverter,
},
})
end

Expand Down
18 changes: 17 additions & 1 deletion src/invidious/helpers/macros.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
macro add_mapping(mapping)
macro db_mapping(mapping)
def initialize({{*mapping.keys.map { |id| "@#{id}".id }}})
end

Expand All @@ -18,6 +18,22 @@ macro json_mapping(mapping)
end

JSON.mapping({{mapping}})
YAML.mapping({{mapping}})
end

macro yaml_mapping(mapping)
def initialize({{*mapping.keys.map { |id| "@#{id}".id }}})
end

def to_a
return [{{*mapping.keys.map { |id| "@#{id}".id }}}]
end

def to_tuple
return { {{*mapping.keys.map { |id| "@#{id}".id }}} }
end

YAML.mapping({{mapping}})
end

macro templated(filename, template = "template")
Expand Down
4 changes: 2 additions & 2 deletions src/invidious/mixes.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct MixVideo
add_mapping({
db_mapping({
title: String,
id: String,
author: String,
Expand All @@ -11,7 +11,7 @@ struct MixVideo
end

struct Mix
add_mapping({
db_mapping({
title: String,
id: String,
videos: Array(MixVideo),
Expand Down
4 changes: 2 additions & 2 deletions src/invidious/playlists.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct PlaylistVideo
add_mapping({
db_mapping({
title: String,
id: String,
author: String,
Expand All @@ -13,7 +13,7 @@ struct PlaylistVideo
end

struct Playlist
add_mapping({
db_mapping({
title: String,
id: String,
author: String,
Expand Down
8 changes: 4 additions & 4 deletions src/invidious/search.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct SearchVideo
add_mapping({
db_mapping({
title: String,
id: String,
author: String,
Expand All @@ -17,15 +17,15 @@ struct SearchVideo
end

struct SearchPlaylistVideo
add_mapping({
db_mapping({
title: String,
id: String,
length_seconds: Int32,
})
end

struct SearchPlaylist
add_mapping({
db_mapping({
title: String,
id: String,
author: String,
Expand All @@ -37,7 +37,7 @@ struct SearchPlaylist
end

struct SearchChannel
add_mapping({
db_mapping({
author: String,
ucid: String,
author_thumbnail: String,
Expand Down
Loading

0 comments on commit bd4f5eb

Please sign in to comment.