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.

271 lines
6.7 KiB

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