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.
 
 
 
 

219 lines
5.1 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. dispatch(updateTimeline('home', response.data));
  62. }).catch(function (error) {
  63. dispatch(submitComposeFail(error));
  64. });
  65. };
  66. };
  67. export function submitComposeRequest() {
  68. return {
  69. type: COMPOSE_SUBMIT_REQUEST
  70. };
  71. };
  72. export function submitComposeSuccess(status) {
  73. return {
  74. type: COMPOSE_SUBMIT_SUCCESS,
  75. status: status
  76. };
  77. };
  78. export function submitComposeFail(error) {
  79. return {
  80. type: COMPOSE_SUBMIT_FAIL,
  81. error: error
  82. };
  83. };
  84. export function uploadCompose(files) {
  85. return function (dispatch, getState) {
  86. dispatch(uploadComposeRequest());
  87. let data = new FormData();
  88. data.append('file', files[0]);
  89. api(getState).post('/api/v1/media', data, {
  90. onUploadProgress: function (e) {
  91. dispatch(uploadComposeProgress(e.loaded, e.total));
  92. }
  93. }).then(function (response) {
  94. dispatch(uploadComposeSuccess(response.data));
  95. }).catch(function (error) {
  96. dispatch(uploadComposeFail(error));
  97. });
  98. };
  99. };
  100. export function uploadComposeRequest() {
  101. return {
  102. type: COMPOSE_UPLOAD_REQUEST
  103. };
  104. };
  105. export function uploadComposeProgress(loaded, total) {
  106. return {
  107. type: COMPOSE_UPLOAD_PROGRESS,
  108. loaded: loaded,
  109. total: total
  110. };
  111. };
  112. export function uploadComposeSuccess(media) {
  113. return {
  114. type: COMPOSE_UPLOAD_SUCCESS,
  115. media: media
  116. };
  117. };
  118. export function uploadComposeFail(error) {
  119. return {
  120. type: COMPOSE_UPLOAD_FAIL,
  121. error: error
  122. };
  123. };
  124. export function undoUploadCompose(media_id) {
  125. return {
  126. type: COMPOSE_UPLOAD_UNDO,
  127. media_id: media_id
  128. };
  129. };
  130. export function clearComposeSuggestions() {
  131. return {
  132. type: COMPOSE_SUGGESTIONS_CLEAR
  133. };
  134. };
  135. export function fetchComposeSuggestions(token) {
  136. return (dispatch, getState) => {
  137. api(getState).get('/api/v1/accounts/search', {
  138. params: {
  139. q: token,
  140. resolve: false,
  141. limit: 4
  142. }
  143. }).then(response => {
  144. dispatch(readyComposeSuggestions(token, response.data));
  145. });
  146. };
  147. };
  148. export function readyComposeSuggestions(token, accounts) {
  149. return {
  150. type: COMPOSE_SUGGESTIONS_READY,
  151. token,
  152. accounts
  153. };
  154. };
  155. export function selectComposeSuggestion(position, accountId) {
  156. return (dispatch, getState) => {
  157. const completion = getState().getIn(['accounts', accountId, 'acct']);
  158. dispatch({
  159. type: COMPOSE_SUGGESTION_SELECT,
  160. position,
  161. completion
  162. });
  163. };
  164. };
  165. export function mountCompose() {
  166. return {
  167. type: COMPOSE_MOUNT
  168. };
  169. };
  170. export function unmountCompose() {
  171. return {
  172. type: COMPOSE_UNMOUNT
  173. };
  174. };
  175. export function changeComposeSensitivity(checked) {
  176. return {
  177. type: COMPOSE_SENSITIVITY_CHANGE,
  178. checked
  179. };
  180. };
  181. export function changeComposeVisibility(checked) {
  182. return {
  183. type: COMPOSE_VISIBILITY_CHANGE,
  184. checked
  185. };
  186. };