statuses.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Map as ImmutableMap, fromJS } from 'immutable';
  2. import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
  3. import { normalizeStatusTranslation } from '../actions/importer/normalizer';
  4. import {
  5. REBLOG_REQUEST,
  6. REBLOG_FAIL,
  7. UNREBLOG_REQUEST,
  8. UNREBLOG_FAIL,
  9. FAVOURITE_REQUEST,
  10. FAVOURITE_FAIL,
  11. UNFAVOURITE_REQUEST,
  12. UNFAVOURITE_FAIL,
  13. BOOKMARK_REQUEST,
  14. BOOKMARK_FAIL,
  15. UNBOOKMARK_REQUEST,
  16. UNBOOKMARK_FAIL,
  17. } from '../actions/interactions';
  18. import {
  19. STATUS_MUTE_SUCCESS,
  20. STATUS_UNMUTE_SUCCESS,
  21. STATUS_REVEAL,
  22. STATUS_HIDE,
  23. STATUS_COLLAPSE,
  24. STATUS_TRANSLATE_SUCCESS,
  25. STATUS_TRANSLATE_UNDO,
  26. STATUS_FETCH_REQUEST,
  27. STATUS_FETCH_FAIL,
  28. } from '../actions/statuses';
  29. import { TIMELINE_DELETE } from '../actions/timelines';
  30. const importStatus = (state, status) => state.set(status.id, fromJS(status));
  31. const importStatuses = (state, statuses) =>
  32. state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
  33. const deleteStatus = (state, id, references) => {
  34. references.forEach(ref => {
  35. state = deleteStatus(state, ref, []);
  36. });
  37. return state.delete(id);
  38. };
  39. const statusTranslateSuccess = (state, id, translation) => {
  40. return state.withMutations(map => {
  41. map.setIn([id, 'translation'], fromJS(normalizeStatusTranslation(translation, map.get(id))));
  42. const list = map.getIn([id, 'media_attachments']);
  43. if (translation.media_attachments && list) {
  44. translation.media_attachments.forEach(item => {
  45. const index = list.findIndex(i => i.get('id') === item.id);
  46. map.setIn([id, 'media_attachments', index, 'translation'], fromJS({ description: item.description }));
  47. });
  48. }
  49. });
  50. };
  51. const statusTranslateUndo = (state, id) => {
  52. return state.withMutations(map => {
  53. map.deleteIn([id, 'translation']);
  54. map.getIn([id, 'media_attachments']).forEach((item, index) => map.deleteIn([id, 'media_attachments', index, 'translation']));
  55. });
  56. };
  57. const initialState = ImmutableMap();
  58. export default function statuses(state = initialState, action) {
  59. switch(action.type) {
  60. case STATUS_FETCH_REQUEST:
  61. return state.setIn([action.id, 'isLoading'], true);
  62. case STATUS_FETCH_FAIL:
  63. return state.delete(action.id);
  64. case STATUS_IMPORT:
  65. return importStatus(state, action.status);
  66. case STATUSES_IMPORT:
  67. return importStatuses(state, action.statuses);
  68. case FAVOURITE_REQUEST:
  69. return state.setIn([action.status.get('id'), 'favourited'], true);
  70. case FAVOURITE_FAIL:
  71. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
  72. case UNFAVOURITE_REQUEST:
  73. return state.setIn([action.status.get('id'), 'favourited'], false);
  74. case UNFAVOURITE_FAIL:
  75. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], true);
  76. case BOOKMARK_REQUEST:
  77. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], true);
  78. case BOOKMARK_FAIL:
  79. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], false);
  80. case UNBOOKMARK_REQUEST:
  81. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], false);
  82. case UNBOOKMARK_FAIL:
  83. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], true);
  84. case REBLOG_REQUEST:
  85. return state.setIn([action.status.get('id'), 'reblogged'], true);
  86. case REBLOG_FAIL:
  87. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], false);
  88. case UNREBLOG_REQUEST:
  89. return state.setIn([action.status.get('id'), 'reblogged'], false);
  90. case UNREBLOG_FAIL:
  91. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], true);
  92. case STATUS_MUTE_SUCCESS:
  93. return state.setIn([action.id, 'muted'], true);
  94. case STATUS_UNMUTE_SUCCESS:
  95. return state.setIn([action.id, 'muted'], false);
  96. case STATUS_REVEAL:
  97. return state.withMutations(map => {
  98. action.ids.forEach(id => {
  99. if (!(state.get(id) === undefined)) {
  100. map.setIn([id, 'hidden'], false);
  101. }
  102. });
  103. });
  104. case STATUS_HIDE:
  105. return state.withMutations(map => {
  106. action.ids.forEach(id => {
  107. if (!(state.get(id) === undefined)) {
  108. map.setIn([id, 'hidden'], true);
  109. }
  110. });
  111. });
  112. case STATUS_COLLAPSE:
  113. return state.setIn([action.id, 'collapsed'], action.isCollapsed);
  114. case TIMELINE_DELETE:
  115. return deleteStatus(state, action.id, action.references);
  116. case STATUS_TRANSLATE_SUCCESS:
  117. return statusTranslateSuccess(state, action.id, action.translation);
  118. case STATUS_TRANSLATE_UNDO:
  119. return statusTranslateUndo(state, action.id);
  120. default:
  121. return state;
  122. }
  123. }