Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 5f5d8c5e rédigé par Benjamin Neff's avatar Benjamin Neff
Parcourir les fichiers

Replace diaspora:// links in the text

parent 4a994b0a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -95,6 +95,12 @@ module Diaspora
def normalize
@message = self.class.normalize(@message)
end
def diaspora_links
@message = @message.gsub(DiasporaFederation::Federation::DiasporaUrlParser::DIASPORA_URL_REGEX) {|match_str|
Regexp.last_match(2) == "post" ? AppConfig.url_to("/posts/#{Regexp.last_match(3)}") : match_str
}
end
end
DEFAULTS = {mentioned_people: [],
......@@ -158,6 +164,7 @@ module Diaspora
def plain_text opts={}
process(opts) {
make_mentions_plain_text
diaspora_links
squish
append_and_truncate
}
......@@ -167,6 +174,7 @@ module Diaspora
def plain_text_without_markdown opts={}
process(opts) {
make_mentions_plain_text
diaspora_links
strip_markdown
squish
append_and_truncate
......@@ -177,6 +185,7 @@ module Diaspora
def plain_text_for_json opts={}
process(opts) {
normalize
diaspora_links
camo_urls if AppConfig.privacy.camo.proxy_markdown_images?
}
end
......@@ -186,6 +195,7 @@ module Diaspora
process(opts) {
escape
normalize
diaspora_links
render_mentions
render_tags
squish
......@@ -198,6 +208,7 @@ module Diaspora
process(opts) {
process_newlines
normalize
diaspora_links
camo_urls if AppConfig.privacy.camo.proxy_markdown_images?
markdownify
render_mentions
......
# frozen_string_literal: true
describe Diaspora::MessageRenderer do
MESSAGE_NORMALIZTIONS = {
"\u202a#\u200eUSA\u202c" => "#USA",
"ള്‍" => "ള്‍"
}
def message(text, opts={})
Diaspora::MessageRenderer.new(text, opts)
end
......@@ -100,6 +95,20 @@ describe Diaspora::MessageRenderer do
end
end
end
context "with diaspora:// links" do
it "replaces diaspora:// links with pod-local links" do
target = FactoryGirl.create(:status_message)
expect(
message("Have a look at diaspora://#{target.diaspora_handle}/post/#{target.guid}.").html
).to match(/Have a look at #{AppConfig.url_to("/posts/#{target.guid}")}./)
end
it "doesn't touch invalid diaspora:// links" do
text = "You can create diaspora://author/type/guid links!"
expect(message(text).html).to match(/#{text}/)
end
end
end
describe "#markdownified" do
......@@ -128,8 +137,11 @@ describe Diaspora::MessageRenderer do
end
it "normalizes" do
MESSAGE_NORMALIZTIONS.each do |input, output|
expect(message(input).plain_text_for_json).to eq output
{
"\u202a#\u200eUSA\u202c" => "<p><a class=\"tag\" href=\"/tags/USA\">#USA</a></p>\n",
"ള്‍" => "<p>ള്‍</p>\n"
}.each do |input, output|
expect(message(input).markdownified).to eq output
end
end
......@@ -180,6 +192,25 @@ describe Diaspora::MessageRenderer do
entities = '&amp; &szlig; &#x27; &#39; &quot;'
expect(message(entities).markdownified).to eq "<p>#{entities}</p>\n"
end
context "with diaspora:// links" do
it "replaces diaspora:// links with pod-local links" do
target1 = FactoryGirl.create(:status_message)
target2 = FactoryGirl.create(:status_message)
text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \
"this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}."
rendered = message(text).markdownified
expect(rendered).to match(%r{at <a href="#{AppConfig.url_to("/posts/#{target1.guid}")}">this post</a> and})
expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./)
end
it "doesn't touch invalid diaspora:// links" do
text = "You can create diaspora://author/type/guid links!"
expect(message(text).markdownified).to match(/#{text}/)
end
end
end
end
......@@ -210,6 +241,25 @@ describe Diaspora::MessageRenderer do
expect(msg.plain_text_without_markdown).to eq "@#{alice.diaspora_handle} is cool"
end
end
context "with diaspora:// links" do
it "replaces diaspora:// links with pod-local links" do
target1 = FactoryGirl.create(:status_message)
target2 = FactoryGirl.create(:status_message)
text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \
"this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}."
rendered = message(text).plain_text_without_markdown
expect(rendered).to match(/look at this post \(#{AppConfig.url_to("/posts/#{target1.guid}")}\) and/)
expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./)
end
it "doesn't touch invalid diaspora:// links" do
text = "You can create diaspora://author/type/guid links!"
expect(message(text).plain_text_without_markdown).to match(/#{text}/)
end
end
end
describe "#urls" do
......@@ -241,9 +291,31 @@ describe Diaspora::MessageRenderer do
describe "#plain_text_for_json" do
it "normalizes" do
MESSAGE_NORMALIZTIONS.each do |input, output|
{
"\u202a#\u200eUSA\u202c" => "#USA",
"ള്‍" => "ള്‍"
}.each do |input, output|
expect(message(input).plain_text_for_json).to eq output
end
end
context "with diaspora:// links" do
it "replaces diaspora:// links with pod-local links" do
target1 = FactoryGirl.create(:status_message)
target2 = FactoryGirl.create(:status_message)
text = "Have a look at [this post](diaspora://#{target1.diaspora_handle}/post/#{target1.guid}) and " \
"this one too diaspora://#{target2.diaspora_handle}/post/#{target2.guid}."
rendered = message(text).plain_text_for_json
expect(rendered).to match(/look at \[this post\]\(#{AppConfig.url_to("/posts/#{target1.guid}")}\) and/)
expect(rendered).to match(/this one too #{AppConfig.url_to("/posts/#{target2.guid}")}./)
end
it "doesn't touch invalid diaspora:// links" do
text = "You can create diaspora://author/type/guid links!"
expect(message(text).plain_text_for_json).to match(/#{text}/)
end
end
end
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter