The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

146 lines
4.8 KiB

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