From 41f61be3ba24be0fd9bad7468cdf1cb536295ca9 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sat, 9 Nov 2024 15:24:05 +0100 Subject: [PATCH] Remove AMS and replace with simple json rendering --- Gemfile | 2 - Gemfile.lock | 9 --- app/controllers/albums_controller.rb | 29 ++++++-- app/controllers/application_controller.rb | 2 - app/controllers/artists_controller.rb | 19 +++-- app/controllers/auth_tokens_controller.rb | 17 ++++- .../codec_conversions_controller.rb | 13 ++-- app/controllers/codecs_controller.rb | 13 ++-- app/controllers/concerns/image_rendering.rb | 42 +++++++++++ app/controllers/cover_filenames_controller.rb | 11 ++- app/controllers/genres_controller.rb | 15 ++-- app/controllers/image_types_controller.rb | 13 ++-- app/controllers/labels_controller.rb | 15 ++-- app/controllers/locations_controller.rb | 11 ++- app/controllers/playlists_controller.rb | 13 ++-- app/controllers/plays_controller.rb | 14 +++- app/controllers/rescans_controller.rb | 11 ++- app/controllers/tracks_controller.rb | 71 ++++++++++++++++--- app/controllers/users_controller.rb | 13 ++-- app/serializers/album_artist_serializer.rb | 27 ------- app/serializers/album_label_serializer.rb | 23 ------ app/serializers/album_serializer.rb | 67 ----------------- app/serializers/artist_serializer.rb | 61 ---------------- app/serializers/auth_token_serializer.rb | 23 ------ .../auth_token_with_secret_serializer.rb | 3 - .../codec_conversion_serializer.rb | 21 ------ app/serializers/codec_serializer.rb | 15 ---- app/serializers/cover_filename_serializer.rb | 14 ---- app/serializers/genre_serializer.rb | 16 ----- app/serializers/image_type_serializer.rb | 15 ---- app/serializers/label_serializer.rb | 15 ---- app/serializers/location_serializer.rb | 14 ---- app/serializers/play_serializer.rb | 23 ------ app/serializers/play_stat_serializer.rb | 3 - app/serializers/playlist_serializer.rb | 24 ------- app/serializers/rescan_runner_serializer.rb | 23 ------ app/serializers/track_artist_serializer.rb | 28 -------- app/serializers/track_moderator_serializer.rb | 15 ---- app/serializers/track_serializer.rb | 46 ------------ app/serializers/user_serializer.rb | 16 ----- gemset.nix | 32 --------- 41 files changed, 258 insertions(+), 599 deletions(-) create mode 100644 app/controllers/concerns/image_rendering.rb delete mode 100644 app/serializers/album_artist_serializer.rb delete mode 100644 app/serializers/album_label_serializer.rb delete mode 100644 app/serializers/album_serializer.rb delete mode 100644 app/serializers/artist_serializer.rb delete mode 100644 app/serializers/auth_token_serializer.rb delete mode 100644 app/serializers/auth_token_with_secret_serializer.rb delete mode 100644 app/serializers/codec_conversion_serializer.rb delete mode 100644 app/serializers/codec_serializer.rb delete mode 100644 app/serializers/cover_filename_serializer.rb delete mode 100644 app/serializers/genre_serializer.rb delete mode 100644 app/serializers/image_type_serializer.rb delete mode 100644 app/serializers/label_serializer.rb delete mode 100644 app/serializers/location_serializer.rb delete mode 100644 app/serializers/play_serializer.rb delete mode 100644 app/serializers/play_stat_serializer.rb delete mode 100644 app/serializers/playlist_serializer.rb delete mode 100644 app/serializers/rescan_runner_serializer.rb delete mode 100644 app/serializers/track_artist_serializer.rb delete mode 100644 app/serializers/track_moderator_serializer.rb delete mode 100644 app/serializers/track_serializer.rb delete mode 100644 app/serializers/user_serializer.rb diff --git a/Gemfile b/Gemfile index 6a21b4ba..6d3a247b 100644 --- a/Gemfile +++ b/Gemfile @@ -9,8 +9,6 @@ gem 'rails', '~> 7.2' gem 'pg', '>= 1.2.3', '< 2.0' # Use Puma as the app server gem 'puma', '~> 6.4' -# Use ActiveModelSerializer for serializing to JSON -gem 'active_model_serializers', '~> 0.10' # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1' diff --git a/Gemfile.lock b/Gemfile.lock index 2b567f7d..70c71eaf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -45,11 +45,6 @@ GEM erubi (~> 1.11) rails-dom-testing (~> 2.2) rails-html-sanitizer (~> 1.6) - active_model_serializers (0.10.14) - actionpack (>= 4.1) - activemodel (>= 4.1) - case_transform (>= 0.2) - jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (7.2.2) activesupport (= 7.2.2) globalid (>= 0.3.6) @@ -88,8 +83,6 @@ GEM brakeman (6.2.2) racc builder (3.3.0) - case_transform (0.2) - activesupport concurrent-ruby (1.3.4) connection_pool (2.4.1) crass (1.0.6) @@ -135,7 +128,6 @@ GEM rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.5) - jsonapi-renderer (0.2.2) language_server-protocol (3.17.0.3) logger (1.6.1) loofah (2.23.1) @@ -290,7 +282,6 @@ PLATFORMS ruby DEPENDENCIES - active_model_serializers (~> 0.10) annotaterb (~> 4.13) bcrypt (~> 3.1) bootsnap (~> 1.18.4) diff --git a/app/controllers/albums_controller.rb b/app/controllers/albums_controller.rb index d77f5a3d..0fb2544f 100644 --- a/app/controllers/albums_controller.rb +++ b/app/controllers/albums_controller.rb @@ -1,4 +1,6 @@ class AlbumsController < ApplicationController + include ImageRendering + before_action :set_album, only: %i[show update destroy merge] has_scope :by_filter, as: 'filter' @@ -12,11 +14,12 @@ def index .includes(:album_artists, :album_labels, image: [{ image_attachment: :blob }, :image_type]) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@albums) - render json: @albums + + render json: @albums.map { |it| transform_album_for_json(it) } end def show - render json: @album + render json: transform_album_for_json(@album) end def create @@ -24,7 +27,7 @@ def create @album = Album.new(transformed_attributes) if @album.save - render json: @album, status: :created + render json: transform_album_for_json(@album), status: :created else render json: @album.errors, status: :unprocessable_entity end @@ -32,7 +35,7 @@ def create def update if @album.update(transformed_attributes) - render json: @album, status: :ok + render json: transform_album_for_json(@album), status: :ok else render json: @album.errors, status: :unprocessable_entity end @@ -90,4 +93,22 @@ def transformed_attributes attributes end + + def transform_album_for_json(album) + result = %i[id title normalized_title release review_comment edition edition_description created_at updated_at].index_with { |it| album.send(it) } + %i[image image100 image250 image500 image_type].each do |attr| + result[attr] = send(attr, album) + end + result[:album_artists] = album.album_artists.map { |it| transform_album_artist_for_json(it) } + result[:album_labels] = album.album_labels.map { |it| transform_album_label_for_json(it) } + result + end + + def transform_album_artist_for_json(album_artist) + %i[artist_id name normalized_name order separator].index_with { |attr| album_artist.send(attr) } + end + + def transform_album_label_for_json(album_label) + %i[label_id catalogue_number].index_with { |attr| album_label.send(attr) } + end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0f0dab5e..35921db4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -16,8 +16,6 @@ class ApplicationController < ActionController::API has_scope :sorted, default: nil, allow_blank: true, except: :stats - serialization_scope :url_options - protected def add_pagination_headers(collection) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 7a1911fa..b2c6dbf5 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -1,4 +1,6 @@ class ArtistsController < ApplicationController + include ImageRendering + before_action :set_artist, only: %i[show update destroy merge] has_scope :by_filter, as: 'filter' @@ -9,11 +11,12 @@ def index .includes(image: [{ image_attachment: :blob }, :image_type]) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@artists) - render json: @artists + + render json: @artists.map { |it| transform_artist_for_json(it) } end def show - render json: @artist + render json: transform_artist_for_json(@artist) end def create @@ -21,7 +24,7 @@ def create @artist = Artist.new(transformed_attributes) if @artist.save - render json: @artist, status: :created + render json: transform_artist_for_json(@artist), status: :created else render json: @artist.errors, status: :unprocessable_entity end @@ -29,7 +32,7 @@ def create def update if @artist.update(transformed_attributes) - render json: @artist, status: :ok + render json: transform_artist_for_json(@artist), status: :ok else render json: @artist.errors, status: :unprocessable_entity end @@ -76,4 +79,12 @@ def transformed_attributes end attributes end + + def transform_artist_for_json(artist) + result = %i[id name normalized_name review_comment created_at updated_at].index_with { |it| artist.send(it) } + %i[image image100 image250 image500 image_type].each do |attr| + result[attr] = send(attr, artist) + end + result + end end diff --git a/app/controllers/auth_tokens_controller.rb b/app/controllers/auth_tokens_controller.rb index 393bb0aa..b9578eb8 100644 --- a/app/controllers/auth_tokens_controller.rb +++ b/app/controllers/auth_tokens_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@auth_tokens) - render json: @auth_tokens + + render json: @auth_tokens.map { |it| transform_auth_token_for_json(it) } end def show - render json: @auth_token + render json: transform_auth_token_for_json(@auth_token) end def create @@ -31,7 +32,7 @@ def create ) if @auth_token.save - render json: @auth_token, serializer: AuthTokenWithSecretSerializer, status: :created + render json: transform_auth_token_for_json_with_secret(@auth_token), status: :created else render json: @auth_token.errors, status: :unprocessable_entity end @@ -47,4 +48,14 @@ def set_auth_token @auth_token = AuthToken.find(params[:id]) authorize @auth_token end + + def transform_auth_token_for_json(auth_token) + %i[id device_id user_id user_agent application].index_with { |it| auth_token.send(it) } + end + + def transform_auth_token_for_json_with_secret(auth_token) + result = transform_auth_token_for_json(auth_token) + result[:secret] = auth_token.secret + result + end end diff --git a/app/controllers/codec_conversions_controller.rb b/app/controllers/codec_conversions_controller.rb index 7ae9369a..3265509b 100644 --- a/app/controllers/codec_conversions_controller.rb +++ b/app/controllers/codec_conversions_controller.rb @@ -9,11 +9,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@codec_conversions) - render json: @codec_conversions + + render json: @codec_conversions.map { |it| transform_codec_conversion_for_json(it) } end def show - render json: @codec_conversion + render json: transform_codec_conversion_for_json(@codec_conversion) end def create @@ -21,7 +22,7 @@ def create @codec_conversion = CodecConversion.new(permitted_attributes(CodecConversion)) if @codec_conversion.save - render json: @codec_conversion, status: :created + render json: transform_codec_conversion_for_json(@codec_conversion), status: :created else render json: @codec_conversion.errors, status: :unprocessable_entity end @@ -29,7 +30,7 @@ def create def update if @codec_conversion.update(permitted_attributes(CodecConversion)) - render json: @codec_conversion, status: :ok + render json: transform_codec_conversion_for_json(@codec_conversion), status: :ok else render json: @codec_conversion.errors, status: :unprocessable_entity end @@ -45,4 +46,8 @@ def set_codec_conversion @codec_conversion = CodecConversion.find(params[:id]) authorize @codec_conversion end + + def transform_codec_conversion_for_json(codec_conversion) + %i[id name ffmpeg_params resulting_codec_id].index_with { |it| codec_conversion.send(it) } + end end diff --git a/app/controllers/codecs_controller.rb b/app/controllers/codecs_controller.rb index 6010a3be..0d11c507 100644 --- a/app/controllers/codecs_controller.rb +++ b/app/controllers/codecs_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@codecs) - render json: @codecs + + render json: @codecs.map { |it| transform_codec_for_json(it) } end def show - render json: @codec + render json: transform_codec_for_json(@codec) end def create @@ -19,7 +20,7 @@ def create @codec = Codec.new(permitted_attributes(Codec)) if @codec.save - render json: @codec, status: :created + render json: transform_codec_for_json(@codec), status: :created else render json: @codec.errors, status: :unprocessable_entity end @@ -27,7 +28,7 @@ def create def update if @codec.update(permitted_attributes(@codec)) - render json: @codec, status: :ok + render json: transform_codec_for_json(@codec), status: :ok else render json: @codec.errors, status: :unprocessable_entity end @@ -43,4 +44,8 @@ def set_codec @codec = Codec.find(params[:id]) authorize @codec end + + def transform_codec_for_json(codec) + %i[id mimetype extension].index_with { |it| codec.send(it) } + end end diff --git a/app/controllers/concerns/image_rendering.rb b/app/controllers/concerns/image_rendering.rb new file mode 100644 index 00000000..68cfcb6f --- /dev/null +++ b/app/controllers/concerns/image_rendering.rb @@ -0,0 +1,42 @@ +module ImageRendering + extend ActiveSupport::Concern + + included do + private + + def image(object) + return nil if object.image.blank? + return nil unless object.image.image.variable? + + rails_blob_url(object.image.image) + end + + def image100(object) + return nil if object.image.blank? + return nil unless object.image.image.variable? + + rails_representation_url(object.image.image.variant(resize_to_cover: [100, 100])) + end + + def image250(object) + return nil if object.image.blank? + return nil unless object.image.image.variable? + + rails_representation_url(object.image.image.variant(resize_to_cover: [250, 250])) + end + + def image500(object) + return nil if object.image.blank? + return nil unless object.image.image.variable? + + rails_representation_url(object.image.image.variant(resize_to_cover: [500, 500])) + end + + def image_type(object) + return nil if object.image.blank? + return nil unless object.image.image.variable? + + object.image.image_type.mimetype + end + end +end diff --git a/app/controllers/cover_filenames_controller.rb b/app/controllers/cover_filenames_controller.rb index 0d1eb603..ce8402d5 100644 --- a/app/controllers/cover_filenames_controller.rb +++ b/app/controllers/cover_filenames_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@cover_filenames) - render json: @cover_filenames + + render json: @cover_filenames.map { |it| transform_cover_filename_for_json(it) } end def show - render json: @cover_filename + render json: transform_cover_filename_for_json(@cover_filename) end def create @@ -19,7 +20,7 @@ def create @cover_filename = CoverFilename.new(permitted_attributes(CoverFilename)) if @cover_filename.save - render json: @cover_filename, status: :created + render json: transform_cover_filename_for_json(@cover_filename), status: :created else render json: @cover_filename.errors, status: :unprocessable_entity end @@ -35,4 +36,8 @@ def set_cover_filename @cover_filename = CoverFilename.find(params[:id]) authorize @cover_filename end + + def transform_cover_filename_for_json(cover_filename) + %i[id filename].index_with { |it| cover_filename.send(it) } + end end diff --git a/app/controllers/genres_controller.rb b/app/controllers/genres_controller.rb index ac14122a..ed2b8915 100644 --- a/app/controllers/genres_controller.rb +++ b/app/controllers/genres_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@genres) - render json: @genres + + render json: @genres.map { |it| transform_genre_for_json(it) } end def show - render json: @genre + render json: transform_genre_for_json(@genre) end def create @@ -19,7 +20,7 @@ def create @genre = Genre.new(permitted_attributes(Genre)) if @genre.save - render json: @genre, status: :created + render json: transform_genre_for_json(@genre), status: :created else render json: @genre.errors, status: :unprocessable_entity end @@ -27,7 +28,7 @@ def create def update if @genre.update(permitted_attributes(@genre)) - render json: @genre, status: :ok + render json: transform_genre_for_json(@genre), status: :ok else render json: @genre.errors, status: :unprocessable_entity end @@ -45,7 +46,7 @@ def destroy_empty def merge @genre.merge(Genre.find(params[:source_id])) # We don't do error handling here. The merge action isn't supposed to fail. - render json: @genre, status: :ok + render json: transform_genre_for_json(@genre), status: :ok end private @@ -54,4 +55,8 @@ def set_genre @genre = Genre.find(params[:id]) authorize @genre end + + def transform_genre_for_json(genre) + %i[id name normalized_name].index_with { |it| genre.send(it) } + end end diff --git a/app/controllers/image_types_controller.rb b/app/controllers/image_types_controller.rb index 34e33500..b411edc6 100644 --- a/app/controllers/image_types_controller.rb +++ b/app/controllers/image_types_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@image_types) - render json: @image_types + + render json: @image_types.map { |it| transform_image_type_for_json(it) } end def show - render json: @image_type + render json: transform_image_type_for_json(@image_type) end def create @@ -19,7 +20,7 @@ def create @image_type = ImageType.new(permitted_attributes(ImageType)) if @image_type.save - render json: @image_type, status: :created + render json: transform_image_type_for_json(@image_type), status: :created else render json: @image_type.errors, status: :unprocessable_entity end @@ -27,7 +28,7 @@ def create def update if @image_type.update(permitted_attributes(@image_type)) - render json: @image_type, status: :ok + render json: transform_image_type_for_json(@image_type), status: :ok else render json: @image_type.errors, status: :unprocessable_entity end @@ -43,4 +44,8 @@ def set_image_type @image_type = ImageType.find(params[:id]) authorize @image_type end + + def transform_image_type_for_json(image_type) + %i[id extension mimetype].index_with { |it| image_type.send(it) } + end end diff --git a/app/controllers/labels_controller.rb b/app/controllers/labels_controller.rb index ce757c9b..291d4c28 100644 --- a/app/controllers/labels_controller.rb +++ b/app/controllers/labels_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@labels) - render json: @labels + + render json: @labels.map { |it| transform_label_for_json(it) } end def show - render json: @label + render json: transform_label_for_json(@label) end def create @@ -19,7 +20,7 @@ def create @label = Label.new(permitted_attributes(Label)) if @label.save - render json: @label, status: :created + render json: transform_label_for_json(@label), status: :created else render json: @label.errors, status: :unprocessable_entity end @@ -27,7 +28,7 @@ def create def update if @label.update(permitted_attributes(@label)) - render json: @label, status: :ok + render json: transform_label_for_json(@label), status: :ok else render json: @label.errors, status: :unprocessable_entity end @@ -45,7 +46,7 @@ def destroy_empty def merge @label.merge(Label.find(params[:source_id])) # We don't do error handling here. The merge action isn't supposed to fail. - render json: @label, status: :ok + render json: transform_label_for_json(@label), status: :ok end private @@ -54,4 +55,8 @@ def set_label @label = Label.find(params[:id]) authorize @label end + + def transform_label_for_json(label) + %i[id name normalized_name].index_with { |it| label.send(it) } + end end diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index fc22533e..18252708 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@locations) - render json: @locations + + render json: @locations.map { |it| transform_location_for_json(it) } end def show - render json: @location + render json: transform_location_for_json(@location) end def create @@ -19,7 +20,7 @@ def create @location = Location.new(permitted_attributes(Location)) if @location.save - render json: @location, status: :created + render json: transform_location_for_json(@location), status: :created else render json: @location.errors, status: :unprocessable_entity end @@ -35,4 +36,8 @@ def set_location @location = Location.find(params[:id]) authorize @location end + + def transform_location_for_json(location) + %i[id path].index_with { |it| location.send(it) } + end end diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index 921acff8..892388cd 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -8,11 +8,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@playlists) - render json: @playlists + + render json: @playlists.map { |it| transform_playlist_for_json(it) } end def show - render json: @playlist + render json: transform_playlist_for_json(@playlist) end def create @@ -20,7 +21,7 @@ def create @playlist = Playlist.new(permitted_attributes(Playlist).merge({ user: current_user })) if @playlist.save - render json: @playlist, status: :created + render json: transform_playlist_for_json(@playlist), status: :created else render json: @playlist.errors, status: :unprocessable_entity end @@ -28,7 +29,7 @@ def create def update if @playlist.update(permitted_attributes(@playlist)) - render json: @playlist, status: :ok + render json: transform_playlist_for_json(@playlist), status: :ok else render json: @playlist.errors, status: :unprocessable_entity end @@ -50,4 +51,8 @@ def set_playlist @playlist = Playlist.find(params[:id]) authorize @playlist end + + def transform_playlist_for_json(playlist) + %i[id name description user_id playlist_type created_at updated_at item_ids access].index_with { |it| playlist.send(it) } + end end diff --git a/app/controllers/plays_controller.rb b/app/controllers/plays_controller.rb index 1a74f7a4..e033cd04 100644 --- a/app/controllers/plays_controller.rb +++ b/app/controllers/plays_controller.rb @@ -10,7 +10,7 @@ def index .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@plays) - render json: @plays, each_serializer: PlaySerializer + render json: @plays.map { |it| transform_play_for_json(it) } end def create @@ -18,7 +18,7 @@ def create @play = Play.new(transformed_attributes) if @play.save - render json: @play, status: :created + render json: transform_play_for_json(@play), status: :created else render json: @play.errors, status: :unprocessable_entity end @@ -34,7 +34,7 @@ def stats .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@stats) - render json: @stats, each_serializer: PlayStatSerializer + render json: @stats.map { |it| transform_play_stat_for_json(it) } end private @@ -42,4 +42,12 @@ def stats def transformed_attributes permitted_attributes(Play).merge(user: current_user) end + + def transform_play_for_json(play) + %i[id played_at track_id user_id].index_with { |it| play.send(it) } + end + + def transform_play_stat_for_json(play_stat) + %i[count track_id last_played_at total_length].index_with { |it| play_stat.send(it) } + end end diff --git a/app/controllers/rescans_controller.rb b/app/controllers/rescans_controller.rb index 2e40ac23..34d6998e 100644 --- a/app/controllers/rescans_controller.rb +++ b/app/controllers/rescans_controller.rb @@ -5,16 +5,17 @@ def index authorize RescanRunner @rescans = policy_scope(RescanRunner).paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@rescans) - render json: @rescans + + render json: @rescans.map { |it| transform_rescan_runner_for_json(it) } end def show - render json: @rescan + render json: transform_rescan_runner_for_json(@rescan) end def start @rescan.schedule - render json: @rescan + render json: transform_rescan_runner_for_json(@rescan) end def start_all @@ -28,4 +29,8 @@ def set_rescan @rescan = RescanRunner.find(params[:id]) authorize @rescan end + + def transform_rescan_runner_for_json(rescan_runner) + %i[id error_text warning_text processed running finished_at location_id].index_with { |it| rescan_runner.send(it) } + end end diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index 81014019..9b3655e7 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -15,11 +15,11 @@ def index .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@tracks) - render json: @tracks, each_serializer: serializer + render json: @tracks.map { |it| transform_track_for_json(it) } end def show - render json: @track, serializer: + render json: transform_track_for_json(@track) end def create @@ -27,7 +27,7 @@ def create @track = Track.new(transformed_attributes) if @track.save - render(json: @track, serializer:, status: :created) + render json: transform_track_for_json(@track), status: :created else render json: @track.errors, status: :unprocessable_entity end @@ -35,7 +35,7 @@ def create def update if @track.update(transformed_attributes) - render(json: @track, serializer:, status: :ok) + render json: transform_track_for_json(@track), status: :ok else render json: @track.errors, status: :unprocessable_entity end @@ -81,7 +81,7 @@ def download def merge @track.merge(Track.find(params[:source_id])) # We don't do error handling here. The merge action isn't supposed to fail. - render json: @track, status: :ok + render json: transform_track_for_json(@track), status: :ok end private @@ -130,10 +130,6 @@ def audio_with_stream(stream, mimetype, total_size) response.stream.close end - def serializer - current_user.moderator? ? TrackModeratorSerializer : TrackSerializer - end - def set_track @track = Track.find(params[:id]) authorize @track @@ -150,4 +146,61 @@ def transformed_attributes attributes end + + def transform_track_for_json(track) + if current_user.moderator? + transform_track_for_json_for_moderator(track) + else + transform_track_for_json_for_user(track) + end + end + + def transform_track_for_json_for_user(track) + result = %i[id title normalized_title number album_id review_comment created_at updated_at genre_ids audio_file_id].index_with { |it| track.send(it) } + %i[codec_id length bitrate location_id].each do |attr| + result[attr] = send(attr, track) + end + result[:track_artists] = track.track_artists.map { |it| transform_track_artist_for_json(it) } + result + end + + def transform_track_artist_for_json(track_artist) + %i[artist_id name normalized_name role order hidden].index_with { |it| track_artist.send(it) } + end + + def transform_track_for_json_for_moderator(track) + result = transform_track_for_json_for_user(track) + %i[filename sample_rate bit_depth].each do |attr| + result[attr] = send(attr, track) + end + result + end + + def codec_id(track) + track.audio_file&.codec_id + end + + def length(track) + track.audio_file&.length + end + + def bitrate(track) + track.audio_file&.bitrate + end + + def location_id(track) + track.audio_file&.location_id + end + + def filename(track) + track.audio_file&.filename + end + + def sample_rate(track) + track.audio_file&.sample_rate + end + + def bit_depth(track) + track.audio_file&.bit_depth + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1c79d0ca..c88e9905 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,11 +7,12 @@ def index .order(id: :asc) .paginate(page: params[:page], per_page: params[:per_page]) add_pagination_headers(@users) - render json: @users + + render json: @users.map { |it| transform_user_for_json(it) } end def show - render json: @user + render json: transform_user_for_json(@user) end def create @@ -19,7 +20,7 @@ def create @user = User.new(permitted_attributes(User)) if @user.save - render json: @user, status: :created + render json: transform_user_for_json(@user), status: :created else render json: @user.errors, status: :unprocessable_entity end @@ -35,7 +36,7 @@ def update end if @user.update(permitted_attributes(@user)) - render json: @user, status: :ok + render json: transform_user_for_json(@user), status: :ok else render json: @user.errors, status: :unprocessable_entity end @@ -51,4 +52,8 @@ def set_user @user = User.find(params[:id]) authorize @user end + + def transform_user_for_json(user) + %i[id name permission].index_with { |it| user.send(it) } + end end diff --git a/app/serializers/album_artist_serializer.rb b/app/serializers/album_artist_serializer.rb deleted file mode 100644 index fcd4e5fb..00000000 --- a/app/serializers/album_artist_serializer.rb +++ /dev/null @@ -1,27 +0,0 @@ -# == Schema Information -# -# Table name: album_artists -# -# id :bigint not null, primary key -# name :string not null -# normalized_name :string not null -# order :integer not null -# separator :string -# album_id :bigint not null -# artist_id :bigint not null -# -# Indexes -# -# index_album_artists_on_album_id (album_id) -# index_album_artists_on_album_id_and_artist_id_and_name (album_id,artist_id,name) UNIQUE -# index_album_artists_on_artist_id (artist_id) -# index_album_artists_on_normalized_name (normalized_name) -# -# Foreign Keys -# -# fk_rails_... (album_id => albums.id) -# fk_rails_... (artist_id => artists.id) -# -class AlbumArtistSerializer < ActiveModel::Serializer - attributes :artist_id, :name, :normalized_name, :order, :separator -end diff --git a/app/serializers/album_label_serializer.rb b/app/serializers/album_label_serializer.rb deleted file mode 100644 index 9b72ea16..00000000 --- a/app/serializers/album_label_serializer.rb +++ /dev/null @@ -1,23 +0,0 @@ -# == Schema Information -# -# Table name: album_labels -# -# id :bigint not null, primary key -# catalogue_number :string -# album_id :bigint not null -# label_id :bigint not null -# -# Indexes -# -# index_album_labels_on_album_id (album_id) -# index_album_labels_on_album_id_and_label_id (album_id,label_id) UNIQUE -# index_album_labels_on_label_id (label_id) -# -# Foreign Keys -# -# fk_rails_... (album_id => albums.id) -# fk_rails_... (label_id => labels.id) -# -class AlbumLabelSerializer < ActiveModel::Serializer - attributes :label_id, :catalogue_number -end diff --git a/app/serializers/album_serializer.rb b/app/serializers/album_serializer.rb deleted file mode 100644 index 1951d0ec..00000000 --- a/app/serializers/album_serializer.rb +++ /dev/null @@ -1,67 +0,0 @@ -# == Schema Information -# -# Table name: albums -# -# id :bigint not null, primary key -# edition :date -# edition_description :string -# normalized_title :string not null -# release :date default(Thu, 01 Jan 0000), not null -# review_comment :string -# title :string not null -# created_at :datetime not null -# updated_at :datetime not null -# image_id :bigint -# -# Indexes -# -# index_albums_on_image_id (image_id) UNIQUE -# index_albums_on_normalized_title (normalized_title) -# -# Foreign Keys -# -# fk_rails_... (image_id => images.id) -# -class AlbumSerializer < ActiveModel::Serializer - include Rails.application.routes.url_helpers - - attributes :id, :title, :normalized_title, :release, :review_comment, :edition, :edition_description, :created_at, :updated_at, :image, :image100, :image250, :image500, :image_type - - has_many :album_artists - has_many :album_labels - - def image - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_blob_url(object.image.image, **scope) - end - - def image100 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [100, 100]), **scope) - end - - def image250 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [250, 250]), **scope) - end - - def image500 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [500, 500]), **scope) - end - - def image_type - return nil if object.image.blank? - return nil unless object.image.image.variable? - - object.image.image_type.mimetype - end -end diff --git a/app/serializers/artist_serializer.rb b/app/serializers/artist_serializer.rb deleted file mode 100644 index 9cabcffc..00000000 --- a/app/serializers/artist_serializer.rb +++ /dev/null @@ -1,61 +0,0 @@ -# == Schema Information -# -# Table name: artists -# -# id :bigint not null, primary key -# name :string not null -# normalized_name :string not null -# review_comment :string -# created_at :datetime not null -# updated_at :datetime not null -# image_id :bigint -# -# Indexes -# -# index_artists_on_image_id (image_id) UNIQUE -# index_artists_on_normalized_name (normalized_name) -# -# Foreign Keys -# -# fk_rails_... (image_id => images.id) -# -class ArtistSerializer < ActiveModel::Serializer - include Rails.application.routes.url_helpers - - attributes :id, :name, :normalized_name, :review_comment, :created_at, :updated_at, :image, :image100, :image250, :image500, :image_type - - def image - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_blob_url(object.image.image, **scope) - end - - def image100 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [100, 100]), **scope) - end - - def image250 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [250, 250]), **scope) - end - - def image500 - return nil if object.image.blank? - return nil unless object.image.image.variable? - - rails_representation_url(object.image.image.variant(resize_to_cover: [500, 500]), **scope) - end - - def image_type - return nil if object.image.blank? - return nil unless object.image.image.variable? - - object.image.image_type.mimetype - end -end diff --git a/app/serializers/auth_token_serializer.rb b/app/serializers/auth_token_serializer.rb deleted file mode 100644 index aed1e323..00000000 --- a/app/serializers/auth_token_serializer.rb +++ /dev/null @@ -1,23 +0,0 @@ -# == Schema Information -# -# Table name: auth_tokens -# -# id :bigint not null, primary key -# application :string -# hashed_secret :string not null -# user_agent :string not null -# device_id :string not null -# user_id :bigint not null -# -# Indexes -# -# index_auth_tokens_on_device_id (device_id) UNIQUE -# index_auth_tokens_on_user_id (user_id) -# -# Foreign Keys -# -# fk_rails_... (user_id => users.id) -# -class AuthTokenSerializer < ActiveModel::Serializer - attributes :id, :device_id, :user_id, :user_agent, :application -end diff --git a/app/serializers/auth_token_with_secret_serializer.rb b/app/serializers/auth_token_with_secret_serializer.rb deleted file mode 100644 index 1279d35c..00000000 --- a/app/serializers/auth_token_with_secret_serializer.rb +++ /dev/null @@ -1,3 +0,0 @@ -class AuthTokenWithSecretSerializer < AuthTokenSerializer - attribute :secret -end diff --git a/app/serializers/codec_conversion_serializer.rb b/app/serializers/codec_conversion_serializer.rb deleted file mode 100644 index 9cce9cd3..00000000 --- a/app/serializers/codec_conversion_serializer.rb +++ /dev/null @@ -1,21 +0,0 @@ -# == Schema Information -# -# Table name: codec_conversions -# -# id :bigint not null, primary key -# ffmpeg_params :string not null -# name :string not null -# resulting_codec_id :bigint not null -# -# Indexes -# -# index_codec_conversions_on_name (name) UNIQUE -# index_codec_conversions_on_resulting_codec_id (resulting_codec_id) -# -# Foreign Keys -# -# fk_rails_... (resulting_codec_id => codecs.id) -# -class CodecConversionSerializer < ActiveModel::Serializer - attributes :id, :name, :ffmpeg_params, :resulting_codec_id -end diff --git a/app/serializers/codec_serializer.rb b/app/serializers/codec_serializer.rb deleted file mode 100644 index 6c47775d..00000000 --- a/app/serializers/codec_serializer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# == Schema Information -# -# Table name: codecs -# -# id :bigint not null, primary key -# extension :string not null -# mimetype :string not null -# -# Indexes -# -# index_codecs_on_extension (extension) UNIQUE -# -class CodecSerializer < ActiveModel::Serializer - attributes :id, :mimetype, :extension -end diff --git a/app/serializers/cover_filename_serializer.rb b/app/serializers/cover_filename_serializer.rb deleted file mode 100644 index f76faeda..00000000 --- a/app/serializers/cover_filename_serializer.rb +++ /dev/null @@ -1,14 +0,0 @@ -# == Schema Information -# -# Table name: cover_filenames -# -# id :bigint not null, primary key -# filename :string not null -# -# Indexes -# -# index_cover_filenames_on_filename (filename) UNIQUE -# -class CoverFilenameSerializer < ActiveModel::Serializer - attributes :id, :filename -end diff --git a/app/serializers/genre_serializer.rb b/app/serializers/genre_serializer.rb deleted file mode 100644 index 2ad1139f..00000000 --- a/app/serializers/genre_serializer.rb +++ /dev/null @@ -1,16 +0,0 @@ -# == Schema Information -# -# Table name: genres -# -# id :bigint not null, primary key -# name :string not null -# normalized_name :string not null -# -# Indexes -# -# index_genres_on_name (name) UNIQUE -# index_genres_on_normalized_name (normalized_name) -# -class GenreSerializer < ActiveModel::Serializer - attributes :id, :name, :normalized_name -end diff --git a/app/serializers/image_type_serializer.rb b/app/serializers/image_type_serializer.rb deleted file mode 100644 index 81cded3b..00000000 --- a/app/serializers/image_type_serializer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# == Schema Information -# -# Table name: image_types -# -# id :bigint not null, primary key -# extension :string not null -# mimetype :string not null -# -# Indexes -# -# index_image_types_on_extension (extension) UNIQUE -# -class ImageTypeSerializer < ActiveModel::Serializer - attributes :id, :extension, :mimetype -end diff --git a/app/serializers/label_serializer.rb b/app/serializers/label_serializer.rb deleted file mode 100644 index 1819b66c..00000000 --- a/app/serializers/label_serializer.rb +++ /dev/null @@ -1,15 +0,0 @@ -# == Schema Information -# -# Table name: labels -# -# id :bigint not null, primary key -# name :string not null -# normalized_name :string not null -# -# Indexes -# -# index_labels_on_normalized_name (normalized_name) -# -class LabelSerializer < ActiveModel::Serializer - attributes :id, :name, :normalized_name -end diff --git a/app/serializers/location_serializer.rb b/app/serializers/location_serializer.rb deleted file mode 100644 index 75aee012..00000000 --- a/app/serializers/location_serializer.rb +++ /dev/null @@ -1,14 +0,0 @@ -# == Schema Information -# -# Table name: locations -# -# id :bigint not null, primary key -# path :string not null -# -# Indexes -# -# index_locations_on_path (path) UNIQUE -# -class LocationSerializer < ActiveModel::Serializer - attributes :id, :path -end diff --git a/app/serializers/play_serializer.rb b/app/serializers/play_serializer.rb deleted file mode 100644 index 317bffb4..00000000 --- a/app/serializers/play_serializer.rb +++ /dev/null @@ -1,23 +0,0 @@ -# == Schema Information -# -# Table name: plays -# -# id :bigint not null, primary key -# played_at :datetime not null -# track_id :bigint not null -# user_id :bigint not null -# -# Indexes -# -# index_plays_on_track_id (track_id) -# index_plays_on_user_id (user_id) -# index_plays_on_user_id_and_track_id (user_id,track_id) -# -# Foreign Keys -# -# fk_rails_... (track_id => tracks.id) -# fk_rails_... (user_id => users.id) -# -class PlaySerializer < ActiveModel::Serializer - attributes :id, :played_at, :track_id, :user_id -end diff --git a/app/serializers/play_stat_serializer.rb b/app/serializers/play_stat_serializer.rb deleted file mode 100644 index ec94f741..00000000 --- a/app/serializers/play_stat_serializer.rb +++ /dev/null @@ -1,3 +0,0 @@ -class PlayStatSerializer < ActiveModel::Serializer - attributes :count, :track_id, :last_played_at, :total_length -end diff --git a/app/serializers/playlist_serializer.rb b/app/serializers/playlist_serializer.rb deleted file mode 100644 index a449ddfd..00000000 --- a/app/serializers/playlist_serializer.rb +++ /dev/null @@ -1,24 +0,0 @@ -# == Schema Information -# -# Table name: playlists -# -# id :bigint not null, primary key -# access :integer default("shared") -# description :string -# name :string not null -# playlist_type :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# user_id :bigint not null -# -# Indexes -# -# index_playlists_on_user_id (user_id) -# -# Foreign Keys -# -# fk_rails_... (user_id => users.id) -# -class PlaylistSerializer < ActiveModel::Serializer - attributes :id, :name, :description, :user_id, :playlist_type, :created_at, :updated_at, :item_ids, :access -end diff --git a/app/serializers/rescan_runner_serializer.rb b/app/serializers/rescan_runner_serializer.rb deleted file mode 100644 index ebf96a2c..00000000 --- a/app/serializers/rescan_runner_serializer.rb +++ /dev/null @@ -1,23 +0,0 @@ -# == Schema Information -# -# Table name: rescan_runners -# -# id :bigint not null, primary key -# error_text :text -# finished_at :datetime not null -# processed :integer default(0), not null -# running :boolean default(FALSE), not null -# warning_text :text -# location_id :bigint not null -# -# Indexes -# -# index_rescan_runners_on_location_id (location_id) -# -# Foreign Keys -# -# fk_rails_... (location_id => locations.id) -# -class RescanRunnerSerializer < ActiveModel::Serializer - attributes :id, :error_text, :warning_text, :processed, :running, :finished_at, :location_id -end diff --git a/app/serializers/track_artist_serializer.rb b/app/serializers/track_artist_serializer.rb deleted file mode 100644 index 8cdc1c8a..00000000 --- a/app/serializers/track_artist_serializer.rb +++ /dev/null @@ -1,28 +0,0 @@ -# == Schema Information -# -# Table name: track_artists -# -# id :bigint not null, primary key -# hidden :boolean default(FALSE), not null -# name :string not null -# normalized_name :string not null -# order :integer not null -# role :integer not null -# artist_id :bigint not null -# track_id :bigint not null -# -# Indexes -# -# index_track_artists_on_artist_id (artist_id) -# index_track_artists_on_normalized_name (normalized_name) -# index_track_artists_on_track_id (track_id) -# index_track_artists_on_track_id_and_artist_id_and_name_and_role (track_id,artist_id,name,role) UNIQUE -# -# Foreign Keys -# -# fk_rails_... (artist_id => artists.id) -# fk_rails_... (track_id => tracks.id) -# -class TrackArtistSerializer < ActiveModel::Serializer - attributes :artist_id, :name, :normalized_name, :role, :order, :hidden -end diff --git a/app/serializers/track_moderator_serializer.rb b/app/serializers/track_moderator_serializer.rb deleted file mode 100644 index ddcdba55..00000000 --- a/app/serializers/track_moderator_serializer.rb +++ /dev/null @@ -1,15 +0,0 @@ -class TrackModeratorSerializer < TrackSerializer - attributes :filename, :sample_rate, :bit_depth - - def filename - object.audio_file&.filename - end - - def sample_rate - object.audio_file&.sample_rate - end - - def bit_depth - object.audio_file&.bit_depth - end -end diff --git a/app/serializers/track_serializer.rb b/app/serializers/track_serializer.rb deleted file mode 100644 index 9d8c4bff..00000000 --- a/app/serializers/track_serializer.rb +++ /dev/null @@ -1,46 +0,0 @@ -# == Schema Information -# -# Table name: tracks -# -# id :bigint not null, primary key -# normalized_title :string not null -# number :integer not null -# review_comment :string -# title :string not null -# created_at :datetime not null -# updated_at :datetime not null -# album_id :bigint not null -# audio_file_id :bigint -# -# Indexes -# -# index_tracks_on_album_id (album_id) -# index_tracks_on_audio_file_id (audio_file_id) UNIQUE -# index_tracks_on_normalized_title (normalized_title) -# -# Foreign Keys -# -# fk_rails_... (album_id => albums.id) -# fk_rails_... (audio_file_id => audio_files.id) -# -class TrackSerializer < ActiveModel::Serializer - attributes :id, :title, :normalized_title, :number, :album_id, :review_comment, :created_at, :updated_at, :genre_ids, :codec_id, :length, :bitrate, :location_id, :audio_file_id - - has_many :track_artists - - def codec_id - object.audio_file&.codec_id - end - - def length - object.audio_file&.length - end - - def bitrate - object.audio_file&.bitrate - end - - def location_id - object.audio_file&.location_id - end -end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb deleted file mode 100644 index 10418092..00000000 --- a/app/serializers/user_serializer.rb +++ /dev/null @@ -1,16 +0,0 @@ -# == Schema Information -# -# Table name: users -# -# id :bigint not null, primary key -# name :string not null -# password_digest :string not null -# permission :integer default("user"), not null -# -# Indexes -# -# index_users_on_name (name) UNIQUE -# -class UserSerializer < ActiveModel::Serializer - attributes :id, :name, :permission -end diff --git a/gemset.nix b/gemset.nix index 0554f241..fe631f57 100644 --- a/gemset.nix +++ b/gemset.nix @@ -65,17 +65,6 @@ }; version = "7.2.2"; }; - active_model_serializers = { - dependencies = ["actionpack" "activemodel" "case_transform" "jsonapi-renderer"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "13n1ipn0dg3k852xhfzdvkr1ljq76xvfnm79qzdix2ishiy1gphl"; - type = "gem"; - }; - version = "0.10.14"; - }; activejob = { dependencies = ["activesupport" "globalid"]; groups = ["default"]; @@ -223,17 +212,6 @@ }; version = "3.3.0"; }; - case_transform = { - dependencies = ["activesupport"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0fzyws6spn5arqf6q604dh9mrj84a36k5hsc8z7jgcpfvhc49bg2"; - type = "gem"; - }; - version = "0.2"; - }; concurrent-ruby = { groups = ["default" "development" "test"]; platforms = []; @@ -466,16 +444,6 @@ }; version = "2.7.5"; }; - jsonapi-renderer = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0ys4drd0k9rw5ixf8n8fx8v0pjh792w4myh0cpdspd317l1lpi5m"; - type = "gem"; - }; - version = "0.2.2"; - }; language_server-protocol = { groups = ["default" "development"]; platforms = [];