FIX: Show auto-group flair according to user preferences (#21221)

This commit is contained in:
Jan Cernik 2023-04-24 16:04:26 -03:00 committed by GitHub
parent 6890beb95a
commit c03f83bbea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 87 additions and 13 deletions

View File

@ -7,7 +7,7 @@ export default Component.extend({
@discourseComputed("user") @discourseComputed("user")
flair(user) { flair(user) {
if (!user) { if (!user || !user.flair_group_id) {
return; return;
} }

View File

@ -26,6 +26,7 @@ export function transformBasicPost(post) {
flair_url: post.flair_url, flair_url: post.flair_url,
flair_bg_color: post.flair_bg_color, flair_bg_color: post.flair_bg_color,
flair_color: post.flair_color, flair_color: post.flair_color,
flair_group_id: post.flair_group_id,
wiki: post.wiki, wiki: post.wiki,
lastWikiEdit: post.last_wiki_edit, lastWikiEdit: post.last_wiki_edit,
firstPost: post.post_number === 1, firstPost: post.post_number === 1,

View File

@ -226,13 +226,15 @@ createWidget("post-avatar", {
const postAvatarBody = [body]; const postAvatarBody = [body];
if (attrs.flair_url || attrs.flair_bg_color) { if (attrs.flair_group_id) {
postAvatarBody.push(this.attach("avatar-flair", attrs)); if (attrs.flair_url || attrs.flair_bg_color) {
} else { postAvatarBody.push(this.attach("avatar-flair", attrs));
const autoFlairAttrs = autoGroupFlairForUser(this.site, attrs); } else {
const autoFlairAttrs = autoGroupFlairForUser(this.site, attrs);
if (autoFlairAttrs) { if (autoFlairAttrs) {
postAvatarBody.push(this.attach("avatar-flair", autoFlairAttrs)); postAvatarBody.push(this.attach("avatar-flair", autoFlairAttrs));
}
} }
} }

View File

@ -80,12 +80,14 @@ createWidget("topic-participant", {
linkContents.push(h("span.post-count", attrs.post_count.toString())); linkContents.push(h("span.post-count", attrs.post_count.toString()));
} }
if (attrs.flair_url || attrs.flair_bg_color) { if (attrs.flair_group_id) {
linkContents.push(this.attach("avatar-flair", attrs)); if (attrs.flair_url || attrs.flair_bg_color) {
} else { linkContents.push(this.attach("avatar-flair", attrs));
const autoFlairAttrs = autoGroupFlairForUser(this.site, attrs); } else {
if (autoFlairAttrs) { const autoFlairAttrs = autoGroupFlairForUser(this.site, attrs);
linkContents.push(this.attach("avatar-flair", autoFlairAttrs)); if (autoFlairAttrs) {
linkContents.push(this.attach("avatar-flair", autoFlairAttrs));
}
} }
} }
return h( return h(

View File

@ -54,6 +54,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
admin: true, admin: true,
moderator: false, moderator: false,
trust_level: 2, trust_level: 2,
flair_group_id: 12,
}); });
setupSiteGroups(this); setupSiteGroups(this);
@ -73,6 +74,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
admin: false, admin: false,
moderator: true, moderator: true,
trust_level: 2, trust_level: 2,
flair_group_id: 12,
}); });
setupSiteGroups(this); setupSiteGroups(this);
@ -92,6 +94,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
admin: false, admin: false,
moderator: false, moderator: false,
trust_level: 2, trust_level: 2,
flair_group_id: 12,
}); });
setupSiteGroups(this); setupSiteGroups(this);
@ -106,11 +109,26 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
); );
}); });
test("avatar flair for trust level when set to none", async function (assert) {
this.set("args", {
admin: false,
moderator: false,
trust_level: 2,
flair_group_id: null,
});
setupSiteGroups(this);
await render(hbs`<UserAvatarFlair @user={{this.args}} />`);
assert.ok(!exists(".avatar-flair"), "it does not render a flair");
});
test("avatar flair for trust level with fallback", async function (assert) { test("avatar flair for trust level with fallback", async function (assert) {
this.set("args", { this.set("args", {
admin: false, admin: false,
moderator: false, moderator: false,
trust_level: 3, trust_level: 3,
flair_group_id: 13,
}); });
setupSiteGroups(this); setupSiteGroups(this);
@ -130,6 +148,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
admin: false, admin: false,
moderator: false, moderator: false,
trust_level: 3, trust_level: 3,
flair_group_id: 13,
}); });
// Groups not serialized for anon on login_required // Groups not serialized for anon on login_required
this.site.groups = undefined; this.site.groups = undefined;
@ -148,6 +167,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
flair_url: "fa-times", flair_url: "fa-times",
flair_bg_color: "123456", flair_bg_color: "123456",
flair_color: "B0B0B0", flair_color: "B0B0B0",
flair_group_id: 41,
primary_group_name: "Band Geeks", primary_group_name: "Band Geeks",
}); });
setupSiteGroups(this); setupSiteGroups(this);
@ -168,6 +188,7 @@ module("Integration | Component | user-avatar-flair", function (hooks) {
admin: false, admin: false,
moderator: false, moderator: false,
trust_level: 1, trust_level: 1,
flair_group_id: 11,
}); });
await render(hbs`<UserAvatarFlair @user={{this.args}} />`); await render(hbs`<UserAvatarFlair @user={{this.args}} />`);

