discourse/app/controllers/admin/embeddable_hosts_controller.rb
Natalie Tay 9e99066b07
DEV: Expand top_tags, topic.tags, etc, to return an array of tag objects instead of tag names (#36678)
Currently in several endpoints, we return an array of strings for tags.
Our goal with this PR is to expand array tag name strings to an array of
tag objects.

#### before: Tags were returned as string arrays
```
{ "tags": ["support", "bug-report"] }
```

#### after: Tags are returned as object arrays
```
{ "tags": [{"id": 12, "name": "support", "slug": "support"}, {"id": 13, "name": "bug-report", "slug": "bug-report"}] }
```

This allows us to start referencing tags by their ids, and return more
information for a tag for future features.

This commit involves updating several areas:
- topic lists (/latest.json, /top.json, /c/:category/:id.json, etc, for
`top_tags`)
- tag chooser components (`MiniTagChooser`, `TagDrop`, etc)
- topic view (/t/:id.json)
- tag groups (/tag_groups.json, tags, parent_tag)
- category settings
- staff action logs
- synonyms
- ...

APIs that reference tags based on their names will still be supported
with a deprecation warning. Moving on, we will reference them using
their tag ids.
2026-02-02 10:03:02 +08:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class Admin::EmbeddableHostsController < Admin::AdminController
def create
save_host(EmbeddableHost.new, :create)
end
def update
host = EmbeddableHost.where(id: params[:id]).first
save_host(host, :update)
end
def destroy
host = EmbeddableHost.where(id: params[:id]).first
host.destroy
StaffActionLogger.new(current_user).log_embeddable_host(
host,
UserHistory.actions[:embeddable_host_destroy],
)
render json: success_json
end
protected
def save_host(host, action)
host.host = params[:embeddable_host][:host]
host.allowed_paths = params[:embeddable_host][:allowed_paths]
host.category_id = params[:embeddable_host][:category_id]
host.category_id = SiteSetting.uncategorized_category_id if host.category.blank?
username = params[:embeddable_host][:user]
if username.blank?
host.user = nil
else
host.user = User.find_by_username(username)
end
ActiveRecord::Base.transaction do
if host.save
manage_tags(host, params[:embeddable_host][:tags])
changes = host.saved_changes if action == :update
StaffActionLogger.new(current_user).log_embeddable_host(
host,
UserHistory.actions[:"embeddable_host_#{action}"],
changes: changes,
)
render_serialized(
host,
EmbeddableHostSerializer,
root: "embeddable_host",
rest_serializer: true,
)
else
render_json_error(host)
raise ActiveRecord::Rollback
end
end
end
def manage_tags(host, tag_ids)
tag_ids.blank? ? host.tags.clear : host.tags = Tag.where(id: tag_ids)
end
end