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.

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