Recorder.format_recordings()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
c 0
b 0
f 0
dl 0
loc 29
rs 8.2506
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
module Recorder
20
  extend ActiveSupport::Concern
21
  include RecordingsHelper
22
23
  # Fetches all recordings for a room.
24
  def recordings(room_bbb_id, search_params = {}, ret_search_params = false)
25
    res = get_recordings(room_bbb_id)
26
27
    format_recordings(res, search_params, ret_search_params)
28
  end
29
30
  # Fetches a rooms public recordings.
31
  def public_recordings(room_bbb_id, search_params = {}, ret_search_params = false)
32
    search, order_col, order_dir, recs = recordings(room_bbb_id, search_params, ret_search_params)
33
    [search, order_col, order_dir, recs.select { |r| r[:metadata][:"gl-listed"] == "true" }]
34
  end
35
36
  # Makes paginated API calls to get recordings
37
  def all_recordings(room_bbb_ids, search_params = {}, ret_search_params = false, search_name = false)
38
    res = { recordings: [] }
39
40
    until room_bbb_ids.empty?
41
      # bbb.get_recordings returns an object
42
      # take only the array portion of the object that is returned
43
      full_res = get_multiple_recordings(room_bbb_ids.pop(Rails.configuration.pagination_number))
44
      res[:recordings].push(*full_res[:recordings])
45
    end
46
47
    format_recordings(res, search_params, ret_search_params, search_name)
48
  end
49
50
  # Format, filter, and sort recordings to match their current use in the app
51
  def format_recordings(api_res, search_params, ret_search_params, search_name = false)
52
    search = search_params[:search] || ""
53
    order_col = search_params[:column] && search_params[:direction] != "none" ? search_params[:column] : "end_time"
54
    order_dir = search_params[:column] && search_params[:direction] != "none" ? search_params[:direction] : "desc"
55
56
    search = search.downcase
57
58
    api_res[:recordings].each do |r|
59
      next if r.key?(:error)
60
      # Format playbacks in a more pleasant way.
61
      r[:playbacks] = if !r[:playback] || !r[:playback][:format]
62
        []
63
      elsif r[:playback][:format].is_a?(Array)
64
        r[:playback][:format]
65
      else
66
        [r[:playback][:format]]
67
      end
68
      r.delete(:playback)
69
    end
70
71
    recs = filter_recordings(api_res, search, search_name)
72
    recs = sort_recordings(recs, order_col, order_dir)
73
74
    if ret_search_params
75
      [search, order_col, order_dir, recs]
76
    else
77
      recs
78
    end
79
  end
80
81
  def filter_recordings(api_res, search, search_name = false)
82
    api_res[:recordings].select do |r|
83
             (!r[:metadata].nil? && ((!r[:metadata][:name].nil? &&
84
                    r[:metadata][:name].downcase.include?(search)) ||
85
                  (r[:metadata][:"gl-listed"] == "true" && search == "public") ||
86
                  (r[:metadata][:"gl-listed"] == "false" && search == "unlisted"))) ||
87
               ((r[:metadata].nil? || r[:metadata][:name].nil?) &&
88
                 r[:name].downcase.include?(search)) ||
89
               r[:participants].include?(search) ||
90
               !r[:playbacks].select { |p| p[:type].downcase.include?(search) }.empty? ||
91
               (search_name && Room.find_by(bbb_id: r[:meetingID]).owner.email.downcase.include?(search))
92
    end
93
  end
94
95
  def sort_recordings(recs, order_col, order_dir)
96
    recs = case order_col
97
           when "end_time"
98
              recs.sort_by { |r| r[:endTime] }
99
           when "name"
100
              recs.sort_by do |r|
101
                if !r[:metadata].nil? && !r[:metadata][:name].nil?
102
                  r[:metadata][:name].downcase
103
                else
104
                  r[:name].downcase
105
                end
106
              end
107
           when "length"
108
              recs.sort_by { |r| r[:playbacks].reject { |p| p[:type] == "statistics" }.first[:length] }
109
           when "users"
110
              recs.sort_by { |r| r[:participants] }
111
           when "visibility"
112
              recs.sort_by { |r| r[:metadata][:"gl-listed"] }
113
           when "formats"
114
              recs.sort_by { |r| r[:playbacks].first[:type].downcase }
115
            else
116
              recs.sort_by { |r| r[:endTime] }
117
            end
118
119
    if order_dir == 'asc'
120
      recs
121
    else
122
      recs.reverse
123
    end
124
  end
125
end
126