logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

compose.js (10028B)


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