FEATURE: add title expansion for off topic quotes

This commit is contained in:
Sam 2015-09-25 13:35:14 +10:00
parent 7ca75a88ce
commit e5234b38b8
3 changed files with 48 additions and 12 deletions

View File

@ -23,11 +23,11 @@ Discourse.BBCode.register('quote', {noWrap: true, singlePara: true}, function(co
}
var avatarImg;
var postNumber = parseInt(params['data-post'], 10);
var topicId = parseInt(params['data-topic'], 10);
if (options.lookupAvatarByPostNumber) {
// client-side, we can retrieve the avatar from the post
var postNumber = parseInt(params['data-post'], 10);
var topicId = parseInt(params['data-topic'], 10);
avatarImg = options.lookupAvatarByPostNumber(postNumber, topicId);
} else if (options.lookupAvatar) {
// server-side, we need to lookup the avatar from the username
@ -39,12 +39,22 @@ Discourse.BBCode.register('quote', {noWrap: true, singlePara: true}, function(co
return ['p', ['aside', params, ['blockquote'].concat(contents)]];
}
return ['aside', params,
['div', {'class': 'title'},
['div', {'class': 'quote-controls'}],
avatarImg ? ['__RAW', avatarImg] : "",
username ? I18n.t('user.said', {username: username}) : ""
],
['blockquote'].concat(contents)
];
var header = [ 'div', {'class': 'title'},
['div', {'class': 'quote-controls'}],
avatarImg ? ['__RAW', avatarImg] : "",
username ? I18n.t('user.said', {username: username}) : ""
];
if (options.topicId && postNumber && options.getTopicInfo && topicId !== options.topicId) {
var topicInfo = options.getTopicInfo(topicId);
if (topicInfo) {
header.push(' ');
var href = topicInfo.href;
if (postNumber > 0) { href += "/" + postNumber; }
header.push(['a', {'href': href}, topicInfo.title]);
}
}
return ['aside', params, header, ['blockquote'].concat(contents)];
});

View File

@ -39,6 +39,18 @@ module PrettyText
username = username.downcase
User.exec_sql('SELECT 1 FROM users WHERE username_lower = ?', username).values.length == 1
end
def get_topic_info(topic_id)
return unless Fixnum === topic_id
# TODO this only handles public topics, secured one do not get this
topic = Topic.find_by(id: topic_id)
if topic && Guardian.new.can_see?(topic)
{
title: topic.title,
href: topic.url
}
end
end
end
@mutex = Mutex.new
@ -184,6 +196,7 @@ module PrettyText
context.eval('opts["mentionLookup"] = function(u){return helpers.is_username_valid(u);}')
context.eval('opts["lookupAvatar"] = function(p){return Discourse.Utilities.avatarImg({size: "tiny", avatarTemplate: helpers.avatar_template(p)});}')
context.eval('opts["getTopicInfo"] = function(i){return helpers.get_topic_info(i)};')
baked = context.eval('Discourse.Markdown.markdownConverter(opts).makeHtml(raw)')
end

View File

@ -8,10 +8,23 @@ describe PrettyText do
describe "Cooking" do
describe "off topic quoting" do
it "can correctly populate topic title" do
topic = Fabricate(:topic, title: "this is a test topic")
expected = <<HTML
<aside class="quote" data-post="2" data-topic="#{topic.id}"><div class="title">
<div class="quote-controls"></div>EvilTrout: <a href="http://test.localhost/t/this-is-a-test-topic/#{topic.id}/2">This is a test topic</a>
</div>
<blockquote><p>ddd</p></blockquote></aside>
HTML
expect(PrettyText.cook("[quote=\"EvilTrout, post:2, topic:#{topic.id}\"]ddd\n[/quote]", topic_id: 1)).to match_html expected
end
end
describe "with avatar" do
let(:default_avatar) { "//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png" }
before(:each) do
before do
eviltrout = User.new
User.stubs(:default_template).returns(default_avatar)
User.expects(:find_by).with(username_lower: "eviltrout").returns(eviltrout)