GRN-59: Implemented pagination on the API call (#370)

* Added the env variable and functionality to paginate the call to the bbbapi

* Update user.rb
This commit is contained in:
farhatahmad 2019-03-12 13:50:20 -04:00 committed by Jesus Federico
parent ab6655554c
commit 3195bb4429
12 changed files with 278 additions and 124 deletions

View File

@ -0,0 +1,55 @@
# frozen_string_literal: true
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>
class RecordingsController < ApplicationController
before_action :find_room
before_action :verify_room_ownership
META_LISTED = "gl-listed"
# POST /:meetingID/:record_id
def update_recording
meta = {
"meta_#{META_LISTED}" => (params[:state] == "public"),
}
res = @room.update_recording(params[:record_id], meta)
# Redirects to the page that made the initial request
redirect_to request.referrer if res[:updated]
end
# DELETE /:meetingID/:record_id
def delete_recording
@room.delete_recording(params[:record_id])
# Redirects to the page that made the initial request
redirect_to request.referrer
end
private
def find_room
@room = Room.find_by!(bbb_id: params[:meetingID])
end
# Ensure the user is logged into the room they are accessing.
def verify_room_ownership
redirect_to root_path unless @room.owned_by?(current_user)
end
end

View File

@ -17,6 +17,8 @@
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
class RoomsController < ApplicationController
include RecordingsHelper
before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms }
before_action :validate_verified_email, except: [:show, :join],
unless: -> { !Rails.configuration.enable_email_verification }
@ -24,9 +26,6 @@ class RoomsController < ApplicationController
before_action :verify_room_ownership, except: [:create, :show, :join, :logout]
before_action :verify_room_owner_verified, only: [:show, :join]
include RecordingsHelper
META_LISTED = "gl-listed"
# POST /
def create
redirect_to(root_path) && return unless current_user
@ -52,10 +51,7 @@ class RoomsController < ApplicationController
def show
if current_user && @room.owned_by?(current_user)
recs = @room.recordings
# Add the room id to each recording object
recs.each do |rec|
rec[:room_uid] = @room.uid
end
@recordings = recs
@is_running = @room.running?
else
@ -168,26 +164,6 @@ class RoomsController < ApplicationController
redirect_to @room
end
# POST /:room_uid/:record_id
def update_recording
meta = {
"meta_#{META_LISTED}" => (params[:state] == "public"),
}
res = @room.update_recording(params[:record_id], meta)
# Redirects to the page that made the initial request
redirect_to request.referrer if res[:updated]
end
# DELETE /:room_uid/:record_id
def delete_recording
@room.delete_recording(params[:record_id])
# Redirects to the page that made the initial request
redirect_to request.referrer
end
private
def update_room_attributes(update_type)

View File

@ -17,11 +17,11 @@
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
class UsersController < ApplicationController
include RecordingsHelper
before_action :find_user, only: [:edit, :update, :destroy]
before_action :ensure_unauthenticated, only: [:new, :create]
include RecordingsHelper
# POST /u
def create
# Verify that GreenLight is configured to allow user signup.
@ -135,19 +135,7 @@ class UsersController < ApplicationController
# GET /u/:user_uid/recordings
def recordings
if current_user && current_user.uid == params[:user_uid]
@recordings = []
current_user.rooms.each do |room|
# Check that current user is the room owner
next unless room.owner == current_user
recs = room.recordings
# Add the room id to each recording object
recs.each do |rec|
rec[:room_uid] = room.uid
end
# Adds an array to another array
@recordings.push(*recs)
end
@recordings = current_user.all_recordings
else
redirect_to root_path
end

View File

