闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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