From de0b12a0e47f3d5862395c9c77d48ebf6844ef9c Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 14 Sep 2020 15:57:38 +0300 Subject: [PATCH] add live meeting in broadcast screen --- .env.sample | 3 +++ app/controllers/broadcasts_controller.rb | 2 +- app/controllers/live_meetings_controller.rb | 6 +++++ .../public/broadcasts_controller.rb | 2 +- .../public/live_meetings_controller.rb | 9 +++++++ app/models/broadcast.rb | 9 +++++++ app/views/live_meetings/show.html.erb | 13 ++++++++++ app/views/public/live_meetings/show.html.erb | 13 ++++++++++ config/initializers/daily.rb | 1 + config/routes.rb | 4 +-- ...113410_add_live_meeting_url_to_projects.rb | 5 ++++ lib/daily.rb | 26 +++++++++++++++++++ 12 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 app/controllers/live_meetings_controller.rb create mode 100644 app/controllers/public/live_meetings_controller.rb create mode 100644 app/views/live_meetings/show.html.erb create mode 100644 app/views/public/live_meetings/show.html.erb create mode 100644 config/initializers/daily.rb create mode 100644 db/migrate/20200914113410_add_live_meeting_url_to_projects.rb create mode 100644 lib/daily.rb diff --git a/.env.sample b/.env.sample index 93c5cb8..1143b5c 100644 --- a/.env.sample +++ b/.env.sample @@ -32,3 +32,6 @@ CUSTOM_API_TOKEN= # Required for simulcasting to Millicast for director mode MILLICAST_API_SECRET= MILLICAST_ACCOUNT_ID= + +# Daily.co live chat API token +DAILY_API_TOKEN= \ No newline at end of file diff --git a/app/controllers/broadcasts_controller.rb b/app/controllers/broadcasts_controller.rb index 1e8839d..5524d0a 100644 --- a/app/controllers/broadcasts_controller.rb +++ b/app/controllers/broadcasts_controller.rb @@ -114,7 +114,7 @@ class BroadcastsController < ApplicationController end def conference_url_for(broadcast) - broadcast.video_conference_url_override.presence || url_for([broadcast.project, broadcast, :zoom_meeting]) + broadcast.video_conference_url_override.presence || url_for([broadcast.project, broadcast, :live_meeting]) end def log_create_analytics diff --git a/app/controllers/live_meetings_controller.rb b/app/controllers/live_meetings_controller.rb new file mode 100644 index 0000000..e946f1b --- /dev/null +++ b/app/controllers/live_meetings_controller.rb @@ -0,0 +1,6 @@ +class LiveMeetingsController < ApplicationController + def show + authorize broadcast = Broadcast.find(params[:broadcast_id]) + @live_meeting_url = broadcast.project.live_meeting_url + end +end \ No newline at end of file diff --git a/app/controllers/public/broadcasts_controller.rb b/app/controllers/public/broadcasts_controller.rb index 84ecd35..1104bae 100644 --- a/app/controllers/public/broadcasts_controller.rb +++ b/app/controllers/public/broadcasts_controller.rb @@ -44,7 +44,7 @@ class Public::BroadcastsController < Public::BaseController end def conference_url_for(broadcast) - broadcast.video_conference_url_override.presence || broadcast_zoom_meeting_url(broadcast.token) + broadcast.video_conference_url_override.presence || broadcast_live_meeting_url(broadcast.token) end class MultiViewBroadcast diff --git a/app/controllers/public/live_meetings_controller.rb b/app/controllers/public/live_meetings_controller.rb new file mode 100644 index 0000000..61805af --- /dev/null +++ b/app/controllers/public/live_meetings_controller.rb @@ -0,0 +1,9 @@ +class Public::LiveMeetingsController < Public::BaseController + skip_after_action :verify_authorized + + def show + broadcast = Broadcast.find_by_token!(params[:broadcast_token]) + @live_meeting_url = broadcast.project.live_meeting_url + render 'public/live_meetings/show' + end +end \ No newline at end of file diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index a27d801..90a114b 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -14,6 +14,7 @@ class Broadcast < ApplicationRecord # Should we use callbacks for this, or something else? after_create :create_mux_live_stream + after_create :create_live_meeting_if_not_created after_destroy :destroy_mux_live_stream pg_search_scope :search, { @@ -59,6 +60,14 @@ class Broadcast < ApplicationRecord self.save! end + def create_live_meeting_if_not_created + if project.live_meeting_url.blank? + room = Daily.create_room + room_url = room['url'] + project.update live_meeting_url: room_url + end + end + def destroy_mux_live_stream begin stream = MuxLiveStream.new diff --git a/app/views/live_meetings/show.html.erb b/app/views/live_meetings/show.html.erb new file mode 100644 index 0000000..a7262c8 --- /dev/null +++ b/app/views/live_meetings/show.html.erb @@ -0,0 +1,13 @@ +<%= javascript_include_tag "https://unpkg.com/@daily-co/daily-js" %> + +<%= javascript_tag nonce: true do %> + callFrame = window.DailyIframe.createFrame({ + showLeaveButton: true, + iframeStyle: { + position: 'fixed', + width: '100%', + height: '90%' + } + }); + callFrame.join({ url: '<%= @live_meeting_url %>' }); +<% end %> \ No newline at end of file diff --git a/app/views/public/live_meetings/show.html.erb b/app/views/public/live_meetings/show.html.erb new file mode 100644 index 0000000..0a91f26 --- /dev/null +++ b/app/views/public/live_meetings/show.html.erb @@ -0,0 +1,13 @@ +<%= javascript_include_tag "https://unpkg.com/@daily-co/daily-js" %> + +<%= javascript_tag nonce: true do %> + callFrame = window.DailyIframe.createFrame({ + showLeaveButton: true, + iframeStyle: { + position: 'fixed', + width: '100%', + height: '90%' + } + }); + callFrame.join({ url: '<%= @live_meeting_url %>' }); +<% end %> \ No newline at end of file diff --git a/config/initializers/daily.rb b/config/initializers/daily.rb new file mode 100644 index 0000000..b2af2f7 --- /dev/null +++ b/config/initializers/daily.rb @@ -0,0 +1 @@ +require 'daily' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d93745c..f9e4062 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -104,7 +104,7 @@ Rails.application.routes.draw do member do delete :destroy_file end - resource :zoom_meeting, only: [:show] + resource :live_meeting, only: [:show] resources :broadcast_recordings, only: [:destroy] end resources :directories, except: [:index] do @@ -149,7 +149,7 @@ Rails.application.routes.draw do end end resources :broadcasts, param: :token, only: [:show, :update] do - resource :zoom_meeting, only: [:show] + resource :live_meeting, only: [:show] resources :broadcast_recordings, only: [:edit, :update] do resources :broadcast_recording_starrings, only: :create end diff --git a/db/migrate/20200914113410_add_live_meeting_url_to_projects.rb b/db/migrate/20200914113410_add_live_meeting_url_to_projects.rb new file mode 100644 index 0000000..8270b81 --- /dev/null +++ b/db/migrate/20200914113410_add_live_meeting_url_to_projects.rb @@ -0,0 +1,5 @@ +class AddLiveMeetingUrlToProjects < ActiveRecord::Migration[6.0] + def change + add_column :projects, :live_meeting_url, :text + end +end diff --git a/lib/daily.rb b/lib/daily.rb new file mode 100644 index 0000000..9ff881c --- /dev/null +++ b/lib/daily.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class Daily + include HTTParty + base_uri 'api.daily.co/v1' + + class << self + def create_room + response = post "#{base_uri}/rooms", headers: headers + + if response.code != 200 + throw StandardError.new('Failed to create a room') + end + + response.parsed_response + end + + private + + def headers + { + 'Authorization': "Bearer #{ENV['DAILY_API_TOKEN']}" + } + end + end +end \ No newline at end of file