diff --git a/app/models/status_reference.rb b/app/models/status_reference.rb index dd912d995c..8d5d6eba8b 100644 --- a/app/models/status_reference.rb +++ b/app/models/status_reference.rb @@ -9,6 +9,7 @@ # target_status_id :bigint(8) not null # created_at :datetime not null # updated_at :datetime not null +# attribute_type :string # class StatusReference < ApplicationRecord diff --git a/app/services/process_references_service.rb b/app/services/process_references_service.rb index 67f6e1beec..05aa5ef345 100644 --- a/app/services/process_references_service.rb +++ b/app/services/process_references_service.rb @@ -75,9 +75,11 @@ class ProcessReferencesService < BaseService def scan_text! text = extract_status_plain_text(@status) - statuses = fetch_statuses!(text.scan(REFURL_EXP).pluck(3).uniq) + scaned = text.scan(REFURL_EXP) + statuses = fetch_statuses!(scaned.pluck(3).uniq) @again = true if !@fetch_remote && statuses.any?(&:nil?) + @attributes = scaned.pluck(0).zip(statuses).to_h { |pair| [pair[1]&.id, pair[0]] } @scan_text = statuses.compact.map(&:id).uniq.filter { |status_id| !status_id.zero? } end @@ -99,7 +101,7 @@ class ProcessReferencesService < BaseService statuses = Status.where(id: added_references) statuses.each do |status| - @added_objects << @status.reference_objects.new(target_status: status) + @added_objects << @status.reference_objects.new(target_status: status, attribute_type: @attributes[status.id]) status.increment_count!(:status_referred_by_count) @references_count += 1 diff --git a/db/migrate/20230919232836_add_attribute_to_status_references.rb b/db/migrate/20230919232836_add_attribute_to_status_references.rb new file mode 100644 index 0000000000..ea77b3eddb --- /dev/null +++ b/db/migrate/20230919232836_add_attribute_to_status_references.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require Rails.root.join('lib', 'mastodon', 'migration_helpers') + +class AddAttributeToStatusReferences < ActiveRecord::Migration[7.0] + include Mastodon::MigrationHelpers + + disable_ddl_transaction! + + def change + safety_assured do + add_column :status_references, :attribute_type, :string + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f322d5e715..c5dcfc6a33 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_09_11_022527) do +ActiveRecord::Schema[7.0].define(version: 2023_09_19_232836) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1132,6 +1132,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_11_022527) do t.bigint "target_status_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false + t.string "attribute_type" t.index ["status_id"], name: "index_status_references_on_status_id" t.index ["target_status_id"], name: "index_status_references_on_target_status_id" end diff --git a/spec/services/process_references_service_spec.rb b/spec/services/process_references_service_spec.rb index 0cb2d2f190..e9099fb935 100644 --- a/spec/services/process_references_service_spec.rb +++ b/spec/services/process_references_service_spec.rb @@ -13,7 +13,7 @@ RSpec.describe ProcessReferencesService, type: :service do describe 'posting new status' do subject do described_class.new.call(status, reference_parameters, urls: urls, fetch_remote: fetch_remote) - status.references.pluck(:id) + status.reference_objects.pluck(:target_status_id, :attribute_type) end let(:reference_parameters) { [] } @@ -24,9 +24,9 @@ RSpec.describe ProcessReferencesService, type: :service do let(:text) { "Hello RT #{target_status_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 1 - expect(ids).to include target_status.id + expect(subject.size).to eq 1 + expect(subject.pluck(0)).to include target_status.id + expect(subject.pluck(1)).to include 'RT' end end @@ -36,10 +36,9 @@ RSpec.describe ProcessReferencesService, type: :service do let(:text) { "Hello RT #{target_status_uri}\nBT #{target_status2_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 2 - expect(ids).to include target_status.id - expect(ids).to include target_status2.id + expect(subject.size).to eq 2 + expect(subject).to include [target_status.id, 'RT'] + expect(subject).to include [target_status2.id, 'BT'] end end @@ -47,8 +46,7 @@ RSpec.describe ProcessReferencesService, type: :service do let(:text) { "Hello #{target_status_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 0 + expect(subject.size).to eq 0 end end @@ -67,7 +65,7 @@ RSpec.describe ProcessReferencesService, type: :service do updated: '2022-01-22T16:00:00Z', } end - let(:text) { 'BT https://example.com/test_post' } + let(:text) { 'BT:https://example.com/test_post' } before do stub_request(:get, 'https://example.com/test_post').to_return(status: 200, body: Oj.dump(object_json), headers: { 'Content-Type' => 'application/activity+json' }) @@ -75,10 +73,10 @@ RSpec.describe ProcessReferencesService, type: :service do end it 'reference it' do - ids = subject - expect(ids.size).to eq 1 + expect(subject.size).to eq 1 + expect(subject[0][1]).to eq 'BT' - status = Status.find_by(id: ids[0]) + status = Status.find_by(id: subject[0][0]) expect(status).to_not be_nil expect(status.url).to eq 'https://example.com/test_post' end @@ -87,7 +85,7 @@ RSpec.describe ProcessReferencesService, type: :service do let(:fetch_remote) { false } it 'reference it' do - ids = subject + ids = subject.pluck(0) expect(ids.size).to eq 1 status = Status.find_by(id: ids[0]) @@ -101,11 +99,11 @@ RSpec.describe ProcessReferencesService, type: :service do let(:text) { "RT #{ActivityPub::TagManager.instance.uri_for(target_status)} BT https://example.com/test_post" } it 'reference it' do - ids = subject - expect(ids.size).to eq 2 - expect(ids).to include target_status.id + expect(subject.size).to eq 2 + expect(subject).to include [target_status.id, 'RT'] + expect(subject.pluck(1)).to include 'BT' - status = Status.find_by(id: ids, uri: 'https://example.com/test_post') + status = Status.find_by(id: subject.pluck(0), uri: 'https://example.com/test_post') expect(status).to_not be_nil end end @@ -114,8 +112,7 @@ RSpec.describe ProcessReferencesService, type: :service do let(:text) { 'BT https://example.com/not_found' } it 'reference it' do - ids = subject - expect(ids.size).to eq 0 + expect(subject.size).to eq 0 end end end @@ -144,9 +141,8 @@ RSpec.describe ProcessReferencesService, type: :service do let(:new_text) { "BT #{target_status_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 1 - expect(ids).to include target_status.id + expect(subject.size).to eq 1 + expect(subject).to include target_status.id end end @@ -155,10 +151,9 @@ RSpec.describe ProcessReferencesService, type: :service do let(:new_text) { "BT #{target_status_uri}\nBT #{target_status2_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 2 - expect(ids).to include target_status.id - expect(ids).to include target_status2.id + expect(subject.size).to eq 2 + expect(subject).to include target_status.id + expect(subject).to include target_status2.id end end @@ -167,9 +162,8 @@ RSpec.describe ProcessReferencesService, type: :service do let(:new_text) { "BT #{target_status_uri}\nBT #{target_status_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 1 - expect(ids).to include target_status.id + expect(subject.size).to eq 1 + expect(subject).to include target_status.id end end @@ -178,8 +172,7 @@ RSpec.describe ProcessReferencesService, type: :service do let(:new_text) { 'Hello' } it 'post status' do - ids = subject - expect(ids.size).to eq 0 + expect(subject.size).to eq 0 end end @@ -188,9 +181,8 @@ RSpec.describe ProcessReferencesService, type: :service do let(:new_text) { "BT #{target_status2_uri}" } it 'post status' do - ids = subject - expect(ids.size).to eq 1 - expect(ids).to include target_status2.id + expect(subject.size).to eq 1 + expect(subject).to include target_status2.id end end end