Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -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
Expand Down
53 changes: 53 additions & 0 deletions assets/javascripts/discourse/initializers/initialize-ad-plugin.gjs
Original file line number Diff line number Diff line change
@@ -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);
});
}
32 changes: 0 additions & 32 deletions assets/javascripts/discourse/initializers/initialize-ad-plugin.js

This file was deleted.

182 changes: 94 additions & 88 deletions test/javascripts/acceptance/adsense-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,107 @@ import {
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";

acceptance("AdSense", function (needs) {
needs.user();
needs.settings({
no_ads_for_groups: "47",
no_ads_for_categories: "1",
adsense_publisher_code: "MYADSENSEID",
adsense_display_groups: [
AUTO_GROUPS.trust_level_1,
AUTO_GROUPS.trust_level_2,
],
adsense_topic_list_top_code: "list_top_ad_unit",
adsense_topic_list_top_ad_sizes: "728*90 - leaderboard",
adsense_mobile_topic_list_top_code: "mobile_list_top_ad_unit",
adsense_mobile_topic_list_top_ad_size: "300*250 - medium rectangle",
adsense_post_bottom_code: "post_bottom_ad_unit",
adsense_post_bottom_ad_sizes: "728*90 - leaderboard",
adsense_mobile_post_bottom_code: "mobile_post_bottom_ad_unit",
adsense_mobile_post_bottom_ad_size: "300*250 - medium rectangle",
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",
});
needs.site({
house_creatives: {
settings: {
topic_list_top: "",
topic_above_post_stream: "",
topic_above_suggested: "",
post_bottom: "",
after_nth_post: 20,
},
creatives: {},
},
});
["enabled", "disabled"].forEach((postStreamMode) => {
acceptance(
`AdSense (glimmer_post_stream_mode = ${postStreamMode})`,
function (needs) {
needs.user();
needs.settings({
no_ads_for_groups: "47",
no_ads_for_categories: "1",
adsense_publisher_code: "MYADSENSEID",
adsense_display_groups: [
AUTO_GROUPS.trust_level_1,
AUTO_GROUPS.trust_level_2,
],
adsense_topic_list_top_code: "list_top_ad_unit",
adsense_topic_list_top_ad_sizes: "728*90 - leaderboard",
adsense_mobile_topic_list_top_code: "mobile_list_top_ad_unit",
adsense_mobile_topic_list_top_ad_size: "300*250 - medium rectangle",
adsense_post_bottom_code: "post_bottom_ad_unit",
adsense_post_bottom_ad_sizes: "728*90 - leaderboard",
adsense_mobile_post_bottom_code: "mobile_post_bottom_ad_unit",
adsense_mobile_post_bottom_ad_size: "300*250 - medium rectangle",
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: {
settings: {
topic_list_top: "",
topic_above_post_stream: "",
topic_above_suggested: "",
post_bottom: "",
after_nth_post: 20,
},
creatives: {},
},
});

test("correct number of ads should show", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 1,
groups: [AUTO_GROUPS.trust_level_1],
show_adsense_ads: true,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts
test("correct number of ads should show", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 1,
groups: [AUTO_GROUPS.trust_level_1],
show_adsense_ads: true,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts

assert
.dom(".google-adsense.adsense-topic-above-post-stream")
.exists({ count: 1 }, "it should render 1 ad above post stream");
assert
.dom(".google-adsense.adsense-topic-above-post-stream")
.exists({ count: 1 }, "it should render 1 ad above post stream");

assert
.dom(".google-adsense.adsense-post-bottom")
.exists({ count: 3 }, "it should render 3 ads");
assert
.dom(".google-adsense.adsense-post-bottom")
.exists({ count: 3 }, "it should render 3 ads");

assert
.dom("#post_6 + .widget-connector .google-adsense.adsense-post-bottom")
.exists({ count: 1 }, "ad after 6th post");
assert
.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")
.exists({ count: 1 }, "ad after 12th post");
assert
.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")
.exists({ count: 1 }, "ad after 18th post");
});
assert
.dom("#post_18 + .ad-connector .google-adsense.adsense-post-bottom")
.exists({ count: 1 }, "ad after 18th post");
});

test("no ads for trust level 3", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 3,
groups: [AUTO_GROUPS.trust_level_3],
});
await visit("/t/280");
assert
.dom(".google-adsense.adsense-post-bottom")
.doesNotExist("it should render 0 ads");
});
test("no ads for trust level 3", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 3,
groups: [AUTO_GROUPS.trust_level_3],
});
await visit("/t/280");
assert
.dom(".google-adsense.adsense-post-bottom")
.doesNotExist("it should render 0 ads");
});

test("can omit ads based on groups", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 1,
groups: [groupFixtures["/groups/discourse.json"].group],
});
await visit("/t/280");
assert
.dom(".google-adsense.adsense-post-bottom")
.doesNotExist("it should render 0 ads");
});
test("can omit ads based on groups", async (assert) => {
updateCurrentUser({
staff: false,
trust_level: 1,
groups: [groupFixtures["/groups/discourse.json"].group],
});
await visit("/t/280");
assert
.dom(".google-adsense.adsense-post-bottom")
.doesNotExist("it should render 0 ads");
});

test("can omit ads based on category", async (assert) => {
updateCurrentUser({ staff: false, trust_level: 1 });
await visit("/t/28830");
assert
.dom(".google-adsense.adsense-topic-above-post-stream")
.doesNotExist("it should render 0 ads");
});
test("can omit ads based on category", async (assert) => {
updateCurrentUser({ staff: false, trust_level: 1 });
await visit("/t/28830");
assert
.dom(".google-adsense.adsense-topic-above-post-stream")
.doesNotExist("it should render 0 ads");
});
}
);
});
Loading