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.

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