@ -0,0 +1,101 @@
# frozen_string_literal: true
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
module APIConcern
extend ActiveSupport::Concern
def bbb_endpoint
Rails.configuration.bigbluebutton_endpoint
end
def bbb_secret
Rails.configuration.bigbluebutton_secret
end
# Sets a BigBlueButtonApi object for interacting with the API.
def bbb
@bbb ||= if Rails.configuration.loadbalanced_configuration
lb_user = retrieve_loadbalanced_credentials(owner.provider)
BigBlueButton::BigBlueButtonApi.new(remove_slash(lb_user["apiURL"]), lb_user["secret"], "0.8")
else
BigBlueButton::BigBlueButtonApi.new(remove_slash(bbb_endpoint), bbb_secret, "0.8")
end
end
# Rereives the loadbalanced BigBlueButton credentials for a user.
def retrieve_loadbalanced_credentials(provider)
# Include Omniauth accounts under the Greenlight provider.
provider = "greenlight" if Rails.configuration.providers.include?(provider.to_sym)
# Build the URI.
uri = encode_bbb_url(
Rails.configuration.loadbalancer_endpoint + "getUser",
Rails.configuration.loadbalancer_secret,
name: provider
)
# Make the request.
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
response = http.get(uri.request_uri)
unless response.is_a?(Net::HTTPSuccess)
raise "Error retrieving provider credentials: #{response.code} #{response.message}"
end
# Parse XML.
doc = XmlSimple.xml_in(response.body, 'ForceArray' => false)
# Return the user credentials if the request succeeded on the loadbalancer.
return doc['user'] if doc['returncode'] == RETURNCODE_SUCCESS
raise "User with provider #{provider} does not exist." if doc['messageKey'] == "noSuchUser"
raise "API call #{url} failed with #{doc['messageKey']}."
end
# Builds a request to retrieve credentials from the load balancer.
def encode_bbb_url(base_url, secret, params)
encoded_params = OAuth::Helper.normalize(params)
string = "getUser" + encoded_params + secret
checksum = OpenSSL::Digest.digest('sha1', string).unpack("H*").first
URI.parse("#{base_url}?#{encoded_params}&checksum=#{checksum}")
end
# Removes trailing forward slash from a URL.
def remove_slash(s)
s.nil? ? nil : s.chomp("/")
end
# Format recordings to match their current use in the app
def format_recordings(api_res)
api_res[:recordings].each do |r|
next if r.key?(:error)
# Format playbacks in a more pleasant way.
r[:playbacks] = if !r[:playback] || !r[:playback][:format]
[]
elsif r[:playback][:format].is_a?(Array)
r[:playback][:format]
else
[r[:playback][:format]]
end
r.delete(:playback)
end
api_res[:recordings].sort_by { |rec| rec[:endTime] }.reverse
end
end

View File

