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.

654 lines
23 KiB

7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import {
  2. COMPOSE_MOUNT,
  3. COMPOSE_UNMOUNT,
  4. COMPOSE_CHANGE,
  5. COMPOSE_CYCLE_ELEFRIEND,
  6. COMPOSE_REPLY,
  7. COMPOSE_REPLY_CANCEL,
  8. COMPOSE_DIRECT,
  9. COMPOSE_MENTION,
  10. COMPOSE_SUBMIT_REQUEST,
  11. COMPOSE_SUBMIT_SUCCESS,
  12. COMPOSE_SUBMIT_FAIL,
  13. COMPOSE_UPLOAD_REQUEST,
  14. COMPOSE_UPLOAD_SUCCESS,
  15. COMPOSE_UPLOAD_FAIL,
  16. COMPOSE_UPLOAD_UNDO,
  17. COMPOSE_UPLOAD_PROGRESS,
  18. COMPOSE_UPLOAD_PROCESSING,
  19. THUMBNAIL_UPLOAD_REQUEST,
  20. THUMBNAIL_UPLOAD_SUCCESS,
  21. THUMBNAIL_UPLOAD_FAIL,
  22. THUMBNAIL_UPLOAD_PROGRESS,
  23. COMPOSE_SUGGESTIONS_CLEAR,
  24. COMPOSE_SUGGESTIONS_READY,
  25. COMPOSE_SUGGESTION_SELECT,
  26. COMPOSE_SUGGESTION_IGNORE,
  27. COMPOSE_SUGGESTION_TAGS_UPDATE,
  28. COMPOSE_TAG_HISTORY_UPDATE,
  29. COMPOSE_ADVANCED_OPTIONS_CHANGE,
  30. COMPOSE_SENSITIVITY_CHANGE,
  31. COMPOSE_SPOILERNESS_CHANGE,
  32. COMPOSE_SPOILER_TEXT_CHANGE,
  33. COMPOSE_VISIBILITY_CHANGE,
  34. COMPOSE_LANGUAGE_CHANGE,
  35. COMPOSE_CONTENT_TYPE_CHANGE,
  36. COMPOSE_EMOJI_INSERT,
  37. COMPOSE_UPLOAD_CHANGE_REQUEST,
  38. COMPOSE_UPLOAD_CHANGE_SUCCESS,
  39. COMPOSE_UPLOAD_CHANGE_FAIL,
  40. COMPOSE_DOODLE_SET,
  41. COMPOSE_RESET,
  42. COMPOSE_POLL_ADD,
  43. COMPOSE_POLL_REMOVE,
  44. COMPOSE_POLL_OPTION_ADD,
  45. COMPOSE_POLL_OPTION_CHANGE,
  46. COMPOSE_POLL_OPTION_REMOVE,
  47. COMPOSE_POLL_SETTINGS_CHANGE,
  48. INIT_MEDIA_EDIT_MODAL,
  49. COMPOSE_CHANGE_MEDIA_DESCRIPTION,
  50. COMPOSE_CHANGE_MEDIA_FOCUS,
  51. COMPOSE_SET_STATUS,
  52. } from 'flavours/glitch/actions/compose';
  53. import { TIMELINE_DELETE } from 'flavours/glitch/actions/timelines';
  54. import { STORE_HYDRATE } from 'flavours/glitch/actions/store';
  55. import { REDRAFT } from 'flavours/glitch/actions/statuses';
  56. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  57. import uuid from '../uuid';
  58. import { privacyPreference } from 'flavours/glitch/utils/privacy_preference';
  59. import { me, defaultContentType } from 'flavours/glitch/initial_state';
  60. import { overwrite } from 'flavours/glitch/utils/js_helpers';
  61. import { unescapeHTML } from 'flavours/glitch/utils/html';
  62. import { recoverHashtags } from 'flavours/glitch/utils/hashtag';
  63. const totalElefriends = 3;
  64. // ~4% chance you'll end up with an unexpected friend
  65. // glitch-soc/mastodon repo created_at date: 2017-04-20T21:55:28Z
  66. const glitchProbability = 1 - 0.0420215528;
  67. const initialState = ImmutableMap({
  68. mounted: 0,
  69. advanced_options: ImmutableMap({
  70. do_not_federate: false,
  71. threaded_mode: false,
  72. }),
  73. sensitive: false,
  74. elefriend: Math.random() < glitchProbability ? Math.floor(Math.random() * totalElefriends) : totalElefriends,
  75. spoiler: false,
  76. spoiler_text: '',
  77. privacy: null,
  78. id: null,
  79. content_type: defaultContentType || 'text/plain',
  80. text: '',
  81. focusDate: null,
  82. caretPosition: null,
  83. preselectDate: null,
  84. in_reply_to: null,
  85. is_submitting: false,
  86. is_uploading: false,
  87. is_changing_upload: false,
  88. progress: 0,
  89. isUploadingThumbnail: false,
  90. thumbnailProgress: 0,
  91. media_attachments: ImmutableList(),
  92. pending_media_attachments: 0,
  93. poll: null,
  94. suggestion_token: null,
  95. suggestions: ImmutableList(),
  96. default_advanced_options: ImmutableMap({
  97. do_not_federate: false,
  98. threaded_mode: null, // Do not reset
  99. }),
  100. default_privacy: 'public',
  101. default_sensitive: false,
  102. default_language: 'en',
  103. resetFileKey: Math.floor((Math.random() * 0x10000)),
  104. idempotencyKey: null,
  105. tagHistory: ImmutableList(),
  106. media_modal: ImmutableMap({
  107. id: null,
  108. description: '',
  109. focusX: 0,
  110. focusY: 0,
  111. dirty: false,
  112. }),
  113. doodle: ImmutableMap({
  114. fg: 'rgb( 0, 0, 0)',
  115. bg: 'rgb(255, 255, 255)',
  116. swapped: false,
  117. mode: 'draw',
  118. size: 'normal',
  119. weight: 2,
  120. opacity: 1,
  121. adaptiveStroke: true,
  122. smoothing: false,
  123. }),
  124. });
  125. const initialPoll = ImmutableMap({
  126. options: ImmutableList(['', '']),
  127. expires_in: 24 * 3600,
  128. multiple: false,
  129. });
  130. function statusToTextMentions(state, status) {
  131. let set = ImmutableOrderedSet([]);
  132. if (status.getIn(['account', 'id']) !== me) {
  133. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  134. }
  135. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  136. }
  137. function apiStatusToTextMentions (state, status) {
  138. let set = ImmutableOrderedSet([]);
  139. if (status.account.id !== me) {
  140. set = set.add(`@${status.account.acct} `);
  141. }
  142. return set.union(status.mentions.filter(
  143. mention => mention.id !== me,
  144. ).map(
  145. mention => `@${mention.acct} `,
  146. )).join('');
  147. }
  148. function apiStatusToTextHashtags (state, status) {
  149. const text = unescapeHTML(status.content);
  150. return ImmutableOrderedSet([]).union(recoverHashtags(status.tags, text).map(
  151. (name) => `#${name} `,
  152. )).join('');
  153. }
  154. function clearAll(state) {
  155. return state.withMutations(map => {
  156. map.set('id', null);
  157. map.set('text', '');
  158. if (defaultContentType) map.set('content_type', defaultContentType);
  159. map.set('spoiler', false);
  160. map.set('spoiler_text', '');
  161. map.set('is_submitting', false);
  162. map.set('is_changing_upload', false);
  163. map.set('in_reply_to', null);
  164. map.update(
  165. 'advanced_options',
  166. map => map.mergeWith(overwrite, state.get('default_advanced_options')),
  167. );
  168. map.set('privacy', state.get('default_privacy'));
  169. map.set('sensitive', state.get('default_sensitive'));
  170. map.set('language', state.get('default_language'));
  171. map.update('media_attachments', list => list.clear());
  172. map.set('poll', null);
  173. map.set('idempotencyKey', uuid());
  174. });
  175. }
  176. function continueThread (state, status) {
  177. return state.withMutations(function (map) {
  178. let text = apiStatusToTextMentions(state, status);
  179. text = text + apiStatusToTextHashtags(state, status);
  180. map.set('text', text);
  181. if (status.spoiler_text) {
  182. map.set('spoiler', true);
  183. map.set('spoiler_text', status.spoiler_text);
  184. } else {
  185. map.set('spoiler', false);
  186. map.set('spoiler_text', '');
  187. }
  188. map.set('is_submitting', false);
  189. map.set('in_reply_to', status.id);
  190. map.update(
  191. 'advanced_options',
  192. map => map.merge(new ImmutableMap({ do_not_federate: status.local_only })),
  193. );
  194. map.set('privacy', status.visibility);
  195. map.set('sensitive', false);
  196. map.update('media_attachments', list => list.clear());
  197. map.set('poll', null);
  198. map.set('idempotencyKey', uuid());
  199. map.set('focusDate', new Date());
  200. map.set('caretPosition', null);
  201. map.set('preselectDate', new Date());
  202. });
  203. }
  204. function appendMedia(state, media, file) {
  205. const prevSize = state.get('media_attachments').size;
  206. return state.withMutations(map => {
  207. if (media.get('type') === 'image') {
  208. media = media.set('file', file);
  209. }
  210. map.update('media_attachments', list => list.push(media.set('unattached', true)));
  211. map.set('is_uploading', false);
  212. map.set('is_processing', false);
  213. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  214. map.set('idempotencyKey', uuid());
  215. map.update('pending_media_attachments', n => n - 1);
  216. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  217. map.set('sensitive', true);
  218. }
  219. });
  220. }
  221. function removeMedia(state, mediaId) {
  222. const prevSize = state.get('media_attachments').size;
  223. return state.withMutations(map => {
  224. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  225. map.set('idempotencyKey', uuid());
  226. if (prevSize === 1) {
  227. map.set('sensitive', false);
  228. }
  229. });
  230. }
  231. const insertSuggestion = (state, position, token, completion, path) => {
  232. return state.withMutations(map => {
  233. map.updateIn(path, oldText => `${oldText.slice(0, position)}${completion}${completion[0] === ':' ? '\u200B' : ' '}${oldText.slice(position + token.length)}`);
  234. map.set('suggestion_token', null);
  235. map.set('suggestions', ImmutableList());
  236. if (path.length === 1 && path[0] === 'text') {
  237. map.set('focusDate', new Date());
  238. map.set('caretPosition', position + completion.length + 1);
  239. }
  240. map.set('idempotencyKey', uuid());
  241. });
  242. };
  243. const ignoreSuggestion = (state, position, token, completion, path) => {
  244. return state.withMutations(map => {
  245. map.updateIn(path, oldText => `${oldText.slice(0, position + token.length)} ${oldText.slice(position + token.length)}`);
  246. map.set('suggestion_token', null);
  247. map.set('suggestions', ImmutableList());
  248. map.set('focusDate', new Date());
  249. map.set('caretPosition', position + token.length + 1);
  250. map.set('idempotencyKey', uuid());
  251. });
  252. };
  253. const sortHashtagsByUse = (state, tags) => {
  254. const personalHistory = state.get('tagHistory').map(tag => tag.toLowerCase());
  255. const tagsWithLowercase = tags.map(t => ({ ...t, lowerName: t.name.toLowerCase() }));
  256. const sorted = tagsWithLowercase.sort((a, b) => {
  257. const usedA = personalHistory.includes(a.lowerName);
  258. const usedB = personalHistory.includes(b.lowerName);
  259. if (usedA === usedB) {
  260. return 0;
  261. } else if (usedA && !usedB) {
  262. return -1;
  263. } else {
  264. return 1;
  265. }
  266. });
  267. sorted.forEach(tag => delete tag.lowerName);
  268. return sorted;
  269. };
  270. const insertEmoji = (state, position, emojiData) => {
  271. const emoji = emojiData.native;
  272. return state.withMutations(map => {
  273. map.update('text', oldText => `${oldText.slice(0, position)}${emoji}\u200B${oldText.slice(position)}`);
  274. map.set('focusDate', new Date());
  275. map.set('caretPosition', position + emoji.length + 1);
  276. map.set('idempotencyKey', uuid());
  277. });
  278. };
  279. const hydrate = (state, hydratedState) => {
  280. state = clearAll(state.merge(hydratedState));
  281. if (hydratedState.get('text')) {
  282. state = state.set('text', hydratedState.get('text')).set('focusDate', new Date());
  283. }
  284. return state;
  285. };
  286. const domParser = new DOMParser();
  287. const expandMentions = status => {
  288. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  289. status.get('mentions').forEach(mention => {
  290. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  291. });
  292. return fragment.innerHTML;
  293. };
  294. const expiresInFromExpiresAt = expires_at => {
  295. if (!expires_at) return 24 * 3600;
  296. const delta = (new Date(expires_at).getTime() - Date.now()) / 1000;
  297. return [300, 1800, 3600, 21600, 86400, 259200, 604800].find(expires_in => expires_in >= delta) || 24 * 3600;
  298. };
  299. const mergeLocalHashtagResults = (suggestions, prefix, tagHistory) => {
  300. prefix = prefix.toLowerCase();
  301. if (suggestions.length < 4) {
  302. const localTags = tagHistory.filter(tag => tag.toLowerCase().startsWith(prefix) && !suggestions.some(suggestion => suggestion.type === 'hashtag' && suggestion.name.toLowerCase() === tag.toLowerCase()));
  303. return suggestions.concat(localTags.slice(0, 4 - suggestions.length).toJS().map(tag => ({ type: 'hashtag', name: tag })));
  304. } else {
  305. return suggestions;
  306. }
  307. };
  308. const normalizeSuggestions = (state, { accounts, emojis, tags, token }) => {
  309. if (accounts) {
  310. return accounts.map(item => ({ id: item.id, type: 'account' }));
  311. } else if (emojis) {
  312. return emojis.map(item => ({ ...item, type: 'emoji' }));
  313. } else {
  314. return mergeLocalHashtagResults(sortHashtagsByUse(state, tags.map(item => ({ ...item, type: 'hashtag' }))), token.slice(1), state.get('tagHistory'));
  315. }
  316. };
  317. const updateSuggestionTags = (state, token) => {
  318. const prefix = token.slice(1);
  319. const suggestions = state.get('suggestions').toJS();
  320. return state.merge({
  321. suggestions: ImmutableList(mergeLocalHashtagResults(suggestions, prefix, state.get('tagHistory'))),
  322. suggestion_token: token,
  323. });
  324. };
  325. export default function compose(state = initialState, action) {
  326. switch(action.type) {
  327. case STORE_HYDRATE:
  328. return hydrate(state, action.state.get('compose'));
  329. case COMPOSE_MOUNT:
  330. return state.set('mounted', state.get('mounted') + 1);
  331. case COMPOSE_UNMOUNT:
  332. return state.set('mounted', Math.max(state.get('mounted') - 1, 0));
  333. case COMPOSE_ADVANCED_OPTIONS_CHANGE:
  334. return state
  335. .set('advanced_options', state.get('advanced_options').set(action.option, !!overwrite(!state.getIn(['advanced_options', action.option]), action.value)))
  336. .set('idempotencyKey', uuid());
  337. case COMPOSE_SENSITIVITY_CHANGE:
  338. return state.withMutations(map => {
  339. if (!state.get('spoiler')) {
  340. map.set('sensitive', !state.get('sensitive'));
  341. }
  342. map.set('idempotencyKey', uuid());
  343. });
  344. case COMPOSE_SPOILERNESS_CHANGE:
  345. return state.withMutations(map => {
  346. map.set('spoiler', !state.get('spoiler'));
  347. map.set('idempotencyKey', uuid());
  348. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  349. map.set('sensitive', true);
  350. }
  351. });
  352. case COMPOSE_SPOILER_TEXT_CHANGE:
  353. return state
  354. .set('spoiler_text', action.text)
  355. .set('idempotencyKey', uuid());
  356. case COMPOSE_VISIBILITY_CHANGE:
  357. return state
  358. .set('privacy', action.value)
  359. .set('idempotencyKey', uuid());
  360. case COMPOSE_CONTENT_TYPE_CHANGE:
  361. return state
  362. .set('content_type', action.value)
  363. .set('idempotencyKey', uuid());
  364. case COMPOSE_CHANGE:
  365. return state
  366. .set('text', action.text)
  367. .set('idempotencyKey', uuid());
  368. case COMPOSE_CYCLE_ELEFRIEND:
  369. return state
  370. .set('elefriend', (state.get('elefriend') + 1) % totalElefriends);
  371. case COMPOSE_REPLY:
  372. return state.withMutations(map => {
  373. map.set('id', null);
  374. map.set('in_reply_to', action.status.get('id'));
  375. map.set('text', statusToTextMentions(state, action.status));
  376. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  377. map.update(
  378. 'advanced_options',
  379. map => map.merge(new ImmutableMap({ do_not_federate: !!action.status.get('local_only') })),
  380. );
  381. map.set('focusDate', new Date());
  382. map.set('caretPosition', null);
  383. map.set('preselectDate', new Date());
  384. map.set('idempotencyKey', uuid());
  385. map.update('media_attachments', list => list.filter(media => media.get('unattached')));
  386. if (action.status.get('language') && !action.status.has('translation')) {
  387. map.set('language', action.status.get('language'));
  388. } else {
  389. map.set('language', state.get('default_language'));
  390. }
  391. if (action.status.get('spoiler_text').length > 0) {
  392. let spoiler_text = action.status.get('spoiler_text');
  393. if (action.prependCWRe && !spoiler_text.match(/^re[: ]/i)) {
  394. spoiler_text = 're: '.concat(spoiler_text);
  395. }
  396. map.set('spoiler', true);
  397. map.set('spoiler_text', spoiler_text);
  398. } else {
  399. map.set('spoiler', false);
  400. map.set('spoiler_text', '');
  401. }
  402. });
  403. case COMPOSE_REPLY_CANCEL:
  404. state = state.setIn(['advanced_options', 'threaded_mode'], false);
  405. case COMPOSE_RESET:
  406. return state.withMutations(map => {
  407. map.set('in_reply_to', null);
  408. if (defaultContentType) map.set('content_type', defaultContentType);
  409. map.set('text', '');
  410. map.set('spoiler', false);
  411. map.set('spoiler_text', '');
  412. map.set('privacy', state.get('default_privacy'));
  413. map.set('id', null);
  414. map.set('poll', null);
  415. map.update(
  416. 'advanced_options',
  417. map => map.mergeWith(overwrite, state.get('default_advanced_options')),
  418. );
  419. map.set('idempotencyKey', uuid());
  420. });
  421. case COMPOSE_SUBMIT_REQUEST:
  422. return state.set('is_submitting', true);
  423. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  424. return state.set('is_changing_upload', true);
  425. case COMPOSE_SUBMIT_SUCCESS:
  426. return action.status && state.getIn(['advanced_options', 'threaded_mode']) ? continueThread(state, action.status) : clearAll(state);
  427. case COMPOSE_SUBMIT_FAIL:
  428. return state.set('is_submitting', false);
  429. case COMPOSE_UPLOAD_CHANGE_FAIL:
  430. return state.set('is_changing_upload', false);
  431. case COMPOSE_UPLOAD_REQUEST:
  432. return state.set('is_uploading', true).update('pending_media_attachments', n => n + 1);
  433. case COMPOSE_UPLOAD_PROCESSING:
  434. return state.set('is_processing', true);
  435. case COMPOSE_UPLOAD_SUCCESS:
  436. return appendMedia(state, fromJS(action.media), action.file);
  437. case COMPOSE_UPLOAD_FAIL:
  438. return state.set('is_uploading', false).set('is_processing', false).update('pending_media_attachments', n => n - 1);
  439. case COMPOSE_UPLOAD_UNDO:
  440. return removeMedia(state, action.media_id);
  441. case COMPOSE_UPLOAD_PROGRESS:
  442. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  443. case THUMBNAIL_UPLOAD_REQUEST:
  444. return state.set('isUploadingThumbnail', true);
  445. case THUMBNAIL_UPLOAD_PROGRESS:
  446. return state.set('thumbnailProgress', Math.round((action.loaded / action.total) * 100));
  447. case THUMBNAIL_UPLOAD_FAIL:
  448. return state.set('isUploadingThumbnail', false);
  449. case THUMBNAIL_UPLOAD_SUCCESS:
  450. return state
  451. .set('isUploadingThumbnail', false)
  452. .update('media_attachments', list => list.map(item => {
  453. if (item.get('id') === action.media.id) {
  454. return fromJS(action.media);
  455. }
  456. return item;
  457. }));
  458. case INIT_MEDIA_EDIT_MODAL:
  459. const media = state.get('media_attachments').find(item => item.get('id') === action.id);
  460. return state.set('media_modal', ImmutableMap({
  461. id: action.id,
  462. description: media.get('description') || '',
  463. focusX: media.getIn(['meta', 'focus', 'x'], 0),
  464. focusY: media.getIn(['meta', 'focus', 'y'], 0),
  465. dirty: false,
  466. }));
  467. case COMPOSE_CHANGE_MEDIA_DESCRIPTION:
  468. return state.setIn(['media_modal', 'description'], action.description).setIn(['media_modal', 'dirty'], true);
  469. case COMPOSE_CHANGE_MEDIA_FOCUS:
  470. return state.setIn(['media_modal', 'focusX'], action.focusX).setIn(['media_modal', 'focusY'], action.focusY).setIn(['media_modal', 'dirty'], true);
  471. case COMPOSE_MENTION:
  472. return state.withMutations(map => {
  473. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  474. map.set('focusDate', new Date());
  475. map.set('caretPosition', null);
  476. map.set('idempotencyKey', uuid());
  477. });
  478. case COMPOSE_DIRECT:
  479. return state.withMutations(map => {
  480. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  481. map.set('privacy', 'direct');
  482. map.set('focusDate', new Date());
  483. map.set('caretPosition', null);
  484. map.set('idempotencyKey', uuid());
  485. });
  486. case COMPOSE_SUGGESTIONS_CLEAR:
  487. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  488. case COMPOSE_SUGGESTIONS_READY:
  489. return state.set('suggestions', ImmutableList(normalizeSuggestions(state, action))).set('suggestion_token', action.token);
  490. case COMPOSE_SUGGESTION_SELECT:
  491. return insertSuggestion(state, action.position, action.token, action.completion, action.path);
  492. case COMPOSE_SUGGESTION_IGNORE:
  493. return ignoreSuggestion(state, action.position, action.token, action.completion, action.path);
  494. case COMPOSE_SUGGESTION_TAGS_UPDATE:
  495. return updateSuggestionTags(state, action.token);
  496. case COMPOSE_TAG_HISTORY_UPDATE:
  497. return state.set('tagHistory', fromJS(action.tags));
  498. case TIMELINE_DELETE:
  499. if (action.id === state.get('in_reply_to')) {
  500. return state.set('in_reply_to', null);
  501. } else if (action.id === state.get('id')) {
  502. return state.set('id', null);
  503. } else {
  504. return state;
  505. }
  506. case COMPOSE_EMOJI_INSERT:
  507. return insertEmoji(state, action.position, action.emoji);
  508. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  509. return state
  510. .set('is_changing_upload', false)
  511. .setIn(['media_modal', 'dirty'], false)
  512. .update('media_attachments', list => list.map(item => {
  513. if (item.get('id') === action.media.id) {
  514. return fromJS(action.media).set('unattached', !action.attached);
  515. }
  516. return item;
  517. }));
  518. case COMPOSE_DOODLE_SET:
  519. return state.mergeIn(['doodle'], action.options);
  520. case REDRAFT:
  521. const do_not_federate = !!action.status.get('local_only');
  522. let text = action.raw_text || unescapeHTML(expandMentions(action.status));
  523. if (do_not_federate) text = text.replace(/ ?👁\ufe0f?\u200b?$/, '');
  524. return state.withMutations(map => {
  525. map.set('text', text);
  526. map.set('content_type', action.content_type || 'text/plain');
  527. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  528. map.set('privacy', action.status.get('visibility'));
  529. map.set('media_attachments', action.status.get('media_attachments').map((media) => media.set('unattached', true)));
  530. map.set('focusDate', new Date());
  531. map.set('caretPosition', null);
  532. map.set('idempotencyKey', uuid());
  533. map.set('sensitive', action.status.get('sensitive'));
  534. map.set('language', action.status.get('language'));
  535. map.update(
  536. 'advanced_options',
  537. map => map.merge(new ImmutableMap({ do_not_federate })),
  538. );
  539. map.set('id', null);
  540. if (action.status.get('spoiler_text').length > 0) {
  541. map.set('spoiler', true);
  542. map.set('spoiler_text', action.status.get('spoiler_text'));
  543. if (map.get('media_attachments').size >= 1) {
  544. map.set('sensitive', true);
  545. }
  546. } else {
  547. map.set('spoiler', false);
  548. map.set('spoiler_text', '');
  549. }
  550. if (action.status.get('poll')) {
  551. map.set('poll', ImmutableMap({
  552. options: action.status.getIn(['poll', 'options']).map(x => x.get('title')),
  553. multiple: action.status.getIn(['poll', 'multiple']),
  554. expires_in: expiresInFromExpiresAt(action.status.getIn(['poll', 'expires_at'])),
  555. }));
  556. }
  557. });
  558. case COMPOSE_SET_STATUS:
  559. return state.withMutations(map => {
  560. map.set('id', action.status.get('id'));
  561. map.set('text', action.text);
  562. map.set('content_type', action.content_type || 'text/plain');
  563. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  564. map.set('privacy', action.status.get('visibility'));
  565. map.set('media_attachments', action.status.get('media_attachments'));
  566. map.set('focusDate', new Date());
  567. map.set('caretPosition', null);
  568. map.set('idempotencyKey', uuid());
  569. map.set('sensitive', action.status.get('sensitive'));
  570. map.set('language', action.status.get('language'));
  571. if (action.spoiler_text.length > 0) {
  572. map.set('spoiler', true);
  573. map.set('spoiler_text', action.spoiler_text);
  574. } else {
  575. map.set('spoiler', false);
  576. map.set('spoiler_text', '');
  577. }
  578. if (action.status.get('poll')) {
  579. map.set('poll', ImmutableMap({
  580. options: action.status.getIn(['poll', 'options']).map(x => x.get('title')),
  581. multiple: action.status.getIn(['poll', 'multiple']),
  582. expires_in: expiresInFromExpiresAt(action.status.getIn(['poll', 'expires_at'])),
  583. }));
  584. }
  585. });
  586. case COMPOSE_POLL_ADD:
  587. return state.set('poll', initialPoll);
  588. case COMPOSE_POLL_REMOVE:
  589. return state.set('poll', null);
  590. case COMPOSE_POLL_OPTION_ADD:
  591. return state.updateIn(['poll', 'options'], options => options.push(action.title));
  592. case COMPOSE_POLL_OPTION_CHANGE:
  593. return state.setIn(['poll', 'options', action.index], action.title);
  594. case COMPOSE_POLL_OPTION_REMOVE:
  595. return state.updateIn(['poll', 'options'], options => options.delete(action.index));
  596. case COMPOSE_POLL_SETTINGS_CHANGE:
  597. return state.update('poll', poll => poll.set('expires_in', action.expiresIn).set('multiple', action.isMultiple));
  598. case COMPOSE_LANGUAGE_CHANGE:
  599. return state.set('language', action.language);
  600. default:
  601. return state;
  602. }
  603. }