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.

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