Commit 4a994b0a authored by Benjamin Neff's avatar Benjamin Neff

Creates references for linked posts after create

parent c82e891c
......@@ -9,8 +9,17 @@ class Reference < ApplicationRecord
extend ActiveSupport::Concern
included do
after_create :create_references
has_many :references, as: :source, dependent: :destroy
end
def create_references
text&.scan(DiasporaFederation::Federation::DiasporaUrlParser::DIASPORA_URL_REGEX)&.each do |author, type, guid|
class_name = DiasporaFederation::Entity.entity_class(type).to_s.rpartition("::").last
entity = Diaspora::Federation::Mappings.model_class_for(class_name).find_by(guid: guid)
references.find_or_create_by(target: entity) if entity.diaspora_handle == author
end
end
end
module Target
......
......@@ -14,6 +14,43 @@ shared_examples_for "a reference source" do
expect(Reference.where(id: reference.id)).not_to exist
end
end
describe "#create_references" do
it "creates a reference for every referenced post after create" 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}."
post = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text)
post.save
expect(post.references.map(&:target).map(&:guid)).to match_array([target1, target2].map(&:guid))
end
it "only creates one reference, even when it is referenced twice" do
target = FactoryGirl.create(:status_message)
text = "Have a look at [this post](diaspora://#{target.diaspora_handle}/post/#{target.guid}) and " \
"this one too diaspora://#{target.diaspora_handle}/post/#{target.guid}."
post = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text)
post.save
expect(post.references.map(&:target).map(&:guid)).to match_array([target.guid])
end
it "only creates references, when the author of the known entity matches" 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://#{target1.diaspora_handle}/post/#{target2.guid}."
post = FactoryGirl.build(described_class.to_s.underscore.to_sym, text: text)
post.save
expect(post.references.map(&:target).map(&:guid)).to match_array([target1.guid])
end
end
end
shared_examples_for "a reference target" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment