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.

177 lines
4.2 KiB

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