Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RoomLocker.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. let dialog = null;
  112. function lock (newPass) {
  113. return room.lock(newPass).then(function () {
  114. password = newPass;
  115. }).catch(function (err) {
  116. console.error(err);
  117. if (err === ConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  118. notifyPasswordNotSupported();
  119. } else {
  120. notifyPasswordFailed(err);
  121. }
  122. throw err;
  123. });
  124. }
  125. /**
  126. * @class RoomLocker
  127. */
  128. return {
  129. get isLocked () {
  130. return !!password;
  131. },
  132. get password () {
  133. return password;
  134. },
  135. /**
  136. * Allows to remove password from the conference (asks user first).
  137. * @returns {Promise}
  138. */
  139. askToUnlock () {
  140. return askToUnlock().then(
  141. () => { return lock(); }
  142. ).then(function () {
  143. AnalyticsAdapter.sendEvent('toolbar.lock.disabled');
  144. }).catch(
  145. reason => {
  146. if (reason !== APP.UI.messageHandler.CANCEL)
  147. console.error(reason);
  148. }
  149. );
  150. },
  151. /**
  152. * Allows to set password for the conference.
  153. * It asks user for new password and locks the room.
  154. * @returns {Promise}
  155. */
  156. askToLock () {
  157. return askForNewPassword().then(
  158. newPass => { return lock(newPass);}
  159. ).then(function () {
  160. AnalyticsAdapter.sendEvent('toolbar.lock.enabled');
  161. }).catch(
  162. reason => {
  163. if (reason !== APP.UI.messageHandler.CANCEL)
  164. console.error(reason);
  165. }
  166. );
  167. },
  168. /**
  169. * Asks user for required conference password.
  170. */
  171. requirePassword () {
  172. return askForPassword().then(
  173. newPass => { password = newPass; }
  174. ).catch(
  175. reason => {
  176. if (reason !== APP.UI.messageHandler.CANCEL)
  177. console.error(reason);
  178. }
  179. );
  180. },
  181. /**
  182. * Show notification that to set/remove password user must be moderator.
  183. */
  184. notifyModeratorRequired () {
  185. if (dialog)
  186. return;
  187. let closeCallback = function () {
  188. dialog = null;
  189. };
  190. if (password) {
  191. dialog = APP.UI.messageHandler
  192. .openMessageDialog(null, "dialog.passwordError",
  193. null, null, closeCallback);
  194. } else {
  195. dialog = APP.UI.messageHandler
  196. .openMessageDialog(null, "dialog.passwordError2",
  197. null, null, closeCallback);
  198. }
  199. }
  200. };
  201. }