bbb_client_controller.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. require 'digest/sha1'
  3. module BigBlue
  4. class BbbClientController < ApplicationController
  5. before_action :ensure_logged_in
  6. def create
  7. render json: {
  8. url: create_and_join(params)
  9. }
  10. end
  11. def status
  12. render json: get_status(params)
  13. end
  14. private
  15. def create_and_join(args)
  16. return false unless SiteSetting.bbb_endpoint && SiteSetting.bbb_secret
  17. meeting_id = args['meetingID']
  18. attendee_pw = args['attendeePW']
  19. moderator_pw = args['moderatorPW']
  20. query = {
  21. meetingID: meeting_id,
  22. attendeePW: attendee_pw,
  23. moderatorPW: moderator_pw,
  24. logoutURL: SiteSetting.bbb_full_window ? Discourse.base_url : "default"
  25. }.to_query
  26. create_url = build_url("create", query)
  27. response = Excon.get(create_url)
  28. if response.status != 200
  29. Rails.logger.warn("Could not create meeting: #{response.inspect}")
  30. return false
  31. end
  32. join_params = {
  33. fullName: current_user.name || current_user.username,
  34. meetingID: meeting_id,
  35. userID: current_user.username,
  36. password: is_moderator ? moderator_pw : attendee_pw
  37. }.to_query
  38. build_url("join", join_params)
  39. end
  40. def get_status(args)
  41. return {} unless SiteSetting.bbb_endpoint && SiteSetting.bbb_secret
  42. url = build_url("getMeetingInfo", "meetingID=#{args['meeting_id']}")
  43. response = Excon.get(url)
  44. data = Hash.from_xml(response.body)
  45. if data['response']['returncode'] == "SUCCESS"
  46. att = data['response']['attendees']['attendee']
  47. {
  48. count: data['response']['participantCount'],
  49. usernames: att.is_a?(Array) ? att.pluck("userID") : [att["userID"]]
  50. }
  51. else
  52. {}
  53. end
  54. end
  55. def build_url(type, query)
  56. secret = SiteSetting.bbb_secret
  57. checksum = Digest::SHA1.hexdigest(type + query + secret)
  58. "#{SiteSetting.bbb_endpoint}#{type}?#{query}&checksum=#{checksum}"
  59. end
  60. def is_moderator
  61. return true if current_user.staff?
  62. group = SiteSetting.bbb_moderator_group_name
  63. return true if group.present? && current_user.groups.pluck(:name).include?(group)
  64. end
  65. end
  66. end