闭社主体 forked from https://github.com/tootsuite/mastodon
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.

147 lines
4.9 KiB

  1. import {
  2. COMPOSE_MOUNT,
  3. COMPOSE_UNMOUNT,
  4. COMPOSE_CHANGE,
  5. COMPOSE_REPLY,
  6. COMPOSE_REPLY_CANCEL,
  7. COMPOSE_MENTION,
  8. COMPOSE_SUBMIT_REQUEST,
  9. COMPOSE_SUBMIT_SUCCESS,
  10. COMPOSE_SUBMIT_FAIL,
  11. COMPOSE_UPLOAD_REQUEST,
  12. COMPOSE_UPLOAD_SUCCESS,
  13. COMPOSE_UPLOAD_FAIL,
  14. COMPOSE_UPLOAD_UNDO,
  15. COMPOSE_UPLOAD_PROGRESS,
  16. COMPOSE_SUGGESTIONS_CLEAR,
  17. COMPOSE_SUGGESTIONS_READY,
  18. COMPOSE_SUGGESTION_SELECT,
  19. COMPOSE_SENSITIVITY_CHANGE,
  20. COMPOSE_VISIBILITY_CHANGE,
  21. COMPOSE_LISTABILITY_CHANGE
  22. } from '../actions/compose';
  23. import { TIMELINE_DELETE } from '../actions/timelines';
  24. import { ACCOUNT_SET_SELF } from '../actions/accounts';
  25. import Immutable from 'immutable';
  26. const initialState = Immutable.Map({
  27. mounted: false,
  28. sensitive: false,
  29. unlisted: false,
  30. private: false,
  31. text: '',
  32. in_reply_to: null,
  33. is_submitting: false,
  34. is_uploading: false,
  35. progress: 0,
  36. media_attachments: Immutable.List(),
  37. suggestion_token: null,
  38. suggestions: Immutable.List(),
  39. me: null
  40. });
  41. function statusToTextMentions(state, status) {
  42. let set = Immutable.OrderedSet([]);
  43. let me = state.get('me');
  44. if (status.getIn(['account', 'id']) !== me) {
  45. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  46. }
  47. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  48. };
  49. function clearAll(state) {
  50. return state.withMutations(map => {
  51. map.set('text', '');
  52. map.set('is_submitting', false);
  53. map.set('in_reply_to', null);
  54. map.update('media_attachments', list => list.clear());
  55. });
  56. };
  57. function appendMedia(state, media) {
  58. return state.withMutations(map => {
  59. map.update('media_attachments', list => list.push(media));
  60. map.set('is_uploading', false);
  61. map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim());
  62. });
  63. };
  64. function removeMedia(state, mediaId) {
  65. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  66. return state.withMutations(map => {
  67. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  68. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  69. });
  70. };
  71. const insertSuggestion = (state, position, token, completion) => {
  72. return state.withMutations(map => {
  73. map.update('text', oldText => `${oldText.slice(0, position)}${completion}${oldText.slice(position + token.length)}`);
  74. map.set('suggestion_token', null);
  75. map.update('suggestions', Immutable.List(), list => list.clear());
  76. });
  77. };
  78. export default function compose(state = initialState, action) {
  79. switch(action.type) {
  80. case COMPOSE_MOUNT:
  81. return state.set('mounted', true);
  82. case COMPOSE_UNMOUNT:
  83. return state.set('mounted', false);
  84. case COMPOSE_SENSITIVITY_CHANGE:
  85. return state.set('sensitive', action.checked);
  86. case COMPOSE_VISIBILITY_CHANGE:
  87. return state.set('private', action.checked);
  88. case COMPOSE_LISTABILITY_CHANGE:
  89. return state.set('unlisted', action.checked);
  90. case COMPOSE_CHANGE:
  91. return state.set('text', action.text);
  92. case COMPOSE_REPLY:
  93. return state.withMutations(map => {
  94. map.set('in_reply_to', action.status.get('id'));
  95. map.set('text', statusToTextMentions(state, action.status));
  96. });
  97. case COMPOSE_REPLY_CANCEL:
  98. return state.withMutations(map => {
  99. map.set('in_reply_to', null);
  100. map.set('text', '');
  101. });
  102. case COMPOSE_SUBMIT_REQUEST:
  103. return state.set('is_submitting', true);
  104. case COMPOSE_SUBMIT_SUCCESS:
  105. return clearAll(state);
  106. case COMPOSE_SUBMIT_FAIL:
  107. return state.set('is_submitting', false);
  108. case COMPOSE_UPLOAD_REQUEST:
  109. return state.set('is_uploading', true);
  110. case COMPOSE_UPLOAD_SUCCESS:
  111. return appendMedia(state, Immutable.fromJS(action.media));
  112. case COMPOSE_UPLOAD_FAIL:
  113. return state.set('is_uploading', false);
  114. case COMPOSE_UPLOAD_UNDO:
  115. return removeMedia(state, action.media_id);
  116. case COMPOSE_UPLOAD_PROGRESS:
  117. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  118. case COMPOSE_MENTION:
  119. return state.update('text', text => `${text}@${action.account.get('acct')} `);
  120. case COMPOSE_SUGGESTIONS_CLEAR:
  121. return state.update('suggestions', Immutable.List(), list => list.clear()).set('suggestion_token', null);
  122. case COMPOSE_SUGGESTIONS_READY:
  123. return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id))).set('suggestion_token', action.token);
  124. case COMPOSE_SUGGESTION_SELECT:
  125. return insertSuggestion(state, action.position, action.token, action.completion);
  126. case TIMELINE_DELETE:
  127. if (action.id === state.get('in_reply_to')) {
  128. return state.set('in_reply_to', null);
  129. } else {
  130. return state;
  131. }
  132. case ACCOUNT_SET_SELF:
  133. return state.set('me', action.account.id).set('private', action.account.locked);
  134. default:
  135. return state;
  136. }
  137. };