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.

407 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. reply_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 clearAll(state) {
  107. return state.withMutations(map => {
  108. map.set('text', '');
  109. map.set('spoiler', false);
  110. map.set('spoiler_text', '');
  111. map.set('is_submitting', false);
  112. map.set('in_reply_to', null);
  113. map.update(
  114. 'advanced_options',
  115. map => map.mergeWith(overwrite, state.get('default_advanced_options'))
  116. );
  117. map.set('privacy', state.get('default_privacy'));
  118. map.set('sensitive', false);
  119. map.update('media_attachments', list => list.clear());
  120. map.set('idempotencyKey', uuid());
  121. });
  122. };
  123. function continueThread (state, status) {
  124. return state.withMutations(function (map) {
  125. map.set('text', apiStatusToTextMentions(state, status));
  126. if (status.spoiler_text) {
  127. map.set('spoiler', true);
  128. map.set('spoiler_text', status.spoiler_text);
  129. } else {
  130. map.set('spoiler', false);
  131. map.set('spoiler_text', '');
  132. }
  133. map.set('is_submitting', false);
  134. map.set('in_reply_to', status.id);
  135. map.update(
  136. 'advanced_options',
  137. map => map.merge(new ImmutableMap({ do_not_federate: /👁\ufe0f?\u200b?(?:<\/p>)?$/.test(status.content) }))
  138. );
  139. map.set('privacy', status.visibility);
  140. map.set('sensitive', false);
  141. map.update('media_attachments', list => list.clear());
  142. map.set('idempotencyKey', uuid());
  143. map.set('focusDate', new Date());
  144. map.set('caretPosition', null);
  145. map.set('preselectDate', new Date());
  146. });
  147. }
  148. function appendMedia(state, media) {
  149. const prevSize = state.get('media_attachments').size;
  150. return state.withMutations(map => {
  151. map.update('media_attachments', list => list.push(media));
  152. map.set('is_uploading', false);
  153. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  154. map.set('idempotencyKey', uuid());
  155. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  156. map.set('sensitive', true);
  157. }
  158. });
  159. };
  160. function removeMedia(state, mediaId) {
  161. const prevSize = state.get('media_attachments').size;
  162. return state.withMutations(map => {
  163. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  164. map.set('idempotencyKey', uuid());
  165. if (prevSize === 1) {
  166. map.set('sensitive', false);
  167. }
  168. });
  169. };
  170. const insertSuggestion = (state, position, token, completion) => {
  171. return state.withMutations(map => {
  172. map.update('text', oldText => `${oldText.slice(0, position)}${completion}${completion[0] === ':' ? '\u200B' : ' '}${oldText.slice(position + token.length)}`);
  173. map.set('suggestion_token', null);
  174. map.update('suggestions', ImmutableList(), list => list.clear());
  175. map.set('focusDate', new Date());
  176. map.set('caretPosition', position + completion.length + 1);
  177. map.set('idempotencyKey', uuid());
  178. });
  179. };
  180. const insertEmoji = (state, position, emojiData) => {
  181. const emoji = emojiData.native;
  182. return state.withMutations(map => {
  183. map.update('text', oldText => `${oldText.slice(0, position)}${emoji}\u200B${oldText.slice(position)}`);
  184. map.set('focusDate', new Date());
  185. map.set('caretPosition', position + emoji.length + 1);
  186. map.set('idempotencyKey', uuid());
  187. });
  188. };
  189. const privacyPreference = (a, b) => {
  190. if (a === 'direct' || b === 'direct') {
  191. return 'direct';
  192. } else if (a === 'private' || b === 'private') {
  193. return 'private';
  194. } else if (a === 'unlisted' || b === 'unlisted') {
  195. return 'unlisted';
  196. } else {
  197. return 'public';
  198. }
  199. };
  200. const hydrate = (state, hydratedState) => {
  201. state = clearAll(state.merge(hydratedState));
  202. if (hydratedState.has('text')) {
  203. state = state.set('text', hydratedState.get('text'));
  204. }
  205. return state;
  206. };
  207. const domParser = new DOMParser();
  208. const expandMentions = status => {
  209. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  210. status.get('mentions').forEach(mention => {
  211. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  212. });
  213. return fragment.innerHTML;
  214. };
  215. export default function compose(state = initialState, action) {
  216. switch(action.type) {
  217. case STORE_HYDRATE:
  218. return hydrate(state, action.state.get('compose'));
  219. case COMPOSE_MOUNT:
  220. return state.set('mounted', true);
  221. case COMPOSE_UNMOUNT:
  222. return state.set('mounted', false);
  223. case COMPOSE_ADVANCED_OPTIONS_CHANGE:
  224. return state
  225. .set('advanced_options', state.get('advanced_options').set(action.option, !!overwrite(!state.getIn(['advanced_options', action.option]), action.value)))
  226. .set('idempotencyKey', uuid());
  227. case COMPOSE_SENSITIVITY_CHANGE:
  228. return state.withMutations(map => {
  229. if (!state.get('spoiler')) {
  230. map.set('sensitive', !state.get('sensitive'));
  231. }
  232. map.set('idempotencyKey', uuid());
  233. });
  234. case COMPOSE_SPOILERNESS_CHANGE:
  235. return state.withMutations(map => {
  236. map.set('spoiler_text', '');
  237. map.set('spoiler', !state.get('spoiler'));
  238. map.set('idempotencyKey', uuid());
  239. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  240. map.set('sensitive', true);
  241. }
  242. });
  243. case COMPOSE_SPOILER_TEXT_CHANGE:
  244. return state
  245. .set('spoiler_text', action.text)
  246. .set('idempotencyKey', uuid());
  247. case COMPOSE_VISIBILITY_CHANGE:
  248. return state
  249. .set('privacy', action.value)
  250. .set('idempotencyKey', uuid());
  251. case COMPOSE_CHANGE:
  252. return state
  253. .set('text', action.text)
  254. .set('idempotencyKey', uuid());
  255. case COMPOSE_CYCLE_ELEFRIEND:
  256. return state
  257. .set('elefriend', (state.get('elefriend') + 1) % totalElefriends);
  258. case COMPOSE_REPLY:
  259. return state.withMutations(map => {
  260. map.set('in_reply_to', action.status.get('id'));
  261. map.set('text', statusToTextMentions(state, action.status));
  262. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  263. map.set('reply_privacy', action.status.get('visibility'));
  264. map.update(
  265. 'advanced_options',
  266. map => map.merge(new ImmutableMap({ do_not_federate: /👁\ufe0f?\u200b?(?:<\/p>)?$/.test(action.status.get('content')) }))
  267. );
  268. map.set('focusDate', new Date());
  269. map.set('caretPosition', null);
  270. map.set('preselectDate', new Date());
  271. map.set('idempotencyKey', uuid());
  272. if (action.status.get('spoiler_text').length > 0) {
  273. map.set('spoiler', true);
  274. map.set('spoiler_text', action.status.get('spoiler_text'));
  275. } else {
  276. map.set('spoiler', false);
  277. map.set('spoiler_text', '');
  278. }
  279. });
  280. case COMPOSE_REPLY_CANCEL:
  281. case COMPOSE_RESET:
  282. return state.withMutations(map => {
  283. map.set('in_reply_to', null);
  284. map.set('text', '');
  285. map.set('spoiler', false);
  286. map.set('spoiler_text', '');
  287. map.set('privacy', state.get('default_privacy'));
  288. map.set('reply_privacy', null);
  289. map.update(
  290. 'advanced_options',
  291. map => map.mergeWith(overwrite, state.get('default_advanced_options'))
  292. );
  293. map.set('idempotencyKey', uuid());
  294. });
  295. case COMPOSE_SUBMIT_REQUEST:
  296. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  297. return state.set('is_submitting', true);
  298. case COMPOSE_SUBMIT_SUCCESS:
  299. return action.status && state.getIn(['advanced_options', 'threaded_mode']) ? continueThread(state, action.status) : clearAll(state);
  300. case COMPOSE_SUBMIT_FAIL:
  301. case COMPOSE_UPLOAD_CHANGE_FAIL:
  302. return state.set('is_submitting', false);
  303. case COMPOSE_UPLOAD_REQUEST:
  304. return state.set('is_uploading', true);
  305. case COMPOSE_UPLOAD_SUCCESS:
  306. return appendMedia(state, fromJS(action.media));
  307. case COMPOSE_UPLOAD_FAIL:
  308. return state.set('is_uploading', false);
  309. case COMPOSE_UPLOAD_UNDO:
  310. return removeMedia(state, action.media_id);
  311. case COMPOSE_UPLOAD_PROGRESS:
  312. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  313. case COMPOSE_MENTION:
  314. return state.withMutations(map => {
  315. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  316. map.set('focusDate', new Date());
  317. map.set('caretPosition', null);
  318. map.set('idempotencyKey', uuid());
  319. });
  320. case COMPOSE_DIRECT:
  321. return state.withMutations(map => {
  322. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  323. map.set('privacy', 'direct');
  324. map.set('focusDate', new Date());
  325. map.set('caretPosition', null);
  326. map.set('idempotencyKey', uuid());
  327. });
  328. case COMPOSE_SUGGESTIONS_CLEAR:
  329. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  330. case COMPOSE_SUGGESTIONS_READY:
  331. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  332. case COMPOSE_SUGGESTION_SELECT:
  333. return insertSuggestion(state, action.position, action.token, action.completion);
  334. case TIMELINE_DELETE:
  335. if (action.id === state.get('in_reply_to')) {
  336. return state.set('in_reply_to', null);
  337. } else {
  338. return state;
  339. }
  340. case COMPOSE_EMOJI_INSERT:
  341. return insertEmoji(state, action.position, action.emoji);
  342. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  343. return state
  344. .set('is_submitting', false)
  345. .update('media_attachments', list => list.map(item => {
  346. if (item.get('id') === action.media.id) {
  347. return item.set('description', action.media.description);
  348. }
  349. return item;
  350. }));
  351. case COMPOSE_DOODLE_SET:
  352. return state.mergeIn(['doodle'], action.options);
  353. case REDRAFT:
  354. return state.withMutations(map => {
  355. map.set('text', unescapeHTML(expandMentions(action.status)));
  356. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  357. map.set('privacy', action.status.get('visibility'));
  358. map.set('media_attachments', action.status.get('media_attachments'));
  359. map.set('focusDate', new Date());
  360. map.set('caretPosition', null);
  361. map.set('idempotencyKey', uuid());
  362. if (action.status.get('spoiler_text').length > 0) {
  363. map.set('spoiler', true);
  364. map.set('spoiler_text', action.status.get('spoiler_text'));
  365. } else {
  366. map.set('spoiler', false);
  367. map.set('spoiler_text', '');
  368. }
  369. });
  370. default:
  371. return state;
  372. }
  373. };