Add reference attribute

This commit is contained in:
KMY 2023-09-20 08:57:11 +09:00
parent a2d4a9295d
commit b59c41cf41
5 changed files with 50 additions and 39 deletions

View file

@ -9,6 +9,7 @@
# target_status_id :bigint(8) not null # target_status_id :bigint(8) not null
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# attribute_type :string
# #
class StatusReference < ApplicationRecord class StatusReference < ApplicationRecord

View file

@ -75,9 +75,11 @@ class ProcessReferencesService < BaseService
def scan_text! def scan_text!
text = extract_status_plain_text(@status) 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?) @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? } @scan_text = statuses.compact.map(&:id).uniq.filter { |status_id| !status_id.zero? }
end end
@ -99,7 +101,7 @@ class ProcessReferencesService < BaseService
statuses = Status.where(id: added_references) statuses = Status.where(id: added_references)
statuses.each do |status| 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) status.increment_count!(:status_referred_by_count)
@references_count += 1 @references_count += 1

View file

@ -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

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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.bigint "target_status_id", null: false
t.datetime "created_at", precision: nil, null: false t.datetime "created_at", precision: nil, null: false
t.datetime "updated_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 ["status_id"], name: "index_status_references_on_status_id"
t.index ["target_status_id"], name: "index_status_references_on_target_status_id" t.index ["target_status_id"], name: "index_status_references_on_target_status_id"
end end

View file

@ -13,7 +13,7 @@ RSpec.describe ProcessReferencesService, type: :service do
describe 'posting new status' do describe 'posting new status' do
subject do subject do
described_class.new.call(status, reference_parameters, urls: urls, fetch_remote: fetch_remote) 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 end
let(:reference_parameters) { [] } let(:reference_parameters) { [] }
@ -24,9 +24,9 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:text) { "Hello RT #{target_status_uri}" } let(:text) { "Hello RT #{target_status_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 1
expect(ids.size).to eq 1 expect(subject.pluck(0)).to include target_status.id
expect(ids).to include target_status.id expect(subject.pluck(1)).to include 'RT'
end end
end end
@ -36,10 +36,9 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:text) { "Hello RT #{target_status_uri}\nBT #{target_status2_uri}" } let(:text) { "Hello RT #{target_status_uri}\nBT #{target_status2_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 2
expect(ids.size).to eq 2 expect(subject).to include [target_status.id, 'RT']
expect(ids).to include target_status.id expect(subject).to include [target_status2.id, 'BT']
expect(ids).to include target_status2.id
end end
end end
@ -47,8 +46,7 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:text) { "Hello #{target_status_uri}" } let(:text) { "Hello #{target_status_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 0
expect(ids.size).to eq 0
end end
end end
@ -67,7 +65,7 @@ RSpec.describe ProcessReferencesService, type: :service do
updated: '2022-01-22T16:00:00Z', updated: '2022-01-22T16:00:00Z',
} }
end end
let(:text) { 'BT https://example.com/test_post' } let(:text) { 'BT:https://example.com/test_post' }
before do 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' }) 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 end
it 'reference it' do it 'reference it' do
ids = subject expect(subject.size).to eq 1
expect(ids.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).to_not be_nil
expect(status.url).to eq 'https://example.com/test_post' expect(status.url).to eq 'https://example.com/test_post'
end end
@ -87,7 +85,7 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:fetch_remote) { false } let(:fetch_remote) { false }
it 'reference it' do it 'reference it' do
ids = subject ids = subject.pluck(0)
expect(ids.size).to eq 1 expect(ids.size).to eq 1
status = Status.find_by(id: ids[0]) 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" } let(:text) { "RT #{ActivityPub::TagManager.instance.uri_for(target_status)} BT https://example.com/test_post" }
it 'reference it' do it 'reference it' do
ids = subject expect(subject.size).to eq 2
expect(ids.size).to eq 2 expect(subject).to include [target_status.id, 'RT']
expect(ids).to include target_status.id 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 expect(status).to_not be_nil
end end
end end
@ -114,8 +112,7 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:text) { 'BT https://example.com/not_found' } let(:text) { 'BT https://example.com/not_found' }
it 'reference it' do it 'reference it' do
ids = subject expect(subject.size).to eq 0
expect(ids.size).to eq 0
end end
end end
end end
@ -144,9 +141,8 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:new_text) { "BT #{target_status_uri}" } let(:new_text) { "BT #{target_status_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 1
expect(ids.size).to eq 1 expect(subject).to include target_status.id
expect(ids).to include target_status.id
end end
end end
@ -155,10 +151,9 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:new_text) { "BT #{target_status_uri}\nBT #{target_status2_uri}" } let(:new_text) { "BT #{target_status_uri}\nBT #{target_status2_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 2
expect(ids.size).to eq 2 expect(subject).to include target_status.id
expect(ids).to include target_status.id expect(subject).to include target_status2.id
expect(ids).to include target_status2.id
end end
end end
@ -167,9 +162,8 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:new_text) { "BT #{target_status_uri}\nBT #{target_status_uri}" } let(:new_text) { "BT #{target_status_uri}\nBT #{target_status_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 1
expect(ids.size).to eq 1 expect(subject).to include target_status.id
expect(ids).to include target_status.id
end end
end end
@ -178,8 +172,7 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:new_text) { 'Hello' } let(:new_text) { 'Hello' }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 0
expect(ids.size).to eq 0
end end
end end
@ -188,9 +181,8 @@ RSpec.describe ProcessReferencesService, type: :service do
let(:new_text) { "BT #{target_status2_uri}" } let(:new_text) { "BT #{target_status2_uri}" }
it 'post status' do it 'post status' do
ids = subject expect(subject.size).to eq 1
expect(ids.size).to eq 1 expect(subject).to include target_status2.id
expect(ids).to include target_status2.id
end end
end end
end end