From ccad3ca226618fd6f233aee5eb3e8156c7c5eed2 Mon Sep 17 00:00:00 2001
From: Penar Musaraj <pmusaraj@gmail.com>
Date: Mon, 4 Feb 2019 16:14:43 -0500
Subject: [PATCH] FEAT: ads.txt support

Adds a plugin setting that holds text content served on a new /ads.txt route.
---
 config/locales/server.en.yml |   1 +
 config/settings.yml          | 156 ++++++++++++++++++-----------------
 plugin.rb                    |  17 ++++
 3 files changed, 98 insertions(+), 76 deletions(-)

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 93ed448..aa95a26 100755
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -51,6 +51,7 @@ en:
     adsense_post_bottom_ad_sizes: "Choose your ad size"
     adsense_mobile_post_bottom_ad_size: "Choose your ad size"
     adsense_nth_post_code: "Show an ad after every N posts, where N is this value."
+    adsense_ads_txt: "Contents of your ads.txt file. More details available at <a href='https://support.google.com/adsense/answer/7532444?hl=en' target='_blank'>this Google AdSense help page</a>."
 
     amazon_through_trust_level: "Show your ads to users based on trust levels. Users with trust level higher than this value will not see ads."
     amazon_topic_list_top_src_code: "Enter src code to display at topic list top location"
diff --git a/config/settings.yml b/config/settings.yml
index 6d7b628..8e7ee93 100755
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -1,20 +1,20 @@
 adsense_plugin:
   adsense_publisher_code:
     client: true
-    default: ''
+    default: ""
     regex: '^[\d]*$'
   adsense_through_trust_level:
     client: true
     default: 2
-    enum: 'TrustLevelSetting'
+    enum: "TrustLevelSetting"
   adsense_topic_list_top_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_topic_list_top_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: &adsense_choices
       - responsive
@@ -36,94 +36,98 @@ adsense_plugin:
       - 690*250
   adsense_mobile_topic_list_top_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_mobile_topic_list_top_ad_size:
     client: true
-    default: 'responsive'
+    default: "responsive"
     type: enum
     choices: *adsense_choices
   adsense_topic_above_post_stream_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_topic_above_post_stream_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *adsense_choices
   adsense_mobile_topic_above_post_stream_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_mobile_topic_above_post_stream_ad_size:
     client: true
-    default: 'responsive'
+    default: "responsive"
     type: enum
     choices: *adsense_choices
   adsense_topic_above_suggested_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_topic_above_suggested_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *adsense_choices
   adsense_mobile_topic_above_suggested_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_mobile_topic_above_suggested_ad_size:
     client: true
-    default: 'responsive'
+    default: "responsive"
     type: enum
     choices: *adsense_choices
   adsense_post_bottom_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_post_bottom_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *adsense_choices
   adsense_mobile_post_bottom_code:
     client: true
-    default: ''
+    default: ""
     max: 50
     regex: '^[\d]*$'
   adsense_mobile_post_bottom_ad_size:
     client: true
-    default: 'responsive'
+    default: "responsive"
     type: enum
     choices: *adsense_choices
   adsense_nth_post_code:
     client: true
-    default: ''
+    default: ""
     min: 1
+  adsense_ads_txt:
+    client: false
+    default: ""
+    textarea: true
 
 dfp_plugin:
   dfp_publisher_id:
     client: true
-    default: ''
+    default: ""
   dfp_through_trust_level:
     client: true
     default: 2
-    enum: 'TrustLevelSetting'
+    enum: "TrustLevelSetting"
   dfp_topic_list_top_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_topic_list_top_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: &dfp_choices
       - 728*90 - leaderboard
@@ -161,102 +165,102 @@ dfp_plugin:
       - 168*28
   dfp_mobile_topic_list_top_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_mobile_topic_list_top_ad_sizes:
     client: true
-    default: '320*50 - mobile leaderboard'
+    default: "320*50 - mobile leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_target_topic_list_top_key_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_target_topic_list_top_value_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_topic_above_post_stream_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_topic_above_post_stream_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_mobile_topic_above_post_stream_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_mobile_topic_above_post_stream_ad_sizes:
     client: true
-    default: '320*50 - mobile leaderboard'
+    default: "320*50 - mobile leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_target_topic_above_post_stream_key_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_target_topic_above_post_stream_value_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_topic_above_suggested_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_topic_above_suggested_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_mobile_topic_above_suggested_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_mobile_topic_above_suggested_ad_sizes:
     client: true
-    default: '320*50 - mobile leaderboard'
+    default: "320*50 - mobile leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_target_topic_above_suggested_key_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_target_topic_above_suggested_value_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
     max: 50
   dfp_nth_post_code:
     client: true
-    default: ''
+    default: ""
     min: 1
   dfp_post_bottom_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_post_bottom_ad_sizes:
     client: true
-    default: '728*90 - leaderboard'
+    default: "728*90 - leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_mobile_post_bottom_code:
     client: true
-    default: ''
+    default: ""
     max: 50
   dfp_mobile_post_bottom_ad_sizes:
     client: true
-    default: '320*50 - mobile leaderboard'
+    default: "320*50 - mobile leaderboard"
     type: enum
     choices: *dfp_choices
   dfp_target_post_bottom_key_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
   dfp_target_post_bottom_value_code:
-    default: ''
+    default: ""
     type: value_list
     client: true
 
@@ -264,98 +268,98 @@ amazon_plugin:
   amazon_through_trust_level:
     client: true
     default: 2
-    enum: 'TrustLevelSetting'
+    enum: "TrustLevelSetting"
   amazon_topic_list_top_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_list_top_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_list_top_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_list_top_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_list_top_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_list_top_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_post_stream_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_post_stream_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_post_stream_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_post_stream_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_post_stream_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_post_stream_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_suggested_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_suggested_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_topic_above_suggested_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_suggested_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_suggested_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_topic_above_suggested_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_post_bottom_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_nth_post_code:
     client: true
-    default: ''
+    default: ""
     min: 1
   amazon_post_bottom_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_post_bottom_ad_height_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_post_bottom_src_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_post_bottom_ad_width_code:
     client: true
-    default: ''
+    default: ""
   amazon_mobile_post_bottom_ad_height_code:
     client: true
-    default: ''
+    default: ""
 
 codefund_plugin:
   codefund_property_id:
     client: true
-    default: ''
+    default: ""
   codefund_advertiser_label:
     client: true
-    default: 'Advertiser'
+    default: "Advertiser"
   codefund_advertiser_short_label:
     client: true
-    default: 'Ad'
+    default: "Ad"
   codefund_through_trust_level:
     client: true
     default: 2
-    enum: 'TrustLevelSetting'
+    enum: "TrustLevelSetting"
   codefund_nth_post:
     client: true
     default: 4
diff --git a/plugin.rb b/plugin.rb
index 18cdafe..2a3fd63 100755
--- a/plugin.rb
+++ b/plugin.rb
@@ -5,3 +5,20 @@
 # url: https://github.com/discourse/discourse-adplugin
 
 register_asset "stylesheets/adplugin.scss"
+
+after_initialize do
+  require_dependency 'application_controller'
+  class ::AdstxtController < ::ApplicationController
+    skip_before_action :check_xhr
+
+    def index
+      raise Discourse::NotFound unless SiteSetting.adsense_ads_txt.present?
+
+      render plain: SiteSetting.adsense_ads_txt
+    end
+  end
+
+  Discourse::Application.routes.append do
+    get '/ads.txt' => "adstxt#index"
+  end
+end