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.

373 lines
12 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. COMPOSE_POLL_ADD,
  33. COMPOSE_POLL_REMOVE,
  34. COMPOSE_POLL_OPTION_ADD,
  35. COMPOSE_POLL_OPTION_CHANGE,
  36. COMPOSE_POLL_OPTION_REMOVE,
  37. COMPOSE_POLL_SETTINGS_CHANGE,
  38. } from '../actions/compose';
  39. import { TIMELINE_DELETE } from '../actions/timelines';
  40. import { STORE_HYDRATE } from '../actions/store';
  41. import { REDRAFT } from '../actions/statuses';
  42. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  43. import uuid from '../uuid';
  44. import { me } from '../initial_state';
  45. import { unescapeHTML } from '../utils/html';
  46. const initialState = ImmutableMap({
  47. mounted: 0,
  48. sensitive: false,
  49. spoiler: false,
  50. spoiler_text: '',
  51. privacy: null,
  52. text: '',
  53. focusDate: null,
  54. caretPosition: null,
  55. preselectDate: null,
  56. in_reply_to: null,
  57. is_composing: false,
  58. is_submitting: false,
  59. is_changing_upload: false,
  60. is_uploading: false,
  61. progress: 0,
  62. media_attachments: ImmutableList(),
  63. poll: null,
  64. suggestion_token: null,
  65. suggestions: ImmutableList(),
  66. default_privacy: 'public',
  67. default_sensitive: false,
  68. resetFileKey: Math.floor((Math.random() * 0x10000)),
  69. idempotencyKey: null,
  70. tagHistory: ImmutableList(),
  71. });
  72. const initialPoll = ImmutableMap({
  73. options: ImmutableList(['', '']),
  74. expires_in: 24 * 3600,
  75. multiple: false,
  76. });
  77. function statusToTextMentions(state, status) {
  78. let set = ImmutableOrderedSet([]);
  79. if (status.getIn(['account', 'id']) !== me) {
  80. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  81. }
  82. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  83. };
  84. function clearAll(state) {
  85. return state.withMutations(map => {
  86. map.set('text', '');
  87. map.set('spoiler', false);
  88. map.set('spoiler_text', '');
  89. map.set('is_submitting', false);
  90. map.set('is_changing_upload', false);
  91. map.set('in_reply_to', null);
  92. map.set('privacy', state.get('default_privacy'));
  93. map.set('sensitive', false);
  94. map.update('media_attachments', list => list.clear());
  95. map.set('poll', null);
  96. map.set('idempotencyKey', uuid());
  97. });
  98. };
  99. function appendMedia(state, media) {
  100. const prevSize = state.get('media_attachments').size;
  101. return state.withMutations(map => {
  102. map.update('media_attachments', list => list.push(media));
  103. map.set('is_uploading', false);
  104. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  105. map.set('idempotencyKey', uuid());
  106. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  107. map.set('sensitive', true);
  108. }
  109. });
  110. };
  111. function removeMedia(state, mediaId) {
  112. const prevSize = state.get('media_attachments').size;
  113. return state.withMutations(map => {
  114. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  115. map.set('idempotencyKey', uuid());
  116. if (prevSize === 1) {
  117. map.set('sensitive', false);
  118. }
  119. });
  120. };
  121. const insertSuggestion = (state, position, token, completion, path) => {
  122. return state.withMutations(map => {
  123. map.updateIn(path, oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  124. map.set('suggestion_token', null);
  125. map.set('suggestions', ImmutableList());
  126. if (path.length === 1 && path[0] === 'text') {
  127. map.set('focusDate', new Date());
  128. map.set('caretPosition', position + completion.length + 1);
  129. }
  130. map.set('idempotencyKey', uuid());
  131. });
  132. };
  133. const updateSuggestionTags = (state, token) => {
  134. const prefix = token.slice(1);
  135. return state.merge({
  136. suggestions: state.get('tagHistory')
  137. .filter(tag => tag.toLowerCase().startsWith(prefix.toLowerCase()))
  138. .slice(0, 4)
  139. .map(tag => '#' + tag),
  140. suggestion_token: token,
  141. });
  142. };
  143. const insertEmoji = (state, position, emojiData, needsSpace) => {
  144. const oldText = state.get('text');
  145. const emoji = needsSpace ? ' ' + emojiData.native : emojiData.native;
  146. return state.merge({
  147. text: `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`,
  148. focusDate: new Date(),
  149. caretPosition: position + emoji.length + 1,
  150. idempotencyKey: uuid(),
  151. });
  152. };
  153. const privacyPreference = (a, b) => {
  154. const order = ['public', 'unlisted', 'private', 'direct'];
  155. return order[Math.max(order.indexOf(a), order.indexOf(b), 0)];
  156. };
  157. const hydrate = (state, hydratedState) => {
  158. state = clearAll(state.merge(hydratedState));
  159. if (hydratedState.has('text')) {
  160. state = state.set('text', hydratedState.get('text'));
  161. }
  162. return state;
  163. };
  164. const domParser = new DOMParser();
  165. const expandMentions = status => {
  166. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  167. status.get('mentions').forEach(mention => {
  168. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  169. });
  170. return fragment.innerHTML;
  171. };
  172. export default function compose(state = initialState, action) {
  173. switch(action.type) {
  174. case STORE_HYDRATE:
  175. return hydrate(state, action.state.get('compose'));
  176. case COMPOSE_MOUNT:
  177. return state.set('mounted', state.get('mounted') + 1);
  178. case COMPOSE_UNMOUNT:
  179. return state
  180. .set('mounted', Math.max(state.get('mounted') - 1, 0))
  181. .set('is_composing', false);
  182. case COMPOSE_SENSITIVITY_CHANGE:
  183. return state.withMutations(map => {
  184. if (!state.get('spoiler')) {
  185. map.set('sensitive', !state.get('sensitive'));
  186. }
  187. map.set('idempotencyKey', uuid());
  188. });
  189. case COMPOSE_SPOILERNESS_CHANGE:
  190. return state.withMutations(map => {
  191. map.set('spoiler_text', '');
  192. map.set('spoiler', !state.get('spoiler'));
  193. map.set('idempotencyKey', uuid());
  194. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  195. map.set('sensitive', true);
  196. }
  197. });
  198. case COMPOSE_SPOILER_TEXT_CHANGE:
  199. return state
  200. .set('spoiler_text', action.text)
  201. .set('idempotencyKey', uuid());
  202. case COMPOSE_VISIBILITY_CHANGE:
  203. return state
  204. .set('privacy', action.value)
  205. .set('idempotencyKey', uuid());
  206. case COMPOSE_CHANGE:
  207. return state
  208. .set('text', action.text)
  209. .set('idempotencyKey', uuid());
  210. case COMPOSE_COMPOSING_CHANGE:
  211. return state.set('is_composing', action.value);
  212. case COMPOSE_REPLY:
  213. return state.withMutations(map => {
  214. map.set('in_reply_to', action.status.get('id'));
  215. map.set('text', statusToTextMentions(state, action.status));
  216. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  217. map.set('focusDate', new Date());
  218. map.set('caretPosition', null);
  219. map.set('preselectDate', new Date());
  220. map.set('idempotencyKey', uuid());
  221. if (action.status.get('spoiler_text').length > 0) {
  222. map.set('spoiler', true);
  223. map.set('spoiler_text', action.status.get('spoiler_text'));
  224. } else {
  225. map.set('spoiler', false);
  226. map.set('spoiler_text', '');
  227. }
  228. });
  229. case COMPOSE_REPLY_CANCEL:
  230. case COMPOSE_RESET:
  231. return state.withMutations(map => {
  232. map.set('in_reply_to', null);
  233. map.set('text', '');
  234. map.set('spoiler', false);
  235. map.set('spoiler_text', '');
  236. map.set('privacy', state.get('default_privacy'));
  237. map.set('poll', null);
  238. map.set('idempotencyKey', uuid());
  239. });
  240. case COMPOSE_SUBMIT_REQUEST:
  241. return state.set('is_submitting', true);
  242. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  243. return state.set('is_changing_upload', true);
  244. case COMPOSE_SUBMIT_SUCCESS:
  245. return clearAll(state);
  246. case COMPOSE_SUBMIT_FAIL:
  247. return state.set('is_submitting', false);
  248. case COMPOSE_UPLOAD_CHANGE_FAIL:
  249. return state.set('is_changing_upload', false);
  250. case COMPOSE_UPLOAD_REQUEST:
  251. return state.set('is_uploading', true);
  252. case COMPOSE_UPLOAD_SUCCESS:
  253. return appendMedia(state, fromJS(action.media));
  254. case COMPOSE_UPLOAD_FAIL:
  255. return state.set('is_uploading', false);
  256. case COMPOSE_UPLOAD_UNDO:
  257. return removeMedia(state, action.media_id);
  258. case COMPOSE_UPLOAD_PROGRESS:
  259. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  260. case COMPOSE_MENTION:
  261. return state.withMutations(map => {
  262. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  263. map.set('focusDate', new Date());
  264. map.set('caretPosition', null);
  265. map.set('idempotencyKey', uuid());
  266. });
  267. case COMPOSE_DIRECT:
  268. return state.withMutations(map => {
  269. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  270. map.set('privacy', 'direct');
  271. map.set('focusDate', new Date());
  272. map.set('caretPosition', null);
  273. map.set('idempotencyKey', uuid());
  274. });
  275. case COMPOSE_SUGGESTIONS_CLEAR:
  276. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  277. case COMPOSE_SUGGESTIONS_READY:
  278. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  279. case COMPOSE_SUGGESTION_SELECT:
  280. return insertSuggestion(state, action.position, action.token, action.completion, action.path);
  281. case COMPOSE_SUGGESTION_TAGS_UPDATE:
  282. return updateSuggestionTags(state, action.token);
  283. case COMPOSE_TAG_HISTORY_UPDATE:
  284. return state.set('tagHistory', fromJS(action.tags));
  285. case TIMELINE_DELETE:
  286. if (action.id === state.get('in_reply_to')) {
  287. return state.set('in_reply_to', null);
  288. } else {
  289. return state;
  290. }
  291. case COMPOSE_EMOJI_INSERT:
  292. return insertEmoji(state, action.position, action.emoji, action.needsSpace);
  293. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  294. return state
  295. .set('is_changing_upload', false)
  296. .update('media_attachments', list => list.map(item => {
  297. if (item.get('id') === action.media.id) {
  298. return fromJS(action.media);
  299. }
  300. return item;
  301. }));
  302. case REDRAFT:
  303. return state.withMutations(map => {
  304. map.set('text', action.raw_text || unescapeHTML(expandMentions(action.status)));
  305. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  306. map.set('privacy', action.status.get('visibility'));
  307. map.set('media_attachments', action.status.get('media_attachments'));
  308. map.set('focusDate', new Date());
  309. map.set('caretPosition', null);
  310. map.set('idempotencyKey', uuid());
  311. if (action.status.get('spoiler_text').length > 0) {
  312. map.set('spoiler', true);
  313. map.set('spoiler_text', action.status.get('spoiler_text'));
  314. } else {
  315. map.set('spoiler', false);
  316. map.set('spoiler_text', '');
  317. }
  318. if (action.status.get('poll')) {
  319. map.set('poll', ImmutableMap({
  320. options: action.status.getIn(['poll', 'options']).map(x => x.get('title')),
  321. multiple: action.status.getIn(['poll', 'multiple']),
  322. expires_in: 24 * 3600,
  323. }));
  324. }
  325. });
  326. case COMPOSE_POLL_ADD:
  327. return state.set('poll', initialPoll);
  328. case COMPOSE_POLL_REMOVE:
  329. return state.set('poll', null);
  330. case COMPOSE_POLL_OPTION_ADD:
  331. return state.updateIn(['poll', 'options'], options => options.push(action.title));
  332. case COMPOSE_POLL_OPTION_CHANGE:
  333. return state.setIn(['poll', 'options', action.index], action.title);
  334. case COMPOSE_POLL_OPTION_REMOVE:
  335. return state.updateIn(['poll', 'options'], options => options.delete(action.index));
  336. case COMPOSE_POLL_SETTINGS_CHANGE:
  337. return state.update('poll', poll => poll.set('expires_in', action.expiresIn).set('multiple', action.isMultiple));
  338. default:
  339. return state;
  340. }
  341. };