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

206 lines
6.7 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_SPOILERNESS_CHANGE,
  21. COMPOSE_SPOILER_TEXT_CHANGE,
  22. COMPOSE_VISIBILITY_CHANGE,
  23. COMPOSE_LISTABILITY_CHANGE,
  24. COMPOSE_EMOJI_INSERT
  25. } from '../actions/compose';
  26. import { TIMELINE_DELETE } from '../actions/timelines';
  27. import { STORE_HYDRATE } from '../actions/store';
  28. import Immutable from 'immutable';
  29. const initialState = Immutable.Map({
  30. mounted: false,
  31. sensitive: false,
  32. spoiler: false,
  33. spoiler_text: '',
  34. privacy: null,
  35. text: '',
  36. focusDate: null,
  37. preselectDate: null,
  38. in_reply_to: null,
  39. is_submitting: false,
  40. is_uploading: false,
  41. progress: 0,
  42. media_attachments: Immutable.List(),
  43. suggestion_token: null,
  44. suggestions: Immutable.List(),
  45. me: null,
  46. default_privacy: 'public',
  47. resetFileKey: Math.floor((Math.random() * 0x10000))
  48. });
  49. function statusToTextMentions(state, status) {
  50. let set = Immutable.OrderedSet([]);
  51. let me = state.get('me');
  52. if (status.getIn(['account', 'id']) !== me) {
  53. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  54. }
  55. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  56. };
  57. function clearAll(state) {
  58. return state.withMutations(map => {
  59. map.set('text', '');
  60. map.set('spoiler', false);
  61. map.set('spoiler_text', '');
  62. map.set('is_submitting', false);
  63. map.set('in_reply_to', null);
  64. map.set('privacy', state.get('default_privacy'));
  65. map.update('media_attachments', list => list.clear());
  66. });
  67. };
  68. function appendMedia(state, media) {
  69. return state.withMutations(map => {
  70. map.update('media_attachments', list => list.push(media));
  71. map.set('is_uploading', false);
  72. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  73. map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim());
  74. });
  75. };
  76. function removeMedia(state, mediaId) {
  77. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  78. const prevSize = state.get('media_attachments').size;
  79. return state.withMutations(map => {
  80. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  81. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  82. if (prevSize === 1) {
  83. map.set('sensitive', false);
  84. }
  85. });
  86. };
  87. const insertSuggestion = (state, position, token, completion) => {
  88. return state.withMutations(map => {
  89. map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  90. map.set('suggestion_token', null);
  91. map.update('suggestions', Immutable.List(), list => list.clear());
  92. map.set('focusDate', new Date());
  93. });
  94. };
  95. const insertEmoji = (state, position, emojiData) => {
  96. const emoji = emojiData.shortname;
  97. return state.withMutations(map => {
  98. map.update('text', oldText => `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`);
  99. map.set('focusDate', new Date());
  100. });
  101. };
  102. const privacyPreference = (a, b) => {
  103. if (a === 'direct' || b === 'direct') {
  104. return 'direct';
  105. } else if (a === 'private' || b === 'private') {
  106. return 'private';
  107. } else if (a === 'unlisted' || b === 'unlisted') {
  108. return 'unlisted';
  109. } else {
  110. return 'public';
  111. }
  112. };
  113. export default function compose(state = initialState, action) {
  114. switch(action.type) {
  115. case STORE_HYDRATE:
  116. return clearAll(state.merge(action.state.get('compose')));
  117. case COMPOSE_MOUNT:
  118. return state.set('mounted', true);
  119. case COMPOSE_UNMOUNT:
  120. return state.set('mounted', false);
  121. case COMPOSE_SENSITIVITY_CHANGE:
  122. return state.set('sensitive', !state.get('sensitive'));
  123. case COMPOSE_SPOILERNESS_CHANGE:
  124. return state.withMutations(map => {
  125. map.set('spoiler_text', '');
  126. map.set('spoiler', !state.get('spoiler'));
  127. });
  128. case COMPOSE_SPOILER_TEXT_CHANGE:
  129. return state.set('spoiler_text', action.text);
  130. case COMPOSE_VISIBILITY_CHANGE:
  131. return state.set('privacy', action.value);
  132. case COMPOSE_CHANGE:
  133. return state.set('text', action.text);
  134. case COMPOSE_REPLY:
  135. return state.withMutations(map => {
  136. map.set('in_reply_to', action.status.get('id'));
  137. map.set('text', statusToTextMentions(state, action.status));
  138. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  139. map.set('focusDate', new Date());
  140. map.set('preselectDate', new Date());
  141. if (action.status.get('spoiler_text').length > 0) {
  142. map.set('spoiler', true);
  143. map.set('spoiler_text', action.status.get('spoiler_text'));
  144. }
  145. });
  146. case COMPOSE_REPLY_CANCEL:
  147. return state.withMutations(map => {
  148. map.set('in_reply_to', null);
  149. map.set('text', '');
  150. map.set('spoiler', false);
  151. map.set('spoiler_text', '');
  152. map.set('privacy', state.get('default_privacy'));
  153. });
  154. case COMPOSE_SUBMIT_REQUEST:
  155. return state.set('is_submitting', true);
  156. case COMPOSE_SUBMIT_SUCCESS:
  157. return clearAll(state);
  158. case COMPOSE_SUBMIT_FAIL:
  159. return state.set('is_submitting', false);
  160. case COMPOSE_UPLOAD_REQUEST:
  161. return state.withMutations(map => {
  162. map.set('is_uploading', true);
  163. });
  164. case COMPOSE_UPLOAD_SUCCESS:
  165. return appendMedia(state, Immutable.fromJS(action.media));
  166. case COMPOSE_UPLOAD_FAIL:
  167. return state.set('is_uploading', false);
  168. case COMPOSE_UPLOAD_UNDO:
  169. return removeMedia(state, action.media_id);
  170. case COMPOSE_UPLOAD_PROGRESS:
  171. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  172. case COMPOSE_MENTION:
  173. return state.update('text', text => `${text}@${action.account.get('acct')} `).set('focusDate', new Date());
  174. case COMPOSE_SUGGESTIONS_CLEAR:
  175. return state.update('suggestions', Immutable.List(), list => list.clear()).set('suggestion_token', null);
  176. case COMPOSE_SUGGESTIONS_READY:
  177. return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id))).set('suggestion_token', action.token);
  178. case COMPOSE_SUGGESTION_SELECT:
  179. return insertSuggestion(state, action.position, action.token, action.completion);
  180. case TIMELINE_DELETE:
  181. if (action.id === state.get('in_reply_to')) {
  182. return state.set('in_reply_to', null);
  183. } else {
  184. return state;
  185. }
  186. case COMPOSE_EMOJI_INSERT:
  187. return insertEmoji(state, action.position, action.emoji);
  188. default:
  189. return state;
  190. }
  191. };