DEV: Move requested_group_id custom field from post to topic (#9127)

Follow-up-to accbbded15
This commit is contained in:
Bianca Nenciu 2020-03-24 11:12:52 +02:00 committed by GitHub
parent bef8468510
commit d8640fd042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 71 additions and 56 deletions

View File

@ -139,6 +139,10 @@ export default function transformPost(
}
}
if (post.post_number === 1 && topic.requested_group_name) {
postAtts.requestedGroupName = topic.requested_group_name;
}
const showPMMap =
topic.archetype === "private_message" && post.post_number === 1;
if (showPMMap) {

View File

@ -344,6 +344,22 @@ createWidget("expand-post-button", {
}
});
createWidget("post-group-request", {
buildKey: attrs => `post-group-request-${attrs.id}`,
buildClasses() {
return ["group-request"];
},
html(attrs) {
const href = Discourse.getURL(
"/g/" + attrs.requestedGroupName + "/requests?filter=" + attrs.username
);
return h("a", { attributes: { href } }, I18n.t("groups.requests.handle"));
}
});
createWidget("post-contents", {
buildKey: attrs => `post-contents-${attrs.id}`,
@ -366,6 +382,11 @@ createWidget("post-contents", {
let result = [
new PostCooked(attrs, new DecoratorHelper(this), this.currentUser)
];
if (attrs.requestedGroupName) {
result.push(this.attach("post-group-request", attrs));
}
result = result.concat(applyDecorators(this, "after-cooked", attrs, state));
if (attrs.cooked_hidden) {

View File

@ -552,6 +552,10 @@ blockquote {
.cooked {
overflow: hidden;
}
.group-request {
border-top: 1px solid $primary-low;
padding-top: 0.5em;
}
&.highlighted {
animation: background-fade-highlight 2.5s ease-out;
}

View File

@ -613,6 +613,9 @@ blockquote {
.cooked {
padding: 0 $topic-body-width-padding 0.25em $topic-body-width-padding;
}
.group-request {
padding: 0.5em $topic-body-width-padding 0 $topic-body-width-padding;
}
a.expand-hidden {
padding-left: $topic-body-width-padding;
}

View File

@ -439,7 +439,7 @@ class GroupsController < ApplicationController
raw: params[:reason],
archetype: Archetype.private_message,
target_usernames: usernames.join(','),
custom_fields: { requested_group_id: group.id },
topic_opts: { custom_fields: { requested_group_id: group.id } },
skip_validations: true
).create!

View File

@ -40,34 +40,7 @@ class BasicPostSerializer < ApplicationSerializer
I18n.t('flagging.user_must_edit')
end
else
cooked = object.filter_quotes(@parent_post)
if scope&.user
# PERF: this should not run on every post, only specific ones
# also, why is this in basic post serializer?
requested_group_id = post_custom_fields['requested_group_id'].to_i
if requested_group_id > 0
group = Group
.joins('JOIN group_users ON groups.id = group_users.group_id')
.find_by(
id: object.custom_fields['requested_group_id'].to_i,
group_users: { user_id: scope.user.id, owner: true }
)
if group
cooked << <<~EOF
<hr />
<a href="#{Discourse.base_uri}/g/#{group.name}/requests">
#{I18n.t('groups.request_membership_pm.handle')}
</a>
EOF
end
end
end
cooked
object.filter_quotes(@parent_post)
end
end

View File

@ -70,7 +70,8 @@ class TopicViewSerializer < ApplicationSerializer
:destination_category_id,
:pm_with_non_human_user,
:queued_posts_count,
:show_read_indicator
:show_read_indicator,
:requested_group_name
)
has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects
@ -255,4 +256,21 @@ class TopicViewSerializer < ApplicationSerializer
def show_read_indicator
object.show_read_indicator?
end
def requested_group_name
if scope&.user
group = Group
.joins('JOIN group_users ON groups.id = group_users.group_id')
.find_by(
id: object.topic.custom_fields['requested_group_id'].to_i,
group_users: { user_id: scope.user.id, owner: true }
)
group.name if group
end
end
def include_requested_group_name?
object.personal_message
end
end

View File

@ -614,6 +614,7 @@ en:
deny: "Deny"
denied: "denied"
undone: "request undone"
handle: "handle membership request"
manage:
title: "Manage"
name: "Name"

View File

@ -427,7 +427,6 @@ en:
trust_level_4: "trust_level_4"
request_membership_pm:
title: "Membership Request for @%{group_name}"
handle: "handle membership request"
view_hidden_topic_request_reason: "I would like to join the group '%{group_name}', so I may access [this topic](%{topic_url})"
education:

View File

@ -35,7 +35,7 @@ class TopicView
end
def self.default_post_custom_fields
@default_post_custom_fields ||= [Post::NOTICE_TYPE, Post::NOTICE_ARGS, "action_code_who", "requested_group_id"]
@default_post_custom_fields ||= [Post::NOTICE_TYPE, Post::NOTICE_ARGS, "action_code_who"]
end
def self.post_custom_fields_whitelisters

View File

@ -1479,7 +1479,7 @@ describe GroupsController do
body = JSON.parse(response.body)
expect(body['relative_url']).to eq(topic.relative_url)
expect(post.custom_fields['requested_group_id'].to_i).to eq(group.id)
expect(post.topic.custom_fields['requested_group_id'].to_i).to eq(group.id)
expect(post.user).to eq(user)
expect(topic.title).to eq(I18n.t('groups.request_membership_pm.title',

View File

@ -22,27 +22,4 @@ describe BasicPostSerializer do
end
context "cooked" do
it "includes membership requests" do
user = Fabricate(:user)
member = Fabricate(:user)
owner = Fabricate(:user)
group = Fabricate(:group)
group.add(member)
group.add_owner(owner)
post = Fabricate(:post, custom_fields: { requested_group_id: group.id })
json = BasicPostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
expect(json[:cooked]).not_to include(I18n.t('groups.request_membership_pm.handle'))
json = BasicPostSerializer.new(post, scope: Guardian.new(member), root: false).as_json
expect(json[:cooked]).not_to include(I18n.t('groups.request_membership_pm.handle'))
json = BasicPostSerializer.new(post, scope: Guardian.new(owner), root: false).as_json
expect(json[:cooked]).to include(I18n.t('groups.request_membership_pm.handle'))
end
end
end

View File

@ -922,3 +922,18 @@ widgetTest("post notice - with name", {
);
}
});
widgetTest("show group request in post", {
template: '{{mount-widget widget="post" args=args}}',
beforeEach() {
this.set("args", {
username: "foo",
requestedGroupName: "testGroup"
});
},
test(assert) {
const link = find(".group-request a");
assert.equal(link.text().trim(), I18n.t("groups.requests.handle"));
assert.equal(link.attr("href"), "/g/testGroup/requests?filter=foo");
}
});