DEV: Add compatibility with the Glimmer Post Stream (#247)

This commit updates the ad plugin to support the Glimmer Post Stream and refactors ad rendering logic for improved compatibility and maintainability.

Key changes:

- Introduce glimmer_post_stream_mode compatibility across all ad types and test suites.
- Replace .widget-connector with .ad-connector for ad placement after posts.
- Migrate initialize-ad-plugin.js to initialize-ad-plugin.gjs to leverage Glimmer and API improvements.
- Use withSilencedDeprecations to handle widget overrides for post ads.
- Update compatibility constraints in .discourse-compatibility.
- Refactor and expand acceptance tests for AdSense, DFP, and House Ads to run with both Glimmer and legacy post stream modes.
This commit is contained in:
Sérgio Saquetim 2025-07-08 16:28:54 -03:00 committed by GitHub
parent e51aad3bfd
commit 8a3263d050
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 573 additions and 518 deletions

View File

@ -1,3 +1,4 @@
< 3.5.0.beta8-dev: e51aad3bfd41cf2cb9557e025e833122a9f6c892
< 3.5.0.beta5-dev: bd9af0b9a577bfd2c01cc8b81a782a4e8b81b9c5
< 3.5.0.beta1-dev: 6ceba62e985223c269782c4bc9a1bdc4a5adba28
< 3.4.0.beta2-dev: 7685ebf396c93e8accc5a76a81fcec4384a73fa3

View File

@ -0,0 +1,53 @@
import { hbs } from "ember-cli-htmlbars";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { withPluginApi } from "discourse/lib/plugin-api";
import Site from "discourse/models/site";
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
import PostBottomAd from "../components/post-bottom-ad";
export default {
name: "initialize-ad-plugin",
initialize(container) {
withPluginApi((api) => {
customizePost(api);
});
const messageBus = container.lookup("service:message-bus");
const currentUser = container.lookup("service:current-user");
const channel = currentUser
? "/site/house-creatives/logged-in"
: "/site/house-creatives/anonymous";
messageBus.subscribe(channel, function (houseAdsSettings) {
Site.currentProp("house_creatives", houseAdsSettings);
});
},
};
function customizePost(api) {
api.renderAfterWrapperOutlet(
"post-article",
<template>
<div class="ad-connector">
<PostBottomAd @model={{@post}} />
</div>
</template>
);
withSilencedDeprecations("discourse.post-stream-widget-overrides", () =>
customizeWidgetPost(api)
);
}
function customizeWidgetPost(api) {
registerWidgetShim(
"after-post-ad",
"div.ad-connector",
hbs`<PostBottomAd @model={{@data}} />`
);
api.decorateWidget("post:after", (helper) => {
return helper.attach("after-post-ad", helper.widget.model);
});
}

View File

@ -1,32 +0,0 @@
import { hbs } from "ember-cli-htmlbars";
import { withPluginApi } from "discourse/lib/plugin-api";
import Site from "discourse/models/site";
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
export default {
name: "initialize-ad-plugin",
initialize(container) {
registerWidgetShim(
"after-post-ad",
"div.widget-connector",
hbs`<PostBottomAd @model={{@data}} />`
);
withPluginApi("0.1", (api) => {
api.decorateWidget("post:after", (helper) => {
return helper.attach("after-post-ad", helper.widget.model);
});
});
const messageBus = container.lookup("service:message-bus");
const currentUser = container.lookup("service:current-user");
const channel = currentUser
? "/site/house-creatives/logged-in"
: "/site/house-creatives/anonymous";
messageBus.subscribe(channel, function (houseAdsSettings) {
Site.currentProp("house_creatives", houseAdsSettings);
});
},
};

View File

@ -7,7 +7,10 @@ import {
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
acceptance("AdSense", function (needs) {
["enabled", "disabled"].forEach((postStreamMode) => {
acceptance(
`AdSense (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_groups: "47",
@ -28,6 +31,7 @@ acceptance("AdSense", function (needs) {
adsense_nth_post_code: 6,
adsense_topic_above_post_stream_code: "above_post_stream_ad_unit",
adsense_topic_above_post_stream_ad_sizes: "728*90 - leaderboard",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -61,15 +65,15 @@ acceptance("AdSense", function (needs) {
.exists({ count: 3 }, "it should render 3 ads");
assert
.dom("#post_6 + .widget-connector .google-adsense.adsense-post-bottom")
.dom("#post_6 + .ad-connector .google-adsense.adsense-post-bottom")
.exists({ count: 1 }, "ad after 6th post");
assert
.dom("#post_12 + .widget-connector .google-adsense.adsense-post-bottom")
.dom("#post_12 + .ad-connector .google-adsense.adsense-post-bottom")
.exists({ count: 1 }, "ad after 12th post");
assert
.dom("#post_18 + .widget-connector .google-adsense.adsense-post-bottom")
.dom("#post_18 + .ad-connector .google-adsense.adsense-post-bottom")
.exists({ count: 1 }, "ad after 18th post");
});
@ -104,4 +108,6 @@ acceptance("AdSense", function (needs) {
.dom(".google-adsense.adsense-topic-above-post-stream")
.doesNotExist("it should render 0 ads");
});
}
);
});

View File

@ -7,14 +7,20 @@ import {
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
acceptance("DFP Ads", function (needs) {
["enabled", "disabled"].forEach((postStreamMode) => {
acceptance(
`DFP Ads (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_groups: "47",
no_ads_for_categories: "1",
dfp_publisher_id: "MYdfpID",
dfp_through_trust_level: 2,
dfp_display_groups: [AUTO_GROUPS.trust_level_1, AUTO_GROUPS.trust_level_2],
dfp_display_groups: [
AUTO_GROUPS.trust_level_1,
AUTO_GROUPS.trust_level_2,
],
dfp_topic_list_top_code: "list_top_ad_unit",
dfp_topic_list_top_ad_sizes: "728*90 - leaderboard",
dfp_mobile_topic_list_top_code: "mobile_list_top_ad_unit",
@ -26,6 +32,7 @@ acceptance("DFP Ads", function (needs) {
dfp_nth_post_code: 6,
dfp_topic_above_post_stream_code: "list_top_ad_unit",
dfp_topic_above_post_stream_ad_sizes: "728*90 - leaderboard",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -54,15 +61,15 @@ acceptance("DFP Ads", function (needs) {
.exists({ count: 3 }, "it should render 3 ads");
assert
.dom("#post_6 + .widget-connector .google-dfp-ad.dfp-ad-post-bottom")
.dom("#post_6 + .ad-connector .google-dfp-ad.dfp-ad-post-bottom")
.exists({ count: 1 }, "ad after 6th post");
assert
.dom("#post_12 + .widget-connector .google-dfp-ad.dfp-ad-post-bottom")
.dom("#post_12 + .ad-connector .google-dfp-ad.dfp-ad-post-bottom")
.exists({ count: 1 }, "ad after 12th post");
assert
.dom("#post_18 + .widget-connector .google-dfp-ad.dfp-ad-post-bottom")
.dom("#post_18 + .ad-connector .google-dfp-ad.dfp-ad-post-bottom")
.exists({ count: 1 }, "ad after 18th post");
});
@ -101,4 +108,6 @@ acceptance("DFP Ads", function (needs) {
.dom(".google-dfp-ad.dfp-ad-topic-above-post-stream")
.doesNotExist("it should render 0 ads");
});
}
);
});

View File

@ -6,12 +6,16 @@ import {
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
acceptance("House Ads", function (needs) {
["enabled", "disabled"].forEach((postStreamMode) => {
acceptance(
`House Ads (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_categories: "1",
house_ads_after_nth_post: 6,
house_ads_after_nth_topic: 3,
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -50,7 +54,11 @@ acceptance("House Ads", function (needs) {
});
test("correct ads show", async (assert) => {
updateCurrentUser({ staff: false, trust_level: 1, show_to_groups: true });
updateCurrentUser({
staff: false,
trust_level: 1,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts
assert
@ -66,15 +74,15 @@ acceptance("House Ads", function (needs) {
.exists({ count: 3 }, "it should render 3 ads between posts");
assert
.dom("#post_6 + .widget-connector .h-post")
.dom("#post_6 + .ad-connector .h-post")
.exists({ count: 1 }, "ad after 6th post");
assert
.dom("#post_12 + .widget-connector .h-post")
.dom("#post_12 + .ad-connector .h-post")
.exists({ count: 1 }, "ad after 12th post");
assert
.dom("#post_18 + .widget-connector .h-post")
.dom("#post_18 + .ad-connector .h-post")
.exists({ count: 1 }, "ad after 18th post");
await visit("/latest");
@ -125,14 +133,16 @@ acceptance("House Ads", function (needs) {
"no ad above category topic list because category is in no_ads_for_categories"
);
});
});
}
);
acceptance(
"House Ads | Category and Group Permissions | Authenticated | Display Ad",
acceptance(
`House Ads | Category and Group Permissions | Authenticated | Display Ad (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -163,14 +173,15 @@ acceptance(
);
});
}
);
);
acceptance(
"House Ads | Category and Group Permissions | Authenticated | Hide Ad",
acceptance(
`House Ads | Category and Group Permissions | Authenticated | Hide Ad (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -201,13 +212,14 @@ acceptance(
);
});
}
);
);
acceptance(
"House Ads | Category and Group Permissions | Anonymous | Hide Ad",
acceptance(
`House Ads | Category and Group Permissions | Anonymous | Hide Ad (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -233,13 +245,14 @@ acceptance(
);
});
}
);
);
acceptance(
"House Ads | Category and Group Permissions | Anonymous | Hide Ad",
acceptance(
`House Ads | Category and Group Permissions | Anonymous | Hide Ad (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -265,13 +278,14 @@ acceptance(
);
});
}
);
);
acceptance(
"House Ads | Category and Group Permissions | Anonymous | Show Ad",
acceptance(
`House Ads | Category and Group Permissions | Anonymous | Show Ad (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -297,13 +311,14 @@ acceptance(
);
});
}
);
);
acceptance(
"House Ads | Category and Group Permissions | Anonymous | Show non-restricted ads",
acceptance(
`House Ads | Category and Group Permissions | Anonymous | Show non-restricted ads (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.settings({
no_ads_for_categories: "",
glimmer_post_stream_mode: postStreamMode,
});
needs.site({
house_creatives: {
@ -325,7 +340,10 @@ acceptance(
test("shows non-restricted ad to anon users", async (assert) => {
await visit("/c/bug/1");
assert.dom(".h-topic-list-two").exists("non-restricted ad is displayed");
assert
.dom(".h-topic-list-two")
.exists("non-restricted ad is displayed");
});
}
);
);
});