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