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.
 
 
 
 

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