您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RoomLocker.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* global APP, JitsiMeetJS */
  2. import UIUtil from '../util/UIUtil';
  3. /**
  4. * Show dialog which asks user for new password for the conference.
  5. * @returns {Promise<string>} password or nothing if user canceled
  6. */
  7. function askForNewPassword () {
  8. let passMsg = APP.translation.generateTranslationHTML("dialog.passwordMsg");
  9. let yourPassMsg = APP.translation.translateString("dialog.yourPassword");
  10. let msg = `
  11. <h2>${passMsg}</h2>
  12. <input name="lockKey" type="text"
  13. data-i18n="[placeholder]dialog.yourPassword"
  14. placeholder="${yourPassMsg}" autofocus>
  15. `;
  16. return new Promise(function (resolve, reject) {
  17. APP.UI.messageHandler.openTwoButtonDialog(
  18. null, null, null,
  19. msg, false, "dialog.Save",
  20. function (e, v, m, f) {
  21. if (v && f.lockKey) {
  22. resolve(UIUtil.escapeHtml(f.lockKey));
  23. }
  24. else {
  25. reject(APP.UI.messageHandler.CANCEL);
  26. }
  27. },
  28. null, null, 'input:first'
  29. );
  30. });
  31. }
  32. /**
  33. * Show dialog which asks for required conference password.
  34. * @returns {Promise<string>} password or nothing if user canceled
  35. */
  36. function askForPassword () {
  37. let passRequiredMsg = APP.translation.translateString(
  38. "dialog.passwordRequired"
  39. );
  40. let passMsg = APP.translation.translateString("dialog.password");
  41. let msg = `
  42. <h2 data-i18n="dialog.passwordRequired">${passRequiredMsg}</h2>
  43. <input name="lockKey" type="text"
  44. data-i18n="[placeholder]dialog.password"
  45. placeholder="${passMsg}" autofocus>
  46. `;
  47. return new Promise(function (resolve, reject) {
  48. APP.UI.messageHandler.openTwoButtonDialog(
  49. null, null, null, msg,
  50. true, "dialog.Ok",
  51. function () {}, null,
  52. function (e, v, m, f) {
  53. if (v && f.lockKey) {
  54. resolve(UIUtil.escapeHtml(f.lockKey));
  55. } else {
  56. reject(APP.UI.messageHandler.CANCEL);
  57. }
  58. },
  59. ':input:first'
  60. );
  61. });
  62. }
  63. /**
  64. * Show dialog which asks if user want remove password from the conference.
  65. * @returns {Promise}
  66. */
  67. function askToUnlock () {
  68. return new Promise(function (resolve, reject) {
  69. APP.UI.messageHandler.openTwoButtonDialog(
  70. null, null, "dialog.passwordCheck",
  71. null, false, "dialog.Remove",
  72. function (e, v) {
  73. if (v) {
  74. resolve();
  75. } else {
  76. reject(APP.UI.messageHandler.CANCEL);
  77. }
  78. }
  79. );
  80. });
  81. }
  82. /**
  83. * Show notification that user cannot set password for the conference
  84. * because server doesn't support that.
  85. */
  86. function notifyPasswordNotSupported () {
  87. console.warn('room passwords not supported');
  88. APP.UI.messageHandler.showError(
  89. "dialog.warning", "dialog.passwordNotSupported");
  90. }
  91. /**
  92. * Show notification that setting password for the conference failed.
  93. * @param {Error} err error
  94. */
  95. function notifyPasswordFailed(err) {
  96. console.warn('setting password failed', err);
  97. APP.UI.messageHandler.showError(
  98. "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. let dialog = null;
  110. /**
  111. * If the room was locked from someone other than us, we indicate it with
  112. * this property in order to have correct roomLocker state of isLocked.
  113. * @type {boolean} whether room is locked, but not from us.
  114. */
  115. let lockedElsewhere = false;
  116. function lock (newPass) {
  117. return room.lock(newPass).then(function () {
  118. password = newPass;
  119. }).catch(function (err) {
  120. console.error(err);
  121. if (err === ConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  122. notifyPasswordNotSupported();
  123. } else {
  124. notifyPasswordFailed(err);
  125. }
  126. throw err;
  127. });
  128. }
  129. /**
  130. * @class RoomLocker
  131. */
  132. return {
  133. get isLocked () {
  134. return !!password || lockedElsewhere;
  135. },
  136. get password () {
  137. return password;
  138. },
  139. /**
  140. * Sets that the room is locked from another user, not us.
  141. * @param {boolean} value locked/unlocked state
  142. */
  143. set lockedElsewhere (value) {
  144. lockedElsewhere = value;
  145. },
  146. /**
  147. * Whether room is locked from someone else.
  148. * @returns {boolean} whether room is not locked locally,
  149. * but it is still locked.
  150. */
  151. get lockedElsewhere () {
  152. return lockedElsewhere;
  153. },
  154. /**
  155. * Allows to remove password from the conference (asks user first).
  156. * @returns {Promise}
  157. */
  158. askToUnlock () {
  159. return askToUnlock().then(
  160. () => { return lock(); }
  161. ).then(function () {
  162. JitsiMeetJS.analytics.sendEvent('toolbar.lock.disabled');
  163. }).catch(
  164. reason => {
  165. if (reason !== APP.UI.messageHandler.CANCEL)
  166. console.error(reason);
  167. }
  168. );
  169. },
  170. /**
  171. * Allows to set password for the conference.
  172. * It asks user for new password and locks the room.
  173. * @returns {Promise}
  174. */
  175. askToLock () {
  176. return askForNewPassword().then(
  177. newPass => { return lock(newPass);}
  178. ).then(function () {
  179. JitsiMeetJS.analytics.sendEvent('toolbar.lock.enabled');
  180. }).catch(
  181. reason => {
  182. if (reason !== APP.UI.messageHandler.CANCEL)
  183. console.error(reason);
  184. }
  185. );
  186. },
  187. /**
  188. * Asks user for required conference password.
  189. */
  190. requirePassword () {
  191. return askForPassword().then(
  192. newPass => { password = newPass; }
  193. ).catch(
  194. reason => {
  195. // user canceled, no pass was entered.
  196. // clear, as if we use the same instance several times
  197. // pass stays between attempts
  198. password = null;
  199. if (reason !== APP.UI.messageHandler.CANCEL)
  200. console.error(reason);
  201. }
  202. );
  203. },
  204. /**
  205. * Show notification that to set/remove password user must be moderator.
  206. */
  207. notifyModeratorRequired () {
  208. if (dialog)
  209. return;
  210. let closeCallback = function () {
  211. dialog = null;
  212. };
  213. if (this.isLocked) {
  214. dialog = APP.UI.messageHandler
  215. .openMessageDialog(null, "dialog.passwordError",
  216. null, null, closeCallback);
  217. } else {
  218. dialog = APP.UI.messageHandler
  219. .openMessageDialog(null, "dialog.passwordError2",
  220. null, null, closeCallback);
  221. }
  222. }
  223. };
  224. }