From 3195bb44297661e9c15b742e10ccf22918f91a7d Mon Sep 17 00:00:00 2001 From: farhatahmad <35435341+farhatahmad@users.noreply.github.com> Date: Tue, 12 Mar 2019 13:50:20 -0400 Subject: [PATCH] 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 --- app/controllers/recordings_controller.rb | 55 ++++++++++ app/controllers/rooms_controller.rb | 30 +----- app/controllers/users_controller.rb | 18 +--- app/models/concerns/api_concern.rb | 101 ++++++++++++++++++ app/models/room.rb | 80 +------------- app/models/user.rb | 26 +++++ .../shared/components/_recording_row.html.erb | 6 +- config/application.rb | 3 + config/routes.rb | 7 +- sample.env | 4 + .../controllers/recordings_controller_spec.rb | 63 +++++++++++ spec/models/room_spec.rb | 9 ++ 12 files changed, 278 insertions(+), 124 deletions(-) create mode 100644 app/controllers/recordings_controller.rb create mode 100644 app/models/concerns/api_concern.rb create mode 100644 spec/controllers/recordings_controller_spec.rb diff --git a/app/controllers/recordings_controller.rb b/app/controllers/recordings_controller.rb new file mode 100644 index 00000000..e1313f89 --- /dev/null +++ b/app/controllers/recordings_controller.rb @@ -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 + +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 diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index c808f184..bbe36cbf 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -17,6 +17,8 @@ # with BigBlueButton; if not, see . 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) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 145b0fa1..32e5f5bf 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -17,11 +17,11 @@ # with BigBlueButton; if not, see . 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 diff --git a/app/models/concerns/api_concern.rb b/app/models/concerns/api_concern.rb new file mode 100644 index 00000000..c338f59e --- /dev/null +++ b/app/models/concerns/api_concern.rb @@ -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 . + +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 diff --git a/app/models/room.rb b/app/models/room.rb index 7d376606..3901c23d 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -17,6 +17,8 @@ # with BigBlueButton; if not, see . 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 diff --git a/app/models/user.rb b/app/models/user.rb index 0f368d21..f234f7ff 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,8 @@ # with BigBlueButton; if not, see . 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) diff --git a/app/views/shared/components/_recording_row.html.erb b/app/views/shared/components/_recording_row.html.erb index 6489a44d..7d1ab090 100644 --- a/app/views/shared/components/_recording_row.html.erb +++ b/app/views/shared/components/_recording_row.html.erb @@ -53,10 +53,10 @@ <% end %> @@ -79,7 +79,7 @@ <% 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 %> <%= t("delete") %> <% end %> diff --git a/config/application.rb b/config/application.rb index c0ceea45..8b59c316 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 3f7be57b..9d38b89d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/sample.env b/sample.env index a5239729..0deddaa1 100644 --- a/sample.env +++ b/sample.env @@ -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 diff --git a/spec/controllers/recordings_controller_spec.rb b/spec/controllers/recordings_controller_spec.rb new file mode 100644 index 00000000..5e727d28 --- /dev/null +++ b/spec/controllers/recordings_controller_spec.rb @@ -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 . + +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 diff --git a/spec/models/room_spec.rb b/spec/models/room_spec.rb index b2d2f4e6..92e777cd 100644 --- a/spec/models/room_spec.rb +++ b/spec/models/room_spec.rb @@ -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