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.

139 lines
4.1 KiB

  1. import { createSelector } from 'reselect';
  2. import { List as ImmutableList } from 'immutable';
  3. const getAccountBase = (state, id) => state.getIn(['accounts', id], null);
  4. const getAccountCounters = (state, id) => state.getIn(['accounts_counters', id], null);
  5. const getAccountRelationship = (state, id) => state.getIn(['relationships', id], null);
  6. const getAccountMoved = (state, id) => state.getIn(['accounts', state.getIn(['accounts', id, 'moved'])]);
  7. export const makeGetAccount = () => {
  8. return createSelector([getAccountBase, getAccountCounters, getAccountRelationship, getAccountMoved], (base, counters, relationship, moved) => {
  9. if (base === null) {
  10. return null;
  11. }
  12. return base.merge(counters).withMutations(map => {
  13. map.set('relationship', relationship);
  14. map.set('moved', moved);
  15. });
  16. });
  17. };
  18. const toServerSideType = columnType => {
  19. switch (columnType) {
  20. case 'home':
  21. case 'notifications':
  22. case 'public':
  23. case 'thread':
  24. return columnType;
  25. default:
  26. if (columnType.indexOf('list:') > -1) {
  27. return 'home';
  28. } else {
  29. return 'public'; // community, account, hashtag
  30. }
  31. }
  32. };
  33. export const getFilters = (state, { contextType }) => state.get('filters', ImmutableList()).filter(filter => contextType && filter.get('context').includes(toServerSideType(contextType)) && (filter.get('expires_at') === null || Date.parse(filter.get('expires_at')) > (new Date())));
  34. const escapeRegExp = string =>
  35. string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  36. export const regexFromFilters = filters => {
  37. if (filters.size === 0) {
  38. return null;
  39. }
  40. return new RegExp(filters.map(filter => {
  41. let expr = escapeRegExp(filter.get('phrase'));
  42. if (filter.get('whole_word')) {
  43. if (/^[\w]/.test(expr)) {
  44. expr = `\\b${expr}`;
  45. }
  46. if (/[\w]$/.test(expr)) {
  47. expr = `${expr}\\b`;
  48. }
  49. }
  50. return expr;
  51. }).join('|'), 'i');
  52. };
  53. export const makeGetStatus = () => {
  54. return createSelector(
  55. [
  56. (state, { id }) => state.getIn(['statuses', id]),
  57. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  58. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  59. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  60. getFilters,
  61. ],
  62. (statusBase, statusReblog, accountBase, accountReblog, filters) => {
  63. if (!statusBase) {
  64. return null;
  65. }
  66. if (statusReblog) {
  67. statusReblog = statusReblog.set('account', accountReblog);
  68. } else {
  69. statusReblog = null;
  70. }
  71. const regex = regexFromFilters(filters);
  72. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  73. return statusBase.withMutations(map => {
  74. map.set('reblog', statusReblog);
  75. map.set('account', accountBase);
  76. map.set('filtered', filtered);
  77. });
  78. }
  79. );
  80. };
  81. const getAlertsBase = state => state.get('alerts');
  82. export const getAlerts = createSelector([getAlertsBase], (base) => {
  83. let arr = [];
  84. base.forEach(item => {
  85. arr.push({
  86. message: item.get('message'),
  87. title: item.get('title'),
  88. key: item.get('key'),
  89. dismissAfter: 5000,
  90. barStyle: {
  91. zIndex: 200,
  92. },
  93. });
  94. });
  95. return arr;
  96. });
  97. export const makeGetNotification = () => {
  98. return createSelector([
  99. (_, base) => base,
  100. (state, _, accountId) => state.getIn(['accounts', accountId]),
  101. ], (base, account) => {
  102. return base.set('account', account);
  103. });
  104. };
  105. export const getAccountGallery = createSelector([
  106. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  107. state => state.get('statuses'),
  108. ], (statusIds, statuses) => {
  109. let medias = ImmutableList();
  110. statusIds.forEach(statusId => {
  111. const status = statuses.get(statusId);
  112. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  113. });
  114. return medias;
  115. });