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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. return global.localStorage;
  14. }
  15. /**
  16. *
  17. */
  18. function _p8() {
  19. return `${Math.random().toString(16)}000000000`.substr(2, 8);
  20. }
  21. /**
  22. *
  23. */
  24. function generateUniqueId() {
  25. return _p8() + _p8() + _p8() + _p8();
  26. }
  27. /**
  28. * Generate unique id.
  29. * @returns {string} random unique id
  30. */
  31. function generateJitsiMeetId() {
  32. const jitsiMeetId = generateUniqueId();
  33. logger.log('generated id', jitsiMeetId);
  34. return jitsiMeetId;
  35. }
  36. /**
  37. * Generate fake username for callstats.
  38. * @returns {string} fake random username
  39. */
  40. function generateCallStatsUsername() {
  41. const username = UsernameGenerator.generateUsername();
  42. logger.log('generated callstats uid', username);
  43. return username;
  44. }
  45. /**
  46. *
  47. */
  48. class Settings {
  49. /**
  50. *
  51. */
  52. constructor() {
  53. const localStorage = getLocalStorage();
  54. if (localStorage) {
  55. this.userId
  56. = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
  57. this.callStatsUserName
  58. = localStorage.getItem('callStatsUserName')
  59. || generateCallStatsUsername();
  60. this.save();
  61. } else {
  62. logger.log('localStorage is not supported');
  63. this.userId = generateJitsiMeetId();
  64. this.callStatsUserName = generateCallStatsUsername();
  65. }
  66. }
  67. /**
  68. * Save settings to localStorage if browser supports that.
  69. */
  70. save() {
  71. const localStorage = getLocalStorage();
  72. if (localStorage) {
  73. localStorage.setItem('jitsiMeetId', this.userId);
  74. localStorage.setItem('callStatsUserName', this.callStatsUserName);
  75. }
  76. }
  77. /**
  78. * Returns current machine id.
  79. * @returns {string} machine id
  80. */
  81. getMachineId() {
  82. return this.userId;
  83. }
  84. /**
  85. * Returns fake username for callstats
  86. * @returns {string} fake username for callstats
  87. */
  88. getCallStatsUserName() {
  89. return this.callStatsUserName;
  90. }
  91. /**
  92. * Save current session id.
  93. * @param {string} sessionId session id
  94. */
  95. setSessionId(sessionId) {
  96. const localStorage = getLocalStorage();
  97. if (localStorage) {
  98. if (sessionId) {
  99. localStorage.setItem('sessionId', sessionId);
  100. } else {
  101. localStorage.removeItem('sessionId');
  102. }
  103. }
  104. }
  105. /**
  106. * Clear current session id.
  107. */
  108. clearSessionId() {
  109. this.setSessionId(undefined);
  110. }
  111. /**
  112. * Returns current session id.
  113. * @returns {string} current session id
  114. */
  115. getSessionId() {
  116. // We may update sessionId in localStorage from another JitsiConference
  117. // instance and that's why we should always re-read it.
  118. const localStorage = getLocalStorage();
  119. return localStorage ? localStorage.getItem('sessionId') : undefined;
  120. }
  121. }
  122. export default new Settings();