闭社主体 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.

116 lines
3.6 KiB

  1. import {
  2. COMPOSE_CHANGE,
  3. COMPOSE_REPLY,
  4. COMPOSE_REPLY_CANCEL,
  5. COMPOSE_MENTION,
  6. COMPOSE_SUBMIT_REQUEST,
  7. COMPOSE_SUBMIT_SUCCESS,
  8. COMPOSE_SUBMIT_FAIL,
  9. COMPOSE_UPLOAD_REQUEST,
  10. COMPOSE_UPLOAD_SUCCESS,
  11. COMPOSE_UPLOAD_FAIL,
  12. COMPOSE_UPLOAD_UNDO,
  13. COMPOSE_UPLOAD_PROGRESS,
  14. COMPOSE_SUGGESTIONS_CLEAR,
  15. COMPOSE_SUGGESTIONS_READY
  16. } from '../actions/compose';
  17. import { TIMELINE_DELETE } from '../actions/timelines';
  18. import { ACCOUNT_SET_SELF } from '../actions/accounts';
  19. import Immutable from 'immutable';
  20. const initialState = Immutable.Map({
  21. text: '',
  22. in_reply_to: null,
  23. is_submitting: false,
  24. is_uploading: false,
  25. progress: 0,
  26. media_attachments: Immutable.List(),
  27. suggestions: [],
  28. me: null
  29. });
  30. function statusToTextMentions(state, status) {
  31. let set = Immutable.OrderedSet([]);
  32. let me = state.get('me');
  33. if (status.getIn(['account', 'id']) !== me) {
  34. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  35. }
  36. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  37. };
  38. function clearAll(state) {
  39. return state.withMutations(map => {
  40. map.set('text', '');
  41. map.set('is_submitting', false);
  42. map.set('in_reply_to', null);
  43. map.update('media_attachments', list => list.clear());
  44. });
  45. };
  46. function appendMedia(state, media) {
  47. return state.withMutations(map => {
  48. map.update('media_attachments', list => list.push(media));
  49. map.set('is_uploading', false);
  50. map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim());
  51. });
  52. };
  53. function removeMedia(state, mediaId) {
  54. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  55. return state.withMutations(map => {
  56. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  57. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  58. });
  59. };
  60. export default function compose(state = initialState, action) {
  61. switch(action.type) {
  62. case COMPOSE_CHANGE:
  63. return state.set('text', action.text);
  64. case COMPOSE_REPLY:
  65. return state.withMutations(map => {
  66. map.set('in_reply_to', action.status.get('id'));
  67. map.set('text', statusToTextMentions(state, action.status));
  68. });
  69. case COMPOSE_REPLY_CANCEL:
  70. return state.withMutations(map => {
  71. map.set('in_reply_to', null);
  72. map.set('text', '');
  73. });
  74. case COMPOSE_SUBMIT_REQUEST:
  75. return state.set('is_submitting', true);
  76. case COMPOSE_SUBMIT_SUCCESS:
  77. return clearAll(state);
  78. case COMPOSE_SUBMIT_FAIL:
  79. return state.set('is_submitting', false);
  80. case COMPOSE_UPLOAD_REQUEST:
  81. return state.set('is_uploading', true);
  82. case COMPOSE_UPLOAD_SUCCESS:
  83. return appendMedia(state, Immutable.fromJS(action.media));
  84. case COMPOSE_UPLOAD_FAIL:
  85. return state.set('is_uploading', false);
  86. case COMPOSE_UPLOAD_UNDO:
  87. return removeMedia(state, action.media_id);
  88. case COMPOSE_UPLOAD_PROGRESS:
  89. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  90. case COMPOSE_MENTION:
  91. return state.update('text', text => `${text}@${action.account.get('acct')} `);
  92. case COMPOSE_SUGGESTIONS_CLEAR:
  93. return state.set('suggestions', []);
  94. case COMPOSE_SUGGESTIONS_READY:
  95. return state.set('suggestions', action.accounts);
  96. case TIMELINE_DELETE:
  97. if (action.id === state.get('in_reply_to')) {
  98. return state.set('in_reply_to', null);
  99. } else {
  100. return state;
  101. }
  102. case ACCOUNT_SET_SELF:
  103. return state.set('me', action.account.id);
  104. default:
  105. return state;
  106. }
  107. };