@ -17,6 +17,8 @@
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
class Room < ApplicationRecord
include ::APIConcern
before_create :setup
before_destroy :delete_all_recordings
@ -118,21 +120,8 @@ class Room < ApplicationRecord
# Fetches all recordings for a room.
def recordings
res = bbb.get_recordings(meetingID: bbb_id)
# Format playbacks in a more pleasant way.
res[:recordings].each do |r|
next if r.key?(:error)
r[:playbacks] = if !r[:playback] || !r[:playback][:format]
[]
elsif r[:playback][:format].is_a?(Array)
r[:playback][:format]
else
[r[:playback][:format]]
end
r.delete(:playback)
end
res[:recordings].sort_by { |rec| rec[:endTime] }.reverse
format_recordings(res)
end
# Fetches a rooms public recordings.
@ -152,24 +141,6 @@ class Room < ApplicationRecord
private
def bbb_endpoint
Rails.configuration.bigbluebutton_endpoint
end
def bbb_secret
Rails.configuration.bigbluebutton_secret
end
# Sets a BigBlueButtonApi object for interacting with the API.
def bbb
@bbb ||= if Rails.configuration.loadbalanced_configuration
lb_user = retrieve_loadbalanced_credentials(owner.provider)
BigBlueButton::BigBlueButtonApi.new(remove_slash(lb_user["apiURL"]), lb_user["secret"], "0.8")
else
BigBlueButton::BigBlueButtonApi.new(remove_slash(bbb_endpoint), bbb_secret, "0.8")
end
end
# Generates a uid for the room and BigBlueButton.
def setup
self.uid = random_room_uid
@ -193,51 +164,6 @@ class Room < ApplicationRecord
[owner.name_chunk, uid_chunk, uid_chunk].join('-').downcase
end
# Rereives the loadbalanced BigBlueButton credentials for a user.
def retrieve_loadbalanced_credentials(provider)
# Include Omniauth accounts under the Greenlight provider.
provider = "greenlight" if Rails.configuration.providers.include?(provider.to_sym)
# Build the URI.
uri = encode_bbb_url(
Rails.configuration.loadbalancer_endpoint + "getUser",
Rails.configuration.loadbalancer_secret,
name: provider
)
# Make the request.
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
response = http.get(uri.request_uri)
unless response.is_a?(Net::HTTPSuccess)
raise "Error retrieving provider credentials: #{response.code} #{response.message}"
end
# Parse XML.
doc = XmlSimple.xml_in(response.body, 'ForceArray' => false)
# Return the user credentials if the request succeeded on the loadbalancer.
return doc['user'] if doc['returncode'] == RETURNCODE_SUCCESS
raise "User with provider #{provider} does not exist." if doc['messageKey'] == "noSuchUser"
raise "API call #{url} failed with #{doc['messageKey']}."
end
# Builds a request to retrieve credentials from the load balancer.
def encode_bbb_url(base_url, secret, params)
encoded_params = OAuth::Helper.normalize(params)
string = "getUser" + encoded_params + secret
checksum = OpenSSL::Digest.digest('sha1', string).unpack("H*").first
URI.parse("#{base_url}?#{encoded_params}&checksum=#{checksum}")
end
# Removes trailing forward slash from a URL.
def remove_slash(s)
s.nil? ? nil : s.chomp("/")
end
# Generates a random password for a meeting.
def random_password(length)
charset = ("a".."z").to_a + ("A".."Z").to_a

View File

@ -17,6 +17,8 @@
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
class User < ApplicationRecord
include ::APIConcern
attr_accessor :reset_token, :activation_token
after_create :create_home_room_if_verified
before_save { email.try(:downcase!) }
@ -95,6 +97,30 @@ class User < ApplicationRecord
end
end
def all_recordings
pag_num = Rails.configuration.pagination_number
pag_loops = rooms.length / pag_num - 1
res = { recordings: [] }
(0..pag_loops).each do |i|
pag_rooms = rooms[pag_num * i, pag_num]
# bbb.get_recordings returns an object
# take only the array portion of the object that is returned
full_res = bbb.get_recordings(meetingID: pag_rooms.pluck(:bbb_id))
res[:recordings].push(*full_res[:recordings])
end
last_pag_room = rooms[pag_num * (pag_loops + 1), rooms.length % pag_num]
full_res = bbb.get_recordings(meetingID: last_pag_room.pluck(:bbb_id))
res[:recordings].push(*full_res[:recordings])
format_recordings(res)
end
# Activates an account and initialize a users main room
def activate
update_attribute(:email_verified, true)

View File

@ -53,10 +53,10 @@
<button class="btn btn-sm btn-secondary dropdown-toggle" data-toggle="dropdown"><i class="dropdown-icon fas fa-link px-2"></i> <%= t("recording.visibility.unlisted") %></button>
<% end %>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<%= button_to update_recording_path(room_uid: recording[:room_uid], record_id: recording[:recordID], state: "public"), class: "dropdown-item" do %>
<%= button_to update_recording_path(meetingID: recording[:meetingID], record_id: recording[:recordID], state: "public"), class: "dropdown-item" do %>
<i class="dropdown-icon fas fa-globe"></i> <%= t("recording.visibility.public") %>
<% end %>
<%= button_to update_recording_path(room_uid: recording[:room_uid], record_id: recording[:recordID], state: "unlisted"), class: "dropdown-item" do %>
<%= button_to update_recording_path(meetingID: recording[:meetingID], record_id: recording[:recordID], state: "unlisted"), class: "dropdown-item" do %>
<i class="dropdown-icon fas fa-link"></i> <%= t("recording.visibility.unlisted") %>
<% end %>
</div>
@ -79,7 +79,7 @@
<a class="dropdown-item email-link" data-pres-link="<%= p[:url] %>"><i class="dropdown-icon far fa-envelope"></i> <%= t("recording.email") %></a>
<div class="dropdown-divider"></div>
<% end %>
<%= button_to delete_recording_path(room_uid: recording[:room_uid], record_id: recording[:recordID]), method: :delete, class: "dropdown-item" do %>
<%= button_to delete_recording_path(meetingID: recording[:meetingID], record_id: recording[:recordID]), method: :delete, class: "dropdown-item" do %>
<i class="dropdown-icon far fa-trash-alt"></i> <%= t("delete") %>
<% end %>
</div>

