index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { createSelector } from '@reduxjs/toolkit';
  2. import { List as ImmutableList, Map as ImmutableMap } from 'immutable';
  3. import { toServerSideType } from 'mastodon/utils/filters';
  4. import { me } from '../initial_state';
  5. export { makeGetAccount } from "./accounts";
  6. const getFilters = (state, { contextType }) => {
  7. if (!contextType) return null;
  8. const serverSideType = toServerSideType(contextType);
  9. const now = new Date();
  10. return state.get('filters').filter((filter) => filter.get('context').includes(serverSideType) && (filter.get('expires_at') === null || filter.get('expires_at') > now));
  11. };
  12. export const makeGetStatus = () => {
  13. return createSelector(
  14. [
  15. (state, { id }) => state.getIn(['statuses', id]),
  16. (state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
  17. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
  18. (state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
  19. getFilters,
  20. ],
  21. (statusBase, statusReblog, accountBase, accountReblog, filters) => {
  22. if (!statusBase || statusBase.get('isLoading')) {
  23. return null;
  24. }
  25. if (statusReblog) {
  26. statusReblog = statusReblog.set('account', accountReblog);
  27. } else {
  28. statusReblog = null;
  29. }
  30. let filtered = false;
  31. if ((accountReblog || accountBase).get('id') !== me && filters) {
  32. let filterResults = statusReblog?.get('filtered') || statusBase.get('filtered') || ImmutableList();
  33. if (filterResults.some((result) => filters.getIn([result.get('filter'), 'filter_action']) === 'hide')) {
  34. return null;
  35. }
  36. filterResults = filterResults.filter(result => filters.has(result.get('filter')));
  37. if (!filterResults.isEmpty()) {
  38. filtered = filterResults.map(result => filters.getIn([result.get('filter'), 'title']));
  39. }
  40. }
  41. return statusBase.withMutations(map => {
  42. map.set('reblog', statusReblog);
  43. map.set('account', accountBase);
  44. map.set('matched_filters', filtered);
  45. });
  46. },
  47. );
  48. };
  49. export const makeGetPictureInPicture = () => {
  50. return createSelector([
  51. (state, { id }) => state.get('picture_in_picture').statusId === id,
  52. (state) => state.getIn(['meta', 'layout']) !== 'mobile',
  53. ], (inUse, available) => ImmutableMap({
  54. inUse: inUse && available,
  55. available,
  56. }));
  57. };
  58. const ALERT_DEFAULTS = {
  59. dismissAfter: 5000,
  60. style: false,
  61. };
  62. export const getAlerts = createSelector(state => state.get('alerts'), alerts =>
  63. alerts.map(item => ({
  64. ...ALERT_DEFAULTS,
  65. ...item,
  66. })).toArray());
  67. export const makeGetNotification = () => createSelector([
  68. (_, base) => base,
  69. (state, _, accountId) => state.getIn(['accounts', accountId]),
  70. ], (base, account) => base.set('account', account));
  71. export const makeGetReport = () => createSelector([
  72. (_, base) => base,
  73. (state, _, targetAccountId) => state.getIn(['accounts', targetAccountId]),
  74. ], (base, targetAccount) => base.set('target_account', targetAccount));
  75. export const getAccountGallery = createSelector([
  76. (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], ImmutableList()),
  77. state => state.get('statuses'),
  78. (state, id) => state.getIn(['accounts', id]),
  79. ], (statusIds, statuses, account) => {
  80. let medias = ImmutableList();
  81. statusIds.forEach(statusId => {
  82. const status = statuses.get(statusId).set('account', account);
  83. medias = medias.concat(status.get('media_attachments').map(media => media.set('status', status)));
  84. });
  85. return medias;
  86. });
  87. export const getAccountHidden = createSelector([
  88. (state, id) => state.getIn(['accounts', id, 'hidden']),
  89. (state, id) => state.getIn(['relationships', id, 'following']) || state.getIn(['relationships', id, 'requested']),
  90. (state, id) => id === me,
  91. ], (hidden, followingOrRequested, isSelf) => {
  92. return hidden && !(isSelf || followingOrRequested);
  93. });
  94. export const getStatusList = createSelector([
  95. (state, type) => state.getIn(['status_lists', type, 'items']),
  96. ], (items) => items.toList());