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.

336 lines
11 KiB

  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 { REDRAFT } from '../actions/statuses';
  36. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  37. import uuid from '../uuid';
  38. import { me } from '../initial_state';
  39. import { unescapeHTML } from '../utils/html';
  40. const initialState = ImmutableMap({
  41. mounted: 0,
  42. sensitive: false,
  43. spoiler: false,
  44. spoiler_text: '',
  45. privacy: null,
  46. text: '',
  47. focusDate: null,
  48. caretPosition: null,
  49. preselectDate: null,
  50. in_reply_to: null,
  51. is_composing: false,
  52. is_submitting: false,
  53. is_changing_upload: false,
  54. is_uploading: false,
  55. progress: 0,
  56. media_attachments: ImmutableList(),
  57. suggestion_token: null,
  58. suggestions: ImmutableList(),
  59. default_privacy: 'public',
  60. default_sensitive: false,
  61. resetFileKey: Math.floor((Math.random() * 0x10000)),
  62. idempotencyKey: null,
  63. tagHistory: ImmutableList(),
  64. });
  65. function statusToTextMentions(state, status) {
  66. let set = ImmutableOrderedSet([]);
  67. if (status.getIn(['account', 'id']) !== me) {
  68. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  69. }
  70. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  71. };
  72. function clearAll(state) {
  73. return state.withMutations(map => {
  74. map.set('text', '');
  75. map.set('spoiler', false);
  76. map.set('spoiler_text', '');
  77. map.set('is_submitting', false);
  78. map.set('is_changing_upload', false);
  79. map.set('in_reply_to', null);
  80. map.set('privacy', state.get('default_privacy'));
  81. map.set('sensitive', false);
  82. map.update('media_attachments', list => list.clear());
  83. map.set('idempotencyKey', uuid());
  84. });
  85. };
  86. function appendMedia(state, media) {
  87. const prevSize = state.get('media_attachments').size;
  88. return state.withMutations(map => {
  89. map.update('media_attachments', list => list.push(media));
  90. map.set('is_uploading', false);
  91. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  92. map.set('idempotencyKey', uuid());
  93. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  94. map.set('sensitive', true);
  95. }
  96. });
  97. };
  98. function removeMedia(state, mediaId) {
  99. const prevSize = state.get('media_attachments').size;
  100. return state.withMutations(map => {
  101. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  102. map.set('idempotencyKey', uuid());
  103. if (prevSize === 1) {
  104. map.set('sensitive', false);
  105. }
  106. });
  107. };
  108. const insertSuggestion = (state, position, token, completion) => {
  109. return state.withMutations(map => {
  110. map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  111. map.set('suggestion_token', null);
  112. map.update('suggestions', ImmutableList(), list => list.clear());
  113. map.set('focusDate', new Date());
  114. map.set('caretPosition', position + completion.length + 1);
  115. map.set('idempotencyKey', uuid());
  116. });
  117. };
  118. const updateSuggestionTags = (state, token) => {
  119. const prefix = token.slice(1);
  120. return state.merge({
  121. suggestions: state.get('tagHistory')
  122. .filter(tag => tag.toLowerCase().startsWith(prefix.toLowerCase()))
  123. .slice(0, 4)
  124. .map(tag => '#' + tag),
  125. suggestion_token: token,
  126. });
  127. };
  128. const insertEmoji = (state, position, emojiData, needsSpace) => {
  129. const oldText = state.get('text');
  130. const emoji = needsSpace ? ' ' + emojiData.native : emojiData.native;
  131. return state.merge({
  132. text: `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`,
  133. focusDate: new Date(),
  134. caretPosition: position + emoji.length + 1,
  135. idempotencyKey: uuid(),
  136. });
  137. };
  138. const privacyPreference = (a, b) => {
  139. const order = ['public', 'unlisted', 'private', 'direct'];
  140. return order[Math.max(order.indexOf(a), order.indexOf(b), 0)];
  141. };
  142. const hydrate = (state, hydratedState) => {
  143. state = clearAll(state.merge(hydratedState));
  144. if (hydratedState.has('text')) {
  145. state = state.set('text', hydratedState.get('text'));
  146. }
  147. return state;
  148. };
  149. const domParser = new DOMParser();
  150. const expandMentions = status => {
  151. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  152. status.get('mentions').forEach(mention => {
  153. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  154. });
  155. return fragment.innerHTML;
  156. };
  157. export default function compose(state = initialState, action) {
  158. switch(action.type) {
  159. case STORE_HYDRATE:
  160. return hydrate(state, action.state.get('compose'));
  161. case COMPOSE_MOUNT:
  162. return state.set('mounted', state.get('mounted') + 1);
  163. case COMPOSE_UNMOUNT:
  164. return state
  165. .set('mounted', Math.max(state.get('mounted') - 1, 0))
  166. .set('is_composing', false);
  167. case COMPOSE_SENSITIVITY_CHANGE:
  168. return state.withMutations(map => {
  169. if (!state.get('spoiler')) {
  170. map.set('sensitive', !state.get('sensitive'));
  171. }
  172. map.set('idempotencyKey', uuid());
  173. });
  174. case COMPOSE_SPOILERNESS_CHANGE:
  175. return state.withMutations(map => {
  176. map.set('spoiler_text', '');
  177. map.set('spoiler', !state.get('spoiler'));
  178. map.set('idempotencyKey', uuid());
  179. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  180. map.set('sensitive', true);
  181. }
  182. });
  183. case COMPOSE_SPOILER_TEXT_CHANGE:
  184. return state
  185. .set('spoiler_text', action.text)
  186. .set('idempotencyKey', uuid());
  187. case COMPOSE_VISIBILITY_CHANGE:
  188. return state
  189. .set('privacy', action.value)
  190. .set('idempotencyKey', uuid());
  191. case COMPOSE_CHANGE:
  192. return state
  193. .set('text', action.text)
  194. .set('idempotencyKey', uuid());
  195. case COMPOSE_COMPOSING_CHANGE:
  196. return state.set('is_composing', action.value);
  197. case COMPOSE_REPLY:
  198. return state.withMutations(map => {
  199. map.set('in_reply_to', action.status.get('id'));
  200. map.set('text', statusToTextMentions(state, action.status));
  201. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  202. map.set('focusDate', new Date());
  203. map.set('caretPosition', null);
  204. map.set('preselectDate', new Date());
  205. map.set('idempotencyKey', uuid());
  206. if (action.status.get('spoiler_text').length > 0) {
  207. map.set('spoiler', true);
  208. map.set('spoiler_text', action.status.get('spoiler_text'));
  209. } else {
  210. map.set('spoiler', false);
  211. map.set('spoiler_text', '');
  212. }
  213. });
  214. case COMPOSE_REPLY_CANCEL:
  215. case COMPOSE_RESET:
  216. return state.withMutations(map => {
  217. map.set('in_reply_to', null);
  218. map.set('text', '');
  219. map.set('spoiler', false);
  220. map.set('spoiler_text', '');
  221. map.set('privacy', state.get('default_privacy'));
  222. map.set('idempotencyKey', uuid());
  223. });
  224. case COMPOSE_SUBMIT_REQUEST:
  225. return state.set('is_submitting', true);
  226. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  227. return state.set('is_changing_upload', true);
  228. case COMPOSE_SUBMIT_SUCCESS:
  229. return clearAll(state);
  230. case COMPOSE_SUBMIT_FAIL:
  231. return state.set('is_submitting', false);
  232. case COMPOSE_UPLOAD_CHANGE_FAIL:
  233. return state.set('is_changing_upload', false);
  234. case COMPOSE_UPLOAD_REQUEST:
  235. return state.set('is_uploading', true);
  236. case COMPOSE_UPLOAD_SUCCESS:
  237. return appendMedia(state, fromJS(action.media));
  238. case COMPOSE_UPLOAD_FAIL:
  239. return state.set('is_uploading', false);
  240. case COMPOSE_UPLOAD_UNDO:
  241. return removeMedia(state, action.media_id);
  242. case COMPOSE_UPLOAD_PROGRESS:
  243. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  244. case COMPOSE_MENTION:
  245. return state.withMutations(map => {
  246. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  247. map.set('focusDate', new Date());
  248. map.set('caretPosition', null);
  249. map.set('idempotencyKey', uuid());
  250. });
  251. case COMPOSE_DIRECT:
  252. return state.withMutations(map => {
  253. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  254. map.set('privacy', 'direct');
  255. map.set('focusDate', new Date());
  256. map.set('caretPosition', null);
  257. map.set('idempotencyKey', uuid());
  258. });
  259. case COMPOSE_SUGGESTIONS_CLEAR:
  260. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  261. case COMPOSE_SUGGESTIONS_READY:
  262. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  263. case COMPOSE_SUGGESTION_SELECT:
  264. return insertSuggestion(state, action.position, action.token, action.completion);
  265. case COMPOSE_SUGGESTION_TAGS_UPDATE:
  266. return updateSuggestionTags(state, action.token);
  267. case COMPOSE_TAG_HISTORY_UPDATE:
  268. return state.set('tagHistory', fromJS(action.tags));
  269. case TIMELINE_DELETE:
  270. if (action.id === state.get('in_reply_to')) {
  271. return state.set('in_reply_to', null);
  272. } else {
  273. return state;
  274. }
  275. case COMPOSE_EMOJI_INSERT:
  276. return insertEmoji(state, action.position, action.emoji, action.needsSpace);
  277. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  278. return state
  279. .set('is_changing_upload', false)
  280. .update('media_attachments', list => list.map(item => {
  281. if (item.get('id') === action.media.id) {
  282. return fromJS(action.media);
  283. }
  284. return item;
  285. }));
  286. case REDRAFT:
  287. return state.withMutations(map => {
  288. map.set('text', unescapeHTML(expandMentions(action.status)));
  289. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  290. map.set('privacy', action.status.get('visibility'));
  291. map.set('media_attachments', action.status.get('media_attachments'));
  292. map.set('focusDate', new Date());
  293. map.set('caretPosition', null);
  294. map.set('idempotencyKey', uuid());
  295. if (action.status.get('spoiler_text').length > 0) {
  296. map.set('spoiler', true);
  297. map.set('spoiler_text', action.status.get('spoiler_text'));
  298. } else {
  299. map.set('spoiler', false);
  300. map.set('spoiler_text', '');
  301. }
  302. });
  303. default:
  304. return state;
  305. }
  306. };