闭社主体 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.

270 lines
6.6 KiB

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