bbb_client_controller.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. name: meeting_id,
  23. attendeePW: attendee_pw,
  24. moderatorPW: moderator_pw,
  25. logoutURL: Discourse.base_url
  26. }.to_query
  27. create_url = build_url("create", query)
  28. response = Excon.get(create_url)
  29. if response.status != 200
  30. Rails.logger.warn("Could not create meeting: #{response.inspect}")
  31. return false
  32. end
  33. join_params = {
  34. fullName: current_user.name || current_user.username,
  35. meetingID: meeting_id,
  36. userID: current_user.username,
  37. password: is_moderator ? moderator_pw : attendee_pw
  38. }.to_query
  39. build_url("join", join_params)
  40. end
  41. def get_status(args)
  42. return {} unless SiteSetting.bbb_endpoint && SiteSetting.bbb_secret
  43. url = build_url("getMeetingInfo", "meetingID=#{args['meeting_id']}")
  44. response = Excon.get(url)
  45. data = Hash.from_xml(response.body)
  46. if data['response']['returncode'] == "SUCCESS"
  47. att = data['response']['attendees']['attendee']
  48. usernames = att.is_a?(Array) ? att.pluck("userID") : [att["userID"]]
  49. users = User.where("username IN (?)", usernames)
  50. avatars = users.map do |s|
  51. {
  52. name: s.name || s.username,
  53. avatar_url: s.avatar_template_url.gsub('{size}', '25')
  54. }
  55. end
  56. {
  57. count: data['response']['participantCount'],
  58. avatars: avatars
  59. }
  60. else
  61. {}
  62. end
  63. end
  64. def build_url(type, query)
  65. secret = SiteSetting.bbb_secret
  66. checksum = Digest::SHA1.hexdigest(type + query + secret)
  67. "#{SiteSetting.bbb_endpoint}#{type}?#{query}&checksum=#{checksum}"
  68. end
  69. def is_moderator
  70. return true if current_user.staff?
  71. group = SiteSetting.bbb_moderator_group_name
  72. return true if group.present? && current_user.groups.pluck(:name).include?(group)
  73. end
  74. end
  75. end