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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* global APP, JitsiMeetJS */
  2. import messageHandler from '../util/MessageHandler';
  3. import UIUtil from '../util/UIUtil';
  4. //FIXME:
  5. import AnalyticsAdapter from '../../statistics/AnalyticsAdapter';
  6. /**
  7. * Show dialog which asks user for new password for the conference.
  8. * @returns {Promise<string>} password or nothing if user canceled
  9. */
  10. function askForNewPassword () {
  11. let passMsg = APP.translation.generateTranslationHTML("dialog.passwordMsg");
  12. let yourPassMsg = APP.translation.translateString("dialog.yourPassword");
  13. let msg = `
  14. <h2>${passMsg}</h2>
  15. <input name="lockKey" type="text"
  16. data-i18n="[placeholder]dialog.yourPassword"
  17. placeholder="${yourPassMsg}" autofocus>
  18. `;
  19. return new Promise(function (resolve, reject) {
  20. messageHandler.openTwoButtonDialog(
  21. null, null, null,
  22. msg, false, "dialog.Save",
  23. function (e, v, m, f) {
  24. if (v && f.lockKey) {
  25. resolve(UIUtil.escapeHtml(f.lockKey));
  26. } else {
  27. reject();
  28. }
  29. },
  30. null, null, 'input:first'
  31. );
  32. });
  33. }
  34. /**
  35. * Show dialog which asks for required conference password.
  36. * @returns {Promise<string>} password or nothing if user canceled
  37. */
  38. function askForPassword () {
  39. let passRequiredMsg = APP.translation.translateString(
  40. "dialog.passwordRequired"
  41. );
  42. let passMsg = APP.translation.translateString("dialog.password");
  43. let msg = `
  44. <h2 data-i18n="dialog.passwordRequired">${passRequiredMsg}</h2>
  45. <input name="lockKey" type="text"
  46. data-i18n="[placeholder]dialog.password"
  47. placeholder="${passMsg}" autofocus>
  48. `;
  49. return new Promise(function (resolve, reject) {
  50. messageHandler.openTwoButtonDialog(
  51. null, null, null, msg,
  52. true, "dialog.Ok",
  53. function (e, v, m, f) {}, null,
  54. function (e, v, m, f) {
  55. if (v && f.lockKey) {
  56. resolve(UIUtil.escapeHtml(f.lockKey));
  57. } else {
  58. reject();
  59. }
  60. },
  61. ':input:first'
  62. );
  63. });
  64. }
  65. /**
  66. * Show dialog which asks if user want remove password from the conference.
  67. * @returns {Promise}
  68. */
  69. function askToUnlock () {
  70. return new Promise(function (resolve, reject) {
  71. messageHandler.openTwoButtonDialog(
  72. null, null, "dialog.passwordCheck",
  73. null, false, "dialog.Remove",
  74. function (e, v) {
  75. if (v) {
  76. resolve();
  77. } else {
  78. reject();
  79. }
  80. }
  81. );
  82. });
  83. }
  84. /**
  85. * Show notification that user cannot set password for the conference
  86. * because server doesn't support that.
  87. */
  88. function notifyPasswordNotSupported () {
  89. console.warn('room passwords not supported');
  90. messageHandler.showError("dialog.warning", "dialog.passwordNotSupported");
  91. }
  92. /**
  93. * Show notification that setting password for the conference failed.
  94. * @param {Error} err error
  95. */
  96. function notifyPasswordFailed(err) {
  97. console.warn('setting password failed', err);
  98. messageHandler.showError("dialog.lockTitle", "dialog.lockMessage");
  99. }
  100. const ConferenceErrors = JitsiMeetJS.errors.conference;
  101. /**
  102. * Create new RoomLocker for the conference.
  103. * It allows to set or remove password for the conference,
  104. * or ask for required password.
  105. * @returns {RoomLocker}
  106. */
  107. export default function createRoomLocker (room) {
  108. let password;
  109. function lock (newPass) {
  110. return room.lock(newPass).then(function () {
  111. password = newPass;
  112. }).catch(function (err) {
  113. console.error(err);
  114. if (err === ConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  115. notifyPasswordNotSupported();
  116. } else {
  117. notifyPasswordFailed(err);
  118. }
  119. throw err;
  120. });
  121. }
  122. /**
  123. * @class RoomLocker
  124. */
  125. return {
  126. get isLocked () {
  127. return !!password;
  128. },
  129. get password () {
  130. return password;
  131. },
  132. /**
  133. * Allows to remove password from the conference (asks user first).
  134. * @returns {Promise}
  135. */
  136. askToUnlock () {
  137. return askToUnlock().then(function () {
  138. return lock();
  139. }).then(function () {
  140. AnalyticsAdapter.sendEvent('toolbar.lock.disabled');
  141. });
  142. },
  143. /**
  144. * Allows to set password for the conference.
  145. * It asks user for new password and locks the room.
  146. * @returns {Promise}
  147. */
  148. askToLock () {
  149. return askForNewPassword().then(function (newPass) {
  150. return lock(newPass);
  151. }).then(function () {
  152. AnalyticsAdapter.sendEvent('toolbar.lock.enabled');
  153. });
  154. },
  155. /**
  156. * Asks user for required conference password.
  157. */
  158. requirePassword () {
  159. return askForPassword().then(function (newPass) {
  160. password = newPass;
  161. });
  162. },
  163. /**
  164. * Show notification that to set/remove password user must be moderator.
  165. */
  166. notifyModeratorRequired () {
  167. if (password) {
  168. messageHandler.openMessageDialog(null, "dialog.passwordError");
  169. } else {
  170. messageHandler.openMessageDialog(null, "dialog.passwordError2");
  171. }
  172. }
  173. };
  174. }