Add setting column and its serializer to Account

This commit is contained in:
noellabo 2022-02-08 13:49:39 +09:00
parent 1877a1dff1
commit 324d402d70
7 changed files with 62 additions and 3 deletions

View file

@ -26,6 +26,7 @@ module ContextHelper
suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
quote_uri: { 'fedibird' => 'http://fedibird.com/ns#', 'quoteUri' => 'fedibird:quoteUri' },
expiry: { 'fedibird' => 'http://fedibird.com/ns#', 'expiry' => 'fedibird:expiry' },
other_setting: { 'fedibird' => 'http://fedibird.com/ns#', 'otherSetting' => 'fedibird:otherSetting' },
}.freeze
def full_context

View file

@ -47,6 +47,7 @@
# devices_url :string
# suspension_origin :integer
# sensitized_at :datetime
# settings :jsonb default("{}"), not null
#
class Account < ApplicationRecord
@ -396,6 +397,14 @@ class Account < ApplicationRecord
ActionController::Base.helpers.strip_tags(note)
end
def settings
self[:settings].class == String ? {} : self[:settings]
end
def other_settings
settings
end
class Field < ActiveModelSerializers::Model
attributes :name, :value, :verified_at, :account

View file

@ -7,7 +7,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer
context_extensions :manually_approves_followers, :featured, :also_known_as,
:moved_to, :property_value, :identity_proof,
:discoverable, :olm, :suspended
:discoverable, :olm, :suspended, :other_setting
attributes :id, :type, :following, :followers,
:inbox, :outbox, :featured, :featured_tags,
@ -25,6 +25,8 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer
attribute :also_known_as, if: :also_known_as?
attribute :suspended, if: :suspended?
has_many :virtual_other_settings, key: :other_setting
class EndpointsSerializer < ActivityPub::Serializer
include RoutingHelper
@ -162,6 +164,16 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer
object.created_at.midnight.iso8601
end
def virtual_other_settings
object.other_settings.map do |k, v|
{
type: 'PropertyValue',
name: k,
value: v,
}
end
end
class CustomEmojiSerializer < ActivityPub::EmojiSerializer
end

View file

@ -22,6 +22,7 @@ class REST::AccountSerializer < ActiveModel::Serializer
end
has_many :fields
has_many :other_settings
def id
object.id.to_s
@ -91,6 +92,10 @@ class REST::AccountSerializer < ActiveModel::Serializer
object.suspended? ? [] : object.fields
end
def other_settings
object.suspended? ? [] : object.other_settings
end
def suspended
object.suspended?
end

View file

@ -98,6 +98,7 @@ class ActivityPub::ProcessAccountService < BaseService
@account.note = @json['summary'] || ''
@account.locked = @json['manuallyApprovesFollowers'] || false
@account.fields = property_values || {}
@account.settings = defer_settings.merge(other_settings || {})
@account.also_known_as = as_array(@json['alsoKnownAs'] || []).map { |item| value_or_id(item) }
@account.discoverable = @json['discoverable'] || false
end
@ -211,6 +212,18 @@ class ActivityPub::ProcessAccountService < BaseService
as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') }
end
DEFER_SETTINGS_KEYS = %w(
).freeze
def defer_settings
(@account.settings || {}).select { |key, _| DEFER_SETTINGS_KEYS.include?(key) }
end
def other_settings
return unless @json['otherSetting'].is_a?(Array)
@json['otherSetting'].each_with_object({}) { |v, h| h.merge!({v['name'] => v['value']}) if v['type'] == 'PropertyValue' }
end
def mismatching_origin?(url)
needle = Addressable::URI.parse(url).host
haystack = Addressable::URI.parse(@uri).host

View file

@ -0,0 +1,17 @@
require Rails.root.join('lib', 'mastodon', 'migration_helpers')
class AddSettingsToAccounts < ActiveRecord::Migration[6.1]
include Mastodon::MigrationHelpers
disable_ddl_transaction!
def up
safety_assured { add_column_with_default :accounts, :settings, :jsonb, default: '{}', allow_null: false }
safety_assured { add_index :accounts, :settings, using: 'gin', algorithm: :concurrently, name: :index_accounts_on_settings }
end
def down
remove_index :accounts, name: :index_accounts_on_settings
remove_column :accounts, :settings
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_12_13_083734) do
ActiveRecord::Schema.define(version: 2022_02_02_115057) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -131,7 +131,7 @@ ActiveRecord::Schema.define(version: 2021_12_13_083734) do
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_account_statuses_cleanup_policies_on_account_id"
end
create_table "account_subscribes", force: :cascade do |t|
t.bigint "account_id"
t.bigint "target_account_id"
@ -207,9 +207,11 @@ ActiveRecord::Schema.define(version: 2021_12_13_083734) do
t.string "devices_url"
t.integer "suspension_origin"
t.datetime "sensitized_at"
t.jsonb "settings", default: "{}", null: false
t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin
t.index "lower((username)::text), COALESCE(lower((domain)::text), ''::text)", name: "index_accounts_on_username_and_domain_lower", unique: true
t.index ["moved_to_account_id"], name: "index_accounts_on_moved_to_account_id"
t.index ["settings"], name: "index_accounts_on_settings", using: :gin
t.index ["uri"], name: "index_accounts_on_uri"
t.index ["url"], name: "index_accounts_on_url"
end