The code powering m.abunchtell.com https://m.abunchtell.com
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

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