Files
old-holivud2/app/controllers/broadcasts_controller.rb
2020-08-20 06:50:51 +02:00

151 lines
4.1 KiB
Ruby

class BroadcastsController < ApplicationController
layout "project"
before_action :set_project
before_action :build_broadcast, only: [:new, :create]
before_action :set_broadcast, only: [:show, :destroy, :update, :destroy_file]
before_action :set_multi_view_broadcasts, only: [:show]
before_action :show_splash_screen, only: :index
def index
@broadcasts = filtered_broadcasts.order_by_recent.paginate(page: params[:page])
end
def new
end
def create
@broadcast.attributes = broadcast_params
if @broadcast.save
log_create_analytics
redirect_to [@project, :broadcasts], notice: t(".notice")
else
render :new
end
rescue MuxRuby::ApiError, ActiveResource::ConnectionError => e
Raven.capture_exception(e)
redirect_to [@project, :broadcasts], alert: t(".alert")
end
def show
@conference_url = conference_url_for(@broadcast)
@recordings = @broadcast.broadcast_recordings.visible.order_by_recent.paginate(page: params[:page])
@files = @broadcast.files.order("created_at DESC").paginate(page: params[:files_page])
render layout: 'application'
end
def update
unless params.has_key?(:broadcast)
@broadcast.regenerate_token
redirect_to([@project, @broadcast], notice: t('.reset_notice')) and return
end
@broadcast.update(broadcast_params)
update_files_section
end
def destroy
if @broadcast.destroy
redirect_to [@project, :broadcasts], alert: t(".alert")
else
redirect_to [@project, :broadcasts], alert: t(".api_error")
end
end
def destroy_file
authorize Broadcast
file = ActiveStorage::Attachment.find(params[:file_id])
file.destroy
update_files_section
end
private
def update_files_section
@files = @broadcast.files.order("created_at DESC").paginate(page: 1)
pagination_content = ApplicationController.render html: helpers.will_paginate(@files, params: { active_tab: params[:active_tab], page: params[:page], active_files_tab: params[:active_files_tab] })
BroadcastsChannel.broadcast_file_upload_updates(@broadcast, @files, pagination_content)
end
def show_splash_screen
render :splash if broadcasts.count.zero?
end
def broadcast_params
params.require(:broadcast).permit(:name, :shoot_location_time_zone, files: [])
end
def set_project
@project = policy_scope(Project).find(params[:project_id])
end
def set_broadcast
@broadcast = authorize policy_scope(Broadcast).find(params[:id])
end
def build_broadcast
@broadcast = authorize @project.broadcasts.build
end
def broadcasts
authorize policy_scope(@project.broadcasts)
end
def set_multi_view_broadcasts
authorized_broadcasts = authorize policy_scope(Broadcast).where(id: params[:multi_view_ids]).order_by_recent
@multi_view_broadcasts = authorized_broadcasts.map do |b|
files_page = params[:files_page] if params[:active_files_tab] == b.token
MultiViewBroadcast.new(b, params[:multi_view_ids], files_page)
end
end
def filtered_broadcasts
results = broadcasts
if params[:query].present?
results = results.search(params[:query])
end
results
end
def conference_url_for(broadcast)
broadcast.video_conference_url_override || url_for([broadcast.project, broadcast, :zoom_meeting])
end
def log_create_analytics
TrackAnalyticsJob.perform_later(Current.user, Current.account, :track_create_live_stream, user_agent: request.user_agent, user_ip: request.remote_ip)
end
class MultiViewBroadcast
include Rails.application.routes.url_helpers
delegate_missing_to :@broadcast
def initialize(broadcast, multi_view_ids, paginate_page)
@broadcast = broadcast
@multi_view_ids = multi_view_ids
@paginate_page = paginate_page
end
def url
project_broadcast_path(@broadcast.project, @broadcast, multi_view_ids: @multi_view_ids, locale: I18n.locale)
end
def files
@broadcast.files.order("created_at DESC").paginate(page: @paginate_page)
end
def uid
id
end
def self.model_name
Broadcast.model_name
end
end
end