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.

Settings.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var UsernameGenerator = require('../util/UsernameGenerator');
  3. /**
  4. * Check if browser supports localStorage.
  5. * @returns {boolean} true if supports, false otherwise
  6. */
  7. function supportsLocalStorage() {
  8. try {
  9. return 'localStorage' in window && window.localStorage !== null;
  10. } catch (e) {
  11. return false;
  12. }
  13. }
  14. function generateUniqueId() {
  15. function _p8() {
  16. return (Math.random().toString(16) + "000000000").substr(2, 8);
  17. }
  18. return _p8() + _p8() + _p8() + _p8();
  19. }
  20. /**
  21. * Generate unique id.
  22. * @returns {string} random unique id
  23. */
  24. function generateJitsiMeetId() {
  25. var jitsiMeetId = generateUniqueId();
  26. logger.log("generated id", jitsiMeetId);
  27. return jitsiMeetId;
  28. }
  29. /**
  30. * Generate fake username for callstats.
  31. * @returns {string} fake random username
  32. */
  33. function generateCallStatsUsername() {
  34. var username = UsernameGenerator.generateUsername();
  35. logger.log('generated callstats uid', username);
  36. return username;
  37. }
  38. function Settings() {
  39. this.userId;
  40. this.callStatsUserName;
  41. if (supportsLocalStorage()) {
  42. this.userId = window.localStorage.getItem('jitsiMeetId')
  43. || generateJitsiMeetId();
  44. this.callStatsUserName = window.localStorage.getItem(
  45. 'callStatsUserName'
  46. ) || generateCallStatsUsername();
  47. this.save();
  48. } else {
  49. logger.log("localStorage is not supported");
  50. this.userId = generateJitsiMeetId();
  51. this.callStatsUserName = generateCallStatsUsername();
  52. }
  53. }
  54. /**
  55. * Save settings to localStorage if browser supports that.
  56. */
  57. Settings.prototype.save = function () {
  58. if (!supportsLocalStorage()) {
  59. return;
  60. }
  61. window.localStorage.setItem('jitsiMeetId', this.userId);
  62. window.localStorage.setItem('callStatsUserName', this.callStatsUserName);
  63. };
  64. /**
  65. * Returns current user id.
  66. * @returns {string} user id
  67. */
  68. Settings.prototype.getUserId = function () {
  69. return this.userId;
  70. };
  71. /**
  72. * Returns fake username for callstats
  73. * @returns {string} fake username for callstats
  74. */
  75. Settings.prototype.getCallStatsUserName = function () {
  76. return this.callStatsUserName;
  77. };
  78. /**
  79. * Save current session id.
  80. * @param {string} sessionId session id
  81. */
  82. Settings.prototype.setSessionId = function (sessionId) {
  83. if (sessionId) {
  84. window.localStorage.setItem('sessionId', sessionId);
  85. } else {
  86. window.localStorage.removeItem('sessionId');
  87. }
  88. };
  89. /**
  90. * Clear current session id.
  91. */
  92. Settings.prototype.clearSessionId = function () {
  93. this.setSessionId(undefined);
  94. };
  95. /**
  96. * Returns current session id.
  97. * @returns {string} current session id
  98. */
  99. Settings.prototype.getSessionId = function () {
  100. // we can update session id in localStorage from
  101. // another JitsiConference instance
  102. // thats why we should always re-read it
  103. return window.localStorage.getItem('sessionId');
  104. };
  105. module.exports = Settings;