1 import api
from '../api';
3 import { updateTimeline
} from './timelines';
5 import * as emojione
from 'emojione';
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';
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';
24 export const COMPOSE_MOUNT
= 'COMPOSE_MOUNT';
25 export const COMPOSE_UNMOUNT
= 'COMPOSE_UNMOUNT';
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';
33 export const COMPOSE_EMOJI_INSERT
= 'COMPOSE_EMOJI_INSERT';
35 export function changeCompose(text
) {
42 export function replyCompose(status
, router
) {
43 return (dispatch
, getState
) => {
49 if (!getState().getIn(['compose', 'mounted'])) {
50 router
.push('/statuses/new');
55 export function cancelReplyCompose() {
57 type: COMPOSE_REPLY_CANCEL
,
61 export function mentionCompose(account
, router
) {
62 return (dispatch
, getState
) => {
64 type: COMPOSE_MENTION
,
68 if (!getState().getIn(['compose', 'mounted'])) {
69 router
.push('/statuses/new');
74 export function submitCompose() {
75 return function (dispatch
, getState
) {
76 const status
= emojione
.shortnameToUnicode(getState().getIn(['compose', 'text'], ''));
77 if (!status
|| !status
.length
) {
80 dispatch(submitComposeRequest());
81 api(getState
).post('/api/v1/statuses', {
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']),
90 'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
92 }).then(function (response
) {
93 dispatch(submitComposeSuccess({ ...response
.data
}));
95 // To make the app more responsive, immediately get the status into the columns
96 dispatch(updateTimeline('home', { ...response
.data
}));
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
}));
103 if (getState().getIn(['timelines', 'public', 'loaded'])) {
104 dispatch(updateTimeline('public', { ...response
.data
}));
107 }).catch(function (error
) {
108 dispatch(submitComposeFail(error
));
113 export function submitComposeRequest() {
115 type: COMPOSE_SUBMIT_REQUEST
,
119 export function submitComposeSuccess(status
) {
121 type: COMPOSE_SUBMIT_SUCCESS
,
126 export function submitComposeFail(error
) {
128 type: COMPOSE_SUBMIT_FAIL
,
133 export function uploadCompose(files
) {
134 return function (dispatch
, getState
) {
135 if (getState().getIn(['compose', 'media_attachments']).size
> 3) {
139 dispatch(uploadComposeRequest());
141 let data
= new FormData();
142 data
.append('file', files
[0]);
144 api(getState
).post('/api/v1/media', data
, {
145 onUploadProgress: function (e
) {
146 dispatch(uploadComposeProgress(e
.loaded
, e
.total
));
148 }).then(function (response
) {
149 dispatch(uploadComposeSuccess(response
.data
));
150 }).catch(function (error
) {
151 dispatch(uploadComposeFail(error
));
156 export function uploadComposeRequest() {
158 type: COMPOSE_UPLOAD_REQUEST
,
163 export function uploadComposeProgress(loaded
, total
) {
165 type: COMPOSE_UPLOAD_PROGRESS
,
171 export function uploadComposeSuccess(media
) {
173 type: COMPOSE_UPLOAD_SUCCESS
,
179 export function uploadComposeFail(error
) {
181 type: COMPOSE_UPLOAD_FAIL
,
187 export function undoUploadCompose(media_id
) {
189 type: COMPOSE_UPLOAD_UNDO
,
194 export function clearComposeSuggestions() {
196 type: COMPOSE_SUGGESTIONS_CLEAR
,
200 export function fetchComposeSuggestions(token
) {
201 return (dispatch
, getState
) => {
202 api(getState
).get('/api/v1/accounts/search', {
208 }).then(response
=> {
209 dispatch(readyComposeSuggestions(token
, response
.data
));
214 export function readyComposeSuggestions(token
, accounts
) {
216 type: COMPOSE_SUGGESTIONS_READY
,
222 export function selectComposeSuggestion(position
, token
, accountId
) {
223 return (dispatch
, getState
) => {
224 const completion
= getState().getIn(['accounts', accountId
, 'acct']);
227 type: COMPOSE_SUGGESTION_SELECT
,
235 export function mountCompose() {
241 export function unmountCompose() {
243 type: COMPOSE_UNMOUNT
,
247 export function changeComposeSensitivity() {
249 type: COMPOSE_SENSITIVITY_CHANGE
,
253 export function changeComposeSpoilerness() {
255 type: COMPOSE_SPOILERNESS_CHANGE
,
259 export function changeComposeSpoilerText(text
) {
261 type: COMPOSE_SPOILER_TEXT_CHANGE
,
266 export function changeComposeVisibility(value
) {
268 type: COMPOSE_VISIBILITY_CHANGE
,
273 export function insertEmojiCompose(position
, emoji
) {
275 type: COMPOSE_EMOJI_INSERT
,