FIX: use imports instead of Discourse global (#118)

Replaces `Discourse.Site` with the imported equivalent and `Discourse.SiteSettings` with `this.siteSettings`
This commit is contained in:
tshenry 2021-09-21 11:27:10 -07:00 committed by GitHub
parent a01058f041
commit f2171a51c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 190 additions and 157 deletions

View File

@ -2,14 +2,11 @@ import AdComponent from "discourse/plugins/discourse-adplugin/discourse/componen
import discourseComputed, { observes } from "discourse-common/utils/decorators"; import discourseComputed, { observes } from "discourse-common/utils/decorators";
import loadScript from "discourse/lib/load-script"; import loadScript from "discourse/lib/load-script";
const publisherId = Discourse.SiteSettings.adbutler_publisher_id;
const adserverHostname = Discourse.SiteSettings.adbutler_adserver_hostname;
let _loaded = false, let _loaded = false,
_promise = null, _promise = null,
_c = 0; _c = 0;
function loadAdbutler() { function loadAdbutler(adserverHostname) {
if (_loaded) { if (_loaded) {
return Ember.RSVP.resolve(); return Ember.RSVP.resolve();
} }
@ -29,6 +26,7 @@ function loadAdbutler() {
export default AdComponent.extend({ export default AdComponent.extend({
divs: null, divs: null,
publisherId: null,
init() { init() {
let dimensions = [728, 90]; let dimensions = [728, 90];
@ -56,7 +54,12 @@ export default AdComponent.extend({
let divId = "placement-" + zoneId + "-" + _c; let divId = "placement-" + zoneId + "-" + _c;
this.set("divId", divId); this.set("divId", divId);
let publisherId = this.siteSettings.adbutler_publisher_id;
this.set("publisherId", publisherId);
_c++; _c++;
this.divs.push({ this.divs.push({
divId: divId, divId: divId,
publisherId: publisherId, publisherId: publisherId,
@ -64,7 +67,6 @@ export default AdComponent.extend({
dimensions: dimensions, dimensions: dimensions,
}); });
this.set("publisherId", publisherId);
this._super(); this._super();
}, },
@ -73,7 +75,9 @@ export default AdComponent.extend({
return; // Don't load external JS during tests return; // Don't load external JS during tests
} }
loadAdbutler().then( const adserverHostname = this.siteSettings.adbutler_adserver_hostname;
loadAdbutler(adserverHostname).then(
function () { function () {
if (this.divs.length > 0) { if (this.divs.length > 0) {
let abkw = window.abkw || ""; let abkw = window.abkw || "";
@ -117,18 +121,24 @@ export default AdComponent.extend({
@discourseComputed("currentUser.trust_level") @discourseComputed("currentUser.trust_level")
showToTrustLevel(trustLevel) { showToTrustLevel(trustLevel) {
return !( return !(
trustLevel && trustLevel && trustLevel > this.siteSettings.adbutler_through_trust_level
trustLevel > Discourse.SiteSettings.adbutler_through_trust_level
); );
}, },
@discourseComputed( @discourseComputed(
"publisherId",
"showToTrustLevel", "showToTrustLevel",
"showToGroups", "showToGroups",
"showAfterPost", "showAfterPost",
"showOnCurrentPage" "showOnCurrentPage"
) )
showAd(showToTrustLevel, showToGroups, showAfterPost, showOnCurrentPage) { showAd(
publisherId,
showToTrustLevel,
showToGroups,
showAfterPost,
showOnCurrentPage
) {
return ( return (
publisherId && publisherId &&
showToTrustLevel && showToTrustLevel &&

View File

@ -1,134 +1,6 @@
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component"; import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
const data = {
"topic-list-top": {},
"topic-above-post-stream": {},
"topic-above-suggested": {},
"post-bottom": {},
};
let mobileView = Discourse.Site.currentProp("mobileView");
if (!mobileView && Discourse.SiteSettings.amazon_topic_list_top_src_code) {
data["topic-list-top"]["user_input"] =
Discourse.SiteSettings.amazon_topic_list_top_src_code;
data["topic-list-top"]["amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_topic_list_top_ad_width_code,
10
);
data["topic-list-top"]["amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_topic_list_top_ad_height_code,
10
);
}
if (
mobileView &&
Discourse.SiteSettings.amazon_mobile_topic_list_top_src_code
) {
data["topic-list-top"]["user_input_mobile"] =
Discourse.SiteSettings.amazon_mobile_topic_list_top_src_code;
data["topic-list-top"]["mobile_amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_list_top_ad_width_code,
10
);
data["topic-list-top"]["mobile_amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_list_top_ad_height_code,
10
);
}
if (
!mobileView &&
Discourse.SiteSettings.amazon_topic_above_post_stream_src_code
) {
data["topic-above-post-stream"]["user_input"] =
Discourse.SiteSettings.amazon_topic_above_post_stream_src_code;
data["topic-above-post-stream"]["amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_topic_above_post_stream_ad_width_code,
10
);
data["topic-above-post-stream"]["amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_topic_above_post_stream_ad_height_code,
10
);
}
if (
mobileView &&
Discourse.SiteSettings.amazon_mobile_topic_above_post_stream_src_code
) {
data["topic-above-post-stream"]["user_input_mobile"] =
Discourse.SiteSettings.amazon_mobile_topic_above_post_stream_src_code;
data["topic-above-post-stream"]["mobile_amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_above_post_stream_ad_width_code,
10
);
data["topic-above-post-stream"]["mobile_amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_above_post_stream_ad_height_code,
10
);
}
if (
!mobileView &&
Discourse.SiteSettings.amazon_topic_above_suggested_src_code
) {
data["topic-above-suggested"]["user_input"] =
Discourse.SiteSettings.amazon_topic_above_suggested_src_code;
data["topic-above-suggested"]["amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_topic_above_suggested_ad_width_code,
10
);
data["topic-above-suggested"]["amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_topic_above_suggested_ad_height_code,
10
);
}
if (
mobileView &&
Discourse.SiteSettings.amazon_mobile_topic_above_suggested_src_code
) {
data["topic-above-suggested"]["user_input_mobile"] =
Discourse.SiteSettings.amazon_mobile_topic_above_suggested_src_code;
data["topic-above-suggested"]["mobile_amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_above_suggested_ad_width_code,
10
);
data["topic-above-suggested"]["mobile_amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_mobile_topic_above_suggested_ad_height_code,
10
);
}
if (!mobileView && Discourse.SiteSettings.amazon_post_bottom_src_code) {
data["post-bottom"]["user_input"] =
Discourse.SiteSettings.amazon_post_bottom_src_code;
data["post-bottom"]["amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_post_bottom_ad_width_code,
10
);
data["post-bottom"]["amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_post_bottom_ad_height_code,
10
);
}
if (mobileView && Discourse.SiteSettings.amazon_mobile_post_bottom_src_code) {
data["post-bottom"]["user_input_mobile"] =
Discourse.SiteSettings.amazon_mobile_post_bottom_src_code;
data["post-bottom"]["mobile_amazon_width"] = parseInt(
Discourse.SiteSettings.amazon_mobile_post_bottom_ad_width_code,
10
);
data["post-bottom"]["mobile_amazon_height"] = parseInt(
Discourse.SiteSettings.amazon_mobile_post_bottom_ad_height_code,
10
);
}
export default AdComponent.extend({ export default AdComponent.extend({
classNames: ["amazon-product-links"], classNames: ["amazon-product-links"],
@ -140,7 +12,139 @@ export default AdComponent.extend({
), ),
init() { init() {
let placement = this.get("placement"); const data = {
"topic-list-top": {},
"topic-above-post-stream": {},
"topic-above-suggested": {},
"post-bottom": {},
};
const mobileView = this.site.get("mobileView");
const placement = this.get("placement");
if (!mobileView && this.siteSettings.amazon_topic_list_top_src_code) {
data["topic-list-top"][
"user_input"
] = this.siteSettings.amazon_topic_list_top_src_code;
data["topic-list-top"]["amazon_width"] = parseInt(
this.siteSettings.amazon_topic_list_top_ad_width_code,
10
);
data["topic-list-top"]["amazon_height"] = parseInt(
this.siteSettings.amazon_topic_list_top_ad_height_code,
10
);
}
if (mobileView && this.siteSettings.amazon_mobile_topic_list_top_src_code) {
data["topic-list-top"][
"user_input_mobile"
] = this.siteSettings.amazon_mobile_topic_list_top_src_code;
data["topic-list-top"]["mobile_amazon_width"] = parseInt(
this.siteSettings.amazon_mobile_topic_list_top_ad_width_code,
10
);
data["topic-list-top"]["mobile_amazon_height"] = parseInt(
this.siteSettings.amazon_mobile_topic_list_top_ad_height_code,
10
);
}
if (
!mobileView &&
this.siteSettings.amazon_topic_above_post_stream_src_code
) {
data["topic-above-post-stream"][
"user_input"
] = this.siteSettings.amazon_topic_above_post_stream_src_code;
data["topic-above-post-stream"]["amazon_width"] = parseInt(
this.siteSettings.amazon_topic_above_post_stream_ad_width_code,
10
);
data["topic-above-post-stream"]["amazon_height"] = parseInt(
this.siteSettings.amazon_topic_above_post_stream_ad_height_code,
10
);
}
if (
mobileView &&
this.siteSettings.amazon_mobile_topic_above_post_stream_src_code
) {
data["topic-above-post-stream"][
"user_input_mobile"
] = this.siteSettings.amazon_mobile_topic_above_post_stream_src_code;
data["topic-above-post-stream"]["mobile_amazon_width"] = parseInt(
this.siteSettings.amazon_mobile_topic_above_post_stream_ad_width_code,
10
);
data["topic-above-post-stream"]["mobile_amazon_height"] = parseInt(
this.siteSettings.amazon_mobile_topic_above_post_stream_ad_height_code,
10
);
}
if (
!mobileView &&
this.siteSettings.amazon_topic_above_suggested_src_code
) {
data["topic-above-suggested"][
"user_input"
] = this.siteSettings.amazon_topic_above_suggested_src_code;
data["topic-above-suggested"]["amazon_width"] = parseInt(
this.siteSettings.amazon_topic_above_suggested_ad_width_code,
10
);
data["topic-above-suggested"]["amazon_height"] = parseInt(
this.siteSettings.amazon_topic_above_suggested_ad_height_code,
10
);
}
if (
mobileView &&
this.siteSettings.amazon_mobile_topic_above_suggested_src_code
) {
data["topic-above-suggested"][
"user_input_mobile"
] = this.siteSettings.amazon_mobile_topic_above_suggested_src_code;
data["topic-above-suggested"]["mobile_amazon_width"] = parseInt(
this.siteSettings.amazon_mobile_topic_above_suggested_ad_width_code,
10
);
data["topic-above-suggested"]["mobile_amazon_height"] = parseInt(
this.siteSettings.amazon_mobile_topic_above_suggested_ad_height_code,
10
);
}
if (!mobileView && this.siteSettings.amazon_post_bottom_src_code) {
data["post-bottom"][
"user_input"
] = this.siteSettings.amazon_post_bottom_src_code;
data["post-bottom"]["amazon_width"] = parseInt(
this.siteSettings.amazon_post_bottom_ad_width_code,
10
);
data["post-bottom"]["amazon_height"] = parseInt(
this.siteSettings.amazon_post_bottom_ad_height_code,
10
);
}
if (mobileView && this.siteSettings.amazon_mobile_post_bottom_src_code) {
data["post-bottom"][
"user_input_mobile"
] = this.siteSettings.amazon_mobile_post_bottom_src_code;
data["post-bottom"]["mobile_amazon_width"] = parseInt(
this.siteSettings.amazon_mobile_post_bottom_ad_width_code,
10
);
data["post-bottom"]["mobile_amazon_height"] = parseInt(
this.siteSettings.amazon_mobile_post_bottom_ad_height_code,
10
);
}
this.set("user_input", data[placement]["user_input"]); this.set("user_input", data[placement]["user_input"]);
this.set("amazon_width", data[placement]["amazon_width"]); this.set("amazon_width", data[placement]["amazon_width"]);
this.set("amazon_height", data[placement]["amazon_height"]); this.set("amazon_height", data[placement]["amazon_height"]);
@ -178,8 +182,7 @@ export default AdComponent.extend({
@discourseComputed("currentUser.trust_level") @discourseComputed("currentUser.trust_level")
showToTrustLevel(trustLevel) { showToTrustLevel(trustLevel) {
return !( return !(
trustLevel && trustLevel && trustLevel > this.siteSettings.amazon_through_trust_level
trustLevel > Discourse.SiteSettings.amazon_through_trust_level
); );
}, },

View File

@ -1,33 +1,45 @@
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component"; import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
const serve_id = Discourse.SiteSettings.carbonads_serve_id,
placement = Discourse.SiteSettings.carbonads_placement;
export default AdComponent.extend({ export default AdComponent.extend({
serve_id: null,
placement: null,
init() { init() {
this.set("serve_id", serve_id); this.set("serve_id", this.siteSettings.carbonads_serve_id);
this.set("placement", this.siteSettings.carbonads_placement);
this._super(); this._super();
}, },
@discourseComputed("serve_id") @discourseComputed("serve_id", "placement")
url(serveId) { url(serveId, placement) {
return `//cdn.carbonads.com/carbon.js?serve=${serveId}&placement=${placement}`.htmlSafe(); return `//cdn.carbonads.com/carbon.js?serve=${serveId}&placement=${placement}`.htmlSafe();
}, },
@discourseComputed("currentUser.trust_level") @discourseComputed("currentUser.trust_level")
showToTrustLevel(trustLevel) { showToTrustLevel(trustLevel) {
return !( return !(
trustLevel && trustLevel && trustLevel > this.siteSettings.carbonads_through_trust_level
trustLevel > Discourse.SiteSettings.carbonads_through_trust_level
); );
}, },
@discourseComputed("showToTrustLevel", "showToGroups", "showOnCurrentPage") @discourseComputed(
showAd(showToTrustLevel, showToGroups, showOnCurrentPage) { "placement",
"serve_id",
"showToTrustLevel",
"showToGroups",
"showOnCurrentPage"
)
showAd(
placement,
serveId,
showToTrustLevel,
showToGroups,
showOnCurrentPage
) {
return ( return (
placement && placement &&
serve_id && serveId &&
showToTrustLevel && showToTrustLevel &&
showToGroups && showToGroups &&
showOnCurrentPage showOnCurrentPage

View File

@ -4,8 +4,7 @@ import loadScript from "discourse/lib/load-script";
let _loaded = false, let _loaded = false,
_promise = null, _promise = null,
renderCounts = {}, renderCounts = {};
publisher_id = Discourse.SiteSettings.adsense_publisher_code;
function parseAdWidth(value) { function parseAdWidth(value) {
if (value === "responsive") { if (value === "responsive") {
@ -102,7 +101,7 @@ export default AdComponent.extend({
], ],
loadedGoogletag: false, loadedGoogletag: false,
publisher_id: publisher_id, publisher_id: null,
ad_width: null, ad_width: null,
ad_height: null, ad_height: null,
@ -134,6 +133,7 @@ export default AdComponent.extend({
this.set("ad_width", parseAdWidth(size)); this.set("ad_width", parseAdWidth(size));
this.set("ad_height", parseAdHeight(size)); this.set("ad_height", parseAdHeight(size));
this.set("ad_code", this.siteSettings[config.code]); this.set("ad_code", this.siteSettings[config.code]);
this.set("publisher_id", this.siteSettings.adsense_publisher_code);
this._super(); this._super();
}, },
@ -220,14 +220,21 @@ export default AdComponent.extend({
}, },
@discourseComputed( @discourseComputed(
"publisher_id",
"showToTrustLevel", "showToTrustLevel",
"showToGroups", "showToGroups",
"showAfterPost", "showAfterPost",
"showOnCurrentPage" "showOnCurrentPage"
) )
showAd(showToTrustLevel, showToGroups, showAfterPost, showOnCurrentPage) { showAd(
publisherId,
showToTrustLevel,
showToGroups,
showAfterPost,
showOnCurrentPage
) {
return ( return (
this.siteSettings.adsense_publisher_code && publisherId &&
showToTrustLevel && showToTrustLevel &&
showToGroups && showToGroups &&
showAfterPost && showAfterPost &&

View File

@ -1,4 +1,5 @@
import { withPluginApi } from "discourse/lib/plugin-api"; import { withPluginApi } from "discourse/lib/plugin-api";
import Site from "discourse/models/site";
export default { export default {
name: "initialize-ad-plugin", name: "initialize-ad-plugin",
@ -28,7 +29,7 @@ export default {
} }
messageBus.subscribe("/site/house-creatives", function (houseAdsSettings) { messageBus.subscribe("/site/house-creatives", function (houseAdsSettings) {
Discourse.Site.currentProp("house_creatives", houseAdsSettings); Site.currentProp("house_creatives", houseAdsSettings);
}); });
}, },
}; };