bbb.js.es6 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { withPluginApi } from "discourse/lib/plugin-api";
  2. import showModal from "discourse/lib/show-modal";
  3. import { iconHTML } from "discourse-common/lib/icon-library";
  4. import { ajax } from "discourse/lib/ajax";
  5. import { popupAjaxError } from "discourse/lib/ajax-error";
  6. function launchBBB($elem) {
  7. const data = $elem.data();
  8. const site = Discourse.__container__.lookup("site:main");
  9. const capabilities = Discourse.__container__.lookup("capabilities:main");
  10. ajax("/bbb/create.json", {
  11. type: "POST",
  12. data: data,
  13. })
  14. .then((res) => {
  15. if (res.url) {
  16. if (
  17. capabilities.isAppWebview ||
  18. (site.mobileView && !data.mobileIframe) ||
  19. (!site.mobileView && !data.desktopIframe)
  20. ) {
  21. window.location.href = res.url;
  22. } else {
  23. $elem.children().hide();
  24. $elem.append(
  25. `<iframe src="${res.url}" allowfullscreen="true" allow="camera; microphone; fullscreen; speaker" width="100%" height="500" style="border:none"></iframe>`
  26. );
  27. }
  28. }
  29. })
  30. .catch(function (error) {
  31. popupAjaxError(error);
  32. });
  33. }
  34. function attachButton($elem) {
  35. const buttonLabel = $elem.data("label") || I18n.t("bbb.launch");
  36. $elem.html(
  37. `<button class='launch-bbb btn'>${iconHTML(
  38. "video"
  39. )} ${buttonLabel}</button>`
  40. );
  41. $elem.find("button").on("click", () => launchBBB($elem));
  42. }
  43. function attachStatus($elem, helper) {
  44. const status = $elem.find(".bbb-status");
  45. const data = $elem.data();
  46. ajax(`/bbb/status/${data.meetingID}.json`).then((res) => {
  47. if (res.avatars) {
  48. status.html(`<span>On the call: </span>`);
  49. res.avatars.forEach(function (avatar) {
  50. status.append(
  51. `<img src="${avatar.avatar_url}" class="avatar" width="25" height="25" title="${avatar.name}" />`
  52. );
  53. });
  54. }
  55. });
  56. }
  57. function attachBBB($elem, helper) {
  58. if (helper) {
  59. $elem.find("[data-wrap=discourse-bbb]").each((idx, val) => {
  60. attachButton($(val));
  61. $(val).append("<span class='bbb-status'></span>");
  62. attachStatus($(val), helper);
  63. });
  64. }
  65. }
  66. export default {
  67. name: "insert-bbb",
  68. initialize() {
  69. withPluginApi("1.13.0", (api) => {
  70. const currentUser = api.getCurrentUser();
  71. const siteSettings = api.container.lookup("site-settings:main");
  72. api.decorateCooked(attachBBB, {
  73. id: "discourse-bbb",
  74. });
  75. if (
  76. !siteSettings.bbb_staff_only ||
  77. (siteSettings.bbb_staff_only && currentUser && currentUser.staff)
  78. ) {
  79. api.addComposerToolbarPopupMenuOption({
  80. icon: "video",
  81. label: "bbb.composer_title",
  82. action: (toolbarEvent) => {
  83. showModal("insert-bbb").setProperties({
  84. toolbarEvent,
  85. });
  86. },
  87. });
  88. }
  89. });
  90. },
  91. };