2019-05-12 22:16:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
module ::AdPlugin
|
|
|
|
class HouseAdSetting
|
|
|
|
DEFAULTS = {
|
2022-12-29 07:29:26 -05:00
|
|
|
topic_list_top: "",
|
|
|
|
topic_above_post_stream: "",
|
|
|
|
topic_above_suggested: "",
|
|
|
|
post_bottom: "",
|
|
|
|
topic_list_between: "",
|
2019-04-18 17:52:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
settings = DEFAULTS.dup
|
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
PluginStoreRow
|
|
|
|
.where(plugin_name: AdPlugin.plugin_name)
|
2019-04-18 17:52:59 -04:00
|
|
|
.where("key LIKE 'ad-setting:%'")
|
2022-12-29 07:29:26 -05:00
|
|
|
.each { |psr| settings[psr.key[11..-1].to_sym] = psr.value }
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
settings
|
|
|
|
end
|
|
|
|
|
2023-04-07 10:56:22 -04:00
|
|
|
def self.settings_and_ads(for_anons: true)
|
2019-04-18 17:52:59 -04:00
|
|
|
settings = AdPlugin::HouseAdSetting.all
|
2022-12-29 07:29:26 -05:00
|
|
|
ad_names = settings.values.map { |v| v.split("|") }.flatten.uniq
|
2023-04-07 10:56:22 -04:00
|
|
|
|
|
|
|
if for_anons
|
|
|
|
ads = AdPlugin::HouseAd.all_for_anons
|
|
|
|
else
|
|
|
|
ads = AdPlugin::HouseAd.all_for_logged_in_users
|
|
|
|
end
|
|
|
|
ads = ads.select { |ad| ad_names.include?(ad.name) }
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
{
|
2022-12-29 07:29:26 -05:00
|
|
|
settings:
|
|
|
|
settings.merge(
|
|
|
|
after_nth_post: SiteSetting.house_ads_after_nth_post,
|
|
|
|
after_nth_topic: SiteSetting.house_ads_after_nth_topic,
|
|
|
|
house_ads_frequency: SiteSetting.house_ads_frequency,
|
|
|
|
),
|
|
|
|
creatives:
|
|
|
|
ads.inject({}) do |h, ad|
|
|
|
|
h[ad.name] = ad.html
|
|
|
|
h
|
|
|
|
end,
|
2019-04-18 17:52:59 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update(setting_name, value)
|
2022-12-29 07:29:26 -05:00
|
|
|
raise Discourse::NotFound unless DEFAULTS.keys.include?(setting_name.to_sym)
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
ad_names = value&.split("|") || []
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
raise Discourse::InvalidParameters if value && ad_names.any? { |v| v !~ HouseAd::NAME_REGEX }
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
ad_names = (HouseAd.all.map(&:name) & ad_names) unless ad_names.empty?
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-12-29 07:29:26 -05:00
|
|
|
new_value = ad_names.join("|")
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
if value.nil? || new_value == DEFAULTS[setting_name.to_sym]
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_delete("ad-setting:#{setting_name}")
|
2019-04-18 17:52:59 -04:00
|
|
|
else
|
2022-12-29 07:29:26 -05:00
|
|
|
AdPlugin.pstore_set("ad-setting:#{setting_name}", new_value)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
2023-04-07 10:56:22 -04:00
|
|
|
Site.clear_anon_cache!
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
publish_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.publish_settings
|
2023-04-07 10:56:22 -04:00
|
|
|
MessageBus.publish("/site/house-creatives/anonymous", settings_and_ads(for_anons: true))
|
|
|
|
MessageBus.publish(
|
|
|
|
"/site/house-creatives/logged-in",
|
|
|
|
settings_and_ads(for_anons: false),
|
|
|
|
group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
|
|
|
|
)
|
2019-04-18 17:52:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|