闭社主体 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.

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