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.

145 lines
4.5 KiB

  1. import { createSelector } from 'reselect';
  2. import { List as ImmutableList } 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. 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())));
  35. const escapeRegExp = string =>
  36. string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  37. export 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. export const makeGetStatus = () => {
  55. return createSelector(
  56. [
  57. (state, { id }) => state.getIn(['statuses', id]),
  58. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  59. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  60. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  61. getFilters,
  62. ],
  63. (statusBase, statusReblog, accountBase, accountReblog, filters) => {
  64. if (!statusBase) {
  65. return null;
  66. }
  67. if (statusReblog) {
  68. statusReblog = statusReblog.set('account', accountReblog);
  69. } else {
  70. statusReblog = null;
  71. }
  72. const dropRegex = (accountReblog || accountBase).get('id') !== me && regexFromFilters(filters.filter(filter => filter.get('irreversible')));
  73. if (dropRegex && dropRegex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'))) {
  74. return null;
  75. }
  76. const regex = (accountReblog || accountBase).get('id') !== me && regexFromFilters(filters);
  77. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  78. return statusBase.withMutations(map => {
  79. map.set('reblog', statusReblog);
  80. map.set('account', accountBase);
  81. map.set('filtered', filtered);
  82. });
  83. }
  84. );
  85. };
  86. const getAlertsBase = state => state.get('alerts');
  87. export const getAlerts = createSelector([getAlertsBase], (base) => {
  88. let arr = [];
  89. base.forEach(item => {
  90. arr.push({
  91. message: item.get('message'),
  92. title: item.get('title'),
  93. key: item.get('key'),
  94. dismissAfter: 5000,
  95. barStyle: {
  96. zIndex: 200,
  97. },
  98. });
  99. });
  100. return arr;
  101. });
  102. export const makeGetNotification = () => {
  103. return createSelector([
  104. (_, base) => base,
  105. (state, _, accountId) => state.getIn(['accounts', accountId]),
  106. ], (base, account) => {
  107. return base.set('account', account);
  108. });
  109. };
  110. export const getAccountGallery = createSelector([
  111. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  112. state => state.get('statuses'),
  113. ], (statusIds, statuses) => {
  114. let medias = ImmutableList();
  115. statusIds.forEach(statusId => {
  116. const status = statuses.get(statusId);
  117. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  118. });
  119. return medias;
  120. });