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.

307 lines
9.7 KiB

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