The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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