FIX: Missing excerpt for post small actions in topic timeline. (#14547)

This commit is contained in:
Alan Guo Xiang Tan 2021-10-12 09:20:35 +08:00 committed by GitHub
parent 79e55ec3f0
commit d0595127cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -2,6 +2,7 @@ import ComponentConnector from "discourse/widgets/component-connector";
import I18n from "I18n";
import RawHtml from "discourse/widgets/raw-html";
import { createWidget } from "discourse/widgets/widget";
import { actionDescriptionHtml } from "discourse/widgets/post-small-action";
import { h } from "virtual-dom";
import { iconNode } from "discourse-common/lib/icon-library";
import { later } from "@ember/runloop";
@ -456,7 +457,16 @@ export default createWidget("topic-timeline", {
excerpt = "<span class='username'>" + info.username + ":</span> ";
}
this.state.excerpt = excerpt + info.excerpt;
if (info.excerpt) {
this.state.excerpt = excerpt + info.excerpt;
} else if (info.action_code) {
this.state.excerpt = `${excerpt} ${actionDescriptionHtml(
info.action_code,
info.created_at,
info.username
)}`;
}
this.scheduleRerender();
}
});

View File

@ -262,14 +262,21 @@ class TopicsController < ApplicationController
@posts = Post.where(hidden: false, deleted_at: nil, topic_id: @topic.id)
.where('posts.id in (?)', post_ids)
.joins("LEFT JOIN users u on u.id = posts.user_id")
.pluck(:id, :cooked, :username)
.map do |post_id, cooked, username|
{
post_id: post_id,
username: username,
excerpt: PrettyText.excerpt(cooked, 800, keep_emoji_images: true)
}
end
.pluck(:id, :cooked, :username, :action_code, :created_at)
.map do |post_id, cooked, username, action_code, created_at|
attrs = {
post_id: post_id,
username: username,
excerpt: PrettyText.excerpt(cooked, 800, keep_emoji_images: true),
}
if action_code
attrs[:action_code] = action_code
attrs[:created_at] = created_at
end
attrs
end
render json: @posts.to_json
end

View File

@ -3444,25 +3444,29 @@ RSpec.describe TopicsController do
it "can correctly get excerpts" do
first_post = create_post(raw: 'This is the first post :)', title: 'This is a test title I am making yay')
second_post = create_post(raw: 'This is second post', topic: first_post.topic)
third_post = first_post.topic.add_small_action(first_post.user, "autobumped")
random_post = Fabricate(:post)
get "/t/#{first_post.topic_id}/excerpts.json", params: {
post_ids: [first_post.id, second_post.id, random_post.id]
post_ids: [first_post.id, second_post.id, third_post.id, random_post.id]
}
json = response.parsed_body
json.sort! { |a, b| a["post_id"] <=> b["post_id"] }
# no random post
expect(json.length).to eq(2)
expect(json.map { |p| p["post_id"] }).to contain_exactly(first_post.id, second_post.id, third_post.id)
# keep emoji images
expect(json[0]["excerpt"]).to match(/emoji/)
expect(json[0]["excerpt"]).to match(/first post/)
expect(json[0]["username"]).to eq(first_post.user.username)
expect(json[0]["post_id"]).to eq(first_post.id)
expect(json[0]["created_at"].present?).to eq(false)
expect(json[1]["excerpt"]).to match(/second post/)
expect(json[2]["action_code"]).to eq("autobumped")
expect(json[2]["created_at"].present?).to eq(true)
end
end