]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/reducers/compose.js
Feature: Direct message from menu (#6956)
[mastodon.git] / app / javascript / mastodon / reducers / compose.js
1 import {
2 COMPOSE_MOUNT,
3 COMPOSE_UNMOUNT,
4 COMPOSE_CHANGE,
5 COMPOSE_REPLY,
6 COMPOSE_REPLY_CANCEL,
7 COMPOSE_DIRECT,
8 COMPOSE_MENTION,
9 COMPOSE_SUBMIT_REQUEST,
10 COMPOSE_SUBMIT_SUCCESS,
11 COMPOSE_SUBMIT_FAIL,
12 COMPOSE_UPLOAD_REQUEST,
13 COMPOSE_UPLOAD_SUCCESS,
14 COMPOSE_UPLOAD_FAIL,
15 COMPOSE_UPLOAD_UNDO,
16 COMPOSE_UPLOAD_PROGRESS,
17 COMPOSE_SUGGESTIONS_CLEAR,
18 COMPOSE_SUGGESTIONS_READY,
19 COMPOSE_SUGGESTION_SELECT,
20 COMPOSE_SUGGESTION_TAGS_UPDATE,
21 COMPOSE_TAG_HISTORY_UPDATE,
22 COMPOSE_SENSITIVITY_CHANGE,
23 COMPOSE_SPOILERNESS_CHANGE,
24 COMPOSE_SPOILER_TEXT_CHANGE,
25 COMPOSE_VISIBILITY_CHANGE,
26 COMPOSE_COMPOSING_CHANGE,
27 COMPOSE_EMOJI_INSERT,
28 COMPOSE_UPLOAD_CHANGE_REQUEST,
29 COMPOSE_UPLOAD_CHANGE_SUCCESS,
30 COMPOSE_UPLOAD_CHANGE_FAIL,
31 COMPOSE_RESET,
32 } from '../actions/compose';
33 import { TIMELINE_DELETE } from '../actions/timelines';
34 import { STORE_HYDRATE } from '../actions/store';
35 import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
36 import uuid from '../uuid';
37 import { me } from '../initial_state';
38
39 const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
40
41 const initialState = ImmutableMap({
42 mounted: 0,
43 sensitive: false,
44 spoiler: false,
45 spoiler_text: '',
46 privacy: null,
47 text: '',
48 focusDate: null,
49 preselectDate: null,
50 in_reply_to: null,
51 is_composing: false,
52 is_submitting: false,
53 is_uploading: false,
54 progress: 0,
55 media_attachments: ImmutableList(),
56 suggestion_token: null,
57 suggestions: ImmutableList(),
58 default_privacy: 'public',
59 default_sensitive: false,
60 resetFileKey: Math.floor((Math.random() * 0x10000)),
61 idempotencyKey: null,
62 tagHistory: ImmutableList(),
63 });
64
65 function statusToTextMentions(state, status) {
66 let set = ImmutableOrderedSet([]);
67
68 if (status.getIn(['account', 'id']) !== me) {
69 set = set.add(`@${status.getIn(['account', 'acct'])} `);
70 }
71
72 return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
73 };
74
75 function clearAll(state) {
76 return state.withMutations(map => {
77 map.set('text', '');
78 map.set('spoiler', false);
79 map.set('spoiler_text', '');
80 map.set('is_submitting', false);
81 map.set('in_reply_to', null);
82 map.set('privacy', state.get('default_privacy'));
83 map.set('sensitive', false);
84 map.update('media_attachments', list => list.clear());
85 map.set('idempotencyKey', uuid());
86 });
87 };
88
89 function appendMedia(state, media) {
90 const prevSize = state.get('media_attachments').size;
91
92 return state.withMutations(map => {
93 map.update('media_attachments', list => list.push(media));
94 map.set('is_uploading', false);
95 map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
96 map.set('focusDate', new Date());
97 map.set('idempotencyKey', uuid());
98
99 if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
100 map.set('sensitive', true);
101 }
102 });
103 };
104
105 function removeMedia(state, mediaId) {
106 const prevSize = state.get('media_attachments').size;
107
108 return state.withMutations(map => {
109 map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
110 map.set('idempotencyKey', uuid());
111
112 if (prevSize === 1) {
113 map.set('sensitive', false);
114 }
115 });
116 };
117
118 const insertSuggestion = (state, position, token, completion) => {
119 return state.withMutations(map => {
120 map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
121 map.set('suggestion_token', null);
122 map.update('suggestions', ImmutableList(), list => list.clear());
123 map.set('focusDate', new Date());
124 map.set('idempotencyKey', uuid());
125 });
126 };
127
128 const updateSuggestionTags = (state, token) => {
129 const prefix = token.slice(1);
130
131 return state.merge({
132 suggestions: state.get('tagHistory')
133 .filter(tag => tag.startsWith(prefix))
134 .slice(0, 4)
135 .map(tag => '#' + tag),
136 suggestion_token: token,
137 });
138 };
139
140 const insertEmoji = (state, position, emojiData) => {
141 const oldText = state.get('text');
142 const needsSpace = emojiData.custom && position > 0 && !allowedAroundShortCode.includes(oldText[position - 1]);
143 const emoji = needsSpace ? ' ' + emojiData.native : emojiData.native;
144
145 return state.merge({
146 text: `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`,
147 focusDate: new Date(),
148 idempotencyKey: uuid(),
149 });
150 };
151
152 const privacyPreference = (a, b) => {
153 if (a === 'direct' || b === 'direct') {
154 return 'direct';
155 } else if (a === 'private' || b === 'private') {
156 return 'private';
157 } else if (a === 'unlisted' || b === 'unlisted') {
158 return 'unlisted';
159 } else {
160 return 'public';
161 }
162 };
163
164 const hydrate = (state, hydratedState) => {
165 state = clearAll(state.merge(hydratedState));
166
167 if (hydratedState.has('text')) {
168 state = state.set('text', hydratedState.get('text'));
169 }
170
171 return state;
172 };
173
174 export default function compose(state = initialState, action) {
175 switch(action.type) {
176 case STORE_HYDRATE:
177 return hydrate(state, action.state.get('compose'));
178 case COMPOSE_MOUNT:
179 return state.set('mounted', state.get('mounted') + 1);
180 case COMPOSE_UNMOUNT:
181 return state
182 .set('mounted', Math.max(state.get('mounted') - 1, 0))
183 .set('is_composing', false);
184 case COMPOSE_SENSITIVITY_CHANGE:
185 return state.withMutations(map => {
186 if (!state.get('spoiler')) {
187 map.set('sensitive', !state.get('sensitive'));
188 }
189
190 map.set('idempotencyKey', uuid());
191 });
192 case COMPOSE_SPOILERNESS_CHANGE:
193 return state.withMutations(map => {
194 map.set('spoiler_text', '');
195 map.set('spoiler', !state.get('spoiler'));
196 map.set('idempotencyKey', uuid());
197
198 if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
199 map.set('sensitive', true);
200 }
201 });
202 case COMPOSE_SPOILER_TEXT_CHANGE:
203 return state
204 .set('spoiler_text', action.text)
205 .set('idempotencyKey', uuid());
206 case COMPOSE_VISIBILITY_CHANGE:
207 return state
208 .set('privacy', action.value)
209 .set('idempotencyKey', uuid());
210 case COMPOSE_CHANGE:
211 return state
212 .set('text', action.text)
213 .set('idempotencyKey', uuid());
214 case COMPOSE_COMPOSING_CHANGE:
215 return state.set('is_composing', action.value);
216 case COMPOSE_REPLY:
217 return state.withMutations(map => {
218 map.set('in_reply_to', action.status.get('id'));
219 map.set('text', statusToTextMentions(state, action.status));
220 map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
221 map.set('focusDate', new Date());
222 map.set('preselectDate', new Date());
223 map.set('idempotencyKey', uuid());
224
225 if (action.status.get('spoiler_text').length > 0) {
226 map.set('spoiler', true);
227 map.set('spoiler_text', action.status.get('spoiler_text'));
228 } else {
229 map.set('spoiler', false);
230 map.set('spoiler_text', '');
231 }
232 });
233 case COMPOSE_REPLY_CANCEL:
234 case COMPOSE_RESET:
235 return state.withMutations(map => {
236 map.set('in_reply_to', null);
237 map.set('text', '');
238 map.set('spoiler', false);
239 map.set('spoiler_text', '');
240 map.set('privacy', state.get('default_privacy'));
241 map.set('idempotencyKey', uuid());
242 });
243 case COMPOSE_SUBMIT_REQUEST:
244 case COMPOSE_UPLOAD_CHANGE_REQUEST:
245 return state.set('is_submitting', true);
246 case COMPOSE_SUBMIT_SUCCESS:
247 return clearAll(state);
248 case COMPOSE_SUBMIT_FAIL:
249 case COMPOSE_UPLOAD_CHANGE_FAIL:
250 return state.set('is_submitting', false);
251 case COMPOSE_UPLOAD_REQUEST:
252 return state.set('is_uploading', true);
253 case COMPOSE_UPLOAD_SUCCESS:
254 return appendMedia(state, fromJS(action.media));
255 case COMPOSE_UPLOAD_FAIL:
256 return state.set('is_uploading', false);
257 case COMPOSE_UPLOAD_UNDO:
258 return removeMedia(state, action.media_id);
259 case COMPOSE_UPLOAD_PROGRESS:
260 return state.set('progress', Math.round((action.loaded / action.total) * 100));
261 case COMPOSE_MENTION:
262 return state
263 .update('text', text => `${text}@${action.account.get('acct')} `)
264 .set('focusDate', new Date())
265 .set('idempotencyKey', uuid());
266 case COMPOSE_DIRECT:
267 return state
268 .update('text', text => `${text}@${action.account.get('acct')} `)
269 .set('privacy', 'direct')
270 .set('focusDate', new Date())
271 .set('idempotencyKey', uuid());
272 case COMPOSE_SUGGESTIONS_CLEAR:
273 return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
274 case COMPOSE_SUGGESTIONS_READY:
275 return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
276 case COMPOSE_SUGGESTION_SELECT:
277 return insertSuggestion(state, action.position, action.token, action.completion);
278 case COMPOSE_SUGGESTION_TAGS_UPDATE:
279 return updateSuggestionTags(state, action.token);
280 case COMPOSE_TAG_HISTORY_UPDATE:
281 return state.set('tagHistory', fromJS(action.tags));
282 case TIMELINE_DELETE:
283 if (action.id === state.get('in_reply_to')) {
284 return state.set('in_reply_to', null);
285 } else {
286 return state;
287 }
288 case COMPOSE_EMOJI_INSERT:
289 return insertEmoji(state, action.position, action.emoji);
290 case COMPOSE_UPLOAD_CHANGE_SUCCESS:
291 return state
292 .set('is_submitting', false)
293 .update('media_attachments', list => list.map(item => {
294 if (item.get('id') === action.media.id) {
295 return fromJS(action.media);
296 }
297
298 return item;
299 }));
300 default:
301 return state;
302 }
303 };
This page took 0.112517 seconds and 4 git commands to generate.