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.

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