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.

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