2020-03-11 09:30:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
class ThemeModifierSet < ActiveRecord::Base
|
|
|
|
class ThemeModifierSetError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to :theme
|
|
|
|
|
2020-03-12 12:35:28 -04:00
|
|
|
def self.modifiers
|
|
|
|
@modifiers ||= self.load_modifiers
|
|
|
|
end
|
2020-03-11 09:30:45 -04:00
|
|
|
|
|
|
|
validate :type_validator
|
|
|
|
|
|
|
|
def type_validator
|
2020-03-12 12:35:28 -04:00
|
|
|
ThemeModifierSet.modifiers.each do |k, config|
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
value = read_attribute(k)
|
2020-03-11 09:30:45 -04:00
|
|
|
next if value.nil?
|
|
|
|
|
|
|
|
case config[:type]
|
|
|
|
when :boolean
|
|
|
|
next if [true, false].include?(value)
|
|
|
|
when :string_array
|
|
|
|
next if value.is_a?(Array) && value.all? { |v| v.is_a?(String) }
|
|
|
|
end
|
|
|
|
errors.add(k, :invalid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_save do
|
|
|
|
SvgSprite.expire_cache if saved_change_to_svg_icons?
|
|
|
|
CSP::Extension.clear_theme_extensions_cache! if saved_change_to_csp_extensions?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Given the ids of multiple active themes / theme components, this function
|
|
|
|
# will combine them into a 'resolved' behavior
|
|
|
|
def self.resolve_modifier_for_themes(theme_ids, modifier_name)
|
2020-03-12 12:35:28 -04:00
|
|
|
return nil if !(config = self.modifiers[modifier_name])
|
2020-03-11 09:30:45 -04:00
|
|
|
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
all_values =
|
|
|
|
self
|
|
|
|
.where(theme_id: theme_ids)
|
|
|
|
.where.not(modifier_name => nil)
|
|
|
|
.map { |s| s.public_send(modifier_name) }
|
2020-03-12 12:35:28 -04:00
|
|
|
case config[:type]
|
|
|
|
when :boolean
|
2020-03-11 09:30:45 -04:00
|
|
|
all_values.any?
|
2020-03-12 12:35:28 -04:00
|
|
|
when :string_array
|
2020-03-11 09:30:45 -04:00
|
|
|
all_values.flatten(1)
|
|
|
|
else
|
2024-10-17 10:16:16 -04:00
|
|
|
raise ThemeModifierSetError, "Invalid theme modifier combine_mode"
|
2020-03-11 09:30:45 -04:00
|
|
|
end
|
|
|
|
end
|
2020-03-12 12:35:28 -04:00
|
|
|
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
def topic_thumbnail_sizes
|
|
|
|
array = read_attribute(:topic_thumbnail_sizes)
|
|
|
|
|
|
|
|
return if array.nil?
|
|
|
|
|
|
|
|
array
|
|
|
|
.map do |dimension|
|
|
|
|
parts = dimension.split("x")
|
|
|
|
next if parts.length != 2
|
|
|
|
[parts[0].to_i, parts[1].to_i]
|
|
|
|
end
|
|
|
|
.filter(&:present?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_thumbnail_sizes=(val)
|
|
|
|
return write_attribute(:topic_thumbnail_sizes, val) if val.nil?
|
|
|
|
return write_attribute(:topic_thumbnail_sizes, val) if !val.is_a?(Array)
|
|
|
|
if !val.all? { |v| v.is_a?(Array) && v.length == 2 }
|
|
|
|
return write_attribute(:topic_thumbnail_sizes, val)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
|
|
|
|
super(val.map { |dim| "#{dim[0]}x#{dim[1]}" })
|
|
|
|
end
|
|
|
|
|
2024-10-17 10:16:16 -04:00
|
|
|
def add_theme_setting_modifier(modifier_name, setting_name)
|
|
|
|
self.theme_setting_modifiers ||= {}
|
|
|
|
self.theme_setting_modifiers[modifier_name] = setting_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_theme_setting_modifiers(target_setting_name: nil, target_setting_value: nil)
|
|
|
|
changed = false
|
|
|
|
if self.theme_setting_modifiers.present?
|
|
|
|
self.theme_setting_modifiers.each do |modifier_name, setting_name|
|
|
|
|
modifier_name = modifier_name.to_sym
|
|
|
|
setting_name = setting_name.to_sym
|
|
|
|
|
|
|
|
next if target_setting_name.present? && target_setting_name.to_sym != setting_name
|
|
|
|
|
|
|
|
value =
|
|
|
|
target_setting_name.present? ? target_setting_value : theme.settings[setting_name]&.value
|
|
|
|
value = coerce_setting_value(modifier_name, value)
|
|
|
|
if read_attribute(modifier_name) != value
|
|
|
|
write_attribute(modifier_name, value)
|
|
|
|
changed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
changed
|
|
|
|
end
|
|
|
|
|
2020-03-12 12:35:28 -04:00
|
|
|
private
|
|
|
|
|
2024-10-17 10:16:16 -04:00
|
|
|
def coerce_setting_value(modifier_name, value)
|
|
|
|
type = ThemeModifierSet.modifiers.dig(modifier_name, :type)
|
|
|
|
if type == :boolean
|
|
|
|
value.to_s != "false"
|
|
|
|
elsif type == :string_array
|
|
|
|
value.is_a?(Array) ? value : value.to_s.split("|")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-12 12:35:28 -04:00
|
|
|
# Build the list of modifiers from the DB schema.
|
|
|
|
# This allows plugins to introduce new modifiers by adding columns to the table
|
|
|
|
def self.load_modifiers
|
|
|
|
hash = {}
|
|
|
|
columns_hash.each do |column_name, info|
|
2024-10-17 10:16:16 -04:00
|
|
|
next if %w[id theme_id theme_setting_modifiers].include?(column_name)
|
2020-03-12 12:35:28 -04:00
|
|
|
|
|
|
|
type = nil
|
|
|
|
if info.type == :string && info.array?
|
|
|
|
type = :string_array
|
|
|
|
elsif info.type == :boolean && !info.array?
|
|
|
|
type = :boolean
|
|
|
|
else
|
|
|
|
if !%i[boolean string].include?(info.type)
|
2024-10-23 05:40:14 -04:00
|
|
|
raise ThemeModifierSetError, "Invalid theme modifier column type"
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2020-03-12 12:35:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
hash[column_name.to_sym] = { type: type }
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
2020-03-11 09:30:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: theme_modifier_sets
|
|
|
|
#
|
2024-10-17 10:16:16 -04:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# theme_id :bigint not null
|
|
|
|
# serialize_topic_excerpts :boolean
|
|
|
|
# csp_extensions :string is an Array
|
|
|
|
# svg_icons :string is an Array
|
|
|
|
# topic_thumbnail_sizes :string is an Array
|
|
|
|
# custom_homepage :boolean
|
|
|
|
# serialize_post_user_badges :string is an Array
|
|
|
|
# theme_setting_modifiers :jsonb
|
2020-03-11 09:30:45 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_theme_modifier_sets_on_theme_id (theme_id) UNIQUE
|
|
|
|
#
|