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.

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