Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 8a3263d

Browse files
authored
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.
1 parent e51aad3 commit 8a3263d

File tree

6 files changed

+538
-483
lines changed

6 files changed

+538
-483
lines changed

.discourse-compatibility

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
< 3.5.0.beta8-dev: e51aad3bfd41cf2cb9557e025e833122a9f6c892
12
< 3.5.0.beta5-dev: bd9af0b9a577bfd2c01cc8b81a782a4e8b81b9c5
23
< 3.5.0.beta1-dev: 6ceba62e985223c269782c4bc9a1bdc4a5adba28
34
< 3.4.0.beta2-dev: 7685ebf396c93e8accc5a76a81fcec4384a73fa3
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { hbs } from "ember-cli-htmlbars";
2+
import { withSilencedDeprecations } from "discourse/lib/deprecated";
3+
import { withPluginApi } from "discourse/lib/plugin-api";
4+
import Site from "discourse/models/site";
5+
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
6+
import PostBottomAd from "../components/post-bottom-ad";
7+
8+
export default {
9+
name: "initialize-ad-plugin",
10+
initialize(container) {
11+
withPluginApi((api) => {
12+
customizePost(api);
13+
});
14+
15+
const messageBus = container.lookup("service:message-bus");
16+
const currentUser = container.lookup("service:current-user");
17+
18+
const channel = currentUser
19+
? "/site/house-creatives/logged-in"
20+
: "/site/house-creatives/anonymous";
21+
22+
messageBus.subscribe(channel, function (houseAdsSettings) {
23+
Site.currentProp("house_creatives", houseAdsSettings);
24+
});
25+
},
26+
};
27+
28+
function customizePost(api) {
29+
api.renderAfterWrapperOutlet(
30+
"post-article",
31+
<template>
32+
<div class="ad-connector">
33+
<PostBottomAd @model={{@post}} />
34+
</div>
35+
</template>
36+
);
37+
38+
withSilencedDeprecations("discourse.post-stream-widget-overrides", () =>
39+
customizeWidgetPost(api)
40+
);
41+
}
42+
43+
function customizeWidgetPost(api) {
44+
registerWidgetShim(
45+
"after-post-ad",
46+
"div.ad-connector",
47+
hbs`<PostBottomAd @model={{@data}} />`
48+
);
49+
50+
api.decorateWidget("post:after", (helper) => {
51+
return helper.attach("after-post-ad", helper.widget.model);
52+
});
53+
}

assets/javascripts/discourse/initializers/initialize-ad-plugin.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

test/javascripts/acceptance/adsense-test.js

Lines changed: 94 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,101 +7,107 @@ import {
77
updateCurrentUser,
88
} from "discourse/tests/helpers/qunit-helpers";
99

