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

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