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.
 
 
 
 

278 lines
8.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_SPOILERNESS_CHANGE,
  21. COMPOSE_SPOILER_TEXT_CHANGE,
  22. COMPOSE_VISIBILITY_CHANGE,
  23. COMPOSE_COMPOSING_CHANGE,
  24. COMPOSE_EMOJI_INSERT,
  25. COMPOSE_UPLOAD_CHANGE_REQUEST,
  26. COMPOSE_UPLOAD_CHANGE_SUCCESS,
  27. COMPOSE_UPLOAD_CHANGE_FAIL,
  28. COMPOSE_RESET,
  29. } from '../actions/compose';
  30. import { TIMELINE_DELETE } from '../actions/timelines';
  31. import { STORE_HYDRATE } from '../actions/store';
  32. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  33. import uuid from '../uuid';
  34. const initialState = ImmutableMap({
  35. mounted: false,
  36. sensitive: false,
  37. spoiler: false,
  38. spoiler_text: '',
  39. privacy: null,
  40. text: '',
  41. focusDate: null,
  42. preselectDate: null,
  43. in_reply_to: null,
  44. is_composing: false,
  45. is_submitting: false,
  46. is_uploading: false,
  47. progress: 0,
  48. media_attachments: ImmutableList(),
  49. suggestion_token: null,
  50. suggestions: ImmutableList(),
  51. me: null,
  52. default_privacy: 'public',
  53. default_sensitive: false,
  54. resetFileKey: Math.floor((Math.random() * 0x10000)),
  55. idempotencyKey: null,
  56. });
  57. function statusToTextMentions(state, status) {
  58. let set = ImmutableOrderedSet([]);
  59. let me = state.get('me');
  60. if (status.getIn(['account', 'id']) !== me) {
  61. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  62. }
  63. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  64. };
  65. function clearAll(state) {
  66. return state.withMutations(map => {
  67. map.set('text', '');
  68. map.set('spoiler', false);
  69. map.set('spoiler_text', '');
  70. map.set('is_submitting', false);
  71. map.set('in_reply_to', null);
  72. map.set('privacy', state.get('default_privacy'));
  73. map.set('sensitive', false);
  74. map.update('media_attachments', list => list.clear());
  75. map.set('idempotencyKey', uuid());
  76. });
  77. };
  78. function appendMedia(state, media) {
  79. const prevSize = state.get('media_attachments').size;
  80. return state.withMutations(map => {
  81. map.update('media_attachments', list => list.push(media));
  82. map.set('is_uploading', false);
  83. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  84. map.update('text', oldText => `${oldText.trim()} ${media.get('text_url')}`);
  85. map.set('focusDate', new Date());
  86. map.set('idempotencyKey', uuid());
  87. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  88. map.set('sensitive', true);
  89. }
  90. });
  91. };
  92. function removeMedia(state, mediaId) {
  93. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  94. const prevSize = state.get('media_attachments').size;
  95. return state.withMutations(map => {
  96. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  97. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  98. map.set('idempotencyKey', uuid());
  99. if (prevSize === 1) {
  100. map.set('sensitive', false);
  101. }
  102. });
  103. };
  104. const insertSuggestion = (state, position, token, completion) => {
  105. return state.withMutations(map => {
  106. map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  107. map.set('suggestion_token', null);
  108. map.update('suggestions', ImmutableList(), list => list.clear());
  109. map.set('focusDate', new Date());
  110. map.set('idempotencyKey', uuid());
  111. });
  112. };
  113. const insertEmoji = (state, position, emojiData) => {
  114. const emoji = emojiData.native;
  115. return state.withMutations(map => {
  116. map.update('text', oldText => `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`);
  117. map.set('focusDate', new Date());
  118. map.set('idempotencyKey', uuid());
  119. });
  120. };
  121. const privacyPreference = (a, b) => {
  122. if (a === 'direct' || b === 'direct') {
  123. return 'direct';
  124. } else if (a === 'private' || b === 'private') {
  125. return 'private';
  126. } else if (a === 'unlisted' || b === 'unlisted') {
  127. return 'unlisted';
  128. } else {
  129. return 'public';
  130. }
  131. };
  132. const hydrate = (state, hydratedState) => {
  133. state = clearAll(state.merge(hydratedState));
  134. if (hydratedState.has('text')) {
  135. state = state.set('text', hydratedState.get('text'));
  136. }
  137. return state;
  138. };
  139. export default function compose(state = initialState, action) {
  140. switch(action.type) {
  141. case STORE_HYDRATE:
  142. return hydrate(state, action.state.get('compose'));
  143. case COMPOSE_MOUNT:
  144. return state.set('mounted', true);
  145. case COMPOSE_UNMOUNT:
  146. return state
  147. .set('mounted', false)
  148. .set('is_composing', false);
  149. case COMPOSE_SENSITIVITY_CHANGE:
  150. return state.withMutations(map => {
  151. if (!state.get('spoiler')) {
  152. map.set('sensitive', !state.get('sensitive'));
  153. }
  154. map.set('idempotencyKey', uuid());
  155. });
  156. case COMPOSE_SPOILERNESS_CHANGE:
  157. return state.withMutations(map => {
  158. map.set('spoiler_text', '');
  159. map.set('spoiler', !state.get('spoiler'));
  160. map.set('idempotencyKey', uuid());
  161. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  162. map.set('sensitive', true);
  163. }
  164. });
  165. case COMPOSE_SPOILER_TEXT_CHANGE:
  166. return state
  167. .set('spoiler_text', action.text)
  168. .set('idempotencyKey', uuid());
  169. case COMPOSE_VISIBILITY_CHANGE:
  170. return state
  171. .set('privacy', action.value)
  172. .set('idempotencyKey', uuid());
  173. case COMPOSE_CHANGE:
  174. return state
  175. .set('text', action.text)
  176. .set('idempotencyKey', uuid());
  177. case COMPOSE_COMPOSING_CHANGE:
  178. return state.set('is_composing', action.value);
  179. case COMPOSE_REPLY:
  180. return state.withMutations(map => {
  181. map.set('in_reply_to', action.status.get('id'));
  182. map.set('text', statusToTextMentions(state, action.status));
  183. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  184. map.set('focusDate', new Date());
  185. map.set('preselectDate', new Date());
  186. map.set('idempotencyKey', uuid());
  187. if (action.status.get('spoiler_text').length > 0) {
  188. map.set('spoiler', true);
  189. map.set('spoiler_text', action.status.get('spoiler_text'));
  190. } else {
  191. map.set('spoiler', false);
  192. map.set('spoiler_text', '');
  193. }
  194. });
  195. case COMPOSE_REPLY_CANCEL:
  196. case COMPOSE_RESET:
  197. return state.withMutations(map => {
  198. map.set('in_reply_to', null);
  199. map.set('text', '');
  200. map.set('spoiler', false);
  201. map.set('spoiler_text', '');
  202. map.set('privacy', state.get('default_privacy'));
  203. map.set('idempotencyKey', uuid());
  204. });
  205. case COMPOSE_SUBMIT_REQUEST:
  206. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  207. return state.set('is_submitting', true);
  208. case COMPOSE_SUBMIT_SUCCESS:
  209. return clearAll(state);
  210. case COMPOSE_SUBMIT_FAIL:
  211. case COMPOSE_UPLOAD_CHANGE_FAIL:
  212. return state.set('is_submitting', false);
  213. case COMPOSE_UPLOAD_REQUEST:
  214. return state.set('is_uploading', true);
  215. case COMPOSE_UPLOAD_SUCCESS:
  216. return appendMedia(state, fromJS(action.media));
  217. case COMPOSE_UPLOAD_FAIL:
  218. return state.set('is_uploading', false);
  219. case COMPOSE_UPLOAD_UNDO:
  220. return removeMedia(state, action.media_id);
  221. case COMPOSE_UPLOAD_PROGRESS:
  222. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  223. case COMPOSE_MENTION:
  224. return state
  225. .update('text', text => `${text}@${action.account.get('acct')} `)
  226. .set('focusDate', new Date())
  227. .set('idempotencyKey', uuid());
  228. case COMPOSE_SUGGESTIONS_CLEAR:
  229. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  230. case COMPOSE_SUGGESTIONS_READY:
  231. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  232. case COMPOSE_SUGGESTION_SELECT:
  233. return insertSuggestion(state, action.position, action.token, action.completion);
  234. case TIMELINE_DELETE:
  235. if (action.id === state.get('in_reply_to')) {
  236. return state.set('in_reply_to', null);
  237. } else {
  238. return state;
  239. }
  240. case COMPOSE_EMOJI_INSERT:
  241. return insertEmoji(state, action.position, action.emoji);
  242. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  243. return state
  244. .set('is_submitting', false)
  245. .update('media_attachments', list => list.map(item => {
  246. if (item.get('id') === action.media.id) {
  247. return item.set('description', action.media.description);
  248. }
  249. return item;
  250. }));
  251. default:
  252. return state;
  253. }
  254. };