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.

164 lines
5.0 KiB

  1. import { createSelector } from 'reselect';
  2. import { List as ImmutableList, is } from 'immutable';
  3. import { me } from '../initial_state';
  4. const getAccountBase = (state, id) => state.getIn(['accounts', id], null);
  5. const getAccountCounters = (state, id) => state.getIn(['accounts_counters', id], null);
  6. const getAccountRelationship = (state, id) => state.getIn(['relationships', id], null);
  7. const getAccountMoved = (state, id) => state.getIn(['accounts', state.getIn(['accounts', id, 'moved'])]);
  8. export const makeGetAccount = () => {
  9. return createSelector([getAccountBase, getAccountCounters, getAccountRelationship, getAccountMoved], (base, counters, relationship, moved) => {
  10. if (base === null) {
  11. return null;
  12. }
  13. return base.merge(counters).withMutations(map => {
  14. map.set('relationship', relationship);
  15. map.set('moved', moved);
  16. });
  17. });
  18. };
  19. const toServerSideType = columnType => {
  20. switch (columnType) {
  21. case 'home':
  22. case 'notifications':
  23. case 'public':
  24. case 'thread':
  25. return columnType;
  26. default:
  27. if (columnType.indexOf('list:') > -1) {
  28. return 'home';
  29. } else {
  30. return 'public'; // community, account, hashtag
  31. }
  32. }
  33. };
  34. const escapeRegExp = string =>
  35. string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  36. 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. // Memoize the filter regexps for each valid server contextType
  54. const makeGetFiltersRegex = () => {
  55. let memo = {};
  56. return (state, { contextType }) => {
  57. if (!contextType) return ImmutableList();
  58. const serverSideType = toServerSideType(contextType);
  59. const filters = state.get('filters', ImmutableList()).filter(filter => filter.get('context').includes(serverSideType) && (filter.get('expires_at') === null || Date.parse(filter.get('expires_at')) > (new Date())));
  60. if (!memo[serverSideType] || !is(memo[serverSideType].filters, filters)) {
  61. const dropRegex = regexFromFilters(filters.filter(filter => filter.get('irreversible')));
  62. const regex = regexFromFilters(filters);
  63. memo[serverSideType] = { filters: filters, results: [dropRegex, regex] };
  64. }
  65. return memo[serverSideType].results;
  66. };
  67. };
  68. export const getFiltersRegex = makeGetFiltersRegex();
  69. export const makeGetStatus = () => {
  70. return createSelector(
  71. [
  72. (state, { id }) => state.getIn(['statuses', id]),
  73. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  74. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  75. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  76. getFiltersRegex,
  77. ],
  78. (statusBase, statusReblog, accountBase, accountReblog, filtersRegex) => {
  79. if (!statusBase) {
  80. return null;
  81. }
  82. if (statusReblog) {
  83. statusReblog = statusReblog.set('account', accountReblog);
  84. } else {
  85. statusReblog = null;
  86. }
  87. const dropRegex = (accountReblog || accountBase).get('id') !== me && filtersRegex[0];
  88. if (dropRegex && dropRegex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'))) {
  89. return null;
  90. }
  91. const regex = (accountReblog || accountBase).get('id') !== me && filtersRegex[1];
  92. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  93. return statusBase.withMutations(map => {
  94. map.set('reblog', statusReblog);
  95. map.set('account', accountBase);
  96. map.set('filtered', filtered);
  97. });
  98. }
  99. );
  100. };
  101. const getAlertsBase = state => state.get('alerts');
  102. export const getAlerts = createSelector([getAlertsBase], (base) => {
  103. let arr = [];
  104. base.forEach(item => {
  105. arr.push({
  106. message: item.get('message'),
  107. title: item.get('title'),
  108. key: item.get('key'),
  109. dismissAfter: 5000,
  110. barStyle: {
  111. zIndex: 200,
  112. },
  113. });
  114. });
  115. return arr;
  116. });
  117. export const makeGetNotification = () => {
  118. return createSelector([
  119. (_, base) => base,
  120. (state, _, accountId) => state.getIn(['accounts', accountId]),
  121. ], (base, account) => {
  122. return base.set('account', account);
  123. });
  124. };
  125. export const getAccountGallery = createSelector([
  126. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  127. state => state.get('statuses'),
  128. ], (statusIds, statuses) => {
  129. let medias = ImmutableList();
  130. statusIds.forEach(statusId => {
  131. const status = statuses.get(statusId);
  132. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  133. });
  134. return medias;
  135. });