docbrown/app/controllers/admin/podcasts_controller.rb
Daniel Uber 4b4d8a7234
Ensure arguments to perform_async are json safe (#16285)
* Convert symbol hash keys to strings when calling .perform_async

Fixes a warning from Sidekiq 6.4.0+ about perform_async arguments
which are not equal when passed to perform (`JSON.parse(JSON.dump(arg))`
should equal arg).

This is a safety measure to prevent passing objects (like classes, or
model instances) rather than their representations (like a class name,
or a model's attributes hash).

This warning will be an error in sidekiq 7

* Turn warning into an error in non-production environments

* Use string keys for reaction notification and article fetched

Missed these two on the first pass

* Update example argument hashes for #enqueues_on_correct_queue

Since this calls perform_async under the hood we need to pass json
safe hashes in the test cases as well.

https://github.com/forem/forem/blob/main/spec/workers/shared_examples/enqueues_on_correct_queue.rb

* Convert keys from FollowData#to_h to string before perform_async

I'm not sure enough where else (outside of notification) to_h is being
called, so I'm converting here when building args, rather than in
FollowData#to_h, which might be my next step.

* Let FollowData#to_h return a hash with string keys

Update spec to use string keys as well.

* Make to_h return string keys for ReactionData

Like FollowData, the #to_h method is only used to call
notifications (this is used to enqueue sidekiq jobs).

* Remove a key that was in the hash

Since reaction_data calls to_h, it gets string and not symbol,
keys. Call Hash#except with a key that was actually there.
2022-01-25 18:07:40 -06:00

80 lines
2.2 KiB
Ruby

module Admin
class PodcastsController < Admin::ApplicationController
layout "admin"
before_action :find_podcast, only: %i[edit update fetch add_owner]
PODCAST_ALLOWED_PARAMS = %i[
title
feed_url
description
itunes_url
overcast_url
android_url
soundcloud_url
website_url
twitter_username
pattern_image
main_color_hex
slug
image
featured
reachable
published
].freeze
def index
@podcasts = Podcast.left_outer_joins(:podcast_episodes)
.select("podcasts.*, count(podcast_episodes) as episodes_count")
.group("podcasts.id").order("podcasts.created_at" => :desc)
.page(params[:page]).per(50)
return if params[:search].blank?
@podcasts = @podcasts.where("podcasts.title ILIKE :search", search: "%#{params[:search]}%")
end
def edit
@podcast_ownership = Podcast.find(params[:id])
end
def update
if @podcast.update(podcast_params)
redirect_to admin_podcasts_path, notice: "Podcast updated"
else
render :edit
end
end
def fetch
limit = params[:limit].to_i.zero? ? nil : params[:limit].to_i
force = params[:force].to_i == 1
Podcasts::GetEpisodesWorker.perform_async("podcast_id" => @podcast.id, "limit" => limit, "force" => force)
flash[:notice] = "Podcast's episodes fetching was scheduled (#{@podcast.title}, ##{@podcast.id})"
redirect_to admin_podcasts_path
end
def add_owner
@podcast_ownership = @podcast.podcast_ownerships.build(user_id: params["podcast"]["user_id"])
if @podcast_ownership.save
redirect_to admin_podcasts_path, notice: "New owner added!"
else
redirect_to edit_admin_podcast_path(@podcast), notice: @podcast_ownership.errors_as_sentence
end
end
private
def find_podcast
@podcast = Podcast.find(params[:id])
end
def find_user
@user = User.find_by(id: params[:podcast][:user_id])
redirect_to edit_admin_podcast_path(@podcast), notice: "No such user" unless @user
end
def podcast_params
params.require(:podcast).permit(PODCAST_ALLOWED_PARAMS)
end
end
end