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.
 
 
 
 

124 lines
3.8 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. const escapeRegExp = string =>
  34. string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  35. const regexFromFilters = filters => {
  36. if (filters.size === 0) {
  37. return null;
  38. }
  39. return new RegExp(filters.map(filter => escapeRegExp(filter.get('phrase'))).join('|'), 'i');
  40. };
  41. export const makeGetStatus = () => {
  42. return createSelector(
  43. [
  44. (state, { id }) => state.getIn(['statuses', id]),
  45. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  46. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  47. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  48. (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()))),
  49. ],
  50. (statusBase, statusReblog, accountBase, accountReblog, filters) => {
  51. if (!statusBase) {
  52. return null;
  53. }
  54. if (statusReblog) {
  55. statusReblog = statusReblog.set('account', accountReblog);
  56. } else {
  57. statusReblog = null;
  58. }
  59. const regex = regexFromFilters(filters);
  60. const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
  61. return statusBase.withMutations(map => {
  62. map.set('reblog', statusReblog);
  63. map.set('account', accountBase);
  64. map.set('filtered', filtered);
  65. });
  66. }
  67. );
  68. };
  69. const getAlertsBase = state => state.get('alerts');
  70. export const getAlerts = createSelector([getAlertsBase], (base) => {
  71. let arr = [];
  72. base.forEach(item => {
  73. arr.push({
  74. message: item.get('message'),
  75. title: item.get('title'),
  76. key: item.get('key'),
  77. dismissAfter: 5000,
  78. barStyle: {
  79. zIndex: 200,
  80. },
  81. });
  82. });
  83. return arr;
  84. });
  85. export const makeGetNotification = () => {
  86. return createSelector([
  87. (_, base) => base,
  88. (state, _, accountId) => state.getIn(['accounts', accountId]),
  89. ], (base, account) => {
  90. return base.set('account', account);
  91. });
  92. };
  93. export const getAccountGallery = createSelector([
  94. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  95. state => state.get('statuses'),
  96. ], (statusIds, statuses) => {
  97. let medias = ImmutableList();
  98. statusIds.forEach(statusId => {
  99. const status = statuses.get(statusId);
  100. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  101. });
  102. return medias;
  103. });