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.

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