闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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