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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var UsernameGenerator = require('../util/UsernameGenerator');
  3. /**
  4. * Gets the localStorage of the browser. (Technically, gets the localStorage of
  5. * the global object because there may be no browser but React Native for
  6. * example).
  7. * @returns {Storage} the local Storage object (if any)
  8. */
  9. function getLocalStorage() {
  10. var global = typeof window == 'undefined' ? this : window;
  11. return global.localStorage;
  12. }
  13. function generateUniqueId() {
  14. function _p8() {
  15. return (Math.random().toString(16) + "000000000").substr(2, 8);
  16. }
  17. return _p8() + _p8() + _p8() + _p8();
  18. }
  19. /**
  20. * Generate unique id.
  21. * @returns {string} random unique id
  22. */
  23. function generateJitsiMeetId() {
  24. var jitsiMeetId = generateUniqueId();
  25. logger.log("generated id", jitsiMeetId);
  26. return jitsiMeetId;
  27. }
  28. /**
  29. * Generate fake username for callstats.
  30. * @returns {string} fake random username
  31. */
  32. function generateCallStatsUsername() {
  33. var username = UsernameGenerator.generateUsername();
  34. logger.log('generated callstats uid', username);
  35. return username;
  36. }
  37. function Settings() {
  38. this.userId;
  39. this.callStatsUserName;
  40. var localStorage = getLocalStorage();
  41. if (localStorage) {
  42. this.userId
  43. = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
  44. this.callStatsUserName
  45. = localStorage.getItem('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. var localStorage = getLocalStorage();
  59. if (localStorage) {
  60. localStorage.setItem('jitsiMeetId', this.userId);
  61. localStorage.setItem('callStatsUserName', this.callStatsUserName);
  62. }
  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. var localStorage = getLocalStorage();
  84. if (localStorage) {
  85. if (sessionId) {
  86. localStorage.setItem('sessionId', sessionId);
  87. } else {
  88. localStorage.removeItem('sessionId');
  89. }
  90. }
  91. };
  92. /**
  93. * Clear current session id.
  94. */
  95. Settings.prototype.clearSessionId = function () {
  96. this.setSessionId(undefined);
  97. };
  98. /**
  99. * Returns current session id.
  100. * @returns {string} current session id
  101. */
  102. Settings.prototype.getSessionId = function () {
  103. // We may update sessionId in localStorage from another JitsiConference
  104. // instance and that's why we should always re-read it.
  105. var localStorage = getLocalStorage();
  106. return localStorage ? localStorage.getItem('sessionId') : undefined;
  107. };
  108. module.exports = Settings;