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.

158 lines
3.9 KiB

  1. import api from '../api'
  2. export const COMPOSE_CHANGE = 'COMPOSE_CHANGE';
  3. export const COMPOSE_SUBMIT_REQUEST = 'COMPOSE_SUBMIT_REQUEST';
  4. export const COMPOSE_SUBMIT_SUCCESS = 'COMPOSE_SUBMIT_SUCCESS';
  5. export const COMPOSE_SUBMIT_FAIL = 'COMPOSE_SUBMIT_FAIL';
  6. export const COMPOSE_REPLY = 'COMPOSE_REPLY';
  7. export const COMPOSE_REPLY_CANCEL = 'COMPOSE_REPLY_CANCEL';
  8. export const COMPOSE_MENTION = 'COMPOSE_MENTION';
  9. export const COMPOSE_UPLOAD_REQUEST = 'COMPOSE_UPLOAD_REQUEST';
  10. export const COMPOSE_UPLOAD_SUCCESS = 'COMPOSE_UPLOAD_SUCCESS';
  11. export const COMPOSE_UPLOAD_FAIL = 'COMPOSE_UPLOAD_FAIL';
  12. export const COMPOSE_UPLOAD_PROGRESS = 'COMPOSE_UPLOAD_PROGRESS';
  13. export const COMPOSE_UPLOAD_UNDO = 'COMPOSE_UPLOAD_UNDO';
  14. export const COMPOSE_SUGGESTIONS_CLEAR = 'COMPOSE_SUGGESTIONS_CLEAR';
  15. export const COMPOSE_SUGGESTIONS_READY = 'COMPOSE_SUGGESTIONS_READY';
  16. export function changeCompose(text) {
  17. return {
  18. type: COMPOSE_CHANGE,
  19. text: text
  20. };
  21. };
  22. export function replyCompose(status) {
  23. return {
  24. type: COMPOSE_REPLY,
  25. status: status
  26. };
  27. };
  28. export function cancelReplyCompose() {
  29. return {
  30. type: COMPOSE_REPLY_CANCEL
  31. };
  32. };
  33. export function mentionCompose(account) {
  34. return {
  35. type: COMPOSE_MENTION,
  36. account: account
  37. };
  38. };
  39. export function submitCompose() {
  40. return function (dispatch, getState) {
  41. dispatch(submitComposeRequest());
  42. api(getState).post('/api/v1/statuses', {
  43. status: getState().getIn(['compose', 'text'], ''),
  44. in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
  45. media_ids: getState().getIn(['compose', 'media_attachments']).map(item => item.get('id'))
  46. }).then(function (response) {
  47. dispatch(submitComposeSuccess(response.data));
  48. }).catch(function (error) {
  49. dispatch(submitComposeFail(error));
  50. });
  51. };
  52. };
  53. export function submitComposeRequest() {
  54. return {
  55. type: COMPOSE_SUBMIT_REQUEST
  56. };
  57. };
  58. export function submitComposeSuccess(status) {
  59. return {
  60. type: COMPOSE_SUBMIT_SUCCESS,
  61. status: status
  62. };
  63. };
  64. export function submitComposeFail(error) {
  65. return {
  66. type: COMPOSE_SUBMIT_FAIL,
  67. error: error
  68. };
  69. };
  70. export function uploadCompose(files) {
  71. return function (dispatch, getState) {
  72. dispatch(uploadComposeRequest());
  73. let data = new FormData();
  74. data.append('file', files[0]);
  75. api(getState).post('/api/v1/media', data, {
  76. onUploadProgress: function (e) {
  77. dispatch(uploadComposeProgress(e.loaded, e.total));
  78. }
  79. }).then(function (response) {
  80. dispatch(uploadComposeSuccess(response.data));
  81. }).catch(function (error) {
  82. dispatch(uploadComposeFail(error));
  83. });
  84. };
  85. };
  86. export function uploadComposeRequest() {
  87. return {
  88. type: COMPOSE_UPLOAD_REQUEST
  89. };
  90. };
  91. export function uploadComposeProgress(loaded, total) {
  92. return {
  93. type: COMPOSE_UPLOAD_PROGRESS,
  94. loaded: loaded,
  95. total: total
  96. };
  97. };
  98. export function uploadComposeSuccess(media) {
  99. return {
  100. type: COMPOSE_UPLOAD_SUCCESS,
  101. media: media
  102. };
  103. };
  104. export function uploadComposeFail(error) {
  105. return {
  106. type: COMPOSE_UPLOAD_FAIL,
  107. error: error
  108. };
  109. };
  110. export function undoUploadCompose(media_id) {
  111. return {
  112. type: COMPOSE_UPLOAD_UNDO,
  113. media_id: media_id
  114. };
  115. };
  116. export function clearComposeSuggestions() {
  117. return {
  118. type: COMPOSE_SUGGESTIONS_CLEAR
  119. };
  120. };
  121. export function fetchComposeSuggestions(token) {
  122. return (dispatch, getState) => {
  123. const loadedCandidates = getState().get('accounts').filter(item => item.get('acct').toLowerCase().slice(0, token.length) === token).map(item => ({
  124. label: item.get('acct'),
  125. completion: item.get('acct').slice(token.length)
  126. })).toList().toJS();
  127. dispatch(readyComposeSuggestions(loadedCandidates));
  128. };
  129. };
  130. export function readyComposeSuggestions(accounts) {
  131. return {
  132. type: COMPOSE_SUGGESTIONS_READY,
  133. accounts
  134. };
  135. };