Tags now link to meaningless query in PostsController
This commit is contained in:
parent
f19857cc40
commit
ea98b1ca81
2 changed files with 50 additions and 14 deletions
|
|
@ -38,12 +38,30 @@ class StatusMessage < Post
|
||||||
write_attribute(:message, text)
|
write_attribute(:message, text)
|
||||||
end
|
end
|
||||||
|
|
||||||
def formatted_message(opts = {})
|
def formatted_message(opts={})
|
||||||
return self.raw_message unless self.raw_message
|
return self.raw_message unless self.raw_message
|
||||||
|
|
||||||
|
escaped_message = opts[:plain_text] ? self.raw_message: ERB::Util.h(self.raw_message)
|
||||||
|
mentioned_message = self.format_mentions(escaped_message, opts)
|
||||||
|
self.format_tags(mentioned_message, opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_tags(text, opts={})
|
||||||
|
return text if opts[:plain_text]
|
||||||
|
regex = /(^|\s)#(\w+)/
|
||||||
|
form_message = text.gsub(regex) do |matched_string|
|
||||||
|
tag = self.tags.detect do |t|
|
||||||
|
t.name == $~[2]
|
||||||
|
end
|
||||||
|
tag ? "#{$~[1]}<a href=\"/p?tag=#{tag.name}\" class=\"tag\">##{ERB::Util.h(tag.name)}</a>" : ERB::Util.h($~[2])
|
||||||
|
end
|
||||||
|
form_message
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_mentions(text, opts = {})
|
||||||
people = self.mentioned_people
|
people = self.mentioned_people
|
||||||
regex = /@\{([^;]+); ([^\}]+)\}/
|
regex = /@\{([^;]+); ([^\}]+)\}/
|
||||||
escaped_message = opts[:plain_text] ? raw_message : ERB::Util.h(raw_message)
|
form_message = text.gsub(regex) do |matched_string|
|
||||||
form_message = escaped_message.gsub(regex) do |matched_string|
|
|
||||||
person = people.detect{ |p|
|
person = people.detect{ |p|
|
||||||
p.diaspora_handle == $~[2]
|
p.diaspora_handle == $~[2]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,11 @@ require 'spec_helper'
|
||||||
|
|
||||||
|
|
||||||
describe StatusMessage do
|
describe StatusMessage do
|
||||||
|
include ActionView::Helpers::UrlHelper
|
||||||
|
include Rails.application.routes.url_helpers
|
||||||
|
def controller
|
||||||
|
mock()
|
||||||
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@user = alice
|
@user = alice
|
||||||
|
|
@ -68,12 +73,6 @@ describe StatusMessage do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'mentions' do
|
describe 'mentions' do
|
||||||
def controller
|
|
||||||
mock()
|
|
||||||
end
|
|
||||||
|
|
||||||
include ActionView::Helpers::UrlHelper
|
|
||||||
include Rails.application.routes.url_helpers
|
|
||||||
before do
|
before do
|
||||||
@people = [alice, bob, eve].map{|u| u.person}
|
@people = [alice, bob, eve].map{|u| u.person}
|
||||||
@test_string = <<-STR
|
@test_string = <<-STR
|
||||||
|
|
@ -83,9 +82,9 @@ STR
|
||||||
@sm = Factory.create(:status_message, :message => @test_string )
|
@sm = Factory.create(:status_message, :message => @test_string )
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#formatted_message' do
|
describe '#format_mentions' do
|
||||||
it 'adds the links in the formated message text' do
|
it 'adds the links in the formated message text' do
|
||||||
@sm.formatted_message.should == <<-STR
|
@sm.format_mentions(@sm.raw_message).should == <<-STR
|
||||||
#{link_to('@' << @people[0].name, person_path(@people[0]), :class => 'mention')} can mention people like Raphael #{link_to('@' << @people[1].name, person_path(@people[1]), :class => 'mention')}
|
#{link_to('@' << @people[0].name, person_path(@people[0]), :class => 'mention')} can mention people like Raphael #{link_to('@' << @people[1].name, person_path(@people[1]), :class => 'mention')}
|
||||||
can mention people like Raphaellike Raphael #{link_to('@' << @people[2].name, person_path(@people[2]), :class => 'mention')} can mention people like Raph
|
can mention people like Raphaellike Raphael #{link_to('@' << @people[2].name, person_path(@people[2]), :class => 'mention')} can mention people like Raph
|
||||||
STR
|
STR
|
||||||
|
|
@ -94,13 +93,13 @@ STR
|
||||||
context 'with :plain_text option' do
|
context 'with :plain_text option' do
|
||||||
it 'removes the mention syntax and displays the unformatted name' do
|
it 'removes the mention syntax and displays the unformatted name' do
|
||||||
status = Factory(:status_message, :message => "@{Barack Obama; barak@joindiaspora.com } is so cool @{Barack Obama; barak@joindiaspora.com } ")
|
status = Factory(:status_message, :message => "@{Barack Obama; barak@joindiaspora.com } is so cool @{Barack Obama; barak@joindiaspora.com } ")
|
||||||
status.formatted_message(:plain_text => true).should == 'Barack Obama is so cool Barack Obama '
|
status.format_mentions(status.raw_message, :plain_text => true).should == 'Barack Obama is so cool Barack Obama '
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'leaves the name of people that cannot be found' do
|
it 'leaves the name of people that cannot be found' do
|
||||||
@sm.stub(:mentioned_people).and_return([])
|
@sm.stub(:mentioned_people).and_return([])
|
||||||
@sm.formatted_message.should == <<-STR
|
@sm.format_mentions(@sm.raw_message).should == <<-STR
|
||||||
Raphael can mention people like Raphael Ilya
|
Raphael can mention people like Raphael Ilya
|
||||||
can mention people like Raphaellike Raphael Daniel can mention people like Raph
|
can mention people like Raphaellike Raphael Daniel can mention people like Raph
|
||||||
STR
|
STR
|
||||||
|
|
@ -113,8 +112,10 @@ STR
|
||||||
.values
|
.values
|
||||||
p.save!
|
p.save!
|
||||||
|
|
||||||
@sm.formatted_message.should_not include(@people[0].profile.first_name)
|
@sm.format_mentions(@sm.raw_message).should_not include(@people[0].profile.first_name)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
describe '#formatted_message' do
|
||||||
it 'escapes the message' do
|
it 'escapes the message' do
|
||||||
xss = "</a> <script> alert('hey'); </script>"
|
xss = "</a> <script> alert('hey'); </script>"
|
||||||
@sm.message << xss
|
@sm.message << xss
|
||||||
|
|
@ -177,6 +178,23 @@ STR
|
||||||
before do
|
before do
|
||||||
@sm = Factory.build(:status_message)
|
@sm = Factory.build(:status_message)
|
||||||
end
|
end
|
||||||
|
describe '#format_tags' do
|
||||||
|
before do
|
||||||
|
@str = '#what #hey'
|
||||||
|
@sm.message = @str
|
||||||
|
@sm.build_tags
|
||||||
|
@sm.save
|
||||||
|
@sm.reload
|
||||||
|
end
|
||||||
|
it 'links the tag to /p' do
|
||||||
|
link = link_to('#what', posts_path(:tag => 'what'), :class => 'tag')
|
||||||
|
@sm.format_tags(@str).should include(link)
|
||||||
|
end
|
||||||
|
it 'responds to plain_text' do
|
||||||
|
@sm.format_tags(@str, :plain_text => true).should == @str
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
describe '#build_tags' do
|
describe '#build_tags' do
|
||||||
it 'builds the tags' do
|
it 'builds the tags' do
|
||||||
@sm.message = '#what'
|
@sm.message = '#what'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue