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.

132 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. return filter.get('whole_word') ? `\\b${expr}\\b` : expr;
  43. }).join('|'), 'i');
  44. };
  45. export const makeGetStatus = () => {
  46. return createSelector(
  47. [
  48. (state, { id }) => state.getIn(['statuses', id]),
  49. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  50. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  51. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  52. getFilters,
  53. ],
  54. (statusBase, statusReblog, accountBase, accountReblog, filters) => {
  55. if (!statusBase) {
  56. return null;
  57. }
  58. const regex = regexFromFilters(filters);
  59. let filtered = false;
  60. if (statusReblog) {
  61. filtered = regex && regex.test(statusReblog.get('search_index'));
  62. statusReblog = statusReblog.set('account', accountReblog);
  63. statusReblog = statusReblog.set('filtered', filtered);
  64. } else {
  65. statusReblog = null;
  66. }
  67. filtered = filtered || regex && regex.test(statusBase.get('search_index'));
  68. return statusBase.withMutations(map => {
  69. map.set('reblog', statusReblog);
  70. map.set('account', accountBase);
  71. map.set('filtered', filtered);
  72. });
  73. }
  74. );
  75. };
  76. const getAlertsBase = state => state.get('alerts');
  77. export const getAlerts = createSelector([getAlertsBase], (base) => {
  78. let arr = [];
  79. base.forEach(item => {
  80. arr.push({
  81. message: item.get('message'),
  82. title: item.get('title'),
  83. key: item.get('key'),
  84. dismissAfter: 5000,
  85. barStyle: {
  86. zIndex: 200,
  87. },
  88. });
  89. });
  90. return arr;
  91. });
  92. export const makeGetNotification = () => {
  93. return createSelector([
  94. (_, base) => base,
  95. (state, _, accountId) => state.getIn(['accounts', accountId]),
  96. ], (base, account) => {
  97. return base.set('account', account);
  98. });
  99. };
  100. export const getAccountGallery = createSelector([
  101. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  102. state => state.get('statuses'),
  103. ], (statusIds, statuses) => {
  104. let medias = ImmutableList();
  105. statusIds.forEach(statusId => {
  106. const status = statuses.get(statusId);
  107. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  108. });
  109. return medias;
  110. });