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.

250 lines
6.2 KiB

8 years ago
8 years ago
8 years ago
  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 const COMPOSE_MOUNT = 'COMPOSE_MOUNT';
  19. export const COMPOSE_UNMOUNT = 'COMPOSE_UNMOUNT';
  20. export const COMPOSE_SENSITIVITY_CHANGE = 'COMPOSE_SENSITIVITY_CHANGE';
  21. export const COMPOSE_SPOILERNESS_CHANGE = 'COMPOSE_SPOILERNESS_CHANGE';
  22. export const COMPOSE_SPOILER_TEXT_CHANGE = 'COMPOSE_SPOILER_TEXT_CHANGE';
  23. export const COMPOSE_VISIBILITY_CHANGE = 'COMPOSE_VISIBILITY_CHANGE';
  24. export const COMPOSE_LISTABILITY_CHANGE = 'COMPOSE_LISTABILITY_CHANGE';
  25. export function changeCompose(text) {
  26. return {
  27. type: COMPOSE_CHANGE,
  28. text: text
  29. };
  30. };
  31. export function replyCompose(status, router) {
  32. return (dispatch, getState) => {
  33. dispatch({
  34. type: COMPOSE_REPLY,
  35. status: status
  36. });
  37. if (!getState().getIn(['compose', 'mounted'])) {
  38. router.push('/statuses/new');
  39. }
  40. };
  41. };
  42. export function cancelReplyCompose() {
  43. return {
  44. type: COMPOSE_REPLY_CANCEL
  45. };
  46. };
  47. export function mentionCompose(account) {
  48. return {
  49. type: COMPOSE_MENTION,
  50. account: account
  51. };
  52. };
  53. export function submitCompose() {
  54. return function (dispatch, getState) {
  55. dispatch(submitComposeRequest());
  56. api(getState).post('/api/v1/statuses', {
  57. status: getState().getIn(['compose', 'text'], ''),
  58. in_reply_to_id: getState().getIn(['compose', 'in_reply_to'], null),
  59. media_ids: getState().getIn(['compose', 'media_attachments']).map(item => item.get('id')),
  60. sensitive: getState().getIn(['compose', 'sensitive']),
  61. spoiler_text: getState().getIn(['compose', 'spoiler_text'], ''),
  62. visibility: getState().getIn(['compose', 'private']) ? 'private' : (getState().getIn(['compose', 'unlisted']) ? 'unlisted' : 'public')
  63. }).then(function (response) {
  64. dispatch(submitComposeSuccess({ ...response.data }));
  65. // To make the app more responsive, immediately get the status into the columns
  66. dispatch(updateTimeline('home', { ...response.data }));
  67. if (response.data.in_reply_to_id === null && !getState().getIn(['compose', 'private']) && !getState().getIn(['compose', 'unlisted'])) {
  68. dispatch(updateTimeline('public', { ...response.data }));
  69. }
  70. }).catch(function (error) {
  71. dispatch(submitComposeFail(error));
  72. });
  73. };
  74. };
  75. export function submitComposeRequest() {
  76. return {
  77. type: COMPOSE_SUBMIT_REQUEST
  78. };
  79. };
  80. export function submitComposeSuccess(status) {
  81. return {
  82. type: COMPOSE_SUBMIT_SUCCESS,
  83. status: status
  84. };
  85. };
  86. export function submitComposeFail(error) {
  87. return {
  88. type: COMPOSE_SUBMIT_FAIL,
  89. error: error
  90. };
  91. };
  92. export function uploadCompose(files) {
  93. return function (dispatch, getState) {
  94. dispatch(uploadComposeRequest());
  95. let data = new FormData();
  96. data.append('file', files[0]);
  97. api(getState).post('/api/v1/media', data, {
  98. onUploadProgress: function (e) {
  99. dispatch(uploadComposeProgress(e.loaded, e.total));
  100. }
  101. }).then(function (response) {
  102. dispatch(uploadComposeSuccess(response.data));
  103. }).catch(function (error) {
  104. dispatch(uploadComposeFail(error));
  105. });
  106. };
  107. };
  108. export function uploadComposeRequest() {
  109. return {
  110. type: COMPOSE_UPLOAD_REQUEST
  111. };
  112. };
  113. export function uploadComposeProgress(loaded, total) {
  114. return {
  115. type: COMPOSE_UPLOAD_PROGRESS,
  116. loaded: loaded,
  117. total: total
  118. };
  119. };
  120. export function uploadComposeSuccess(media) {
  121. return {
  122. type: COMPOSE_UPLOAD_SUCCESS,
  123. media: media
  124. };
  125. };
  126. export function uploadComposeFail(error) {
  127. return {
  128. type: COMPOSE_UPLOAD_FAIL,
  129. error: error
  130. };
  131. };
  132. export function undoUploadCompose(media_id) {
  133. return {
  134. type: COMPOSE_UPLOAD_UNDO,
  135. media_id: media_id
  136. };
  137. };
  138. export function clearComposeSuggestions() {
  139. return {
  140. type: COMPOSE_SUGGESTIONS_CLEAR
  141. };
  142. };
  143. export function fetchComposeSuggestions(token) {
  144. return (dispatch, getState) => {
  145. api(getState).get('/api/v1/accounts/search', {
  146. params: {
  147. q: token,
  148. resolve: false,
  149. limit: 4
  150. }
  151. }).then(response => {
  152. dispatch(readyComposeSuggestions(token, response.data));
  153. });
  154. };
  155. };
  156. export function readyComposeSuggestions(token, accounts) {
  157. return {
  158. type: COMPOSE_SUGGESTIONS_READY,
  159. token,
  160. accounts
  161. };
  162. };
  163. export function selectComposeSuggestion(position, token, accountId) {
  164. return (dispatch, getState) => {
  165. const completion = getState().getIn(['accounts', accountId, 'acct']);
  166. dispatch({
  167. type: COMPOSE_SUGGESTION_SELECT,
  168. position,
  169. token,
  170. completion
  171. });
  172. };
  173. };
  174. export function mountCompose() {
  175. return {
  176. type: COMPOSE_MOUNT
  177. };
  178. };
  179. export function unmountCompose() {
  180. return {
  181. type: COMPOSE_UNMOUNT
  182. };
  183. };
  184. export function changeComposeSensitivity(checked) {
  185. return {
  186. type: COMPOSE_SENSITIVITY_CHANGE,
  187. checked
  188. };
  189. };
  190. export function changeComposeSpoilerness(checked) {
  191. return {
  192. type: COMPOSE_SPOILERNESS_CHANGE,
  193. checked
  194. };
  195. };
  196. export function changeComposeSpoilerText(text) {
  197. return {
  198. type: COMPOSE_SPOILER_TEXT_CHANGE,
  199. text
  200. };
  201. };
  202. export function changeComposeVisibility(checked) {
  203. return {
  204. type: COMPOSE_VISIBILITY_CHANGE,
  205. checked
  206. };
  207. };
  208. export function changeComposeListability(checked) {
  209. return {
  210. type: COMPOSE_LISTABILITY_CHANGE,
  211. checked
  212. };
  213. };