index.js 4.2 KB

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