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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const global = typeof window === 'undefined' ? this : window;
  12. return global.localStorage;
  13. }
  14. function _p8() {
  15. return `${Math.random().toString(16)}000000000`.substr(2, 8);
  16. }
  17. function generateUniqueId() {
  18. return _p8() + _p8() + _p8() + _p8();
  19. }
  20. /**
  21. * Generate unique id.
  22. * @returns {string} random unique id
  23. */
  24. function generateJitsiMeetId() {
  25. const 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. const username = UsernameGenerator.generateUsername();
  35. logger.log('generated callstats uid', username);
  36. return username;
  37. }
  38. class Settings {
  39. constructor() {
  40. const 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. save() {
  58. const localStorage = getLocalStorage();
  59. if (localStorage) {
  60. localStorage.setItem('jitsiMeetId', this.userId);
  61. localStorage.setItem('callStatsUserName', this.callStatsUserName);
  62. }
  63. }
  64. /**
  65. * Returns current machine id.
  66. * @returns {string} machine id
  67. */
  68. getMachineId() {
  69. return this.userId;
  70. }
  71. /**
  72. * Returns fake username for callstats
  73. * @returns {string} fake username for callstats
  74. */
  75. getCallStatsUserName() {
  76. return this.callStatsUserName;
  77. }
  78. /**
  79. * Save current session id.
  80. * @param {string} sessionId session id
  81. */
  82. setSessionId(sessionId) {
  83. const 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. clearSessionId() {
  96. this.setSessionId(undefined);
  97. }
  98. /**
  99. * Returns current session id.
  100. * @returns {string} current session id
  101. */
  102. getSessionId() {
  103. // We may update sessionId in localStorage from another JitsiConference
  104. // instance and that's why we should always re-read it.
  105. const localStorage = getLocalStorage();
  106. return localStorage ? localStorage.getItem('sessionId') : undefined;
  107. }
  108. }
  109. export default new Settings();