diff --git a/app/assets/stylesheets/embed.css.scss b/app/assets/stylesheets/embed.css.scss index 26a77b5bb7b..19deb049b20 100644 --- a/app/assets/stylesheets/embed.css.scss +++ b/app/assets/stylesheets/embed.css.scss @@ -23,6 +23,7 @@ article.post { color: #4a6b82; font-size: 13px; margin: 0; + word-wrap: break-word; } } @@ -36,11 +37,27 @@ article.post { } } +.post-replies { + background-color: #eee; + padding: 5px; + display: inline-block; +} + +.in-reply-to { + font-size: 13px; + margin-top: 4px; + display: inline-block; + color: #999; +} + +.replies { + font-size: 15px; + color: #999; +} + header { padding: 10px 10px 20px 10px; - font-size: 18px; - border-bottom: 1px solid #ddd; } diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rb index 72edca90669..3c60308b2ff 100644 --- a/app/controllers/embed_controller.rb +++ b/app/controllers/embed_controller.rb @@ -5,12 +5,12 @@ class EmbedController < ApplicationController layout 'embed' - def best + def comments embed_url = params.require(:embed_url) topic_id = TopicEmbed.topic_id_for_embed(embed_url) if topic_id - @topic_view = TopicView.new(topic_id, current_user, {best: 5}) + @topic_view = TopicView.new(topic_id, current_user, limit: SiteSetting.embed_post_limit, exclude_first: true) @second_post_url = "#{@topic_view.topic.url}/2" if @topic_view else Jobs.enqueue(:retrieve_topic, user_id: current_user.try(:id), embed_url: embed_url) diff --git a/app/models/post.rb b/app/models/post.rb index 72ef8bd00a6..97e1af5ccf5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -218,6 +218,13 @@ class Post < ActiveRecord::Base (quote_count == 0) && (reply_to_post_number.present?) end + def reply_to_post + return if reply_to_post_number.blank? + @reply_to_post ||= Post.where("topic_id = :topic_id AND post_number = :post_number", + topic_id: topic_id, + post_number: reply_to_post_number).first + end + def reply_notification_target return if reply_to_post_number.blank? Post.where("topic_id = :topic_id AND post_number = :post_number AND user_id <> :user_id", diff --git a/app/views/embed/best.html.erb b/app/views/embed/comments.html.erb similarity index 52% rename from app/views/embed/best.html.erb rename to app/views/embed/comments.html.erb index d60fff511df..4bd293199f2 100644 --- a/app/views/embed/best.html.erb +++ b/app/views/embed/comments.html.erb @@ -1,6 +1,8 @@
<%- if @topic_view.posts.present? %> - <%= link_to(I18n.t('embed.title'), @second_post_url, class: 'button', target: '_blank') %> + <%= link_to(I18n.t('embed.continue'), @second_post_url, class: 'button', target: '_blank') %> + <%= I18n.t('embed.replies', count: @topic_view.topic.posts_count) %> + <%- else %> <%= link_to(I18n.t('embed.start_discussion'), @topic_view.topic.url, class: 'button', target: '_blank') %> <%- end if %> @@ -12,17 +14,29 @@ <%- @topic_view.posts.each do |post| %>
<%= link_to post.created_at.strftime("%e %b %Y"), post.url, class: 'post-date', target: "_blank" %> + <%- if post.reply_to_post.present? %> + <%= link_to I18n.t('embed.in_reply_to', username: post.reply_to_post.username), post.reply_to_post.url, class: 'in-reply-to', target: "_blank" %> + <%- end %> +
-

<%= post.user.username %>

+

<%= post.user.username %>

-
<%= raw post.cooked %>
+
+ <%= raw post.cooked %> + + <%- if post.reply_count > 0 %> + <%= link_to I18n.t('embed.replies', count: post.reply_count), post.url, class: 'post-replies', target: "_blank" %> + <%- end %> +
+
<%- end %> diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 2190379c04f..f9b669da925 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -30,12 +30,15 @@ en: too_many_replies: "Sorry you can't reply any more times in that topic." embed: - title: "Discussion Highlights" start_discussion: "Begin the Discussion" continue: "Continue the Discussion" loading: "Loading Discussion..." permalink: "Permalink" imported_from: "Imported from: %{link}" + in_reply_to: "in reply to %{username}" + replies: + one: "1 reply" + other: "%{count} replies" too_many_mentions: zero: "Sorry, you can't mention other users." @@ -773,6 +776,7 @@ en: feed_polling_enabled: "Whether to import a RSS/ATOM feed as posts" feed_polling_url: "URL of RSS/ATOM feed to import" embed_by_username: "Discourse username of the user who creates the topics" + embed_post_limit: "Maximum number of posts to embed" notification_types: mentioned: "%{display_username} mentioned you in %{link}" diff --git a/config/routes.rb b/config/routes.rb index 066dd988819..bcead033740 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -242,7 +242,7 @@ Discourse::Application.routes.draw do get "topics/private-messages-sent/:username" => "list#private_messages_sent", as: "topics_private_messages_sent", constraints: {username: USERNAME_ROUTE_FORMAT} get "topics/private-messages-unread/:username" => "list#private_messages_unread", as: "topics_private_messages_unread", constraints: {username: USERNAME_ROUTE_FORMAT} - get 'embed/best' => 'embed#best' + get 'embed/comments' => 'embed#comments' # Topic routes get "t/:slug/:topic_id/wordpress" => "topics#wordpress", constraints: {topic_id: /\d+/} diff --git a/config/site_settings.yml b/config/site_settings.yml index f118f60e356..e38486e000b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -356,6 +356,7 @@ embedding: feed_polling_enabled: false feed_polling_url: '' embed_by_username: '' + embed_post_limit: 100 uncategorized: tos_url: diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 0ef5c7a2d07..fecbff9f7c3 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -37,7 +37,7 @@ class TopicView def canonical_path path = @topic.relative_url path << if @post_number - page = ((@post_number.to_i - 1) / SiteSetting.posts_per_page) + 1 + page = ((@post_number.to_i - 1) / @limit) + 1 (page > 1) ? "?page=#{page}" : "" else (@page && @page.to_i > 1) ? "?page=#{@page}" : "" @@ -130,8 +130,12 @@ class TopicView def filter_posts_paged(page) page = [page, 1].max - min = SiteSetting.posts_per_page * (page - 1) - max = (min + SiteSetting.posts_per_page) - 1 + min = @limit * (page - 1) + + # Sometimes we don't care about the OP, for example when embedding comments + min = 1 if min == 0 && @exclude_first + + max = (min + @limit) - 1 filter_posts_in_range(min, max) end @@ -317,12 +321,12 @@ class TopicView return nil if closest_index.nil? # Make sure to get at least one post before, even with rounding - posts_before = (SiteSetting.posts_per_page.to_f / 4).floor + posts_before = (@limit.to_f / 4).floor posts_before = 1 if posts_before.zero? min_idx = closest_index - posts_before min_idx = 0 if min_idx < 0 - max_idx = min_idx + (SiteSetting.posts_per_page - 1) + max_idx = min_idx + (@limit - 1) # Get a full page even if at the end ensure_full_page(min_idx, max_idx) @@ -331,7 +335,7 @@ class TopicView def ensure_full_page(min, max) upper_limit = (filtered_post_ids.length - 1) if max >= upper_limit - return (upper_limit - SiteSetting.posts_per_page) + 1, upper_limit + return (upper_limit - @limit) + 1, upper_limit else return min, max end diff --git a/public/javascripts/embed.js b/public/javascripts/embed.js index f0b01d79b1d..9adbcd5bf09 100644 --- a/public/javascripts/embed.js +++ b/public/javascripts/embed.js @@ -4,7 +4,7 @@ var comments = document.getElementById('discourse-comments'), iframe = document.createElement('iframe'); - iframe.src = discourseUrl + "embed/best?embed_url=" + encodeURIComponent(discourseEmbedUrl); + iframe.src = discourseUrl + "embed/comments?embed_url=" + encodeURIComponent(discourseEmbedUrl); iframe.id = 'discourse-embed-frame'; iframe.width = "100%"; iframe.frameBorder = "0"; diff --git a/spec/controllers/embed_controller_spec.rb b/spec/controllers/embed_controller_spec.rb index 52661962167..5fc1d4f22aa 100644 --- a/spec/controllers/embed_controller_spec.rb +++ b/spec/controllers/embed_controller_spec.rb @@ -6,13 +6,13 @@ describe EmbedController do let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" } it "is 404 without an embed_url" do - get :best + get :comments response.should_not be_success end it "raises an error with a missing host" do SiteSetting.stubs(:embeddable_host).returns(nil) - get :best, embed_url: embed_url + get :comments, embed_url: embed_url response.should_not be_success end @@ -22,7 +22,7 @@ describe EmbedController do end it "raises an error with no referer" do - get :best, embed_url: embed_url + get :comments, embed_url: embed_url response.should_not be_success end @@ -42,13 +42,13 @@ describe EmbedController do retriever = mock TopicRetriever.expects(:new).returns(retriever) retriever.expects(:retrieve) - get :best, embed_url: embed_url + get :comments, embed_url: embed_url end it "creates a topic view when a topic_id is found" do TopicEmbed.expects(:topic_id_for_embed).returns(123) - TopicView.expects(:new).with(123, nil, {best: 5}) - get :best, embed_url: embed_url + TopicView.expects(:new).with(123, nil, {limit: 100, exclude_first: true}) + get :comments, embed_url: embed_url end end