View File

@ -34,6 +34,7 @@ module(
flair_name: "devs", flair_name: "devs",
flair_url: "/images/d-logo-sketch-small.png", flair_url: "/images/d-logo-sketch-small.png",
flair_bg_color: "222", flair_bg_color: "222",
flair_group_id: "41",
}); });
await render( await render(

View File

@ -37,6 +37,7 @@ class PostSerializer < BasicPostSerializer
:flair_url, :flair_url,
:flair_bg_color, :flair_bg_color,
:flair_color, :flair_color,
:flair_group_id,
:version, :version,
:can_edit, :can_edit,
:can_delete, :can_delete,
@ -213,6 +214,10 @@ class PostSerializer < BasicPostSerializer
object.user&.flair_group&.flair_color object.user&.flair_group&.flair_color
end end
def flair_group_id
object.user&.flair_group_id
end
def link_counts def link_counts
return @single_post_link_counts if @single_post_link_counts.present? return @single_post_link_counts if @single_post_link_counts.present?

View File

@ -7,6 +7,7 @@ class TopicPostCountSerializer < BasicUserSerializer
:flair_url, :flair_url,
:flair_color, :flair_color,
:flair_bg_color, :flair_bg_color,
:flair_group_id,
:admin, :admin,
:moderator, :moderator,
:trust_level :trust_level
@ -44,6 +45,10 @@ class TopicPostCountSerializer < BasicUserSerializer
object[:user]&.flair_group&.flair_color object[:user]&.flair_group&.flair_color
end end
def flair_group_id
object[:user]&.flair_group_id
end
def include_admin? def include_admin?
object[:user].admin object[:user].admin
end end

View File

@ -116,6 +116,9 @@ RSpec.describe "posts" do
flair_color: { flair_color: {
type: %i[string null], type: %i[string null],
}, },
flair_group_id: {
type: %i[string null],
},
version: { version: {
type: :integer, type: :integer,
}, },

View File

@ -413,6 +413,9 @@
"flair_color": { "flair_color": {
"type": ["string", "null"] "type": ["string", "null"]
}, },
"flair_group_id": {
"type": ["string", "null"]
},
"bio_raw": { "bio_raw": {
"type": ["string", "null"] "type": ["string", "null"]
}, },

View File

@ -101,6 +101,12 @@
"null" "null"
] ]
}, },
"flair_group_id": {
"type": [
"string",
"null"
]
},
"version": { "version": {
"type": "integer" "type": "integer"
}, },

View File

@ -88,6 +88,12 @@
"null" "null"
] ]
}, },
"flair_group_id": {
"type": [
"string",
"null"
]
},
"version": { "version": {
"type": "integer" "type": "integer"
}, },

View File

@ -92,6 +92,12 @@
"null" "null"
] ]
}, },
"flair_group_id": {
"type": [
"string",
"null"
]
},
"version": { "version": {
"type": "integer" "type": "integer"
}, },

View File

@ -103,6 +103,12 @@
"null" "null"
] ]
}, },
"flair_group_id": {
"type": [
"string",
"null"
]
},
"version": { "version": {
"type": "integer" "type": "integer"
}, },

View File

@ -828,6 +828,12 @@
"null" "null"
] ]
}, },
"flair_group_id": {
"type": [
"string",
"null"
]
},
"admin": { "admin": {
"type": "boolean" "type": "boolean"
}, },

View File

@ -32,6 +32,7 @@ RSpec.describe WebHookPostSerializer do
:display_username, :display_username,
:primary_group_name, :primary_group_name,
:flair_name, :flair_name,
:flair_group_id,
:version, :version,
:user_title, :user_title,
:bookmarked, :bookmarked,