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.8KB

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