parent
2aa91e0b62
commit
a0d200d209
2 changed files with 42 additions and 16 deletions
|
|
@ -1,23 +1,20 @@
|
|||
|
||||
module Diaspora::Mentionable
|
||||
|
||||
# regex for finding mention markup in plain text
|
||||
# ex.
|
||||
# regex for finding mention markup in plain text:
|
||||
# "message @{user@pod.net} text"
|
||||
# it can also contain a name, which gets used as the link text:
|
||||
# "message @{User Name; user@pod.net} text"
|
||||
# will yield "User Name" and "user@pod.net"
|
||||
REGEX = /(@\{(.+?; [^\}]+)\})/
|
||||
REGEX = /@\{(?:([^\}]+?); )?([^\} ]+)\}/
|
||||
|
||||
# class attribute that will be added to all mention html links
|
||||
PERSON_HREF_CLASS = "mention hovercardable"
|
||||
|
||||
def self.mention_attrs(mention_str)
|
||||
mention = mention_str.match(REGEX)[2]
|
||||
del_pos = mention.rindex(/;/)
|
||||
name, diaspora_id = mention_str.match(REGEX).captures
|
||||
|
||||
name = mention[0..(del_pos - 1)].strip
|
||||
handle = mention[(del_pos + 1)..-1].strip
|
||||
|
||||
[name, handle]
|
||||
[name.try(:strip), diaspora_id.strip]
|
||||
end
|
||||
|
||||
# takes a message text and returns the text with mentions in (html escaped)
|
||||
|
|
@ -46,7 +43,7 @@ module Diaspora::Mentionable
|
|||
# @return [Array<Person>] array of people
|
||||
def self.people_from_string(msg_text)
|
||||
identifiers = msg_text.to_s.scan(REGEX).map do |match_str|
|
||||
_, identifier = mention_attrs(match_str.first)
|
||||
identifier = match_str.second.strip
|
||||
identifier if Validation::Rule::DiasporaId.new.valid_value?(identifier)
|
||||
end
|
||||
|
||||
|
|
@ -72,8 +69,6 @@ module Diaspora::Mentionable
|
|||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private_class_method def self.find_or_fetch_person_by_identifier(identifier)
|
||||
Person.find_or_fetch_by_identifier(identifier)
|
||||
rescue DiasporaFederation::Discovery::DiscoveryError
|
||||
|
|
@ -113,5 +108,4 @@ module Diaspora::Mentionable
|
|||
"[#{display_name.presence || person.name}](#{local_or_remote_person_path(person)})"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -18,7 +18,27 @@ three "Eve> E.
|
|||
STR
|
||||
end
|
||||
|
||||
describe "#format" do
|
||||
describe ".mention_attrs" do
|
||||
it "returns name and diaspora ID" do
|
||||
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@names[0]}; #{@people[0].diaspora_handle}}")
|
||||
expect(name).to eq(@names[0])
|
||||
expect(diaspora_id).to eq(@people[0].diaspora_handle)
|
||||
end
|
||||
|
||||
it "returns only diaspora-ID when no name is included" do
|
||||
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@people[0].diaspora_handle}}")
|
||||
expect(diaspora_id).to eq(@people[0].diaspora_handle)
|
||||
expect(name).to be_nil
|
||||
end
|
||||
|
||||
it "trims the name if available" do
|
||||
name, diaspora_id = Diaspora::Mentionable.mention_attrs("@{#{@names[0]} ; #{@people[0].diaspora_handle}}")
|
||||
expect(name).to eq(@names[0])
|
||||
expect(diaspora_id).to eq(@people[0].diaspora_handle)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".format" do
|
||||
context "html output" do
|
||||
it "adds the links to the formatted message" do
|
||||
fmt_msg = Diaspora::Mentionable.format(@test_txt, @people)
|
||||
|
|
@ -64,12 +84,24 @@ STR
|
|||
end
|
||||
end
|
||||
|
||||
describe "#people_from_string" do
|
||||
describe ".people_from_string" do
|
||||
it "extracts the mentioned people from the text" do
|
||||
ppl = Diaspora::Mentionable.people_from_string(@test_txt)
|
||||
expect(ppl).to match_array(@people)
|
||||
end
|
||||
|
||||
it "extracts the mentioned people from the text without name" do
|
||||
text = "test @{#{@people[0].diaspora_handle}} test"
|
||||
ppl = Diaspora::Mentionable.people_from_string(text)
|
||||
expect(ppl).to match_array([@people[0]])
|
||||
end
|
||||
|
||||
it "extracts the mentioned people from the text mixed mentions (with and without name)" do
|
||||
text = "@{#{@people[0].diaspora_handle}} and @{#{@names[1]}; #{@people[1].diaspora_handle}}"
|
||||
ppl = Diaspora::Mentionable.people_from_string(text)
|
||||
expect(ppl).to match_array([@people[0], @people[1]])
|
||||
end
|
||||
|
||||
describe "returns an empty array if nobody was found" do
|
||||
it "gets a post without mentions" do
|
||||
ppl = Diaspora::Mentionable.people_from_string("post w/o mentions")
|
||||
|
|
@ -102,7 +134,7 @@ STR
|
|||
end
|
||||
end
|
||||
|
||||
describe "#filter_people" do
|
||||
describe ".filter_people" do
|
||||
before do
|
||||
@user_a = FactoryGirl.create(:user_with_aspect, username: "user_a")
|
||||
@user_b = FactoryGirl.create(:user, username: "user_b")
|
||||
|
|
|
|||
Loading…
Reference in a new issue