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.

404 lines
13 KiB

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