Add language attribute to collections (#37549)
This commit is contained in:
parent
a1c17fef3a
commit
7cdc05b9a8
@ -81,11 +81,11 @@ class Api::V1Alpha::CollectionsController < Api::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def collection_creation_params
|
def collection_creation_params
|
||||||
params.permit(:name, :description, :sensitive, :discoverable, :tag_name, account_ids: [])
|
params.permit(:name, :description, :language, :sensitive, :discoverable, :tag_name, account_ids: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def collection_update_params
|
def collection_update_params
|
||||||
params.permit(:name, :description, :sensitive, :discoverable, :tag_name)
|
params.permit(:name, :description, :language, :sensitive, :discoverable, :tag_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_feature_enabled
|
def check_feature_enabled
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
# description :text not null
|
# description :text not null
|
||||||
# discoverable :boolean not null
|
# discoverable :boolean not null
|
||||||
# item_count :integer default(0), not null
|
# item_count :integer default(0), not null
|
||||||
|
# language :string
|
||||||
# local :boolean not null
|
# local :boolean not null
|
||||||
# name :string not null
|
# name :string not null
|
||||||
# original_number_of_items :integer
|
# original_number_of_items :integer
|
||||||
@ -36,6 +37,7 @@ class Collection < ApplicationRecord
|
|||||||
presence: true,
|
presence: true,
|
||||||
numericality: { greater_than_or_equal: 0 },
|
numericality: { greater_than_or_equal: 0 },
|
||||||
if: :remote?
|
if: :remote?
|
||||||
|
validates :language, language: { if: :local?, allow_nil: true }
|
||||||
validate :tag_is_usable
|
validate :tag_is_usable
|
||||||
validate :items_do_not_exceed_limit
|
validate :items_do_not_exceed_limit
|
||||||
|
|
||||||
|
|||||||
@ -17,9 +17,12 @@ class ActivityPub::FeaturedCollectionSerializer < ActivityPub::Serializer
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :id, :type, :total_items, :name, :summary, :attributed_to,
|
attributes :id, :type, :total_items, :name, :attributed_to,
|
||||||
:sensitive, :discoverable, :published, :updated
|
:sensitive, :discoverable, :published, :updated
|
||||||
|
|
||||||
|
attribute :summary, unless: :language_present?
|
||||||
|
attribute :summary_map, if: :language_present?
|
||||||
|
|
||||||
has_one :tag, key: :topic, serializer: ActivityPub::NoteSerializer::TagSerializer
|
has_one :tag, key: :topic, serializer: ActivityPub::NoteSerializer::TagSerializer
|
||||||
|
|
||||||
has_many :collection_items, key: :ordered_items, serializer: FeaturedItemSerializer
|
has_many :collection_items, key: :ordered_items, serializer: FeaturedItemSerializer
|
||||||
@ -36,6 +39,10 @@ class ActivityPub::FeaturedCollectionSerializer < ActivityPub::Serializer
|
|||||||
object.description
|
object.description
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def summary_map
|
||||||
|
{ object.language => object.description }
|
||||||
|
end
|
||||||
|
|
||||||
def attributed_to
|
def attributed_to
|
||||||
ActivityPub::TagManager.instance.uri_for(object.account)
|
ActivityPub::TagManager.instance.uri_for(object.account)
|
||||||
end
|
end
|
||||||
@ -51,4 +58,8 @@ class ActivityPub::FeaturedCollectionSerializer < ActivityPub::Serializer
|
|||||||
def updated
|
def updated
|
||||||
object.updated_at.iso8601
|
object.updated_at.iso8601
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def language_present?
|
||||||
|
object.language.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class REST::BaseCollectionSerializer < ActiveModel::Serializer
|
class REST::BaseCollectionSerializer < ActiveModel::Serializer
|
||||||
attributes :id, :uri, :name, :description, :local, :sensitive,
|
attributes :id, :uri, :name, :description, :language, :local, :sensitive,
|
||||||
:discoverable, :item_count, :created_at, :updated_at
|
:discoverable, :item_count, :created_at, :updated_at
|
||||||
|
|
||||||
belongs_to :tag, serializer: REST::StatusSerializer::TagSerializer
|
belongs_to :tag, serializer: REST::StatusSerializer::TagSerializer
|
||||||
|
|||||||
7
db/migrate/20260119153538_add_language_to_collections.rb
Normal file
7
db/migrate/20260119153538_add_language_to_collections.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddLanguageToCollections < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
add_column :collections, :language, :string
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -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[8.0].define(version: 2026_01_15_153219) do
|
ActiveRecord::Schema[8.0].define(version: 2026_01_19_153538) 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 "pg_catalog.plpgsql"
|
enable_extension "pg_catalog.plpgsql"
|
||||||
|
|
||||||
@ -382,6 +382,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_15_153219) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.integer "item_count", default: 0, null: false
|
t.integer "item_count", default: 0, null: false
|
||||||
|
t.string "language"
|
||||||
t.index ["account_id"], name: "index_collections_on_account_id"
|
t.index ["account_id"], name: "index_collections_on_account_id"
|
||||||
t.index ["tag_id"], name: "index_collections_on_tag_id"
|
t.index ["tag_id"], name: "index_collections_on_tag_id"
|
||||||
end
|
end
|
||||||
|
|||||||
@ -16,12 +16,18 @@ RSpec.describe Collection do
|
|||||||
|
|
||||||
it { is_expected.to_not allow_value(nil).for(:discoverable) }
|
it { is_expected.to_not allow_value(nil).for(:discoverable) }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value('en').for(:language) }
|
||||||
|
|
||||||
|
it { is_expected.to_not allow_value('randomstuff').for(:language) }
|
||||||
|
|
||||||
context 'when collection is remote' do
|
context 'when collection is remote' do
|
||||||
subject { Fabricate.build :collection, local: false }
|
subject { Fabricate.build :collection, local: false }
|
||||||
|
|
||||||
it { is_expected.to validate_presence_of(:uri) }
|
it { is_expected.to validate_presence_of(:uri) }
|
||||||
|
|
||||||
it { is_expected.to validate_presence_of(:original_number_of_items) }
|
it { is_expected.to validate_presence_of(:original_number_of_items) }
|
||||||
|
|
||||||
|
it { is_expected.to allow_value('randomstuff').for(:language) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when using a hashtag as category' do
|
context 'when using a hashtag as category' do
|
||||||
|
|||||||
@ -115,6 +115,7 @@ RSpec.describe 'Api::V1Alpha::Collections', feature: :collections do
|
|||||||
{
|
{
|
||||||
name: 'Low-traffic bots',
|
name: 'Low-traffic bots',
|
||||||
description: 'Really nice bots, please follow',
|
description: 'Really nice bots, please follow',
|
||||||
|
language: 'en',
|
||||||
sensitive: '0',
|
sensitive: '0',
|
||||||
discoverable: '1',
|
discoverable: '1',
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,4 +45,20 @@ RSpec.describe ActivityPub::FeaturedCollectionSerializer do
|
|||||||
'updated' => match_api_datetime_format,
|
'updated' => match_api_datetime_format,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when a language is set' do
|
||||||
|
before do
|
||||||
|
collection.language = 'en'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses "summaryMap" to include the language' do
|
||||||
|
expect(subject).to include({
|
||||||
|
'summaryMap' => {
|
||||||
|
'en' => 'These are really amazing',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(subject).to_not have_key('summary')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,6 +18,7 @@ RSpec.describe REST::CollectionSerializer do
|
|||||||
id: 2342,
|
id: 2342,
|
||||||
name: 'Exquisite follows',
|
name: 'Exquisite follows',
|
||||||
description: 'Always worth a follow',
|
description: 'Always worth a follow',
|
||||||
|
language: 'en',
|
||||||
local: true,
|
local: true,
|
||||||
sensitive: true,
|
sensitive: true,
|
||||||
discoverable: false,
|
discoverable: false,
|
||||||
@ -31,6 +32,7 @@ RSpec.describe REST::CollectionSerializer do
|
|||||||
'id' => '2342',
|
'id' => '2342',
|
||||||
'name' => 'Exquisite follows',
|
'name' => 'Exquisite follows',
|
||||||
'description' => 'Always worth a follow',
|
'description' => 'Always worth a follow',
|
||||||
|
'language' => 'en',
|
||||||
'local' => true,
|
'local' => true,
|
||||||
'sensitive' => true,
|
'sensitive' => true,
|
||||||
'discoverable' => false,
|
'discoverable' => false,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user