View File

@ -89,5 +89,8 @@ module Greenlight
# Configure which settings are available to user on room creation/edit after creation
config.room_features = ENV['ROOM_FEATURES'] || ""
# The maximum number of rooms included in one bbbapi call
config.pagination_number = ENV['PAGINATION_NUMBER'].to_i == 0 ? 25 : ENV['PAGINATION_NUMBER'].to_i
end
end

View File

@ -72,11 +72,14 @@ Rails.application.routes.draw do
post '/update_settings', to: 'rooms#update_settings'
post '/start', to: 'rooms#start', as: :start_room
get '/logout', to: 'rooms#logout', as: :logout_room
end
# Recording operations routes
scope '/:meetingID' do
# Manage recordings
scope '/:record_id' do
post '/', to: 'rooms#update_recording', as: :update_recording
delete '/', to: 'rooms#delete_recording', as: :delete_recording
post '/', to: 'recordings#update_recording', as: :update_recording
delete '/', to: 'recordings#delete_recording', as: :delete_recording
end
end

View File

@ -124,6 +124,10 @@ ALLOW_CUSTOM_BRANDING=false
# mute-on-join: Automatically mute users by default when they join a room
ROOM_FEATURES=default-client,mute-on-join
# Specify the maximum number of records to be sent to the BigBlueButton API in one call
# Default is set to 25 records
PAGINATION_NUMBER=25
# Comment this out to send logs to STDOUT in production instead of log/production.log .
#
# RAILS_LOG_TO_STDOUT=true

View File

@ -0,0 +1,63 @@
# frozen_string_literal: true
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
require "rails_helper"
describe RecordingsController, type: :controller do
before do
@user = create(:user)
@room = @user.main_room
@secondary_user = create(:user)
end
context "POST #update_recording" do
it "updates the recordings details" do
@request.session[:user_id] = @user.uid
post :update_recording, params: { meetingID: @room.bbb_id, record_id: Faker::IDNumber.valid, state: "public" }
expect(response).to have_http_status(302)
end
it "redirects to root if not the room owner" do
@request.session[:user_id] = @secondary_user.uid
post :update_recording, params: { meetingID: @room.bbb_id, record_id: Faker::IDNumber.valid, state: "public" }
expect(response).to redirect_to(root_path)
end
end
context "DELETE #delete_recording" do
it "deletes the recording" do
@request.session[:user_id] = @user.uid
post :delete_recording, params: { meetingID: @room.bbb_id, record_id: Faker::IDNumber.valid, state: "public" }
expect(response).to have_http_status(302)
end
it "redirects to root if not the room owner" do
@request.session[:user_id] = @secondary_user.uid
post :delete_recording, params: { meetingID: @room.bbb_id, record_id: Faker::IDNumber.valid, state: "public" }
expect(response).to redirect_to(root_path)
end
end
end

View File

@ -153,5 +153,14 @@ describe Room, type: :model do
playbacks: %w(presentation),
)
end
it "deletes the recording" do
allow_any_instance_of(BigBlueButton::BigBlueButtonApi).to receive(:delete_recordings).and_return(
returncode: true, deleted: true
)
expect(@room.delete_recording(Faker::IDNumber.valid))
.to contain_exactly([:returncode, true], [:deleted, true])
end
end
end