# frozen_string_literal: true class Api::V1Alpha::CollectionsController < Api::BaseController include Authorization DEFAULT_COLLECTIONS_LIMIT = 40 rescue_from ActiveRecord::RecordInvalid, Mastodon::ValidationError do |e| render json: { error: ValidationErrorFormatter.new(e).as_json }, status: 422 end before_action :check_feature_enabled before_action -> { authorize_if_got_token! :read, :'read:collections' }, only: [:index, :show] before_action -> { doorkeeper_authorize! :write, :'write:collections' }, only: [:create, :update, :destroy] before_action :require_user!, only: [:create, :update, :destroy] before_action :set_account, only: [:index] before_action :set_collections, only: [:index] before_action :set_collection, only: [:show, :update, :destroy] after_action :insert_pagination_headers, only: [:index] after_action :verify_authorized def index cache_if_unauthenticated! authorize Collection, :index? render json: @collections, each_serializer: REST::BaseCollectionSerializer end def show cache_if_unauthenticated! authorize @collection, :show? render json: @collection, serializer: REST::CollectionSerializer end def create authorize Collection, :create? @collection = CreateCollectionService.new.call(collection_creation_params, current_user.account) render json: @collection, serializer: REST::CollectionSerializer end def update authorize @collection, :update? @collection.update!(collection_update_params) # TODO: Create a service for this to federate changes render json: @collection, serializer: REST::CollectionSerializer end def destroy authorize @collection, :destroy? @collection.destroy head 200 end private def set_account @account = Account.find(params[:account_id]) end def set_collections @collections = @account.collections .with_tag .with_item_count .order(created_at: :desc) .offset(offset_param) .limit(limit_param(DEFAULT_COLLECTIONS_LIMIT)) end def set_collection @collection = Collection.find(params[:id]) end def collection_creation_params params.permit(:name, :description, :sensitive, :discoverable, :tag_name, account_ids: []) end def collection_update_params params.permit(:name, :description, :sensitive, :discoverable, :tag_name) end def check_feature_enabled raise ActionController::RoutingError unless Mastodon::Feature.collections_enabled? end def next_path return unless records_continue? api_v1_alpha_account_collections_url(@account, pagination_params(offset: offset_param + limit_param(DEFAULT_COLLECTIONS_LIMIT))) end def prev_path return if offset_param.zero? api_v1_alpha_account_collections_url(@account, pagination_params(offset: offset_param - limit_param(DEFAULT_COLLECTIONS_LIMIT))) end def records_continue? ((offset_param * limit_param(DEFAULT_COLLECTIONS_LIMIT)) + @collections.size) < @account.collections.size end def offset_param params[:offset].to_i end end