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.

144 lines
4.3 KiB

  1. import { createSelector } from 'reselect';
  2. import { List as ImmutableList } from 'immutable';
  3. import { me } from 'flavours/glitch/util/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. const regex = (accountReblog || accountBase).get('id') !== me && regexFromFilters(filters);
  68. let filtered = false;
  69. if (statusReblog) {
  70. filtered = regex && regex.test(statusReblog.get('search_index'));
  71. statusReblog = statusReblog.set('account', accountReblog);
  72. statusReblog = statusReblog.set('filtered', filtered);
  73. } else {
  74. statusReblog = null;
  75. }
  76. filtered = filtered || regex && regex.test(statusBase.get('search_index'));
  77. return statusBase.withMutations(map => {
  78. map.set('reblog', statusReblog);
  79. map.set('account', accountBase);
  80. map.set('filtered', filtered);
  81. });
  82. }
  83. );
  84. };
  85. const getAlertsBase = state => state.get('alerts');
  86. export const getAlerts = createSelector([getAlertsBase], (base) => {
  87. let arr = [];
  88. base.forEach(item => {
  89. arr.push({
  90. message: item.get('message'),
  91. title: item.get('title'),
  92. key: item.get('key'),
  93. dismissAfter: 5000,
  94. barStyle: {
  95. zIndex: 200,
  96. },
  97. });
  98. });
  99. return arr;
  100. });
  101. export const makeGetNotification = () => {
  102. return createSelector([
  103. (_, base) => base,
  104. (state, _, accountId) => state.getIn(['accounts', accountId]),
  105. ], (base, account) => {
  106. return base.set('account', account);
  107. });
  108. };
  109. export const getAccountGallery = createSelector([
  110. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  111. state => state.get('statuses'),
  112. ], (statusIds, statuses) => {
  113. let medias = ImmutableList();
  114. statusIds.forEach(statusId => {
  115. const status = statuses.get(statusId);
  116. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  117. });
  118. return medias;
  119. });