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

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