The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

129 lines
4.0 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. if (statusReblog) {
  59. statusReblog = statusReblog.set('account', accountReblog);
  60. } else {
  61. statusReblog = null;
  62. }
  63. const regex = regexFromFilters(filters);
  64. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  65. return statusBase.withMutations(map => {
  66. map.set('reblog', statusReblog);
  67. map.set('account', accountBase);
  68. map.set('filtered', filtered);
  69. });
  70. }
  71. );
  72. };
  73. const getAlertsBase = state => state.get('alerts');
  74. export const getAlerts = createSelector([getAlertsBase], (base) => {
  75. let arr = [];
  76. base.forEach(item => {
  77. arr.push({
  78. message: item.get('message'),
  79. title: item.get('title'),
  80. key: item.get('key'),
  81. dismissAfter: 5000,
  82. barStyle: {
  83. zIndex: 200,
  84. },
  85. });
  86. });
  87. return arr;
  88. });
  89. export const makeGetNotification = () => {
  90. return createSelector([
  91. (_, base) => base,
  92. (state, _, accountId) => state.getIn(['accounts', accountId]),
  93. ], (base, account) => {
  94. return base.set('account', account);
  95. });
  96. };
  97. export const getAccountGallery = createSelector([
  98. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  99. state => state.get('statuses'),
  100. ], (statusIds, statuses) => {
  101. let medias = ImmutableList();
  102. statusIds.forEach(statusId => {
  103. const status = statuses.get(statusId);
  104. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  105. });
  106. return medias;
  107. });