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.

177 lines
5.5 KiB

  1. import { createSelector } from 'reselect';
  2. import { List as ImmutableList, Map as ImmutableMap, 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. export const makeGetPictureInPicture = () => {
  103. return createSelector([
  104. (state, { id }) => state.get('picture_in_picture').statusId === id,
  105. (state) => state.getIn(['meta', 'layout']) !== 'mobile',
  106. ], (inUse, available) => ImmutableMap({
  107. inUse: inUse && available,
  108. available,
  109. }));
  110. };
  111. const getAlertsBase = state => state.get('alerts');
  112. export const getAlerts = createSelector([getAlertsBase], (base) => {
  113. let arr = [];
  114. base.forEach(item => {
  115. arr.push({
  116. message: item.get('message'),
  117. message_values: item.get('message_values'),
  118. title: item.get('title'),
  119. key: item.get('key'),
  120. dismissAfter: 5000,
  121. barStyle: {
  122. zIndex: 200,
  123. },
  124. });
  125. });
  126. return arr;
  127. });
  128. export const makeGetNotification = () => {
  129. return createSelector([
  130. (_, base) => base,
  131. (state, _, accountId) => state.getIn(['accounts', accountId]),
  132. ], (base, account) => {
  133. return base.set('account', account);
  134. });
  135. };
  136. export const getAccountGallery = createSelector([
  137. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  138. state => state.get('statuses'),
  139. (state, id) => state.getIn(['accounts', id]),
  140. ], (statusIds, statuses, account) => {
  141. let medias = ImmutableList();
  142. statusIds.forEach(statusId => {
  143. const status = statuses.get(statusId);
  144. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status).set('account', account)));
  145. });
  146. return medias;
  147. });