Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ app/views/notes/_note.html.erb
app/views/notes/error.html.erb
app/views/notes/index.html.erb
app/views/notes/show.html.erb
app/views/notes/show_in_reply.html.erb
app/views/notification_mailer/_mail_footer.html.erb
app/views/notification_mailer/_mail_header.html.erb
app/views/notification_mailer/article.html.erb
Expand Down Expand Up @@ -386,7 +385,6 @@ lib/publify_core/text_filter/markdown.rb
lib/publify_core/text_filter/markdown_smartquotes.rb
lib/publify_core/text_filter/none.rb
lib/publify_core/text_filter/smartypants.rb
lib/publify_core/text_filter/twitterfilter.rb
lib/publify_core/version.rb
lib/publify_guid.rb
lib/publify_plugins.rb
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def index
def show
@note = Note.published.find_by! permalink: CGI.escape(params[:permalink])

if @note.in_reply_to_message.present?
@reply = JSON.parse(@note.in_reply_to_message)
render :show_in_reply
end
@reply = JSON.parse(@note.in_reply_to_message) if @note.in_reply_to_message.present?
end

private
Expand Down
59 changes: 55 additions & 4 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

require "twitter"
require "json"
require "uri"
require "html/pipeline"
require "html/pipeline/hashtag/hashtag_filter"

class Note < Content
require "twitter"
require "json"
require "uri"
include PublifyGuid
include ConfigManager

Expand All @@ -30,6 +33,40 @@ class Note < Content
TWITTER_HTTPS_URL_LENGTH = 21
TWITTER_LINK_LENGTH = 22

class TwitterHashtagFilter < HTML::Pipeline::HashtagFilter
def initialize(text)
super(text,
tag_url: "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime",
tag_link_attr: "")
end
end

class TwitterMentionFilter < HTML::Pipeline::MentionFilter
def initialize(text)
super(text, base_url: "https://twitter.com")
end

# Override base mentions finder, treating @mention just like any other @foo.
def self.mentioned_logins_in(text, username_pattern = UsernamePattern)
text.gsub MentionPatterns[username_pattern] do |match|
login = Regexp.last_match(1)
yield match, login, false
end
end

# Override base link creator, removing the class
def link_to_mentioned_user(login)
result[:mentioned_usernames] |= [login]

url = base_url.dup
url << "/" unless %r{[/~]\z}.match?(url)

"<a href='#{url << login}'>" \
"@#{login}" \
"</a>"
end
end

def set_permalink
self.permalink = "#{id}-#{body.to_permalink[0..79]}" if permalink.blank?
save
Expand All @@ -43,8 +80,22 @@ def tags
[]
end

def generate_html(field, text = nil)
if field == :in_reply_to
html = TextFilter.make_filter("none").filter_text(text)
html_postprocess(field, html).to_s
else
super
end
end

def html_postprocess(field, html)
super(field, PublifyCore::TextFilter::Twitterfilter.filtertext(html))
helper = PublifyCore::ContentTextHelpers.new
html = helper.auto_link(html)

html = TwitterHashtagFilter.new(html).call
html = TwitterMentionFilter.new(html).call.to_s
super
end

def truncate(message, length)
Expand Down
15 changes: 15 additions & 0 deletions app/views/notes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<div class="hfeed">
<div class='h-entry hentry h-as-note'>
<% if @reply %>
<article class=' well well-small'>
<% if @reply['user']['profile_image_url'] %>
<%= image_tag(@reply['user']['profile_image_url'], class: 'alignleft', alt: @reply['user']['name']) %>
<% end %>
<strong><%= get_reply_context_url(@reply) %></strong>
<p><%= nofollowify_links(@note.generate_html(:in_reply_to, @reply['text'])) %></p>
<p>
<small>
<%= get_reply_context_twitter_link(@reply) %>
</small>
</p>
</article>
<% end %>

<%= render partial: 'note', object: @note %>
</div>
</div>
18 changes: 0 additions & 18 deletions app/views/notes/show_in_reply.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion lib/publify_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
require "publify_core/text_filter/markdown"
require "publify_core/text_filter/markdown_smartquotes"
require "publify_core/text_filter/smartypants"
require "publify_core/text_filter/twitterfilter"
require "publify_core/string_ext"

require "bootstrap"
Expand Down
55 changes: 0 additions & 55 deletions lib/publify_core/text_filter/twitterfilter.rb

This file was deleted.

13 changes: 10 additions & 3 deletions spec/controllers/notes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
end

context "in reply" do
render_views

let(:reply) do
{
"id_str" => "123456789",
"created_at" => DateTime.new(2014, 1, 23, 13, 47).in_time_zone,
"text" => "**original** #foo",
"user" => {
"screen_name" => "a screen name",
"entities" => {
Expand All @@ -57,9 +60,13 @@

before { get :show, params: { permalink: permalink } }

it { expect(response).to be_successful }
it { expect(response).to render_template("show_in_reply") }
it { expect(assigns[:page_title]).to eq("Notes | test blog ") }
it "successfully renders the tweet the note replies to without extra filters" do
aggregate_failures do
expect(response).to be_successful
expect(response.body).to have_text "**original** #foo"
expect(response.body).to have_link "#foo"
end
end
end

context "note not found" do
Expand Down
3 changes: 1 addition & 2 deletions spec/lib/text_filter_plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
PublifyCore::TextFilter::None,
PublifyCore::TextFilter::Markdown,
PublifyCore::TextFilter::Smartypants,
PublifyCore::TextFilter::MarkdownSmartquotes,
PublifyCore::TextFilter::Twitterfilter)
PublifyCore::TextFilter::MarkdownSmartquotes)
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@
expect(note.html).to eq(expected)
end

it "works with a hashtag and a mention" do
note = create(:note, body: "A test tweet with a #hashtag and a @mention")
expected =
"<p>A test tweet with a <a href=\"https://twitter.com/search?q=%23hashtag" \
"&amp;src=tren&amp;mode=realtime\">#hashtag</a> and a" \
" <a href=\"https://twitter.com/mention\">@mention</a></p>"
expect(note.html).to eq expected
end

it "links markdown links only once" do
note = create(:note, body: "A test tweet with [a markdown link](https://link.com)")
expect(note.html)
Expand Down
39 changes: 0 additions & 39 deletions spec/publify_core/text_filter/twitterfilter_spec.rb

This file was deleted.