FIX: Calculate no ads for groups server side (#200)

If the selected group to not display ads to had its visibility set to
not be visible then this setting wouldn't work correctly because that
group wouldn't be available client side. The change moves that group
check to be server side so that we can correctly see all the groups that
should not see ads.
This commit is contained in:
Blake Erickson 2024-02-15 14:52:15 -07:00 committed by GitHub
parent 1677f7ae39
commit b0c95114ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 16 additions and 18 deletions

View File

@ -45,25 +45,13 @@ export default Component.extend({
return topicType === "private_message";
},
@discourseComputed("currentUser.groups")
showToGroups(groups) {
const currentUser = this.currentUser;
if (
!currentUser ||
!groups ||
!this.siteSettings.no_ads_for_groups ||
this.siteSettings.no_ads_for_groups.length === 0
) {
@discourseComputed
showToGroups() {
if (!this.currentUser) {
return true;
}
let noAdsGroups = this.siteSettings.no_ads_for_groups
.split("|")
.filter(Boolean);
let currentGroups = groups.map((g) => g.id.toString());
return !currentGroups.any((g) => noAdsGroups.includes(g));
return this.currentUser.show_to_groups;
},
@discourseComputed(

View File

@ -9,7 +9,6 @@ ad_plugin:
client: true
default: false
no_ads_for_groups:
client: true
default: ""
type: group_list
no_ads_for_categories:

View File

@ -21,5 +21,9 @@ module ::AdPlugin
def show_adbutler_ads?
!self.user.in_any_groups?(SiteSetting.adbutler_exclude_groups_map)
end
def show_to_groups?
!self.user.in_any_groups?(SiteSetting.no_ads_for_groups_map)
end
end
end

View File

@ -71,6 +71,10 @@ after_initialize do
scope.show_adbutler_ads?
end
add_to_serializer :current_user, :show_to_groups do
scope.show_to_groups?
end
class ::AdstxtController < ::ApplicationController
skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required

View File

@ -48,6 +48,7 @@ acceptance("AdSense", function (needs) {
trust_level: 1,
groups: [AUTO_GROUPS.trust_level_1],
show_adsense_ads: true,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts

View File

@ -45,6 +45,7 @@ acceptance("DFP Ads", function (needs) {
staff: false,
trust_level: 1,
show_dfp_ads: true,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts

View File

@ -38,7 +38,7 @@ acceptance("House Ads", function (needs) {
});
test("correct ads show", async (assert) => {
updateCurrentUser({ staff: false, trust_level: 1 });
updateCurrentUser({ staff: false, trust_level: 1, show_to_groups: true });
await visit("/t/280"); // 20 posts
assert

View File

@ -49,6 +49,7 @@ acceptance("Mixed Ads", function (needs) {
staff: false,
trust_level: 1,
show_dfp_ads: true,
show_to_groups: true,
});
await visit("/t/280"); // 20 posts