bbb_client_controller.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: Discourse.base_url
  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. usernames = att.is_a?(Array) ? att.pluck("userID") : [att["userID"]]
  48. users = User.where("username IN (?)", usernames)
  49. avatars = users.map do |s|
  50. {
  51. name: s.name || s.username,
  52. avatar_url: s.avatar_template_url.gsub('{size}', '25')
  53. }
  54. end
  55. {
  56. count: data['response']['participantCount'],
  57. avatars: avatars
  58. }
  59. else
  60. {}
  61. end
  62. end
  63. def build_url(type, query)
  64. secret = SiteSetting.bbb_secret
  65. checksum = Digest::SHA1.hexdigest(type + query + secret)
  66. "#{SiteSetting.bbb_endpoint}#{type}?#{query}&checksum=#{checksum}"
  67. end
  68. def is_moderator
  69. return true if current_user.staff?
  70. group = SiteSetting.bbb_moderator_group_name
  71. return true if group.present? && current_user.groups.pluck(:name).include?(group)
  72. end
  73. end
  74. end