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.

functions.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // @flow
  2. import moment from 'moment';
  3. // MomentJS uses static language bundle loading, so in order to support dynamic
  4. // language selection in the app we need to load all bundles that we support in
  5. // the app.
  6. // FIXME: If we decide to support MomentJS in other features as well we may need
  7. // to move this import and the lenient matcher to the i18n feature.
  8. require('moment/locale/bg');
  9. require('moment/locale/de');
  10. require('moment/locale/eo');
  11. require('moment/locale/es');
  12. require('moment/locale/fr');
  13. require('moment/locale/hy-am');
  14. require('moment/locale/it');
  15. require('moment/locale/nb');
  16. // OC is not available. Please submit OC translation to the MomentJS project.
  17. require('moment/locale/pl');
  18. require('moment/locale/pt');
  19. require('moment/locale/pt-br');
  20. require('moment/locale/ru');
  21. require('moment/locale/sk');
  22. require('moment/locale/sl');
  23. require('moment/locale/sv');
  24. require('moment/locale/tr');
  25. require('moment/locale/zh-cn');
  26. import { i18next } from '../base/i18n';
  27. import { parseURIString } from '../base/util';
  28. /**
  29. * Retrieves the recent room list and generates all the data needed to be
  30. * displayed.
  31. *
  32. * @param {Array<Object>} list - The stored recent list retrieved from redux.
  33. * @returns {Array}
  34. */
  35. export function getRecentRooms(list: Array<Object>): Array<Object> {
  36. const recentRoomDS = [];
  37. if (list.length) {
  38. // We init the locale on every list render, so then it changes
  39. // immediately if a language change happens in the app.
  40. const locale = _getSupportedLocale();
  41. for (const e of list) {
  42. const uri = parseURIString(e.conference);
  43. if (uri && uri.room && uri.hostname) {
  44. const duration
  45. = e.duration || /* legacy */ e.conferenceDuration || 0;
  46. recentRoomDS.push({
  47. baseURL: `${uri.protocol}//${uri.host}`,
  48. conference: e.conference,
  49. dateString: _getDateString(e.date, locale),
  50. dateTimeStamp: e.date,
  51. duration,
  52. durationString: _getDurationString(duration, locale),
  53. initials: _getInitials(uri.room),
  54. room: uri.room,
  55. serverName: uri.hostname
  56. });
  57. }
  58. }
  59. }
  60. return recentRoomDS.reverse();
  61. }
  62. /**
  63. * Returns a well formatted date string to be displayed in the list.
  64. *
  65. * @param {number} dateTimeStamp - The UTC timestamp to be converted to String.
  66. * @param {string} locale - The locale to init the formatter with. Note: This
  67. * locale must be supported by the formatter so ensure this prerequisite before
  68. * invoking the function.
  69. * @private
  70. * @returns {string}
  71. */
  72. function _getDateString(dateTimeStamp: number, locale: string) {
  73. const date = new Date(dateTimeStamp);
  74. const m = _getLocalizedFormatter(date, locale);
  75. if (date.toDateString() === new Date().toDateString()) {
  76. // The date is today, we use fromNow format.
  77. return m.fromNow();
  78. }
  79. return m.format('lll');
  80. }
  81. /**
  82. * Returns a well formatted duration string to be displayed as the conference
  83. * length.
  84. *
  85. * @param {number} duration - The duration in MS.
  86. * @param {string} locale - The locale to init the formatter with. Note: This
  87. * locale must be supported by the formatter so ensure this prerequisite before
  88. * invoking the function.
  89. * @private
  90. * @returns {string}
  91. */
  92. function _getDurationString(duration: number, locale: string) {
  93. return _getLocalizedFormatter(duration, locale).humanize();
  94. }
  95. /**
  96. * Returns the initials supposed to be used based on the room name.
  97. *
  98. * @param {string} room - The room name.
  99. * @private
  100. * @returns {string}
  101. */
  102. function _getInitials(room: string) {
  103. return room && room.charAt(0) ? room.charAt(0).toUpperCase() : '?';
  104. }
  105. /**
  106. * Returns a localized date formatter initialized with a specific {@code Date}
  107. * or duration ({@code number}).
  108. *
  109. * @private
  110. * @param {Date|number} dateOrDuration - The date or duration to format.
  111. * @param {string} locale - The locale to init the formatter with. Note: The
  112. * specified locale must be supported by the formatter so ensure the
  113. * prerequisite is met before invoking the function.
  114. * @returns {Object}
  115. */
  116. function _getLocalizedFormatter(dateOrDuration: Date | number, locale: string) {
  117. const m
  118. = typeof dateOrDuration === 'number'
  119. ? moment.duration(dateOrDuration)
  120. : moment(dateOrDuration);
  121. return m.locale(locale);
  122. }
  123. /**
  124. * A lenient locale matcher to match language and dialect if possible.
  125. *
  126. * @private
  127. * @returns {string}
  128. */
  129. function _getSupportedLocale() {
  130. const i18nLocale = i18next.language;
  131. let supportedLocale;
  132. if (i18nLocale) {
  133. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  134. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  135. if (localeResult) {
  136. const currentLocaleRegexp
  137. = new RegExp(
  138. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  139. supportedLocale
  140. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  141. }
  142. }
  143. return supportedLocale || 'en';
  144. }