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.

262 lines
6.5 KiB

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