compose.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. import { isCancel } 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 } 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_UNDO = 'COMPOSE_UPLOAD_UNDO';
  31. export const THUMBNAIL_UPLOAD_REQUEST = 'THUMBNAIL_UPLOAD_REQUEST';
  32. export const THUMBNAIL_UPLOAD_SUCCESS = 'THUMBNAIL_UPLOAD_SUCCESS';
  33. export const THUMBNAIL_UPLOAD_FAIL = 'THUMBNAIL_UPLOAD_FAIL';
  34. export const THUMBNAIL_UPLOAD_PROGRESS = 'THUMBNAIL_UPLOAD_PROGRESS';
  35. export const COMPOSE_SUGGESTIONS_CLEAR = 'COMPOSE_SUGGESTIONS_CLEAR';
  36. export const COMPOSE_SUGGESTIONS_READY = 'COMPOSE_SUGGESTIONS_READY';
  37. export const COMPOSE_SUGGESTION_SELECT = 'COMPOSE_SUGGESTION_SELECT';
  38. export const COMPOSE_SUGGESTION_IGNORE = 'COMPOSE_SUGGESTION_IGNORE';
  39. export const COMPOSE_SUGGESTION_TAGS_UPDATE = 'COMPOSE_SUGGESTION_TAGS_UPDATE';
  40. export const COMPOSE_TAG_HISTORY_UPDATE = 'COMPOSE_TAG_HISTORY_UPDATE';
  41. export const COMPOSE_MOUNT = 'COMPOSE_MOUNT';
  42. export const COMPOSE_UNMOUNT = 'COMPOSE_UNMOUNT';
  43. export const COMPOSE_SENSITIVITY_CHANGE = 'COMPOSE_SENSITIVITY_CHANGE';
  44. export const COMPOSE_SPOILERNESS_CHANGE = 'COMPOSE_SPOILERNESS_CHANGE';
  45. export const COMPOSE_SPOILER_TEXT_CHANGE = 'COMPOSE_SPOILER_TEXT_CHANGE';
  46. export const COMPOSE_VISIBILITY_CHANGE = 'COMPOSE_VISIBILITY_CHANGE';
  47. export const COMPOSE_COMPOSING_CHANGE = 'COMPOSE_COMPOSING_CHANGE';
  48. export const COMPOSE_LANGUAGE_CHANGE = 'COMPOSE_LANGUAGE_CHANGE';
  49. export const COMPOSE_EMOJI_INSERT = 'COMPOSE_EMOJI_INSERT';
  50. export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST';
  51. export const COMPOSE_UPLOAD_CHANGE_SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS';
  52. export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL';
  53. export const COMPOSE_POLL_ADD = 'COMPOSE_POLL_ADD';
  54. export const COMPOSE_POLL_REMOVE = 'COMPOSE_POLL_REMOVE';
  55. export const COMPOSE_POLL_OPTION_ADD = 'COMPOSE_POLL_OPTION_ADD';
  56. export const COMPOSE_POLL_OPTION_CHANGE = 'COMPOSE_POLL_OPTION_CHANGE';
  57. export const COMPOSE_POLL_OPTION_REMOVE = 'COMPOSE_POLL_OPTION_REMOVE';
  58. export const COMPOSE_POLL_SETTINGS_CHANGE = 'COMPOSE_POLL_SETTINGS_CHANGE';
  59. export const INIT_MEDIA_EDIT_MODAL = 'INIT_MEDIA_EDIT_MODAL';
  60. export const COMPOSE_CHANGE_MEDIA_DESCRIPTION = 'COMPOSE_CHANGE_MEDIA_DESCRIPTION';
  61. export const COMPOSE_CHANGE_MEDIA_FOCUS = 'COMPOSE_CHANGE_MEDIA_FOCUS';
  62. export const COMPOSE_SET_STATUS = 'COMPOSE_SET_STATUS';
  63. const messages = defineMessages({
  64. uploadErrorLimit: { id: 'upload_error.limit', defaultMessage: 'File upload limit exceeded.' },
  65. uploadErrorPoll: { id: 'upload_error.poll', defaultMessage: 'File upload not allowed with polls.' },
  66. });
  67. const COMPOSE_PANEL_BREAKPOINT = 600 + (285 * 1) + (10 * 1);
  68. export const ensureComposeIsVisible = (getState, routerHistory) => {
  69. if (!getState().getIn(['compose', 'mounted']) && window.innerWidth < COMPOSE_PANEL_BREAKPOINT) {
  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 === null && response.data.visibility !== 'direct') {
  164. insertIfOnline('home');
  165. }
  166. if (statusId === null && response.data.in_reply_to_id === null && response.data.visibility === 'public') {
  167. insertIfOnline('community');
  168. insertIfOnline('public');
  169. insertIfOnline(`account:${response.data.account.id}`);
  170. }
  171. }).catch(function (error) {
  172. dispatch(submitComposeFail(error));
  173. });
  174. };
  175. };
  176. export function submitComposeRequest() {
  177. return {
  178. type: COMPOSE_SUBMIT_REQUEST,
  179. };
  180. };
  181. export function submitComposeSuccess(status) {
  182. return {
  183. type: COMPOSE_SUBMIT_SUCCESS,
  184. status: status,
  185. };
  186. };
  187. export function submitComposeFail(error) {
  188. return {
  189. type: COMPOSE_SUBMIT_FAIL,
  190. error: error,
  191. };
  192. };
  193. export function uploadCompose(files) {
  194. return function (dispatch, getState) {
  195. const uploadLimit = 4;
  196. const media = getState().getIn(['compose', 'media_attachments']);
  197. const pending = getState().getIn(['compose', 'pending_media_attachments']);
  198. const progress = new Array(files.length).fill(0);
  199. let total = Array.from(files).reduce((a, v) => a + v.size, 0);
  200. if (files.length + media.size + pending > uploadLimit) {
  201. dispatch(showAlert(undefined, messages.uploadErrorLimit));
  202. return;
  203. }
  204. if (getState().getIn(['compose', 'poll'])) {
  205. dispatch(showAlert(undefined, messages.uploadErrorPoll));
  206. return;
  207. }
  208. dispatch(uploadComposeRequest());
  209. for (const [i, f] of Array.from(files).entries()) {
  210. if (media.size + i > 3) break;
  211. resizeImage(f).then(file => {
  212. const data = new FormData();
  213. data.append('file', file);
  214. // Account for disparity in size of original image and resized data
  215. total += file.size - f.size;
  216. return api(getState).post('/api/v2/media', data, {
  217. onUploadProgress: function({ loaded }){
  218. progress[i] = loaded;
  219. dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
  220. },
  221. }).then(({ status, data }) => {
  222. // If server-side processing of the media attachment has not completed yet,
  223. // poll the server until it is, before showing the media attachment as uploaded
  224. if (status === 200) {
  225. dispatch(uploadComposeSuccess(data, f));
  226. } else if (status === 202) {
  227. let tryCount = 1;
  228. const poll = () => {
  229. api(getState).get(`/api/v1/media/${data.id}`).then(response => {
  230. if (response.status === 200) {
  231. dispatch(uploadComposeSuccess(response.data, f));
  232. } else if (response.status === 206) {
  233. let retryAfter = (Math.log2(tryCount) || 1) * 1000;
  234. tryCount += 1;
  235. setTimeout(() => poll(), retryAfter);
  236. }
  237. }).catch(error => dispatch(uploadComposeFail(error)));
  238. };
  239. poll();
  240. }
  241. });
  242. }).catch(error => dispatch(uploadComposeFail(error)));
  243. };
  244. };
  245. };
  246. export const uploadThumbnail = (id, file) => (dispatch, getState) => {
  247. dispatch(uploadThumbnailRequest());
  248. const total = file.size;
  249. const data = new FormData();
  250. data.append('thumbnail', file);
  251. api(getState).put(`/api/v1/media/${id}`, data, {
  252. onUploadProgress: ({ loaded }) => {
  253. dispatch(uploadThumbnailProgress(loaded, total));
  254. },
  255. }).then(({ data }) => {
  256. dispatch(uploadThumbnailSuccess(data));
  257. }).catch(error => {
  258. dispatch(uploadThumbnailFail(id, error));
  259. });
  260. };
  261. export const uploadThumbnailRequest = () => ({
  262. type: THUMBNAIL_UPLOAD_REQUEST,
  263. skipLoading: true,
  264. });
  265. export const uploadThumbnailProgress = (loaded, total) => ({
  266. type: THUMBNAIL_UPLOAD_PROGRESS,
  267. loaded,
  268. total,
  269. skipLoading: true,
  270. });
  271. export const uploadThumbnailSuccess = media => ({
  272. type: THUMBNAIL_UPLOAD_SUCCESS,
  273. media,
  274. skipLoading: true,
  275. });
  276. export const uploadThumbnailFail = error => ({
  277. type: THUMBNAIL_UPLOAD_FAIL,
  278. error,
  279. skipLoading: true,
  280. });
  281. export function initMediaEditModal(id) {
  282. return dispatch => {
  283. dispatch({
  284. type: INIT_MEDIA_EDIT_MODAL,
  285. id,
  286. });
  287. dispatch(openModal('FOCAL_POINT', { id }));
  288. };
  289. };
  290. export function onChangeMediaDescription(description) {
  291. return {
  292. type: COMPOSE_CHANGE_MEDIA_DESCRIPTION,
  293. description,
  294. };
  295. };
  296. export function onChangeMediaFocus(focusX, focusY) {
  297. return {
  298. type: COMPOSE_CHANGE_MEDIA_FOCUS,
  299. focusX,
  300. focusY,
  301. };
  302. };
  303. export function changeUploadCompose(id, params) {
  304. return (dispatch, getState) => {
  305. dispatch(changeUploadComposeRequest());
  306. api(getState).put(`/api/v1/media/${id}`, params).then(response => {
  307. dispatch(changeUploadComposeSuccess(response.data));
  308. }).catch(error => {
  309. dispatch(changeUploadComposeFail(id, error));
  310. });
  311. };
  312. };
  313. export function changeUploadComposeRequest() {
  314. return {
  315. type: COMPOSE_UPLOAD_CHANGE_REQUEST,
  316. skipLoading: true,
  317. };
  318. };
  319. export function changeUploadComposeSuccess(media) {
  320. return {
  321. type: COMPOSE_UPLOAD_CHANGE_SUCCESS,
  322. media: media,
  323. skipLoading: true,
  324. };
  325. };
  326. export function changeUploadComposeFail(error) {
  327. return {
  328. type: COMPOSE_UPLOAD_CHANGE_FAIL,
  329. error: error,
  330. skipLoading: true,
  331. };
  332. };
  333. export function uploadComposeRequest() {
  334. return {
  335. type: COMPOSE_UPLOAD_REQUEST,
  336. skipLoading: true,
  337. };
  338. };
  339. export function uploadComposeProgress(loaded, total) {
  340. return {
  341. type: COMPOSE_UPLOAD_PROGRESS,
  342. loaded: loaded,
  343. total: total,
  344. };
  345. };
  346. export function uploadComposeSuccess(media, file) {
  347. return {
  348. type: COMPOSE_UPLOAD_SUCCESS,
  349. media: media,
  350. file: file,
  351. skipLoading: true,
  352. };
  353. };
  354. export function uploadComposeFail(error) {
  355. return {
  356. type: COMPOSE_UPLOAD_FAIL,
  357. error: error,
  358. skipLoading: true,
  359. };
  360. };
  361. export function undoUploadCompose(media_id) {
  362. return {
  363. type: COMPOSE_UPLOAD_UNDO,
  364. media_id: media_id,
  365. };
  366. };
  367. export function clearComposeSuggestions() {
  368. if (fetchComposeSuggestionsAccountsController) {
  369. fetchComposeSuggestionsAccountsController.abort();
  370. }
  371. return {
  372. type: COMPOSE_SUGGESTIONS_CLEAR,
  373. };
  374. };
  375. const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) => {
  376. if (fetchComposeSuggestionsAccountsController) {
  377. fetchComposeSuggestionsAccountsController.abort();
  378. }
  379. fetchComposeSuggestionsAccountsController = new AbortController();
  380. api(getState).get('/api/v1/accounts/search', {
  381. signal: fetchComposeSuggestionsAccountsController.signal,
  382. params: {
  383. q: token.slice(1),
  384. resolve: false,
  385. limit: 4,
  386. },
  387. }).then(response => {
  388. dispatch(importFetchedAccounts(response.data));
  389. dispatch(readyComposeSuggestionsAccounts(token, response.data));
  390. }).catch(error => {
  391. if (!isCancel(error)) {
  392. dispatch(showAlertForError(error));
  393. }
  394. });
  395. }, 200, { leading: true, trailing: true });
  396. const fetchComposeSuggestionsEmojis = (dispatch, getState, token) => {
  397. const results = emojiSearch(token.replace(':', ''), { maxResults: 5 });
  398. dispatch(readyComposeSuggestionsEmojis(token, results));
  399. };
  400. const fetchComposeSuggestionsTags = throttle((dispatch, getState, token) => {
  401. if (fetchComposeSuggestionsTagsController) {
  402. fetchComposeSuggestionsTagsController.abort();
  403. }
  404. dispatch(updateSuggestionTags(token));
  405. fetchComposeSuggestionsTagsController = new AbortController();
  406. api(getState).get('/api/v2/search', {
  407. signal: fetchComposeSuggestionsTagsController.signal,
  408. params: {
  409. type: 'hashtags',
  410. q: token.slice(1),
  411. resolve: false,
  412. limit: 4,
  413. exclude_unreviewed: true,
  414. },
  415. }).then(({ data }) => {
  416. dispatch(readyComposeSuggestionsTags(token, data.hashtags));
  417. }).catch(error => {
  418. if (!isCancel(error)) {
  419. dispatch(showAlertForError(error));
  420. }
  421. });
  422. }, 200, { leading: true, trailing: true });
  423. export function fetchComposeSuggestions(token) {
  424. return (dispatch, getState) => {
  425. switch (token[0]) {
  426. case ':':
  427. fetchComposeSuggestionsEmojis(dispatch, getState, token);
  428. break;
  429. case '#':
  430. fetchComposeSuggestionsTags(dispatch, getState, token);
  431. break;
  432. default:
  433. fetchComposeSuggestionsAccounts(dispatch, getState, token);
  434. break;
  435. }
  436. };
  437. };
  438. export function readyComposeSuggestionsEmojis(token, emojis) {
  439. return {
  440. type: COMPOSE_SUGGESTIONS_READY,
  441. token,
  442. emojis,
  443. };
  444. };
  445. export function readyComposeSuggestionsAccounts(token, accounts) {
  446. return {
  447. type: COMPOSE_SUGGESTIONS_READY,
  448. token,
  449. accounts,
  450. };
  451. };
  452. export const readyComposeSuggestionsTags = (token, tags) => ({
  453. type: COMPOSE_SUGGESTIONS_READY,
  454. token,
  455. tags,
  456. });
  457. export function selectComposeSuggestion(position, token, suggestion, path) {
  458. return (dispatch, getState) => {
  459. let completion, startPosition;
  460. if (suggestion.type === 'emoji') {
  461. completion = suggestion.native || suggestion.colons;
  462. startPosition = position - 1;
  463. dispatch(useEmoji(suggestion));
  464. } else if (suggestion.type === 'hashtag') {
  465. completion = `#${suggestion.name}`;
  466. startPosition = position - 1;
  467. } else if (suggestion.type === 'account') {
  468. completion = getState().getIn(['accounts', suggestion.id, 'acct']);
  469. startPosition = position;
  470. }
  471. // 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
  472. // the suggestions are dismissed and the cursor moves forward.
  473. if (suggestion.type !== 'hashtag' || token.slice(1).localeCompare(suggestion.name, undefined, { sensitivity: 'accent' }) !== 0) {
  474. dispatch({
  475. type: COMPOSE_SUGGESTION_SELECT,
  476. position: startPosition,
  477. token,
  478. completion,
  479. path,
  480. });
  481. } else {
  482. dispatch({
  483. type: COMPOSE_SUGGESTION_IGNORE,
  484. position: startPosition,
  485. token,
  486. completion,
  487. path,
  488. });
  489. }
  490. };
  491. };
  492. export function updateSuggestionTags(token) {
  493. return {
  494. type: COMPOSE_SUGGESTION_TAGS_UPDATE,
  495. token,
  496. };
  497. }
  498. export function updateTagHistory(tags) {
  499. return {
  500. type: COMPOSE_TAG_HISTORY_UPDATE,
  501. tags,
  502. };
  503. }
  504. export function hydrateCompose() {
  505. return (dispatch, getState) => {
  506. const me = getState().getIn(['meta', 'me']);
  507. const history = tagHistory.get(me);
  508. if (history !== null) {
  509. dispatch(updateTagHistory(history));
  510. }
  511. };
  512. }
  513. function insertIntoTagHistory(recognizedTags, text) {
  514. return (dispatch, getState) => {
  515. const state = getState();
  516. const oldHistory = state.getIn(['compose', 'tagHistory']);
  517. const me = state.getIn(['meta', 'me']);
  518. // FIXME: Matching input hashtags with recognized hashtags has become more
  519. // complicated because of new normalization rules, it's no longer just
  520. // a case sensitivity issue
  521. const names = recognizedTags.map(tag => {
  522. const matches = text.match(new RegExp(`#${tag.name}`, 'i'));
  523. if (matches && matches.length > 0) {
  524. return matches[0].slice(1);
  525. } else {
  526. return tag.name;
  527. }
  528. });
  529. const intersectedOldHistory = oldHistory.filter(name => names.findIndex(newName => newName.toLowerCase() === name.toLowerCase()) === -1);
  530. names.push(...intersectedOldHistory.toJS());
  531. const newHistory = names.slice(0, 1000);
  532. tagHistory.set(me, newHistory);
  533. dispatch(updateTagHistory(newHistory));
  534. };
  535. }
  536. export function mountCompose() {
  537. return {
  538. type: COMPOSE_MOUNT,
  539. };
  540. };
  541. export function unmountCompose() {
  542. return {
  543. type: COMPOSE_UNMOUNT,
  544. };
  545. };
  546. export function changeComposeSensitivity() {
  547. return {
  548. type: COMPOSE_SENSITIVITY_CHANGE,
  549. };
  550. };
  551. export const changeComposeLanguage = language => ({
  552. type: COMPOSE_LANGUAGE_CHANGE,
  553. language,
  554. });
  555. export function changeComposeSpoilerness() {
  556. return {
  557. type: COMPOSE_SPOILERNESS_CHANGE,
  558. };
  559. };
  560. export function changeComposeSpoilerText(text) {
  561. return {
  562. type: COMPOSE_SPOILER_TEXT_CHANGE,
  563. text,
  564. };
  565. };
  566. export function changeComposeVisibility(value) {
  567. return {
  568. type: COMPOSE_VISIBILITY_CHANGE,
  569. value,
  570. };
  571. };
  572. export function insertEmojiCompose(position, emoji, needsSpace) {
  573. return {
  574. type: COMPOSE_EMOJI_INSERT,
  575. position,
  576. emoji,
  577. needsSpace,
  578. };
  579. };
  580. export function changeComposing(value) {
  581. return {
  582. type: COMPOSE_COMPOSING_CHANGE,
  583. value,
  584. };
  585. };
  586. export function addPoll() {
  587. return {
  588. type: COMPOSE_POLL_ADD,
  589. };
  590. };
  591. export function removePoll() {
  592. return {
  593. type: COMPOSE_POLL_REMOVE,
  594. };
  595. };
  596. export function addPollOption(title) {
  597. return {
  598. type: COMPOSE_POLL_OPTION_ADD,
  599. title,
  600. };
  601. };
  602. export function changePollOption(index, title) {
  603. return {
  604. type: COMPOSE_POLL_OPTION_CHANGE,
  605. index,
  606. title,
  607. };
  608. };
  609. export function removePollOption(index) {
  610. return {
  611. type: COMPOSE_POLL_OPTION_REMOVE,
  612. index,
  613. };
  614. };
  615. export function changePollSettings(expiresIn, isMultiple) {
  616. return {
  617. type: COMPOSE_POLL_SETTINGS_CHANGE,
  618. expiresIn,
  619. isMultiple,
  620. };
  621. };