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.

402 lines
13 KiB

6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import {
  2. COMPOSE_MOUNT,
  3. COMPOSE_UNMOUNT,
  4. COMPOSE_CHANGE,
  5. COMPOSE_CYCLE_ELEFRIEND,
  6. COMPOSE_REPLY,
  7. COMPOSE_REPLY_CANCEL,
  8. COMPOSE_DIRECT,
  9. COMPOSE_MENTION,
  10. COMPOSE_SUBMIT_REQUEST,
  11. COMPOSE_SUBMIT_SUCCESS,
  12. COMPOSE_SUBMIT_FAIL,
  13. COMPOSE_UPLOAD_REQUEST,
  14. COMPOSE_UPLOAD_SUCCESS,
  15. COMPOSE_UPLOAD_FAIL,
  16. COMPOSE_UPLOAD_UNDO,
  17. COMPOSE_UPLOAD_PROGRESS,
  18. COMPOSE_SUGGESTIONS_CLEAR,
  19. COMPOSE_SUGGESTIONS_READY,
  20. COMPOSE_SUGGESTION_SELECT,
  21. COMPOSE_ADVANCED_OPTIONS_CHANGE,
  22. COMPOSE_SENSITIVITY_CHANGE,
  23. COMPOSE_SPOILERNESS_CHANGE,
  24. COMPOSE_SPOILER_TEXT_CHANGE,
  25. COMPOSE_VISIBILITY_CHANGE,
  26. COMPOSE_EMOJI_INSERT,
  27. COMPOSE_UPLOAD_CHANGE_REQUEST,
  28. COMPOSE_UPLOAD_CHANGE_SUCCESS,
  29. COMPOSE_UPLOAD_CHANGE_FAIL,
  30. COMPOSE_DOODLE_SET,
  31. COMPOSE_RESET,
  32. } from 'flavours/glitch/actions/compose';
  33. import { TIMELINE_DELETE } from 'flavours/glitch/actions/timelines';
  34. import { STORE_HYDRATE } from 'flavours/glitch/actions/store';
  35. import { REDRAFT } from 'flavours/glitch/actions/statuses';
  36. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  37. import uuid from 'flavours/glitch/util/uuid';
  38. import { privacyPreference } from 'flavours/glitch/util/privacy_preference';
  39. import { me } from 'flavours/glitch/util/initial_state';
  40. import { overwrite } from 'flavours/glitch/util/js_helpers';
  41. import { unescapeHTML } from 'flavours/glitch/util/html';
  42. const totalElefriends = 3;
  43. // ~4% chance you'll end up with an unexpected friend
  44. // glitch-soc/mastodon repo created_at date: 2017-04-20T21:55:28Z
  45. const glitchProbability = 1 - 0.0420215528;
  46. const initialState = ImmutableMap({
  47. mounted: false,
  48. advanced_options: ImmutableMap({
  49. do_not_federate: false,
  50. threaded_mode: false,
  51. }),
  52. sensitive: false,
  53. elefriend: Math.random() < glitchProbability ? Math.floor(Math.random() * totalElefriends) : totalElefriends,
  54. spoiler: false,
  55. spoiler_text: '',
  56. privacy: null,
  57. text: '',
  58. focusDate: null,
  59. caretPosition: null,
  60. preselectDate: null,
  61. in_reply_to: null,
  62. is_submitting: false,
  63. is_uploading: false,
  64. progress: 0,
  65. media_attachments: ImmutableList(),
  66. suggestion_token: null,
  67. suggestions: ImmutableList(),
  68. default_advanced_options: ImmutableMap({
  69. do_not_federate: false,
  70. threaded_mode: null, // Do not reset
  71. }),
  72. default_privacy: 'public',
  73. default_sensitive: false,
  74. resetFileKey: Math.floor((Math.random() * 0x10000)),
  75. idempotencyKey: null,
  76. doodle: ImmutableMap({
  77. fg: 'rgb( 0, 0, 0)',
  78. bg: 'rgb(255, 255, 255)',
  79. swapped: false,
  80. mode: 'draw',
  81. size: 'normal',
  82. weight: 2,
  83. opacity: 1,
  84. adaptiveStroke: true,
  85. smoothing: false,
  86. }),
  87. });
  88. function statusToTextMentions(state, status) {
  89. let set = ImmutableOrderedSet([]);
  90. if (status.getIn(['account', 'id']) !== me) {
  91. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  92. }
  93. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  94. };
  95. function apiStatusToTextMentions (state, status) {
  96. let set = ImmutableOrderedSet([]);
  97. if (status.account.id !== me) {
  98. set = set.add(`@${status.account.acct} `);
  99. }
  100. return set.union(status.mentions.filter(
  101. mention => mention.id !== me
  102. ).map(
  103. mention => `@${mention.acct} `
  104. )).join('');
  105. }
  106. function apiStatusToTextHashtags (state, status) {
  107. return ImmutableOrderedSet([]).union(status.tags.map(
  108. ({ name }) => `#${name} `
  109. )).join('');
  110. }
  111. function clearAll(state) {
  112. return state.withMutations(map => {
  113. map.set('text', '');
  114. map.set('spoiler', false);
  115. map.set('spoiler_text', '');
  116. map.set('is_submitting', false);
  117. map.set('in_reply_to', null);
  118. map.update(
  119. 'advanced_options',
  120. map => map.mergeWith(overwrite, state.get('default_advanced_options'))
  121. );
  122. map.set('privacy', state.get('default_privacy'));
  123. map.set('sensitive', false);
  124. map.update('media_attachments', list => list.clear());
  125. map.set('idempotencyKey', uuid());
  126. });
  127. };
  128. function continueThread (state, status) {
  129. return state.withMutations(function (map) {
  130. let text = apiStatusToTextMentions(state, status);
  131. text = text + apiStatusToTextHashtags(state, status);
  132. map.set('text', text);
  133. if (status.spoiler_text) {
  134. map.set('spoiler', true);
  135. map.set('spoiler_text', status.spoiler_text);
  136. } else {
  137. map.set('spoiler', false);
  138. map.set('spoiler_text', '');
  139. }
  140. map.set('is_submitting', false);
  141. map.set('in_reply_to', status.id);
  142. map.update(
  143. 'advanced_options',
  144. map => map.merge(new ImmutableMap({ do_not_federate: /👁\ufe0f?\u200b?(?:<\/p>)?$/.test(status.content) }))
  145. );
  146. map.set('privacy', status.visibility);
  147. map.set('sensitive', false);
  148. map.update('media_attachments', list => list.clear());
  149. map.set('idempotencyKey', uuid());
  150. map.set('focusDate', new Date());
  151. map.set('caretPosition', null);
  152. map.set('preselectDate', new Date());
  153. });
  154. }
  155. function appendMedia(state, media) {
  156. const prevSize = state.get('media_attachments').size;
  157. return state.withMutations(map => {
  158. map.update('media_attachments', list => list.push(media));
  159. map.set('is_uploading', false);
  160. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  161. map.set('idempotencyKey', uuid());
  162. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  163. map.set('sensitive', true);
  164. }
  165. });
  166. };
  167. function removeMedia(state, mediaId) {
  168. const prevSize = state.get('media_attachments').size;
  169. return state.withMutations(map => {
  170. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  171. map.set('idempotencyKey', uuid());
  172. if (prevSize === 1) {
  173. map.set('sensitive', false);
  174. }
  175. });
  176. };
  177. const insertSuggestion = (state, position, token, completion) => {
  178. return state.withMutations(map => {
  179. map.update('text', oldText => `${oldText.slice(0, position)}${completion}${completion[0] === ':' ? '\u200B' : ' '}${oldText.slice(position + token.length)}`);
  180. map.set('suggestion_token', null);
  181. map.update('suggestions', ImmutableList(), list => list.clear());
  182. map.set('focusDate', new Date());
  183. map.set('caretPosition', position + completion.length + 1);
  184. map.set('idempotencyKey', uuid());
  185. });
  186. };
  187. const insertEmoji = (state, position, emojiData) => {
  188. const emoji = emojiData.native;
  189. return state.withMutations(map => {
  190. map.update('text', oldText => `${oldText.slice(0, position)}${emoji}\u200B${oldText.slice(position)}`);
  191. map.set('focusDate', new Date());
  192. map.set('caretPosition', position + emoji.length + 1);
  193. map.set('idempotencyKey', uuid());
  194. });
  195. };
  196. const hydrate = (state, hydratedState) => {
  197. state = clearAll(state.merge(hydratedState));
  198. if (hydratedState.has('text')) {
  199. state = state.set('text', hydratedState.get('text'));
  200. }
  201. return state;
  202. };
  203. const domParser = new DOMParser();
  204. const expandMentions = status => {
  205. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  206. status.get('mentions').forEach(mention => {
  207. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  208. });
  209. return fragment.innerHTML;
  210. };
  211. export default function compose(state = initialState, action) {
  212. switch(action.type) {
  213. case STORE_HYDRATE:
  214. return hydrate(state, action.state.get('compose'));
  215. case COMPOSE_MOUNT:
  216. return state.set('mounted', true);
  217. case COMPOSE_UNMOUNT:
  218. return state.set('mounted', false);
  219. case COMPOSE_ADVANCED_OPTIONS_CHANGE:
  220. return state
  221. .set('advanced_options', state.get('advanced_options').set(action.option, !!overwrite(!state.getIn(['advanced_options', action.option]), action.value)))
  222. .set('idempotencyKey', uuid());
  223. case COMPOSE_SENSITIVITY_CHANGE:
  224. return state.withMutations(map => {
  225. if (!state.get('spoiler')) {
  226. map.set('sensitive', !state.get('sensitive'));
  227. }
  228. map.set('idempotencyKey', uuid());
  229. });
  230. case COMPOSE_SPOILERNESS_CHANGE:
  231. return state.withMutations(map => {
  232. map.set('spoiler_text', '');
  233. map.set('spoiler', !state.get('spoiler'));
  234. map.set('idempotencyKey', uuid());
  235. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  236. map.set('sensitive', true);
  237. }
  238. });
  239. case COMPOSE_SPOILER_TEXT_CHANGE:
  240. return state
  241. .set('spoiler_text', action.text)
  242. .set('idempotencyKey', uuid());
  243. case COMPOSE_VISIBILITY_CHANGE:
  244. return state
  245. .set('privacy', action.value)
  246. .set('idempotencyKey', uuid());
  247. case COMPOSE_CHANGE:
  248. return state
  249. .set('text', action.text)
  250. .set('idempotencyKey', uuid());
  251. case COMPOSE_CYCLE_ELEFRIEND:
  252. return state
  253. .set('elefriend', (state.get('elefriend') + 1) % totalElefriends);
  254. case COMPOSE_REPLY:
  255. return state.withMutations(map => {
  256. map.set('in_reply_to', action.status.get('id'));
  257. map.set('text', statusToTextMentions(state, action.status));
  258. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  259. map.update(
  260. 'advanced_options',
  261. map => map.merge(new ImmutableMap({ do_not_federate: /👁\ufe0f?\u200b?(?:<\/p>)?$/.test(action.status.get('content')) }))
  262. );
  263. map.set('focusDate', new Date());
  264. map.set('caretPosition', null);
  265. map.set('preselectDate', new Date());
  266. map.set('idempotencyKey', uuid());
  267. if (action.status.get('spoiler_text').length > 0) {
  268. map.set('spoiler', true);
  269. map.set('spoiler_text', action.status.get('spoiler_text'));
  270. } else {
  271. map.set('spoiler', false);
  272. map.set('spoiler_text', '');
  273. }
  274. });
  275. case COMPOSE_REPLY_CANCEL:
  276. state = state.setIn(['advanced_options', 'threaded_mode'], false);
  277. case COMPOSE_RESET:
  278. return state.withMutations(map => {
  279. map.set('in_reply_to', null);
  280. map.set('text', '');
  281. map.set('spoiler', false);
  282. map.set('spoiler_text', '');
  283. map.set('privacy', state.get('default_privacy'));
  284. map.update(
  285. 'advanced_options',
  286. map => map.mergeWith(overwrite, state.get('default_advanced_options'))
  287. );
  288. map.set('idempotencyKey', uuid());
  289. });
  290. case COMPOSE_SUBMIT_REQUEST:
  291. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  292. return state.set('is_submitting', true);
  293. case COMPOSE_SUBMIT_SUCCESS:
  294. return action.status && state.getIn(['advanced_options', 'threaded_mode']) ? continueThread(state, action.status) : clearAll(state);
  295. case COMPOSE_SUBMIT_FAIL:
  296. case COMPOSE_UPLOAD_CHANGE_FAIL:
  297. return state.set('is_submitting', false);
  298. case COMPOSE_UPLOAD_REQUEST:
  299. return state.set('is_uploading', true);
  300. case COMPOSE_UPLOAD_SUCCESS:
  301. return appendMedia(state, fromJS(action.media));
  302. case COMPOSE_UPLOAD_FAIL:
  303. return state.set('is_uploading', false);
  304. case COMPOSE_UPLOAD_UNDO:
  305. return removeMedia(state, action.media_id);
  306. case COMPOSE_UPLOAD_PROGRESS:
  307. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  308. case COMPOSE_MENTION:
  309. return state.withMutations(map => {
  310. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  311. map.set('focusDate', new Date());
  312. map.set('caretPosition', null);
  313. map.set('idempotencyKey', uuid());
  314. });
  315. case COMPOSE_DIRECT:
  316. return state.withMutations(map => {
  317. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  318. map.set('privacy', 'direct');
  319. map.set('focusDate', new Date());
  320. map.set('caretPosition', null);
  321. map.set('idempotencyKey', uuid());
  322. });
  323. case COMPOSE_SUGGESTIONS_CLEAR:
  324. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  325. case COMPOSE_SUGGESTIONS_READY:
  326. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  327. case COMPOSE_SUGGESTION_SELECT:
  328. return insertSuggestion(state, action.position, action.token, action.completion);
  329. case TIMELINE_DELETE:
  330. if (action.id === state.get('in_reply_to')) {
  331. return state.set('in_reply_to', null);
  332. } else {
  333. return state;
  334. }
  335. case COMPOSE_EMOJI_INSERT:
  336. return insertEmoji(state, action.position, action.emoji);
  337. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  338. return state
  339. .set('is_submitting', false)
  340. .update('media_attachments', list => list.map(item => {
  341. if (item.get('id') === action.media.id) {
  342. return item.set('description', action.media.description);
  343. }
  344. return item;
  345. }));
  346. case COMPOSE_DOODLE_SET:
  347. return state.mergeIn(['doodle'], action.options);
  348. case REDRAFT:
  349. return state.withMutations(map => {
  350. map.set('text', unescapeHTML(expandMentions(action.status)));
  351. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  352. map.set('privacy', action.status.get('visibility'));
  353. map.set('media_attachments', action.status.get('media_attachments'));
  354. map.set('focusDate', new Date());
  355. map.set('caretPosition', null);
  356. map.set('idempotencyKey', uuid());
  357. if (action.status.get('spoiler_text').length > 0) {
  358. map.set('spoiler', true);
  359. map.set('spoiler_text', action.status.get('spoiler_text'));
  360. } else {
  361. map.set('spoiler', false);
  362. map.set('spoiler_text', '');
  363. }
  364. });
  365. default:
  366. return state;
  367. }
  368. };