compose.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. import axios from 'axios';
  2. import { throttle } from 'lodash';
  3. import { defineMessages } from 'react-intl';
  4. import api from 'mastodon/api';
  5. import { search as emojiSearch } from 'mastodon/features/emoji/emoji_mart_search_light';
  6. import { tagHistory } from 'mastodon/settings';
  7. import resizeImage from 'mastodon/utils/resize_image';
  8. import { showAlert, showAlertForError } from './alerts';
  9. import { useEmoji } from './emojis';
  10. import { importFetchedAccounts, importFetchedStatus } from './importer';
  11. import { openModal } from './modal';
  12. import { updateTimeline } from './timelines';
  13. /** @type {AbortController | undefined} */
  14. let fetchComposeSuggestionsAccountsController;
  15. /** @type {AbortController | undefined} */
  16. let fetchComposeSuggestionsTagsController;
  17. export const COMPOSE_CHANGE = 'COMPOSE_CHANGE';
  18. export const COMPOSE_SUBMIT_REQUEST = 'COMPOSE_SUBMIT_REQUEST';
  19. export const COMPOSE_SUBMIT_SUCCESS = 'COMPOSE_SUBMIT_SUCCESS';
  20. export const COMPOSE_SUBMIT_FAIL = 'COMPOSE_SUBMIT_FAIL';
  21. export const COMPOSE_REPLY = 'COMPOSE_REPLY';
  22. export const COMPOSE_REPLY_CANCEL = 'COMPOSE_REPLY_CANCEL';
  23. export const COMPOSE_DIRECT = 'COMPOSE_DIRECT';
  24. export const COMPOSE_MENTION = 'COMPOSE_MENTION';
  25. export const COMPOSE_RESET = 'COMPOSE_RESET';
  26. export const COMPOSE_UPLOAD_REQUEST = 'COMPOSE_UPLOAD_REQUEST';
  27. export const COMPOSE_UPLOAD_SUCCESS = 'COMPOSE_UPLOAD_SUCCESS';
  28. export const COMPOSE_UPLOAD_FAIL = 'COMPOSE_UPLOAD_FAIL';
  29. export const COMPOSE_UPLOAD_PROGRESS = 'COMPOSE_UPLOAD_PROGRESS';
  30. export const COMPOSE_UPLOAD_PROCESSING = 'COMPOSE_UPLOAD_PROCESSING';
  31. export const COMPOSE_UPLOAD_UNDO = 'COMPOSE_UPLOAD_UNDO';
  32. export const THUMBNAIL_UPLOAD_REQUEST = 'THUMBNAIL_UPLOAD_REQUEST';
  33. export const THUMBNAIL_UPLOAD_SUCCESS = 'THUMBNAIL_UPLOAD_SUCCESS';
  34. export const THUMBNAIL_UPLOAD_FAIL = 'THUMBNAIL_UPLOAD_FAIL';
  35. export const THUMBNAIL_UPLOAD_PROGRESS = 'THUMBNAIL_UPLOAD_PROGRESS';
  36. export const COMPOSE_SUGGESTIONS_CLEAR = 'COMPOSE_SUGGESTIONS_CLEAR';
  37. export const COMPOSE_SUGGESTIONS_READY = 'COMPOSE_SUGGESTIONS_READY';
  38. export const COMPOSE_SUGGESTION_SELECT = 'COMPOSE_SUGGESTION_SELECT';
  39. export const COMPOSE_SUGGESTION_IGNORE = 'COMPOSE_SUGGESTION_IGNORE';
  40. export const COMPOSE_SUGGESTION_TAGS_UPDATE = 'COMPOSE_SUGGESTION_TAGS_UPDATE';
  41. export const COMPOSE_TAG_HISTORY_UPDATE = 'COMPOSE_TAG_HISTORY_UPDATE';
  42. export const COMPOSE_MOUNT = 'COMPOSE_MOUNT';
  43. export const COMPOSE_UNMOUNT = 'COMPOSE_UNMOUNT';
  44. export const COMPOSE_SENSITIVITY_CHANGE = 'COMPOSE_SENSITIVITY_CHANGE';
  45. export const COMPOSE_SPOILERNESS_CHANGE = 'COMPOSE_SPOILERNESS_CHANGE';
  46. export const COMPOSE_SPOILER_TEXT_CHANGE = 'COMPOSE_SPOILER_TEXT_CHANGE';
  47. export const COMPOSE_VISIBILITY_CHANGE = 'COMPOSE_VISIBILITY_CHANGE';
  48. export const COMPOSE_COMPOSING_CHANGE = 'COMPOSE_COMPOSING_CHANGE';
  49. export const COMPOSE_LANGUAGE_CHANGE = 'COMPOSE_LANGUAGE_CHANGE';
  50. export const COMPOSE_EMOJI_INSERT = 'COMPOSE_EMOJI_INSERT';
  51. export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST';
  52. export const COMPOSE_UPLOAD_CHANGE_SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS';
  53. export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL';
  54. export const COMPOSE_POLL_ADD = 'COMPOSE_POLL_ADD';
  55. export const COMPOSE_POLL_REMOVE = 'COMPOSE_POLL_REMOVE';
  56. export const COMPOSE_POLL_OPTION_ADD = 'COMPOSE_POLL_OPTION_ADD';
  57. export const COMPOSE_POLL_OPTION_CHANGE = 'COMPOSE_POLL_OPTION_CHANGE';
  58. export const COMPOSE_POLL_OPTION_REMOVE = 'COMPOSE_POLL_OPTION_REMOVE';
  59. export const COMPOSE_POLL_SETTINGS_CHANGE = 'COMPOSE_POLL_SETTINGS_CHANGE';
  60. export const INIT_MEDIA_EDIT_MODAL = 'INIT_MEDIA_EDIT_MODAL';
  61. export const COMPOSE_CHANGE_MEDIA_DESCRIPTION = 'COMPOSE_CHANGE_MEDIA_DESCRIPTION';
  62. export const COMPOSE_CHANGE_MEDIA_FOCUS = 'COMPOSE_CHANGE_MEDIA_FOCUS';
  63. export const COMPOSE_SET_STATUS = 'COMPOSE_SET_STATUS';
  64. const messages = defineMessages({
  65. uploadErrorLimit: { id: 'upload_error.limit', defaultMessage: 'File upload limit exceeded.' },
  66. uploadErrorPoll: { id: 'upload_error.poll', defaultMessage: 'File upload not allowed with polls.' },
  67. });
  68. export const ensureComposeIsVisible = (getState, routerHistory) => {
  69. if (!getState().getIn(['compose', 'mounted'])) {
  70. routerHistory.push('/publish');
  71. }
  72. };
  73. export function setComposeToStatus(status, text, spoiler_text) {
  74. return{
  75. type: COMPOSE_SET_STATUS,
  76. status,
  77. text,
  78. spoiler_text,
  79. };
  80. };
  81. export function changeCompose(text) {
  82. return {
  83. type: COMPOSE_CHANGE,
  84. text: text,
  85. };
  86. };
  87. export function replyCompose(status, routerHistory) {
  88. return (dispatch, getState) => {
  89. dispatch({
  90. type: COMPOSE_REPLY,
  91. status: status,
  92. });
  93. ensureComposeIsVisible(getState, routerHistory);
  94. };
  95. };
  96. export function cancelReplyCompose() {
  97. return {
  98. type: COMPOSE_REPLY_CANCEL,
  99. };
  100. };
  101. export function resetCompose() {
  102. return {
  103. type: COMPOSE_RESET,
  104. };
  105. };
  106. export function mentionCompose(account, routerHistory) {
  107. return (dispatch, getState) => {
  108. dispatch({
  109. type: COMPOSE_MENTION,
  110. account: account,
  111. });
  112. ensureComposeIsVisible(getState, routerHistory);
  113. };
  114. };
  115. export function directCompose(account, routerHistory) {
  116. return (dispatch, getState) => {
  117. dispatch({
  118. type: COMPOSE_DIRECT,
  119. account: account,
  120. });
  121. ensureComposeIsVisible(getState, routerHistory);
  122. };
  123. };
  124. export function submitCompose(routerHistory) {
  125. return function (dispatch, getState) {
  126. const status = getState().getIn(['compose', 'text'], '');
  127. const media = getState().getIn(['compose', 'media_attachments']);
  128. const statusId = getState().getIn(['compose', 'id'], null);
  129. if ((!status || !status.length) && media.size === 0) {
  130. return;
  131. }
  132. dispatch(submitComposeRequest());
  133. api(getState).request({
  134. url: statusId === null ? '/api/v1/statuses' : `/api/v1/statuses/${statusId}`,
  135. method: statusId === null ? 'post' : 'put',
  136. data: {
  137. status,
  138. in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
  139. media_ids: media.map(item => item.get('id')),
  140. sensitive: getState().getIn(['compose', 'sensitive']),
  141. spoiler_text: getState().getIn(['compose', 'spoiler']) ? getState().getIn(['compose', 'spoiler_text'], '') : '',
  142. visibility: getState().getIn(['compose', 'privacy']),
  143. poll: getState().getIn(['compose', 'poll'], null),
  144. language: getState().getIn(['compose', 'language']),
  145. },
  146. headers: {
  147. 'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
  148. },
  149. }).then(function (response) {
  150. if (routerHistory && (routerHistory.location.pathname === '/publish' || routerHistory.location.pathname === '/statuses/new') && window.history.state) {
  151. routerHistory.goBack();
  152. }
  153. dispatch(insertIntoTagHistory(response.data.tags, status));
  154. dispatch(submitComposeSuccess({ ...response.data }));
  155. // To make the app more responsive, immediately push the status
  156. // into the columns
  157. const insertIfOnline = timelineId => {
  158. const timeline = getState().getIn(['timelines', timelineId]);
  159. if (timeline && timeline.get('items').size > 0 && timeline.getIn(['items', 0]) !== null && timeline.get('online')) {
  160. dispatch(updateTimeline(timelineId, { ...response.data }));
  161. }
  162. };
  163. if (statusId) {
  164. dispatch(importFetchedStatus({ ...response.data }));
  165. }
  166. if (statusId === null && response.data.visibility !== 'direct') {
  167. insertIfOnline('home');
  168. }
  169. if (statusId === null && response.data.in_reply_to_id === null && response.data.visibility === 'public') {
  170. insertIfOnline('community');
  171. insertIfOnline('public');
  172. insertIfOnline(`account:${response.data.account.id}`);
  173. }
  174. }).catch(function (error) {
  175. dispatch(submitComposeFail(error));
  176. });
  177. };
  178. };
  179. export function submitComposeRequest() {
  180. return {
  181. type: COMPOSE_SUBMIT_REQUEST,
  182. };
  183. };
  184. export function submitComposeSuccess(status) {
  185. return {
  186. type: COMPOSE_SUBMIT_SUCCESS,
  187. status: status,
  188. };
  189. };
  190. export function submitComposeFail(error) {
  191. return {
  192. type: COMPOSE_SUBMIT_FAIL,
  193. error: error,
  194. };
  195. };
  196. export function uploadCompose(files) {
  197. return function (dispatch, getState) {
  198. const uploadLimit = 4;
  199. const media = getState().getIn(['compose', 'media_attachments']);
  200. const pending = getState().getIn(['compose', 'pending_media_attachments']);
  201. const progress = new Array(files.length).fill(0);
  202. let total = Array.from(files).reduce((a, v) => a + v.size, 0);
  203. if (files.length + media.size + pending > uploadLimit) {
  204. dispatch(showAlert(undefined, messages.uploadErrorLimit));
  205. return;
  206. }
  207. if (getState().getIn(['compose', 'poll'])) {
  208. dispatch(showAlert(undefined, messages.uploadErrorPoll));
  209. return;
  210. }
  211. dispatch(uploadComposeRequest());
  212. for (const [i, f] of Array.from(files).entries()) {
  213. if (media.size + i > 3) break;
  214. resizeImage(f).then(file => {
  215. const data = new FormData();
  216. data.append('file', file);
  217. // Account for disparity in size of original image and resized data
  218. total += file.size - f.size;
  219. return api(getState).post('/api/v2/media', data, {
  220. onUploadProgress: function({ loaded }){
  221. progress[i] = loaded;
  222. dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
  223. },
  224. }).then(({ status, data }) => {
  225. // If server-side processing of the media attachment has not completed yet,
  226. // poll the server until it is, before showing the media attachment as uploaded
  227. if (status === 200) {
  228. dispatch(uploadComposeSuccess(data, f));
  229. } else if (status === 202) {
  230. dispatch(uploadComposeProcessing());
  231. let tryCount = 1;
  232. const poll = () => {
  233. api(getState).get(`/api/v1/media/${data.id}`).then(response => {
  234. if (response.status === 200) {
  235. dispatch(uploadComposeSuccess(response.data, f));
  236. } else if (response.status === 206) {
  237. const retryAfter = (Math.log2(tryCount) || 1) * 1000;
  238. tryCount += 1;
  239. setTimeout(() => poll(), retryAfter);
  240. }
  241. }).catch(error => dispatch(uploadComposeFail(error)));
  242. };
  243. poll();
  244. }
  245. });
  246. }).catch(error => dispatch(uploadComposeFail(error)));
  247. };
  248. };
  249. };
  250. export const uploadComposeProcessing = () => ({
  251. type: COMPOSE_UPLOAD_PROCESSING,
  252. });
  253. export const uploadThumbnail = (id, file) => (dispatch, getState) => {
  254. dispatch(uploadThumbnailRequest());
  255. const total = file.size;
  256. const data = new FormData();
  257. data.append('thumbnail', file);
  258. api(getState).put(`/api/v1/media/${id}`, data, {
  259. onUploadProgress: ({ loaded }) => {
  260. dispatch(uploadThumbnailProgress(loaded, total));
  261. },
  262. }).then(({ data }) => {
  263. dispatch(uploadThumbnailSuccess(data));
  264. }).catch(error => {
  265. dispatch(uploadThumbnailFail(id, error));
  266. });
  267. };
  268. export const uploadThumbnailRequest = () => ({
  269. type: THUMBNAIL_UPLOAD_REQUEST,
  270. skipLoading: true,
  271. });
  272. export const uploadThumbnailProgress = (loaded, total) => ({
  273. type: THUMBNAIL_UPLOAD_PROGRESS,
  274. loaded,
  275. total,
  276. skipLoading: true,
  277. });
  278. export const uploadThumbnailSuccess = media => ({
  279. type: THUMBNAIL_UPLOAD_SUCCESS,
  280. media,
  281. skipLoading: true,
  282. });
  283. export const uploadThumbnailFail = error => ({
  284. type: THUMBNAIL_UPLOAD_FAIL,
  285. error,
  286. skipLoading: true,
  287. });
  288. export function initMediaEditModal(id) {
  289. return dispatch => {
  290. dispatch({
  291. type: INIT_MEDIA_EDIT_MODAL,
  292. id,
  293. });
  294. dispatch(openModal('FOCAL_POINT', { id }));
  295. };
  296. };
  297. export function onChangeMediaDescription(description) {
  298. return {
  299. type: COMPOSE_CHANGE_MEDIA_DESCRIPTION,
  300. description,
  301. };
  302. };
  303. export function onChangeMediaFocus(focusX, focusY) {
  304. return {
  305. type: COMPOSE_CHANGE_MEDIA_FOCUS,
  306. focusX,
  307. focusY,
  308. };
  309. };
  310. export function changeUploadCompose(id, params) {
  311. return (dispatch, getState) => {
  312. dispatch(changeUploadComposeRequest());
  313. api(getState).put(`/api/v1/media/${id}`, params).then(response => {
  314. dispatch(changeUploadComposeSuccess(response.data));
  315. }).catch(error => {
  316. dispatch(changeUploadComposeFail(id, error));
  317. });
  318. };
  319. };
  320. export function changeUploadComposeRequest() {
  321. return {
  322. type: COMPOSE_UPLOAD_CHANGE_REQUEST,
  323. skipLoading: true,
  324. };
  325. };
  326. export function changeUploadComposeSuccess(media) {
  327. return {
  328. type: COMPOSE_UPLOAD_CHANGE_SUCCESS,
  329. media: media,
  330. skipLoading: true,
  331. };
  332. };
  333. export function changeUploadComposeFail(error) {
  334. return {
  335. type: COMPOSE_UPLOAD_CHANGE_FAIL,
  336. error: error,
  337. skipLoading: true,
  338. };
  339. };
  340. export function uploadComposeRequest() {
  341. return {
  342. type: COMPOSE_UPLOAD_REQUEST,
  343. skipLoading: true,
  344. };
  345. };
  346. export function uploadComposeProgress(loaded, total) {
  347. return {
  348. type: COMPOSE_UPLOAD_PROGRESS,
  349. loaded: loaded,
  350. total: total,
  351. };
  352. };
  353. export function uploadComposeSuccess(media, file) {
  354. return {
  355. type: COMPOSE_UPLOAD_SUCCESS,
  356. media: media,
  357. file: file,
  358. skipLoading: true,
  359. };
  360. };
  361. export function uploadComposeFail(error) {
  362. return {
  363. type: COMPOSE_UPLOAD_FAIL,
  364. error: error,
  365. skipLoading: true,
  366. };
  367. };
  368. export function undoUploadCompose(media_id) {
  369. return {
  370. type: COMPOSE_UPLOAD_UNDO,
  371. media_id: media_id,
  372. };
  373. };
  374. export function clearComposeSuggestions() {
  375. if (fetchComposeSuggestionsAccountsController) {
  376. fetchComposeSuggestionsAccountsController.abort();
  377. }
  378. return {
  379. type: COMPOSE_SUGGESTIONS_CLEAR,
  380. };
  381. };
  382. const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) => {
  383. if (fetchComposeSuggestionsAccountsController) {
  384. fetchComposeSuggestionsAccountsController.abort();
  385. }
  386. fetchComposeSuggestionsAccountsController = new AbortController();
  387. api(getState).get('/api/v1/accounts/search', {
  388. signal: fetchComposeSuggestionsAccountsController.signal,
  389. params: {
  390. q: token.slice(1),
  391. resolve: false,
  392. limit: 4,
  393. },
  394. }).then(response => {
  395. dispatch(importFetchedAccounts(response.data));
  396. dispatch(readyComposeSuggestionsAccounts(token, response.data));
  397. }).catch(error => {
  398. if (!axios.isCancel(error)) {
  399. dispatch(showAlertForError(error));
  400. }
  401. }).finally(() => {
  402. fetchComposeSuggestionsAccountsController = undefined;
  403. });
  404. }, 200, { leading: true, trailing: true });
  405. const fetchComposeSuggestionsEmojis = (dispatch, getState, token) => {
  406. const results = emojiSearch(token.replace(':', ''), { maxResults: 5 });
  407. dispatch(readyComposeSuggestionsEmojis(token, results));
  408. };
  409. const fetchComposeSuggestionsTags = throttle((dispatch, getState, token) => {
  410. if (fetchComposeSuggestionsTagsController) {
  411. fetchComposeSuggestionsTagsController.abort();
  412. }
  413. dispatch(updateSuggestionTags(token));
  414. fetchComposeSuggestionsTagsController = new AbortController();
  415. api(getState).get('/api/v2/search', {
  416. signal: fetchComposeSuggestionsTagsController.signal,
  417. params: {
  418. type: 'hashtags',
  419. q: token.slice(1),
  420. resolve: false,
  421. limit: 4,
  422. exclude_unreviewed: true,
  423. },
  424. }).then(({ data }) => {
  425. dispatch(readyComposeSuggestionsTags(token, data.hashtags));
  426. }).catch(error => {
  427. if (!axios.isCancel(error)) {
  428. dispatch(showAlertForError(error));
  429. }
  430. }).finally(() => {
  431. fetchComposeSuggestionsTagsController = undefined;
  432. });
  433. }, 200, { leading: true, trailing: true });
  434. export function fetchComposeSuggestions(token) {
  435. return (dispatch, getState) => {
  436. switch (token[0]) {
  437. case ':':
  438. fetchComposeSuggestionsEmojis(dispatch, getState, token);
  439. break;
  440. case '#':
  441. fetchComposeSuggestionsTags(dispatch, getState, token);
  442. break;
  443. default:
  444. fetchComposeSuggestionsAccounts(dispatch, getState, token);
  445. break;
  446. }
  447. };
  448. };
  449. export function readyComposeSuggestionsEmojis(token, emojis) {
  450. return {
  451. type: COMPOSE_SUGGESTIONS_READY,
  452. token,
  453. emojis,
  454. };
  455. };
  456. export function readyComposeSuggestionsAccounts(token, accounts) {
  457. return {
  458. type: COMPOSE_SUGGESTIONS_READY,
  459. token,
  460. accounts,
  461. };
  462. };
  463. export const readyComposeSuggestionsTags = (token, tags) => ({
  464. type: COMPOSE_SUGGESTIONS_READY,
  465. token,
  466. tags,
  467. });
  468. export function selectComposeSuggestion(position, token, suggestion, path) {
  469. return (dispatch, getState) => {
  470. let completion, startPosition;
  471. if (suggestion.type === 'emoji') {
  472. completion = suggestion.native || suggestion.colons;
  473. startPosition = position - 1;
  474. dispatch(useEmoji(suggestion));
  475. } else if (suggestion.type === 'hashtag') {
  476. completion = `#${suggestion.name}`;
  477. startPosition = position - 1;
  478. } else if (suggestion.type === 'account') {
  479. completion = getState().getIn(['accounts', suggestion.id, 'acct']);
  480. startPosition = position;
  481. }
  482. // We don't want to replace hashtags that vary only in case due to accessibility, but we need to fire off an event so that
  483. // the suggestions are dismissed and the cursor moves forward.
  484. if (suggestion.type !== 'hashtag' || token.slice(1).localeCompare(suggestion.name, undefined, { sensitivity: 'accent' }) !== 0) {
  485. dispatch({
  486. type: COMPOSE_SUGGESTION_SELECT,
  487. position: startPosition,
  488. token,
  489. completion,
  490. path,
  491. });
  492. } else {
  493. dispatch({
  494. type: COMPOSE_SUGGESTION_IGNORE,
  495. position: startPosition,
  496. token,
  497. completion,
  498. path,
  499. });
  500. }
  501. };
  502. };
  503. export function updateSuggestionTags(token) {
  504. return {
  505. type: COMPOSE_SUGGESTION_TAGS_UPDATE,
  506. token,
  507. };
  508. }
  509. export function updateTagHistory(tags) {
  510. return {
  511. type: COMPOSE_TAG_HISTORY_UPDATE,
  512. tags,
  513. };
  514. }
  515. export function hydrateCompose() {
  516. return (dispatch, getState) => {
  517. const me = getState().getIn(['meta', 'me']);
  518. const history = tagHistory.get(me);
  519. if (history !== null) {
  520. dispatch(updateTagHistory(history));
  521. }
  522. };
  523. }
  524. function insertIntoTagHistory(recognizedTags, text) {
  525. return (dispatch, getState) => {
  526. const state = getState();
  527. const oldHistory = state.getIn(['compose', 'tagHistory']);
  528. const me = state.getIn(['meta', 'me']);
  529. // FIXME: Matching input hashtags with recognized hashtags has become more
  530. // complicated because of new normalization rules, it's no longer just
  531. // a case sensitivity issue
  532. const names = recognizedTags.map(tag => {
  533. const matches = text.match(new RegExp(`#${tag.name}`, 'i'));
  534. if (matches && matches.length > 0) {
  535. return matches[0].slice(1);
  536. } else {
  537. return tag.name;
  538. }
  539. });
  540. const intersectedOldHistory = oldHistory.filter(name => names.findIndex(newName => newName.toLowerCase() === name.toLowerCase()) === -1);
  541. names.push(...intersectedOldHistory.toJS());
  542. const newHistory = names.slice(0, 1000);
  543. tagHistory.set(me, newHistory);
  544. dispatch(updateTagHistory(newHistory));
  545. };
  546. }
  547. export function mountCompose() {
  548. return {
  549. type: COMPOSE_MOUNT,
  550. };
  551. };
  552. export function unmountCompose() {
  553. return {
  554. type: COMPOSE_UNMOUNT,
  555. };
  556. };
  557. export function changeComposeSensitivity() {
  558. return {
  559. type: COMPOSE_SENSITIVITY_CHANGE,
  560. };
  561. };
  562. export const changeComposeLanguage = language => ({
  563. type: COMPOSE_LANGUAGE_CHANGE,
  564. language,
  565. });
  566. export function changeComposeSpoilerness() {
  567. return {
  568. type: COMPOSE_SPOILERNESS_CHANGE,
  569. };
  570. };
  571. export function changeComposeSpoilerText(text) {
  572. return {
  573. type: COMPOSE_SPOILER_TEXT_CHANGE,
  574. text,
  575. };
  576. };
  577. export function changeComposeVisibility(value) {
  578. return {
  579. type: COMPOSE_VISIBILITY_CHANGE,
  580. value,
  581. };
  582. };
  583. export function insertEmojiCompose(position, emoji, needsSpace) {
  584. return {
  585. type: COMPOSE_EMOJI_INSERT,
  586. position,
  587. emoji,
  588. needsSpace,
  589. };
  590. };
  591. export function changeComposing(value) {
  592. return {
  593. type: COMPOSE_COMPOSING_CHANGE,
  594. value,
  595. };
  596. };
  597. export function addPoll() {
  598. return {
  599. type: COMPOSE_POLL_ADD,
  600. };
  601. };
  602. export function removePoll() {
  603. return {
  604. type: COMPOSE_POLL_REMOVE,
  605. };
  606. };
  607. export function addPollOption(title) {
  608. return {
  609. type: COMPOSE_POLL_OPTION_ADD,
  610. title,
  611. };
  612. };
  613. export function changePollOption(index, title) {
  614. return {
  615. type: COMPOSE_POLL_OPTION_CHANGE,
  616. index,
  617. title,
  618. };
  619. };
  620. export function removePollOption(index) {
  621. return {
  622. type: COMPOSE_POLL_OPTION_REMOVE,
  623. index,
  624. };
  625. };
  626. export function changePollSettings(expiresIn, isMultiple) {
  627. return {
  628. type: COMPOSE_POLL_SETTINGS_CHANGE,
  629. expiresIn,
  630. isMultiple,
  631. };
  632. };