diff --git a/.github/workflows/ad-remover-check.yml b/.github/workflows/ad-remover-check.yml index ffcfd7ee..0e2703e4 100644 --- a/.github/workflows/ad-remover-check.yml +++ b/.github/workflows/ad-remover-check.yml @@ -63,10 +63,19 @@ jobs: path: plugins/${{ env.PLUGIN_NAME }} fetch-depth: 1 + # Gone from GitHub (deleted or private): nothing to check against, + # which isn't a failure; the steps below skip until it's back - name: Fetch the ad remover id: remover + env: + GIT_TERMINAL_PROMPT: "0" run: | - git clone --depth 1 https://github.com/the-curious-2025/jtech-ad-remover.git ad-remover + if ! git clone --depth 1 https://github.com/the-curious-2025/jtech-ad-remover.git ad-remover; then + echo "found=false" >> $GITHUB_OUTPUT + echo "The ad remover's repository isn't public any more; nothing to check." >> $GITHUB_STEP_SUMMARY + exit 0 + fi + echo "found=true" >> $GITHUB_OUTPUT sha=$(git -C ad-remover rev-parse --short HEAD) version=$(ruby -rjson -e 'puts JSON.parse(File.read("ad-remover/manifest.json"))["version"]') echo "JTECH_AD_REMOVER_DIR=$PWD/ad-remover" >> $GITHUB_ENV @@ -75,26 +84,31 @@ jobs: echo "Ad remover $version at $sha" >> $GITHUB_STEP_SUMMARY - name: Setup Git + if: steps.remover.outputs.found == 'true' run: | git config --global user.email "ci@ci.invalid" git config --global user.name "Discourse CI" - name: Start redis + if: steps.remover.outputs.found == 'true' run: redis-server /etc/redis/redis.conf & - name: Start Postgres + if: steps.remover.outputs.found == 'true' run: | chown -R postgres /var/run/postgresql sudo -E -u postgres script/start_test_db.rb sudo -u postgres psql -c "CREATE ROLE $PGUSER LOGIN SUPERUSER PASSWORD '$PGPASSWORD';" - name: Container envs + if: steps.remover.outputs.found == 'true' id: container-envs run: | echo "ruby_version=$RUBY_VERSION" >> $GITHUB_OUTPUT echo "debian_release=$DEBIAN_RELEASE" >> $GITHUB_OUTPUT - name: Bundler cache + if: steps.remover.outputs.found == 'true' uses: actions/cache@v6 with: path: vendor/bundle @@ -103,6 +117,7 @@ jobs: ${{ runner.os }}-${{ steps.container-envs.outputs.ruby_version }}-${{ steps.container-envs.outputs.debian_release }}-gem- - name: Setup gems + if: steps.remover.outputs.found == 'true' run: | bundle config --local path vendor/bundle bundle config --local deployment true @@ -111,20 +126,24 @@ jobs: bundle clean - name: Install JS Dependencies + if: steps.remover.outputs.found == 'true' run: if [ -f yarn.lock ]; then yarn install --frozen-lockfile; else pnpm install --frozen-lockfile; fi - name: Install playwright + if: steps.remover.outputs.found == 'true' run: | if pnpm playwright -V; then pnpm playwright install --with-deps --no-shell chromium fi - name: Create and migrate database + if: steps.remover.outputs.found == 'true' run: | bin/rake db:create bin/rake db:migrate - name: Ember Build + if: steps.remover.outputs.found == 'true' env: EMBER_ENV: development run: | @@ -132,10 +151,11 @@ jobs: script/assemble_ember_build.rb - name: Run the ad remover against the ads + if: steps.remover.outputs.found == 'true' run: bundle exec rspec --format documentation plugins/${{ env.PLUGIN_NAME }}/spec/system/ads_remover_spec.rb - name: Open an issue when it gets past the ads - if: failure() && github.event_name == 'schedule' + if: failure() && github.event_name == 'schedule' && steps.remover.outputs.found == 'true' env: GH_TOKEN: ${{ github.token }} GH_REPO: ${{ github.repository }} diff --git a/assets/javascripts/discourse/initializers/jt-lines-lists.ts b/assets/javascripts/discourse/initializers/jt-lines-lists.ts new file mode 100644 index 00000000..a07a6105 --- /dev/null +++ b/assets/javascripts/discourse/initializers/jt-lines-lists.ts @@ -0,0 +1,52 @@ +import { withPluginApi } from "discourse/lib/plugin-api"; +import type { + default as JtLinesService, + JtLines, +} from "../services/jt-lines.ts"; + +interface ListResult { + topic_list?: { jt_lines?: JtLines | null } | null; + category_list?: { jt_lines?: JtLines | null } | null; +} + +// The lines come inside the welcome pages' own lists (sub_plugins/ads.rb): +// Latest's topic list, the Categories page's category list. Every way core +// turns such a response into a list passes through one of these two, before +// the page renders, so the welcome area has them as it draws. +export default { + name: "jt-l-lists", + + initialize() { + withPluginApi((api) => { + const lines = api.container.lookup("service:jt-lines") as JtLinesService; + + api.registerValueTransformer( + "topic-list-topics-from", + ({ + value, + context, + }: { + value: unknown; + context: { result?: ListResult }; + }) => { + lines.take(context.result?.topic_list?.jt_lines); + return value; + } + ); + + api.registerValueTransformer( + "category-list-attributes", + ({ + value, + context, + }: { + value: unknown; + context: { result?: ListResult }; + }) => { + lines.take(context.result?.category_list?.jt_lines); + return value; + } + ); + }); + }, +}; diff --git a/assets/javascripts/discourse/services/jt-lines.ts b/assets/javascripts/discourse/services/jt-lines.ts index c8a70a3d..271f7cc7 100644 --- a/assets/javascripts/discourse/services/jt-lines.ts +++ b/assets/javascripts/discourse/services/jt-lines.ts @@ -10,9 +10,10 @@ import { i18n } from "discourse-i18n"; import JtLineCard from "../components/jt-line.gts"; // The ads for the home page's welcome area (lib/discourse_jtech_ads.rb, -// docs/features/ads.md). They arrive inside the preloaded site data as -// site.jt_lines. Nothing in the browser is named after ads: blockers hide -// elements and requests by names like that. +// docs/features/ads.md). They arrive inside the welcome pages' own lists +// (initializers/jt-lines-lists), not in every page's site data. Nothing in +// the browser is named after ads: blockers hide elements and requests by +// names like that. export interface JtLine { k: string; // key @@ -88,6 +89,9 @@ export default class JtLinesService extends Service { @tracked preview: JtLine | null = null; // The ad on its way out during a swap @tracked leaving: JtLine | null = null; + // From the last welcome list that came with them; a page served before + // they moved there still has them in its site data + @tracked listed: JtLines | null = null; // A theme that carries the line itself says so at startup (the JTech // theme's hero does, when it's on); then the plugin's card stays out. @@ -110,7 +114,7 @@ export default class JtLinesService extends Service { } get data(): JtLines | null { - return this.site.jt_lines ?? null; + return this.listed ?? this.site.jt_lines ?? null; } get label(): string { @@ -190,6 +194,14 @@ export default class JtLinesService extends Service { return !!this.currentUser?.staff; } + // A list's lines. One without any (a category's list, a later page) + // leaves the ones there are. + take(lines: JtLines | null | undefined) { + if (lines && lines !== this.listed) { + this.listed = lines; + } + } + vote(line: JtLine): number | null { return line.k in this.votes ? this.votes[line.k] : (line.v ?? null); } diff --git a/docs/features/ads.md b/docs/features/ads.md index 42693be4..b2920bfe 100644 --- a/docs/features/ads.md +++ b/docs/features/ads.md @@ -146,7 +146,7 @@ The hero gets `.--lines` while the ads have it, and `.jt-hero__lines` holds them Ad blockers hide elements and block requests by name: anything called ad, ads, advert, sponsor, banner or promo, and files and paths like them (Discourse's own ad plugin is blocked by its name, `adplugin`). So, in everything the browser sees, nothing is named after ads: -- **No request of its own.** The ads come inside the page's preloaded site data (`site.jt_lines`), filtered for the person on the server. The thumbs go to `/jtech-lines/:key`. +- **No request of its own.** The ads come inside the welcome pages' own lists (`topic_list.jt_lines` on the unscoped Latest and Top lists, `category_list.jt_lines` on the top-level category list), filtered for the person on the server, so pages that don't show them don't carry them. The thumbs go to `/jtech-lines/:key`. - **Neutral names** for classes (`jt-lines`, `jl-*`, `jt-hero__lines`), the service and the data (`h`, `t`, `c`). - **In the JTech theme the ad is the hero**, so a rule that hides it hides the welcome with it. - **One fallback.** About a second and a half after the page settles, the space checks it's actually showing. If something hid it, the same card moves once to the top of the page, inside a closed shadow root (`jt-lines-fallback.gts`). Nothing outside that root can be named by a rule: its class is new on every page load, it has no id or data attribute, and the card's label, thumbs and text are only inside the root. If a script sets it hidden anyway (an inline style, a mark), that's undone. No nagging, no wall, no second attempt. diff --git a/lib/discourse_jtech_ads.rb b/lib/discourse_jtech_ads.rb index f4532561..dabde014 100644 --- a/lib/discourse_jtech_ads.rb +++ b/lib/discourse_jtech_ads.rb @@ -125,6 +125,20 @@ def key_for(name) end # What the site serializer sends. Short, neutral keys on purpose. + # The lists the welcome area's pages are built from (lib/jt-lines-route + # in the browser): Latest (also the homepage, and the Categories page's + # topics) or Top (Categories with top topics), outside a category or tag + def welcome_list?(topic_list) + %w[latest top].include?(topic_list.filter.to_s) && topic_list.category.blank? && + topic_list.tags.blank? + end + + # The Categories page's own list, not a category's subcategories + def welcome_categories?(category_list) + options = category_list.instance_variable_get(:@options) || {} + options[:parent_category_id].blank? + end + def payload(guardian) ads = visible_to(guardian) user = guardian&.user diff --git a/spec/requests/ads_spec.rb b/spec/requests/ads_spec.rb index bb51f8c7..b657b06b 100644 --- a/spec/requests/ads_spec.rb +++ b/spec/requests/ads_spec.rb @@ -26,12 +26,12 @@ end def lines - get "/site.json" + get "/latest.json" expect(response.status).to eq(200) - response.parsed_body["jt_lines"] + response.parsed_body.dig("topic_list", "jt_lines") end - it "puts the ads for each person in the site data, under neutral keys" do + it "puts the ads for each person in the welcome list, under neutral keys" do data = lines expect(data["label"]).to eq("Ad") expect(data["items"].map { it["h"] }).to eq(["Spring sale"]) @@ -41,6 +41,29 @@ def lines expect(lines["items"].map { it["k"] }).to eq([key, members_key]) end + it "comes with the welcome pages' lists only, not every page's site data" do + category = Fabricate(:category) + topic = Fabricate(:post).topic + + get "/categories.json" + expect(response.parsed_body.dig("category_list", "jt_lines", "items").map { it["h"] }).to eq( + ["Spring sale"], + ) + get "/categories_and_latest.json" + expect(response.parsed_body.dig("topic_list", "jt_lines")).to be_present + get "/top.json" + expect(response.parsed_body.dig("topic_list", "jt_lines")).to be_present + + get "/site.json" + expect(response.parsed_body).not_to have_key("jt_lines") + get "/c/#{category.slug}/#{category.id}/l/latest.json" + expect(response.parsed_body["topic_list"]).not_to have_key("jt_lines") + get "/categories.json", params: { parent_category_id: category.id } + expect(response.parsed_body["category_list"]).not_to have_key("jt_lines") + get "/t/#{topic.id}.json" + expect(response.body).not_to include("jt_lines") + end + it "sends nothing with ads off, or with Jtech off" do SiteSetting.jtech_ads_enabled = false expect(lines).to be_nil diff --git a/spec/system/ads_spec.rb b/spec/system/ads_spec.rb index 2d09c4f8..2ea553d6 100644 --- a/spec/system/ads_spec.rb +++ b/spec/system/ads_spec.rb @@ -37,6 +37,20 @@ expect(page).to have_no_css(".jt-lines") end + it "shows the ad on the way home from a topic, the page loaded without it" do + visit topic.relative_url + expect(page).to have_css("#post_1") + find("#site-logo, .title a", match: :first).click + expect(page).to have_css("#main-outlet .topic-list") + expect(page).to have_css(".jt-lines .jl-title-xl", text: "Spring sale") + end + + it "shows the ad on the Categories page" do + Fabricate(:category) + visit "/categories" + expect(page).to have_css(".jt-lines .jl-title-xl", text: "Spring sale") + end + it "keeps the space's size whatever the ad holds" do visit "/latest" expect(page).to have_css(".jt-lines .jl-card") diff --git a/sub_plugins/ads.rb b/sub_plugins/ads.rb index b45571bf..1dfa7579 100644 --- a/sub_plugins/ads.rb +++ b/sub_plugins/ads.rb @@ -15,10 +15,23 @@ register_svg_icon "thumbs-down" after_initialize do - # Inside the page's preloaded site data: no request of the ads' own - add_to_serializer(:site, :jt_lines, include_condition: -> { DiscourseJtechAds.enabled? }) do - DiscourseJtechAds.payload(scope) - end + # Inside the welcome pages' own lists (the unscoped Latest topic list, the + # top-level category list): no request of the ads' own for a blocker to + # drop, and nothing on the pages that don't show them (topic pages are + # most of the traffic). In the site data, every page carried them. + add_to_serializer( + :topic_list, + :jt_lines, + include_condition: -> { DiscourseJtechAds.enabled? && DiscourseJtechAds.welcome_list?(object) }, + ) { DiscourseJtechAds.payload(scope) } + + add_to_serializer( + :category_list, + :jt_lines, + include_condition: -> do + DiscourseJtechAds.enabled? && DiscourseJtechAds.welcome_categories?(object) + end, + ) { DiscourseJtechAds.payload(scope) } # Visitors' site data is cached; a changed ad reaches them right away on(:site_setting_changed) do |name, _old, _new|