You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RoomLocker.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* global APP, JitsiMeetJS */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import RequirePasswordDialog from './RequirePasswordDialog';
  4. /**
  5. * Show notification that user cannot set password for the conference
  6. * because server doesn't support that.
  7. */
  8. function notifyPasswordNotSupported () {
  9. logger.warn('room passwords not supported');
  10. APP.UI.messageHandler.showError(
  11. "dialog.warning", "dialog.passwordNotSupported");
  12. }
  13. /**
  14. * Show notification that setting password for the conference failed.
  15. * @param {Error} err error
  16. */
  17. function notifyPasswordFailed(err) {
  18. logger.warn('setting password failed', err);
  19. APP.UI.messageHandler.showError(
  20. "dialog.lockTitle", "dialog.lockMessage");
  21. }
  22. const ConferenceErrors = JitsiMeetJS.errors.conference;
  23. /**
  24. * Create new RoomLocker for the conference.
  25. * It allows to set or remove password for the conference,
  26. * or ask for required password.
  27. * @returns {RoomLocker}
  28. */
  29. export default function createRoomLocker (room) {
  30. let password;
  31. let requirePasswordDialog = new RequirePasswordDialog();
  32. /**
  33. * If the room was locked from someone other than us, we indicate it with
  34. * this property in order to have correct roomLocker state of isLocked.
  35. * @type {boolean} whether room is locked, but not from us.
  36. */
  37. let lockedElsewhere = false;
  38. /**
  39. * @class RoomLocker
  40. */
  41. return {
  42. get isLocked () {
  43. return !!password || lockedElsewhere;
  44. },
  45. get password () {
  46. return password;
  47. },
  48. /**
  49. * Allows to set new password
  50. * @param newPass
  51. * @returns {Promise.<TResult>}
  52. */
  53. lock (newPass) {
  54. return room.lock(newPass).then(() => {
  55. password = newPass;
  56. // If the password is undefined this means that we're removing
  57. // it for everyone.
  58. if (!password)
  59. lockedElsewhere = false;
  60. }).catch(function (err) {
  61. logger.error(err);
  62. if (err === ConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  63. notifyPasswordNotSupported();
  64. } else {
  65. notifyPasswordFailed(err);
  66. }
  67. throw err;
  68. });
  69. },
  70. /**
  71. * Sets that the room is locked from another user, not us.
  72. * @param {boolean} value locked/unlocked state
  73. */
  74. set lockedElsewhere (value) {
  75. lockedElsewhere = value;
  76. },
  77. /**
  78. * Whether room is locked from someone else.
  79. * @returns {boolean} whether room is not locked locally,
  80. * but it is still locked.
  81. */
  82. get lockedElsewhere () {
  83. return lockedElsewhere;
  84. },
  85. /**
  86. * Reset the password. Can be useful when room
  87. * has been unlocked from elsewhere and we can use
  88. * this method for sync the pass
  89. */
  90. resetPassword() {
  91. password = null;
  92. },
  93. /**
  94. * Asks user for required conference password.
  95. */
  96. requirePassword () {
  97. return requirePasswordDialog.askForPassword().then(
  98. newPass => { password = newPass; }
  99. ).catch(
  100. reason => {
  101. // user canceled, no pass was entered.
  102. // clear, as if we use the same instance several times
  103. // pass stays between attempts
  104. password = null;
  105. if (reason !== APP.UI.messageHandler.CANCEL)
  106. logger.error(reason);
  107. }
  108. );
  109. },
  110. /**
  111. * Hides require password dialog
  112. */
  113. hideRequirePasswordDialog() {
  114. if (requirePasswordDialog.isOpened) {
  115. requirePasswordDialog.close();
  116. }
  117. }
  118. };
  119. }