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.

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