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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { getLogger } from 'jitsi-meet-logger';
  2. const logger = getLogger(__filename);
  3. import UsernameGenerator from '../util/UsernameGenerator';
  4. /**
  5. * Gets the localStorage of the browser. (Technically, gets the localStorage of
  6. * the global object because there may be no browser but React Native for
  7. * example).
  8. * @returns {Storage} the local Storage object (if any)
  9. */
  10. function getLocalStorage() {
  11. // eslint-disable-next-line no-invalid-this
  12. const global = typeof window === 'undefined' ? this : window;
  13. let storage;
  14. try {
  15. storage = global.localStorage;
  16. } catch (error) {
  17. logger.error(error);
  18. }
  19. return storage;
  20. }
  21. /**
  22. *
  23. */
  24. function _p8() {
  25. return `${Math.random().toString(16)}000000000`.substr(2, 8);
  26. }
  27. /**
  28. *
  29. */
  30. function generateUniqueId() {
  31. return _p8() + _p8() + _p8() + _p8();
  32. }
  33. /**
  34. * Generate unique id.
  35. * @returns {string} random unique id
  36. */
  37. function generateJitsiMeetId() {
  38. const jitsiMeetId = generateUniqueId();
  39. logger.log('generated id', jitsiMeetId);
  40. return jitsiMeetId;
  41. }
  42. /**
  43. * Generate fake username for callstats.
  44. * @returns {string} fake random username
  45. */
  46. function generateCallStatsUsername() {
  47. const username = UsernameGenerator.generateUsername();
  48. logger.log('generated callstats uid', username);
  49. return username;
  50. }
  51. /**
  52. *
  53. */
  54. class Settings {
  55. /**
  56. *
  57. */
  58. constructor() {
  59. const localStorage = getLocalStorage();
  60. if (localStorage) {
  61. this.userId
  62. = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
  63. this.callStatsUserName
  64. = localStorage.getItem('callStatsUserName')
  65. || generateCallStatsUsername();
  66. this.save();
  67. } else {
  68. logger.log('localStorage is not supported');
  69. this.userId = generateJitsiMeetId();
  70. this.callStatsUserName = generateCallStatsUsername();
  71. }
  72. }
  73. /**
  74. * Save settings to localStorage if browser supports that.
  75. */
  76. save() {
  77. const localStorage = getLocalStorage();
  78. if (localStorage) {
  79. localStorage.setItem('jitsiMeetId', this.userId);
  80. localStorage.setItem('callStatsUserName', this.callStatsUserName);
  81. }
  82. }
  83. /**
  84. * Returns current machine id.
  85. * @returns {string} machine id
  86. */
  87. getMachineId() {
  88. return this.userId;
  89. }
  90. /**
  91. * Returns fake username for callstats
  92. * @returns {string} fake username for callstats
  93. */
  94. getCallStatsUserName() {
  95. return this.callStatsUserName;
  96. }
  97. /**
  98. * Save current session id.
  99. * @param {string} sessionId session id
  100. */
  101. setSessionId(sessionId) {
  102. const localStorage = getLocalStorage();
  103. if (localStorage) {
  104. if (sessionId) {
  105. localStorage.setItem('sessionId', sessionId);
  106. } else {
  107. localStorage.removeItem('sessionId');
  108. }
  109. }
  110. }
  111. /**
  112. * Clear current session id.
  113. */
  114. clearSessionId() {
  115. this.setSessionId(undefined);
  116. }
  117. /**
  118. * Returns current session id.
  119. * @returns {string} current session id
  120. */
  121. getSessionId() {
  122. // We may update sessionId in localStorage from another JitsiConference
  123. // instance and that's why we should always re-read it.
  124. const localStorage = getLocalStorage();
  125. return localStorage ? localStorage.getItem('sessionId') : undefined;
  126. }
  127. }
  128. export default new Settings();