Get the tags into the database with a raw curl.

pull/2675/head
JS Moore 2024-03-09 11:52:38 -05:00
parent bb3ad2b2a9
commit cc9a5c9654
No known key found for this signature in database
GPG Key ID: E313C2B57F81A0BA
8 changed files with 143 additions and 4 deletions

View File

@ -32,6 +32,7 @@ class Api::V1::Lists::AccountsController < Api::BaseController
private
def set_list
Rails.logger.warn "This is concerning! #{current_account} and #{params}"
@list = List.where(account: current_account).find(params[:list_id])
end

View File

@ -0,0 +1,94 @@
# frozen_string_literal: true
class Api::V1::Lists::TagsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:lists' }, only: [:show]
before_action -> { doorkeeper_authorize! :write, :'write:lists' }, except: [:show]
before_action :require_user!
before_action :set_list
after_action :insert_pagination_headers, only: :show
def show
@tags = load_tags
render json: @tags, each_serializer: REST::TagSerializer
end
def create
ApplicationRecord.transaction do
list_tags.each do |tag|
@list.tags << tag
end
end
render_empty
end
def destroy
ListTag.where(list: @list, tag_id: tag_id).destroy_all
render_empty
end
private
def set_list
Rails.logger.warn "This is tagged concerning! #{current_account} and #{params}"
@list = List.where(account: current_account).find(params[:list_id])
end
def load_tags
if unlimited?
@list.tags.all
else
@list.tags.paginate_by_max_id(limit_param(DEFAULT_TAGS_LIMIT), params[:max_id], params[:since_id])
end
end
def list_tags
Tag.find(tag_ids)
end
def tag_ids
Array(resource_params[:tag_ids])
end
def resource_params
params.permit(tag_ids: [])
end
def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
def next_path
return if unlimited?
api_v1_list_tags_url pagination_params(max_id: pagination_max_id) if records_continue?
end
def prev_path
return if unlimited?
api_v1_list_tags_url pagination_params(since_id: pagination_since_id) unless @tags.empty?
end
def pagination_max_id
@tags.last.id
end
def pagination_since_id
@tags.first.id
end
def records_continue?
@tags.size == limit_param(DEFAULT_TAGS_LIMIT)
end
def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end
def unlimited?
params[:limit] == '0'
end
end

View File

@ -25,6 +25,9 @@ class List < ApplicationRecord
has_many :list_accounts, inverse_of: :list, dependent: :destroy
has_many :accounts, through: :list_accounts
has_many :list_tags, inverse_of: :list, dependent: :destroy
has_many :tags, through: :list_tags
validates :title, presence: true
validates_each :account_id, on: :create do |record, _attr, value|

20
app/models/list_tag.rb Normal file
View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: list_tag
#
# id :bigint(8) not null, primary key
# list_id :bigint(8) not null
# tag_id :bigint(8) not null
#
class ListTag < ApplicationRecord
belongs_to :list
belongs_to :tag
validates :tag_id, uniqueness: { scope: :list_id }
private
end

View File

@ -14,7 +14,6 @@
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# admin :boolean default(FALSE), not null
# confirmation_token :string
# confirmed_at :datetime
# confirmation_sent_at :datetime
@ -29,7 +28,6 @@
# otp_backup_codes :string is an Array
# account_id :bigint(8) not null
# disabled :boolean default(FALSE), not null
# moderator :boolean default(FALSE), not null
# invite_id :bigint(8)
# chosen_languages :string is an Array
# created_by_application_id :bigint(8)

View File

@ -204,7 +204,8 @@ namespace :api, format: false do
resources :followed_tags, only: [:index]
resources :lists, only: [:index, :create, :show, :update, :destroy] do
resource :accounts, only: [:show, :create, :destroy], controller: 'lists/accounts'
resource :accounts, only: [:show, :create, :destroy], controller: 'lists/accounts'
resource :tags, only: [:show, :create, :destroy], controller: 'lists/tags'
end
namespace :featured_tags do

View File

@ -0,0 +1,11 @@
class ListTags < ActiveRecord::Migration[7.1]
def change
create_table :list_tags do |t|
t.belongs_to :list, foreign_key: { on_delete: :cascade }, null: false
t.belongs_to :tag, foreign_key: { on_delete: :cascade }, null: false
end
add_index :list_tags, [:tag_id, :list_id], unique: true
add_index :list_tags, [:list_id, :tag_id]
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_01_11_033014) do
ActiveRecord::Schema[7.1].define(version: 2024_03_09_162900) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -584,6 +584,15 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_11_033014) do
t.index ["list_id", "account_id"], name: "index_list_accounts_on_list_id_and_account_id"
end
create_table "list_tags", force: :cascade do |t|
t.bigint "list_id", null: false
t.bigint "tag_id", null: false
t.index ["list_id", "tag_id"], name: "index_list_tags_on_list_id_and_tag_id"
t.index ["list_id"], name: "index_list_tags_on_list_id"
t.index ["tag_id", "list_id"], name: "index_list_tags_on_tag_id_and_list_id", unique: true
t.index ["tag_id"], name: "index_list_tags_on_tag_id"
end
create_table "lists", force: :cascade do |t|
t.bigint "account_id", null: false
t.string "title", default: "", null: false
@ -1248,6 +1257,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_11_033014) do
add_foreign_key "list_accounts", "follow_requests", on_delete: :cascade
add_foreign_key "list_accounts", "follows", on_delete: :cascade
add_foreign_key "list_accounts", "lists", on_delete: :cascade
add_foreign_key "list_tags", "lists", on_delete: :cascade
add_foreign_key "list_tags", "tags", on_delete: :cascade
add_foreign_key "lists", "accounts", on_delete: :cascade
add_foreign_key "login_activities", "users", on_delete: :cascade
add_foreign_key "markers", "users", on_delete: :cascade