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.

465 lines
12 KiB

7 years ago
7 years ago
  1. import api from '../api';
  2. import { CancelToken, isCancel } from 'axios';
  3. import { throttle } from 'lodash';
  4. import { search as emojiSearch } from '../features/emoji/emoji_mart_search_light';
  5. import { tagHistory } from '../settings';
  6. import { useEmoji } from './emojis';
  7. import { importFetchedAccounts } from './importer';
  8. import { updateTimeline } from './timelines';
  9. import { showAlertForError } from './alerts';
  10. let cancelFetchComposeSuggestionsAccounts;
  11. export const COMPOSE_CHANGE = 'COMPOSE_CHANGE';
  12. export const COMPOSE_SUBMIT_REQUEST = 'COMPOSE_SUBMIT_REQUEST';
  13. export const COMPOSE_SUBMIT_SUCCESS = 'COMPOSE_SUBMIT_SUCCESS';
  14. export const COMPOSE_SUBMIT_FAIL = 'COMPOSE_SUBMIT_FAIL';
  15. export const COMPOSE_REPLY = 'COMPOSE_REPLY';
  16. export const COMPOSE_REPLY_CANCEL = 'COMPOSE_REPLY_CANCEL';
  17. export const COMPOSE_DIRECT = 'COMPOSE_DIRECT';
  18. export const COMPOSE_MENTION = 'COMPOSE_MENTION';
  19. export const COMPOSE_RESET = 'COMPOSE_RESET';
  20. export const COMPOSE_UPLOAD_REQUEST = 'COMPOSE_UPLOAD_REQUEST';
  21. export const COMPOSE_UPLOAD_SUCCESS = 'COMPOSE_UPLOAD_SUCCESS';
  22. export const COMPOSE_UPLOAD_FAIL = 'COMPOSE_UPLOAD_FAIL';
  23. export const COMPOSE_UPLOAD_PROGRESS = 'COMPOSE_UPLOAD_PROGRESS';
  24. export const COMPOSE_UPLOAD_UNDO = 'COMPOSE_UPLOAD_UNDO';
  25. export const COMPOSE_SUGGESTIONS_CLEAR = 'COMPOSE_SUGGESTIONS_CLEAR';
  26. export const COMPOSE_SUGGESTIONS_READY = 'COMPOSE_SUGGESTIONS_READY';
  27. export const COMPOSE_SUGGESTION_SELECT = 'COMPOSE_SUGGESTION_SELECT';
  28. export const COMPOSE_SUGGESTION_TAGS_UPDATE = 'COMPOSE_SUGGESTION_TAGS_UPDATE';
  29. export const COMPOSE_TAG_HISTORY_UPDATE = 'COMPOSE_TAG_HISTORY_UPDATE';
  30. export const COMPOSE_MOUNT = 'COMPOSE_MOUNT';
  31. export const COMPOSE_UNMOUNT = 'COMPOSE_UNMOUNT';
  32. export const COMPOSE_SENSITIVITY_CHANGE = 'COMPOSE_SENSITIVITY_CHANGE';
  33. export const COMPOSE_SPOILERNESS_CHANGE = 'COMPOSE_SPOILERNESS_CHANGE';
  34. export const COMPOSE_SPOILER_TEXT_CHANGE = 'COMPOSE_SPOILER_TEXT_CHANGE';
  35. export const COMPOSE_VISIBILITY_CHANGE = 'COMPOSE_VISIBILITY_CHANGE';
  36. export const COMPOSE_LISTABILITY_CHANGE = 'COMPOSE_LISTABILITY_CHANGE';
  37. export const COMPOSE_COMPOSING_CHANGE = 'COMPOSE_COMPOSING_CHANGE';
  38. export const COMPOSE_EMOJI_INSERT = 'COMPOSE_EMOJI_INSERT';
  39. export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST';
  40. export const COMPOSE_UPLOAD_CHANGE_SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS';
  41. export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL';
  42. export function changeCompose(text) {
  43. return {
  44. type: COMPOSE_CHANGE,
  45. text: text,
  46. };
  47. };
  48. export function replyCompose(status, router) {
  49. return (dispatch, getState) => {
  50. dispatch({
  51. type: COMPOSE_REPLY,
  52. status: status,
  53. });
  54. if (!getState().getIn(['compose', 'mounted'])) {
  55. router.push('/statuses/new');
  56. }
  57. };
  58. };
  59. export function cancelReplyCompose() {
  60. return {
  61. type: COMPOSE_REPLY_CANCEL,
  62. };
  63. };
  64. export function resetCompose() {
  65. return {
  66. type: COMPOSE_RESET,
  67. };
  68. };
  69. export function mentionCompose(account, router) {
  70. return (dispatch, getState) => {
  71. dispatch({
  72. type: COMPOSE_MENTION,
  73. account: account,
  74. });
  75. if (!getState().getIn(['compose', 'mounted'])) {
  76. router.push('/statuses/new');
  77. }
  78. };
  79. };
  80. export function directCompose(account, router) {
  81. return (dispatch, getState) => {
  82. dispatch({
  83. type: COMPOSE_DIRECT,
  84. account: account,
  85. });
  86. if (!getState().getIn(['compose', 'mounted'])) {
  87. router.push('/statuses/new');
  88. }
  89. };
  90. };
  91. export function submitCompose() {
  92. return function (dispatch, getState) {
  93. const status = getState().getIn(['compose', 'text'], '');
  94. const media = getState().getIn(['compose', 'media_attachments']);
  95. if ((!status || !status.length) && media.size === 0) {
  96. return;
  97. }
  98. dispatch(submitComposeRequest());
  99. api(getState).post('/api/v1/statuses', {
  100. status,
  101. in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
  102. media_ids: media.map(item => item.get('id')),
  103. sensitive: getState().getIn(['compose', 'sensitive']),
  104. spoiler_text: getState().getIn(['compose', 'spoiler_text'], ''),
  105. visibility: getState().getIn(['compose', 'privacy']),
  106. }, {
  107. headers: {
  108. 'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
  109. },
  110. }).then(function (response) {
  111. dispatch(insertIntoTagHistory(response.data.tags));
  112. dispatch(submitComposeSuccess({ ...response.data }));
  113. // To make the app more responsive, immediately get the status into the columns
  114. const insertIfOnline = (timelineId) => {
  115. if (getState().getIn(['timelines', timelineId, 'items', 0]) !== null) {
  116. dispatch(updateTimeline(timelineId, { ...response.data }));
  117. }
  118. };
  119. insertIfOnline('home');
  120. if (response.data.in_reply_to_id === null && response.data.visibility === 'public') {
  121. insertIfOnline('community');
  122. insertIfOnline('public');
  123. } else if (response.data.visibility === 'direct') {
  124. insertIfOnline('direct');
  125. }
  126. }).catch(function (error) {
  127. dispatch(submitComposeFail(error));
  128. });
  129. };
  130. };
  131. export function submitComposeRequest() {
  132. return {
  133. type: COMPOSE_SUBMIT_REQUEST,
  134. };
  135. };
  136. export function submitComposeSuccess(status) {
  137. return {
  138. type: COMPOSE_SUBMIT_SUCCESS,
  139. status: status,
  140. };
  141. };
  142. export function submitComposeFail(error) {
  143. return {
  144. type: COMPOSE_SUBMIT_FAIL,
  145. error: error,
  146. };
  147. };
  148. export function uploadCompose(files) {
  149. return function (dispatch, getState) {
  150. if (getState().getIn(['compose', 'media_attachments']).size > 3) {
  151. return;
  152. }
  153. dispatch(uploadComposeRequest());
  154. let data = new FormData();
  155. data.append('file', files[0]);
  156. api(getState).post('/api/v1/media', data, {
  157. onUploadProgress: function (e) {
  158. dispatch(uploadComposeProgress(e.loaded, e.total));
  159. },
  160. }).then(function (response) {
  161. dispatch(uploadComposeSuccess(response.data));
  162. }).catch(function (error) {
  163. dispatch(uploadComposeFail(error));
  164. });
  165. };
  166. };
  167. export function changeUploadCompose(id, params) {
  168. return (dispatch, getState) => {
  169. dispatch(changeUploadComposeRequest());
  170. api(getState).put(`/api/v1/media/${id}`, params).then(response => {
  171. dispatch(changeUploadComposeSuccess(response.data));
  172. }).catch(error => {
  173. dispatch(changeUploadComposeFail(id, error));
  174. });
  175. };
  176. };
  177. export function changeUploadComposeRequest() {
  178. return {
  179. type: COMPOSE_UPLOAD_CHANGE_REQUEST,
  180. skipLoading: true,
  181. };
  182. };
  183. export function changeUploadComposeSuccess(media) {
  184. return {
  185. type: COMPOSE_UPLOAD_CHANGE_SUCCESS,
  186. media: media,
  187. skipLoading: true,
  188. };
  189. };
  190. export function changeUploadComposeFail(error) {
  191. return {
  192. type: COMPOSE_UPLOAD_CHANGE_FAIL,
  193. error: error,
  194. skipLoading: true,
  195. };
  196. };
  197. export function uploadComposeRequest() {
  198. return {
  199. type: COMPOSE_UPLOAD_REQUEST,
  200. skipLoading: true,
  201. };
  202. };
  203. export function uploadComposeProgress(loaded, total) {
  204. return {
  205. type: COMPOSE_UPLOAD_PROGRESS,
  206. loaded: loaded,
  207. total: total,
  208. };
  209. };
  210. export function uploadComposeSuccess(media) {
  211. return {
  212. type: COMPOSE_UPLOAD_SUCCESS,
  213. media: media,
  214. skipLoading: true,
  215. };
  216. };
  217. export function uploadComposeFail(error) {
  218. return {
  219. type: COMPOSE_UPLOAD_FAIL,
  220. error: error,
  221. skipLoading: true,
  222. };
  223. };
  224. export function undoUploadCompose(media_id) {
  225. return {
  226. type: COMPOSE_UPLOAD_UNDO,
  227. media_id: media_id,
  228. };
  229. };
  230. export function clearComposeSuggestions() {
  231. if (cancelFetchComposeSuggestionsAccounts) {
  232. cancelFetchComposeSuggestionsAccounts();
  233. }
  234. return {
  235. type: COMPOSE_SUGGESTIONS_CLEAR,
  236. };
  237. };
  238. const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) => {
  239. if (cancelFetchComposeSuggestionsAccounts) {
  240. cancelFetchComposeSuggestionsAccounts();
  241. }
  242. api(getState).get('/api/v1/accounts/search', {
  243. cancelToken: new CancelToken(cancel => {
  244. cancelFetchComposeSuggestionsAccounts = cancel;
  245. }),
  246. params: {
  247. q: token.slice(1),
  248. resolve: false,
  249. limit: 4,
  250. },
  251. }).then(response => {
  252. dispatch(importFetchedAccounts(response.data));
  253. dispatch(readyComposeSuggestionsAccounts(token, response.data));
  254. }).catch(error => {
  255. if (!isCancel(error)) {
  256. dispatch(showAlertForError(error));
  257. }
  258. });
  259. }, 200, { leading: true, trailing: true });
  260. const fetchComposeSuggestionsEmojis = (dispatch, getState, token) => {
  261. const results = emojiSearch(token.replace(':', ''), { maxResults: 5 });
  262. dispatch(readyComposeSuggestionsEmojis(token, results));
  263. };
  264. const fetchComposeSuggestionsTags = (dispatch, getState, token) => {
  265. dispatch(updateSuggestionTags(token));
  266. };
  267. export function fetchComposeSuggestions(token) {
  268. return (dispatch, getState) => {
  269. switch (token[0]) {
  270. case ':':
  271. fetchComposeSuggestionsEmojis(dispatch, getState, token);
  272. break;
  273. case '#':
  274. fetchComposeSuggestionsTags(dispatch, getState, token);
  275. break;
  276. default:
  277. fetchComposeSuggestionsAccounts(dispatch, getState, token);
  278. break;
  279. }
  280. };
  281. };
  282. export function readyComposeSuggestionsEmojis(token, emojis) {
  283. return {
  284. type: COMPOSE_SUGGESTIONS_READY,
  285. token,
  286. emojis,
  287. };
  288. };
  289. export function readyComposeSuggestionsAccounts(token, accounts) {
  290. return {
  291. type: COMPOSE_SUGGESTIONS_READY,
  292. token,
  293. accounts,
  294. };
  295. };
  296. export function selectComposeSuggestion(position, token, suggestion) {
  297. return (dispatch, getState) => {
  298. let completion, startPosition;
  299. if (typeof suggestion === 'object' && suggestion.id) {
  300. completion = suggestion.native || suggestion.colons;
  301. startPosition = position - 1;
  302. dispatch(useEmoji(suggestion));
  303. } else if (suggestion[0] === '#') {
  304. completion = suggestion;
  305. startPosition = position - 1;
  306. } else {
  307. completion = getState().getIn(['accounts', suggestion, 'acct']);
  308. startPosition = position;
  309. }
  310. dispatch({
  311. type: COMPOSE_SUGGESTION_SELECT,
  312. position: startPosition,
  313. token,
  314. completion,
  315. });
  316. };
  317. };
  318. export function updateSuggestionTags(token) {
  319. return {
  320. type: COMPOSE_SUGGESTION_TAGS_UPDATE,
  321. token,
  322. };
  323. }
  324. export function updateTagHistory(tags) {
  325. return {
  326. type: COMPOSE_TAG_HISTORY_UPDATE,
  327. tags,
  328. };
  329. }
  330. export function hydrateCompose() {
  331. return (dispatch, getState) => {
  332. const me = getState().getIn(['meta', 'me']);
  333. const history = tagHistory.get(me);
  334. if (history !== null) {
  335. dispatch(updateTagHistory(history));
  336. }
  337. };
  338. }
  339. function insertIntoTagHistory(tags) {
  340. return (dispatch, getState) => {
  341. const state = getState();
  342. const oldHistory = state.getIn(['compose', 'tagHistory']);
  343. const me = state.getIn(['meta', 'me']);
  344. const names = tags.map(({ name }) => name);
  345. const intersectedOldHistory = oldHistory.filter(name => !names.includes(name));
  346. names.push(...intersectedOldHistory.toJS());
  347. const newHistory = names.slice(0, 1000);
  348. tagHistory.set(me, newHistory);
  349. dispatch(updateTagHistory(newHistory));
  350. };
  351. }
  352. export function mountCompose() {
  353. return {
  354. type: COMPOSE_MOUNT,
  355. };
  356. };
  357. export function unmountCompose() {
  358. return {
  359. type: COMPOSE_UNMOUNT,
  360. };
  361. };
  362. export function changeComposeSensitivity() {
  363. return {
  364. type: COMPOSE_SENSITIVITY_CHANGE,
  365. };
  366. };
  367. export function changeComposeSpoilerness() {
  368. return {
  369. type: COMPOSE_SPOILERNESS_CHANGE,
  370. };
  371. };
  372. export function changeComposeSpoilerText(text) {
  373. return {
  374. type: COMPOSE_SPOILER_TEXT_CHANGE,
  375. text,
  376. };
  377. };
  378. export function changeComposeVisibility(value) {
  379. return {
  380. type: COMPOSE_VISIBILITY_CHANGE,
  381. value,
  382. };
  383. };
  384. export function insertEmojiCompose(position, emoji, needsSpace) {
  385. return {
  386. type: COMPOSE_EMOJI_INSERT,
  387. position,
  388. emoji,
  389. needsSpace,
  390. };
  391. };
  392. export function changeComposing(value) {
  393. return {
  394. type: COMPOSE_COMPOSING_CHANGE,
  395. value,
  396. };
  397. }