Skip to content
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
24 changes: 22 additions & 2 deletions .github/workflows/ad-remover-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +73 to +76

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fail on clone errors unrelated to repository absence

In the ad-remover-check.yml workflow, this branch treats every nonzero git clone result as proof that the repository is no longer public. DNS, TLS, rate-limit, disk, or runner-network failures therefore set found=false, skip every verification step, and leave the scheduled job green, potentially disabling the monitor indefinitely without an alert. Only the expected repository-unavailable case should be suppressed; unexpected clone failures should still fail the job.

Useful? React with 👍 / 👎.

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -111,31 +126,36 @@ 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: |
export DISCOURSE_DOWNLOAD_PRE_BUILT_ASSETS=$(bin/rails runner 'puts(Discourse.has_needed_version?(Discourse::VERSION::STRING, "2026.3.0-latest") ? 1 : 0)')
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 }}
Expand Down
52 changes: 52 additions & 0 deletions assets/javascripts/discourse/initializers/jt-lines-lists.ts
Original file line number Diff line number Diff line change
@@ -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;
}
);
});
},
};
20 changes: 16 additions & 4 deletions assets/javascripts/discourse/services/jt-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/features/ads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions lib/discourse_jtech_ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Comment on lines +132 to +133

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Include ads for every configured homepage list

When default_homepage is a valid topic-list route such as hot, new, or unread, onWelcomeRoute() treats it as a welcome page, but this predicate only attaches jt_lines to latest and top. On a fresh session the homepage response therefore leaves listed empty and the welcome ad never renders; include every supported homepage filter and cover a non-latest homepage in the request/system specs.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

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
Expand Down
29 changes: 26 additions & 3 deletions spec/requests/ads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(["<b>Spring</b> sale"])
Expand All @@ -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(
["<b>Spring</b> 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
Expand Down
14 changes: 14 additions & 0 deletions spec/system/ads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 17 additions & 4 deletions sub_plugins/ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Comment on lines +25 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid serializing the payload twice on Categories

For /categories_and_latest.json and /categories_and_top.json, the response contains both an unscoped topic_list and a top-level category_list. Both serializer conditions added here are therefore true, so the full ad payload is emitted under both lists (and a signed-in user's vote lookup is performed twice). Ensure that combined Categories responses carry jt_lines on only one of their child lists.

Useful? React with 👍 / 👎.


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|
Expand Down
Loading