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.

166 lines
5.1 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. case 'account':
  26. return columnType;
  27. default:
  28. if (columnType.indexOf('list:') > -1) {
  29. return 'home';
  30. } else {
  31. return 'public'; // community, account, hashtag
  32. }
  33. }
  34. };
  35. const escapeRegExp = string =>
  36. string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  37. const regexFromFilters = filters => {
  38. if (filters.size === 0) {
  39. return null;
  40. }
  41. return new RegExp(filters.map(filter => {
  42. let expr = escapeRegExp(filter.get('phrase'));
  43. if (filter.get('whole_word')) {
  44. if (/^[\w]/.test(expr)) {
  45. expr = `\\b${expr}`;
  46. }
  47. if (/[\w]$/.test(expr)) {
  48. expr = `${expr}\\b`;
  49. }
  50. }
  51. return expr;
  52. }).join('|'), 'i');
  53. };
  54. // Memoize the filter regexps for each valid server contextType
  55. const makeGetFiltersRegex = () => {
  56. let memo = {};
  57. return (state, { contextType }) => {
  58. if (!contextType) return ImmutableList();
  59. const serverSideType = toServerSideType(contextType);
  60. 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())));
  61. if (!memo[serverSideType] || !is(memo[serverSideType].filters, filters)) {
  62. const dropRegex = regexFromFilters(filters.filter(filter => filter.get('irreversible')));
  63. const regex = regexFromFilters(filters);
  64. memo[serverSideType] = { filters: filters, results: [dropRegex, regex] };
  65. }
  66. return memo[serverSideType].results;
  67. };
  68. };
  69. export const getFiltersRegex = makeGetFiltersRegex();
  70. export const makeGetStatus = () => {
  71. return createSelector(
  72. [
  73. (state, { id }) => state.getIn(['statuses', id]),
  74. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  75. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  76. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  77. getFiltersRegex,
  78. ],
  79. (statusBase, statusReblog, accountBase, accountReblog, filtersRegex) => {
  80. if (!statusBase) {
  81. return null;
  82. }
  83. if (statusReblog) {
  84. statusReblog = statusReblog.set('account', accountReblog);
  85. } else {
  86. statusReblog = null;
  87. }
  88. const dropRegex = (accountReblog || accountBase).get('id') !== me && filtersRegex[0];
  89. if (dropRegex && dropRegex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'))) {
  90. return null;
  91. }
  92. const regex = (accountReblog || accountBase).get('id') !== me && filtersRegex[1];
  93. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  94. return statusBase.withMutations(map => {
  95. map.set('reblog', statusReblog);
  96. map.set('account', accountBase);
  97. map.set('filtered', filtered);
  98. });
  99. }
  100. );
  101. };
  102. const getAlertsBase = state => state.get('alerts');
  103. export const getAlerts = createSelector([getAlertsBase], (base) => {
  104. let arr = [];
  105. base.forEach(item => {
  106. arr.push({
  107. message: item.get('message'),
  108. message_values: item.get('message_values'),
  109. title: item.get('title'),
  110. key: item.get('key'),
  111. dismissAfter: 5000,
  112. barStyle: {
  113. zIndex: 200,
  114. },
  115. });
  116. });
  117. return arr;
  118. });
  119. export const makeGetNotification = () => {
  120. return createSelector([
  121. (_, base) => base,
  122. (state, _, accountId) => state.getIn(['accounts', accountId]),
  123. ], (base, account) => {
  124. return base.set('account', account);
  125. });
  126. };
  127. export const getAccountGallery = createSelector([
  128. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  129. state => state.get('statuses'),
  130. ], (statusIds, statuses) => {
  131. let medias = ImmutableList();
  132. statusIds.forEach(statusId => {
  133. const status = statuses.get(statusId);
  134. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  135. });
  136. return medias;
  137. });