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.

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