10-
acceptance("AdSense", function (needs) {
11-
needs.user();
12-
needs.settings({
13-
no_ads_for_groups: "47",
14-
no_ads_for_categories: "1",
15-
adsense_publisher_code: "MYADSENSEID",
16-
adsense_display_groups: [
17-
AUTO_GROUPS.trust_level_1,
18-
AUTO_GROUPS.trust_level_2,
19-
],
20-
adsense_topic_list_top_code: "list_top_ad_unit",
21-
adsense_topic_list_top_ad_sizes: "728*90 - leaderboard",
22-
adsense_mobile_topic_list_top_code: "mobile_list_top_ad_unit",
23-
adsense_mobile_topic_list_top_ad_size: "300*250 - medium rectangle",
24-
adsense_post_bottom_code: "post_bottom_ad_unit",
25-
adsense_post_bottom_ad_sizes: "728*90 - leaderboard",
26-
adsense_mobile_post_bottom_code: "mobile_post_bottom_ad_unit",
27-
adsense_mobile_post_bottom_ad_size: "300*250 - medium rectangle",
28-
adsense_nth_post_code: 6,
29-
adsense_topic_above_post_stream_code: "above_post_stream_ad_unit",
30-
adsense_topic_above_post_stream_ad_sizes: "728*90 - leaderboard",
31-
});
32-
needs.site({
33-
house_creatives: {
34-
settings: {
35-
topic_list_top: "",
36-
topic_above_post_stream: "",
37-
topic_above_suggested: "",
38-
post_bottom: "",
39-
after_nth_post: 20,
40-
},
41-
creatives: {},
42-
},
43-
});
10+
["enabled", "disabled"].forEach((postStreamMode) => {
11+
acceptance(
12+
`AdSense (glimmer_post_stream_mode = ${postStreamMode})`,
13+
function (needs) {
14+
needs.user();
15+
needs.settings({
16+
no_ads_for_groups: "47",
17+
no_ads_for_categories: "1",
18+
adsense_publisher_code: "MYADSENSEID",
19+
adsense_display_groups: [
20+
AUTO_GROUPS.trust_level_1,
21+
AUTO_GROUPS.trust_level_2,
22+
],
23+
adsense_topic_list_top_code: "list_top_ad_unit",
24+
adsense_topic_list_top_ad_sizes: "728*90 - leaderboard",
25+
adsense_mobile_topic_list_top_code: "mobile_list_top_ad_unit",
26+
adsense_mobile_topic_list_top_ad_size: "300*250 - medium rectangle",
27+
adsense_post_bottom_code: "post_bottom_ad_unit",
28+
adsense_post_bottom_ad_sizes: "728*90 - leaderboard",
29+
adsense_mobile_post_bottom_code: "mobile_post_bottom_ad_unit",
30+
adsense_mobile_post_bottom_ad_size: "300*250 - medium rectangle",
31+
adsense_nth_post_code: 6,
32+
adsense_topic_above_post_stream_code: "above_post_stream_ad_unit",
33+
adsense_topic_above_post_stream_ad_sizes: "728*90 - leaderboard",
34+
glimmer_post_stream_mode: postStreamMode,
35+
});
36+
needs.site({
37+
house_creatives: {
38+
settings: {
39+
topic_list_top: "",
40+
topic_above_post_stream: "",
41+
topic_above_suggested: "",
42+
post_bottom: "",
43+
after_nth_post: 20,
44+
},
45+
creatives: {},
46+
},
47+
});
4448

45-
test("correct number of ads should show", async (assert) => {
46-
updateCurrentUser({
47-
staff: false,
48-
trust_level: 1,
49-
groups: [AUTO_GROUPS.trust_level_1],
50-
show_adsense_ads: true,
51-
show_to_groups: true,
52-
});
53-
await visit("/t/280"); // 20 posts
49+
test("correct number of ads should show", async (assert) => {
50+
updateCurrentUser({
51+
staff: false,
52+
trust_level: 1,
53+
groups: [AUTO_GROUPS.trust_level_1],
54+
show_adsense_ads: true,
55+
show_to_groups: true,
56+
});
57+
await visit("/t/280"); // 20 posts
5458

55-
assert
56-
.dom(".google-adsense.adsense-topic-above-post-stream")
57-
.exists({ count: 1 }, "it should render 1 ad above post stream");
59+
assert
60+
.dom(".google-adsense.adsense-topic-above-post-stream")
61+
.exists({ count: 1 }, "it should render 1 ad above post stream");
5862

59-
assert
60-
.dom(".google-adsense.adsense-post-bottom")
61-
.exists({ count: 3 }, "it should render 3 ads");
63+
assert
64+
.dom(".google-adsense.adsense-post-bottom")
65+
.exists({ count: 3 }, "it should render 3 ads");
6266

63-
assert
64-
.dom("#post_6 + .widget-connector .google-adsense.adsense-post-bottom")
65-
.exists({ count: 1 }, "ad after 6th post");
67+
assert
68+
.dom("#post_6 + .ad-connector .google-adsense.adsense-post-bottom")
69+
.exists({ count: 1 }, "ad after 6th post");
6670

67-
assert
68-
.dom("#post_12 + .widget-connector .google-adsense.adsense-post-bottom")
69-
.exists({ count: 1 }, "ad after 12th post");
71+
assert
72+
.dom("#post_12 + .ad-connector .google-adsense.adsense-post-bottom")
73+
.exists({ count: 1 }, "ad after 12th post");
7074

71-
assert
72-
.dom("#post_18 + .widget-connector .google-adsense.adsense-post-bottom")
73-
.exists({ count: 1 }, "ad after 18th post");
74-
});
75+
assert
76+
.dom("#post_18 + .ad-connector .google-adsense.adsense-post-bottom")
77+
.exists({ count: 1 }, "ad after 18th post");
78+
});
7579

76-
test("no ads for trust level 3", async (assert) => {
77-
updateCurrentUser({
78-
staff: false,
79-
trust_level: 3,
80-
groups: [AUTO_GROUPS.trust_level_3],
81-
});
82-
await visit("/t/280");
83-
assert
84-
.dom(".google-adsense.adsense-post-bottom")
85-
.doesNotExist("it should render 0 ads");
86-
});
80+
test("no ads for trust level 3", async (assert) => {
81+
updateCurrentUser({
82+
staff: false,
83+
trust_level: 3,
84+
groups: [AUTO_GROUPS.trust_level_3],
85+
});
86+
await visit("/t/280");
87+
assert
88+
.dom(".google-adsense.adsense-post-bottom")
89+
.doesNotExist("it should render 0 ads");
90+
});
8791

88-
test("can omit ads based on groups", async (assert) => {
89-
updateCurrentUser({
90-
staff: false,
91-
trust_level: 1,
92-
groups: [groupFixtures["/groups/discourse.json"].group],
93-
});
94-
await visit("/t/280");
95-
assert
96-
.dom(".google-adsense.adsense-post-bottom")
97-
.doesNotExist("it should render 0 ads");
98-
});
92+
test("can omit ads based on groups", async (assert) => {
93+
updateCurrentUser({
94+
staff: false,
95+
trust_level: 1,
96+
groups: [groupFixtures["/groups/discourse.json"].group],
97+
});
98+
await visit("/t/280");
99+
assert
100+
.dom(".google-adsense.adsense-post-bottom")
101+
.doesNotExist("it should render 0 ads");
102+
});
99103

100-
test("can omit ads based on category", async (assert) => {
101-
updateCurrentUser({ staff: false, trust_level: 1 });
102-
await visit("/t/28830");
103-
assert
104-
.dom(".google-adsense.adsense-topic-above-post-stream")
105-
.doesNotExist("it should render 0 ads");
106-
});
104+
test("can omit ads based on category", async (assert) => {
105+
updateCurrentUser({ staff: false, trust_level: 1 });
106+
await visit("/t/28830");
107+
assert
108+
.dom(".google-adsense.adsense-topic-above-post-stream")
109+
.doesNotExist("it should render 0 ads");
110+
});
111+
}
112+
);
107113
});

0 commit comments

Comments
 (0)