A platform for community discussion. Free, open, simple.
FIX: Fix inconsistent route filtering in house ads initialization (#37700)
Sam0f1c33fee68a
diff --git a/plugins/discourse-adplugin/assets/javascripts/discourse/components/house-ad.gjs b/plugins/discourse-adplugin/assets/javascripts/discourse/components/house-ad.gjsindex fc2f4741859..0427fd3a7eb 100644--- a/plugins/discourse-adplugin/assets/javascripts/discourse/components/house-ad.gjs+++ b/plugins/discourse-adplugin/assets/javascripts/discourse/components/house-ad.gjs@@ -166,11 +166,24 @@ export default class HouseAd extends AdComponent {// filter out ads that should not be shown on the current pageconst filteredAds = adNames.filter((adName) => {const ad = houseAds.creatives[adName];- return (- ad &&- (!ad.category_ids?.length ||- ad.category_ids.includes(this.currentCategoryId))- );+ if (!ad) {+ return false;+ }++ const hasCategoryScope = ad.category_ids?.length > 0;+ const hasRouteScope =+ this.siteSettings.ad_plugin_routes_enabled && ad.routes?.length > 0;++ if (!hasCategoryScope && !hasRouteScope) {+ return true;+ }++ const matchesCategory =+ hasCategoryScope &&+ ad.category_ids.includes(this.currentCategoryId);+ const matchesRoute =+ hasRouteScope && ad.routes.includes(this.currentRouteName);+ return matchesCategory || matchesRoute;});adIndex[placement] = Math.floor(Math.random() * filteredAds.length);});diff --git a/plugins/discourse-adplugin/test/javascripts/acceptance/house-ad-test.js b/plugins/discourse-adplugin/test/javascripts/acceptance/house-ad-test.jsindex 5289d6eac17..6b506bfb75d 100644--- a/plugins/discourse-adplugin/test/javascripts/acceptance/house-ad-test.js+++ b/plugins/discourse-adplugin/test/javascripts/acceptance/house-ad-test.js@@ -128,6 +128,52 @@ acceptance(`House Ads`, function (needs) {});});+acceptance(`House Ads | Route Filtering | Display Ad`, function (needs) {+ needs.user();+ needs.settings({+ no_ads_for_categories: "",+ ad_plugin_routes_enabled: true,+ });+ needs.site({+ house_creatives: {+ settings: {+ topic_list_top: "Route Scoped Ad",+ },+ creatives: {+ "Route Scoped Ad": {+ html: "<div class='h-route-ad'>ROUTE AD</div>",+ category_ids: [],+ routes: ["discovery.latest"],+ },+ },+ },+ });++ test("displays ad when current route matches ad routes", async function (assert) {+ updateCurrentUser({+ staff: false,+ trust_level: 1,+ show_to_groups: true,+ });+ await visit("/latest");+ assert+ .dom(".h-route-ad")+ .exists("ad is displayed because current route matches ad routes");+ });++ test("hides ad when current route does not match ad routes", async function (assert) {+ updateCurrentUser({+ staff: false,+ trust_level: 1,+ show_to_groups: true,+ });+ await visit("/top");+ assert+ .dom(".h-route-ad")+ .doesNotExist("ad is hidden because current route does not match");+ });+});+acceptance(`House Ads | Category and Group Permissions | Authenticated | Display Ad`,function (needs) {