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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* global APP, JitsiMeetJS */
  2. import UIUtil from '../util/UIUtil';
  3. //FIXME:
  4. import AnalyticsAdapter from '../../statistics/AnalyticsAdapter';
  5. /**
  6. * Show dialog which asks user for new password for the conference.
  7. * @returns {Promise<string>} password or nothing if user canceled
  8. */
  9. function askForNewPassword () {
  10. let passMsg = APP.translation.generateTranslationHTML("dialog.passwordMsg");
  11. let yourPassMsg = APP.translation.translateString("dialog.yourPassword");
  12. let msg = `
  13. <h2>${passMsg}</h2>
  14. <input name="lockKey" type="text"
  15. data-i18n="[placeholder]dialog.yourPassword"
  16. placeholder="${yourPassMsg}" autofocus>
  17. `;
  18. return new Promise(function (resolve, reject) {
  19. APP.UI.messageHandler.openTwoButtonDialog(
  20. null, null, null,
  21. msg, false, "dialog.Save",
  22. function (e, v, m, f) {
  23. if (v && f.lockKey) {
  24. resolve(UIUtil.escapeHtml(f.lockKey));
  25. }
  26. else {
  27. reject(APP.UI.messageHandler.CANCEL);
  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. APP.UI.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(APP.UI.messageHandler.CANCEL);
  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. APP.UI.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(APP.UI.messageHandler.CANCEL);
  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. APP.UI.messageHandler.showError(
  91. "dialog.warning", "dialog.passwordNotSupported");
  92. }
  93. /**
  94. * Show notification that setting password for the conference failed.
  95. * @param {Error} err error
  96. */
  97. function notifyPasswordFailed(err) {
  98. console.warn('setting password failed', err);
  99. APP.UI.messageHandler.showError(
  100. "dialog.lockTitle", "dialog.lockMessage");
  101. }
  102. const ConferenceErrors = JitsiMeetJS.errors.conference;
  103. /**
  104. * Create new RoomLocker for the conference.
  105. * It allows to set or remove password for the conference,
  106. * or ask for required password.
  107. * @returns {RoomLocker}
  108. */
  109. export default function createRoomLocker (room) {
  110. let password;
  111. function lock (newPass) {
  112. return room.lock(newPass).then(function () {
  113. password = newPass;
  114. }).catch(function (err) {
  115. console.error(err);
  116. if (err === ConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  117. notifyPasswordNotSupported();
  118. } else {
  119. notifyPasswordFailed(err);
  120. }
  121. throw err;
  122. });
  123. }
  124. /**
  125. * @class RoomLocker
  126. */
  127. return {
  128. get isLocked () {
  129. return !!password;
  130. },
  131. get password () {
  132. return password;
  133. },
  134. /**
  135. * Allows to remove password from the conference (asks user first).
  136. * @returns {Promise}
  137. */
  138. askToUnlock () {
  139. return askToUnlock().then(
  140. () => { return lock(); }
  141. ).then(function () {
  142. AnalyticsAdapter.sendEvent('toolbar.lock.disabled');
  143. }).catch(
  144. reason => {
  145. if (reason !== APP.UI.messageHandler.CANCEL)
  146. console.error(reason);
  147. }
  148. );
  149. },
  150. /**
  151. * Allows to set password for the conference.
  152. * It asks user for new password and locks the room.
  153. * @returns {Promise}
  154. */
  155. askToLock () {
  156. return askForNewPassword().then(
  157. newPass => { return lock(newPass);}
  158. ).then(function () {
  159. AnalyticsAdapter.sendEvent('toolbar.lock.enabled');
  160. }).catch(
  161. reason => {
  162. if (reason !== APP.UI.messageHandler.CANCEL)
  163. console.error(reason);
  164. }
  165. );
  166. },
  167. /**
  168. * Asks user for required conference password.
  169. */
  170. requirePassword () {
  171. return askForPassword().then(
  172. newPass => { password = newPass; }
  173. ).catch(
  174. reason => {
  175. if (reason !== APP.UI.messageHandler.CANCEL)
  176. console.error(reason);
  177. }
  178. );
  179. },
  180. /**
  181. * Show notification that to set/remove password user must be moderator.
  182. */
  183. notifyModeratorRequired () {
  184. if (password) {
  185. APP.UI.messageHandler
  186. .openMessageDialog(null, "dialog.passwordError");
  187. } else {
  188. APP.UI.messageHandler
  189. .openMessageDialog(null, "dialog.passwordError2");
  190. }
  191. }
  192. };
  193. }