modified lib-jitsi-meet dev repo
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. this.userId;
  41. this.callStatsUserName;
  42. var localStorage = getLocalStorage();
  43. if (localStorage) {
  44. this.userId
  45. = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
  46. this.callStatsUserName
  47. = localStorage.getItem('callStatsUserName')
  48. || generateCallStatsUsername();
  49. this.save();
  50. } else {
  51. logger.log("localStorage is not supported");
  52. this.userId = generateJitsiMeetId();
  53. this.callStatsUserName = generateCallStatsUsername();
  54. }
  55. }
  56. /**
  57. * Save settings to localStorage if browser supports that.
  58. */
  59. save () {
  60. var localStorage = getLocalStorage();
  61. if (localStorage) {
  62. localStorage.setItem('jitsiMeetId', this.userId);
  63. localStorage.setItem('callStatsUserName', this.callStatsUserName);
  64. }
  65. }
  66. /**
  67. * Returns current machine id.
  68. * @returns {string} machine id
  69. */
  70. getMachineId () {
  71. return this.userId;
  72. }
  73. /**
  74. * Returns fake username for callstats
  75. * @returns {string} fake username for callstats
  76. */
  77. getCallStatsUserName () {
  78. return this.callStatsUserName;
  79. }
  80. /**
  81. * Save current session id.
  82. * @param {string} sessionId session id
  83. */
  84. setSessionId (sessionId) {
  85. const localStorage = getLocalStorage();
  86. if (localStorage) {
  87. if (sessionId) {
  88. localStorage.setItem('sessionId', sessionId);
  89. } else {
  90. localStorage.removeItem('sessionId');
  91. }
  92. }
  93. }
  94. /**
  95. * Clear current session id.
  96. */
  97. clearSessionId () {
  98. this.setSessionId(undefined);
  99. }
  100. /**
  101. * Returns current session id.
  102. * @returns {string} current session id
  103. */
  104. getSessionId () {
  105. // We may update sessionId in localStorage from another JitsiConference
  106. // instance and that's why we should always re-read it.
  107. const localStorage = getLocalStorage();
  108. return localStorage ? localStorage.getItem('sessionId') : undefined;
  109. }
  110. }
  111. export default new Settings();