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.

106 lines
3.3 KiB

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