gab-social/app/javascript/gabsocial/actions/compose.js

765 lines
20 KiB
JavaScript
Raw Normal View History

2020-11-15 18:48:32 +00:00
import api from '../api'
2020-05-07 00:40:54 +01:00
import { FormattedMessage } from 'react-intl'
2020-11-15 18:48:32 +00:00
import { CancelToken, isCancel } from 'axios'
2020-04-08 02:06:59 +01:00
import throttle from 'lodash.throttle'
2020-05-02 07:25:55 +01:00
import moment from 'moment-mini'
2020-05-15 04:17:31 +01:00
import { isMobile } from '../utils/is_mobile'
2020-11-15 18:48:32 +00:00
import { search as emojiSearch } from '../components/emoji/emoji_mart_search_light'
2020-04-29 03:24:35 +01:00
import { urlRegex } from '../features/ui/util/url_regex'
2020-11-15 18:48:32 +00:00
import { tagHistory } from '../settings'
import { joinGroup } from './groups'
2020-11-15 18:48:32 +00:00
import { useEmoji } from './emojis'
import resizeImage from '../utils/resize_image'
import { importFetchedAccounts } from './importer'
import {
updateTimelineQueue,
updateTimeline,
} from './timelines'
2020-11-15 18:48:32 +00:00
// import { showAlert, showAlertForError } from './alerts'
import { defineMessages } from 'react-intl'
import { openModal, closeModal } from './modal'
import {
2020-11-25 21:22:37 +00:00
MODAL_COMPOSE,
2020-12-16 00:31:30 +00:00
EXPIRATION_OPTION_5_MINUTES,
2020-12-19 06:33:33 +00:00
EXPIRATION_OPTION_1_HOUR,
2020-12-16 00:31:30 +00:00
EXPIRATION_OPTION_6_HOURS,
2020-12-19 06:33:33 +00:00
EXPIRATION_OPTION_1_DAY,
2020-12-16 00:31:30 +00:00
EXPIRATION_OPTION_3_DAYS,
EXPIRATION_OPTION_7_DAYS,
} from '../constants'
2020-11-15 18:48:32 +00:00
import { me } from '../initial_state'
2020-05-07 00:40:54 +01:00
import { makeGetStatus } from '../selectors'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
let cancelFetchComposeSuggestionsAccounts
export const COMPOSE_CHANGE = 'COMPOSE_CHANGE'
export const COMPOSE_SUBMIT_REQUEST = 'COMPOSE_SUBMIT_REQUEST'
export const COMPOSE_SUBMIT_SUCCESS = 'COMPOSE_SUBMIT_SUCCESS'
export const COMPOSE_SUBMIT_FAIL = 'COMPOSE_SUBMIT_FAIL'
export const COMPOSE_REPLY = 'COMPOSE_REPLY'
export const COMPOSE_QUOTE = 'COMPOSE_QUOTE'
export const COMPOSE_REPLY_CANCEL = 'COMPOSE_REPLY_CANCEL'
export const COMPOSE_MENTION = 'COMPOSE_MENTION'
export const COMPOSE_RESET = 'COMPOSE_RESET'
2020-12-16 22:29:06 +00:00
export const COMPOSE_GROUP_SET = 'COMPOSE_GROUP_SET'
2020-11-15 18:48:32 +00:00
export const COMPOSE_UPLOAD_REQUEST = 'COMPOSE_UPLOAD_REQUEST'
export const COMPOSE_UPLOAD_SUCCESS = 'COMPOSE_UPLOAD_SUCCESS'
export const COMPOSE_UPLOAD_FAIL = 'COMPOSE_UPLOAD_FAIL'
export const COMPOSE_UPLOAD_PROGRESS = 'COMPOSE_UPLOAD_PROGRESS'
export const COMPOSE_UPLOAD_UNDO = 'COMPOSE_UPLOAD_UNDO'
export const COMPOSE_SUGGESTIONS_CLEAR = 'COMPOSE_SUGGESTIONS_CLEAR'
export const COMPOSE_SUGGESTIONS_READY = 'COMPOSE_SUGGESTIONS_READY'
export const COMPOSE_SUGGESTION_SELECT = 'COMPOSE_SUGGESTION_SELECT'
export const COMPOSE_SUGGESTION_TAGS_UPDATE = 'COMPOSE_SUGGESTION_TAGS_UPDATE'
export const COMPOSE_TAG_HISTORY_UPDATE = 'COMPOSE_TAG_HISTORY_UPDATE'
export const COMPOSE_MOUNT = 'COMPOSE_MOUNT'
export const COMPOSE_UNMOUNT = 'COMPOSE_UNMOUNT'
2020-11-25 21:22:37 +00:00
export const COMPOSE_SENSITIVITY_CHANGE = 'COMPOSE_SENSITIVITY_CHANGE'
export const COMPOSE_SPOILERNESS_CHANGE = 'COMPOSE_SPOILERNESS_CHANGE'
2020-11-15 18:48:32 +00:00
export const COMPOSE_SPOILER_TEXT_CHANGE = 'COMPOSE_SPOILER_TEXT_CHANGE'
2020-11-25 21:22:37 +00:00
export const COMPOSE_VISIBILITY_CHANGE = 'COMPOSE_VISIBILITY_CHANGE'
export const COMPOSE_LISTABILITY_CHANGE = 'COMPOSE_LISTABILITY_CHANGE'
export const COMPOSE_COMPOSING_CHANGE = 'COMPOSE_COMPOSING_CHANGE'
2020-11-15 18:48:32 +00:00
export const COMPOSE_EMOJI_INSERT = 'COMPOSE_EMOJI_INSERT'
2020-11-25 21:22:37 +00:00
export const COMPOSE_UPLOAD_CHANGE_REQUEST = 'COMPOSE_UPLOAD_UPDATE_REQUEST'
export const COMPOSE_UPLOAD_CHANGE_SUCCESS = 'COMPOSE_UPLOAD_UPDATE_SUCCESS'
export const COMPOSE_UPLOAD_CHANGE_FAIL = 'COMPOSE_UPLOAD_UPDATE_FAIL'
2020-11-15 18:48:32 +00:00
export const COMPOSE_POLL_ADD = 'COMPOSE_POLL_ADD'
export const COMPOSE_POLL_REMOVE = 'COMPOSE_POLL_REMOVE'
export const COMPOSE_POLL_OPTION_ADD = 'COMPOSE_POLL_OPTION_ADD'
export const COMPOSE_POLL_OPTION_CHANGE = 'COMPOSE_POLL_OPTION_CHANGE'
export const COMPOSE_POLL_OPTION_REMOVE = 'COMPOSE_POLL_OPTION_REMOVE'
export const COMPOSE_POLL_SETTINGS_CHANGE = 'COMPOSE_POLL_SETTINGS_CHANGE'
export const COMPOSE_SCHEDULED_AT_CHANGE = 'COMPOSE_SCHEDULED_AT_CHANGE'
export const COMPOSE_EXPIRES_AT_CHANGE = 'COMPOSE_EXPIRES_AT_CHANGE'
2020-03-31 17:04:50 +01:00
export const COMPOSE_RICH_TEXT_EDITOR_CONTROLS_VISIBILITY = 'COMPOSE_RICH_TEXT_EDITOR_CONTROLS_VISIBILITY'
export const COMPOSE_CLEAR = 'COMPOSE_CLEAR'
2019-07-02 08:10:25 +01:00
const messages = defineMessages({
uploadErrorLimit: { id: 'upload_error.limit', defaultMessage: 'File upload limit exceeded.' },
uploadErrorPoll: { id: 'upload_error.poll', defaultMessage: 'File upload not allowed with polls.' },
2020-11-15 18:48:32 +00:00
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const hydrateCompose = () => (dispatch, getState) => {
const me = getState().getIn(['meta', 'me'])
const history = tagHistory.get(me)
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
if (history !== null) {
dispatch(updateTagHistory(history))
2019-07-02 08:10:25 +01:00
}
2020-11-15 18:48:32 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
const insertIntoTagHistory = (recognizedTags, text) => (dispatch, getState) => {
const state = getState()
const oldHistory = state.getIn(['compose', 'tagHistory'])
const me = state.getIn(['meta', 'me'])
const names = recognizedTags.map(tag => text.match(new RegExp(`#${tag.name}`, 'i'))[0].slice(1))
const intersectedOldHistory = oldHistory.filter(name => names.findIndex(newName => newName.toLowerCase() === name.toLowerCase()) === -1)
names.push(...intersectedOldHistory.toJS())
const newHistory = names.slice(0, 1000)
tagHistory.set(me, newHistory)
dispatch(updateTagHistory(newHistory))
}
/**
*
*/
export const changeCompose = (text, markdown, replyId, isStandalone, caretPosition) => (dispatch, getState) => {
const reduxReplyToId = getState().getIn(['compose', 'in_reply_to'])
const existingText = getState().getIn(['compose', 'text']).trim()
const isModalOpen = getState().getIn(['modal', 'modalType']) === MODAL_COMPOSE || isStandalone
let status
if (!!replyId) {
status = getState().getIn(['statuses', replyId])
status = makeGetStatus()(getState(), {
id: status.get('id')
})
}
2020-05-07 00:40:54 +01:00
2020-11-25 21:22:37 +00:00
if (!!replyId && replyId !== reduxReplyToId && !isModalOpen) {
if (existingText.length === 0 && text.trim().length > 0) {
dispatch({
type: COMPOSE_REPLY,
status,
text: text,
})
} else if (existingText.length > 0 && text.trim().length > 0) {
dispatch(openModal('CONFIRM', {
message: <FormattedMessage id='confirmations.reply.message' defaultMessage='Replying now will overwrite the message you are currently composing. Are you sure you want to proceed?' />,
confirm: <FormattedMessage id='confirmations.reply.confirm' defaultMessage='Reply' />,
onConfirm: () => {
dispatch({
type: COMPOSE_REPLY,
status,
})
dispatch({
type: COMPOSE_CHANGE,
text: text,
markdown: markdown,
caretPosition: caretPosition,
})
}
}))
2020-05-07 00:40:54 +01:00
} else {
2020-11-25 21:22:37 +00:00
dispatch({
type: COMPOSE_REPLY_CANCEL,
})
}
} else if (!replyId && !!reduxReplyToId && !isModalOpen) {
if (existingText.length === 0 && text.trim().length > 0) {
dispatch({
type: COMPOSE_REPLY_CANCEL,
})
2020-05-07 00:40:54 +01:00
dispatch({
type: COMPOSE_CHANGE,
text: text,
markdown: markdown,
caretPosition: caretPosition,
2020-05-07 00:40:54 +01:00
})
2020-11-25 21:22:37 +00:00
} else if (existingText.length > 0 && text.trim().length > 0) {
dispatch(openModal('CONFIRM', {
message: <FormattedMessage id='confirmations.new_compose.message' defaultMessage='Composing now will overwrite the reply you are currently writing. Are you sure you want to proceed?' />,
confirm: <FormattedMessage id='confirmations.new_compose.confirm' defaultMessage='Yes' />,
onConfirm: () => {
dispatch({
type: COMPOSE_REPLY_CANCEL,
})
dispatch({
type: COMPOSE_CHANGE,
text: text,
markdown: markdown,
caretPosition: caretPosition,
})
},
}))
} else {
//
2020-05-07 00:40:54 +01:00
}
2020-11-25 21:22:37 +00:00
} else {
dispatch({
type: COMPOSE_CHANGE,
text: text,
markdown: markdown,
caretPosition: caretPosition,
})
2020-05-07 00:40:54 +01:00
}
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const replyCompose = (status, router, showModal) => (dispatch) => {
dispatch({
type: COMPOSE_REPLY,
status,
})
if (isMobile(window.innerWidth)) {
router.history.push('/compose')
} else {
if (showModal) {
dispatch(openModal(MODAL_COMPOSE))
2020-05-02 07:25:55 +01:00
}
2020-11-25 21:22:37 +00:00
}
}
2019-07-30 19:27:49 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const quoteCompose = (status, router) => (dispatch) => {
dispatch({
type: COMPOSE_QUOTE,
status,
})
if (isMobile(window.innerWidth)) {
router.history.push('/compose')
} else {
dispatch(openModal(MODAL_COMPOSE))
}
}
2019-07-30 19:27:49 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const cancelReplyCompose = () => ({
type: COMPOSE_REPLY_CANCEL,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const resetCompose = () => ({
type: COMPOSE_RESET,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const mentionCompose = (account) => (dispatch) => {
dispatch({
type: COMPOSE_MENTION,
account: account,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
dispatch(openModal(MODAL_COMPOSE))
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const handleComposeSubmit = (dispatch, getState, response, status) => {
if (!dispatch || !getState) return
2020-11-25 21:22:37 +00:00
const isScheduledStatus = response.data.scheduled_at !== undefined
if (isScheduledStatus) {
2020-03-14 17:31:29 +00:00
// dispatch(showAlertForError({
// response: {
// data: {},
// status: 200,
// statusText: 'Successfully scheduled status',
// }
2020-11-25 21:22:37 +00:00
// }))
dispatch(submitComposeSuccess({ ...response.data }))
return
}
2020-11-25 21:22:37 +00:00
dispatch(insertIntoTagHistory(response.data.tags, status))
dispatch(submitComposeSuccess({ ...response.data }))
// To make the app more responsive, immediately push the status into the timeline
// : todo : push into comment, reload parent status, etc.
2020-11-25 21:22:37 +00:00
const insertIfOnline = (timelineId) => {
const timeline = getState().getIn(['timelines', timelineId])
if (timeline && timeline.get('items').size > 0 && timeline.getIn(['items', 0]) !== null && timeline.get('online')) {
2020-11-25 21:22:37 +00:00
dispatch(updateTimeline(timelineId, { ...response.data }))
}
2020-11-25 21:22:37 +00:00
}
2020-05-04 19:44:37 +01:00
if (response.data.visibility === 'public') {
2020-11-25 21:22:37 +00:00
insertIfOnline('home')
}
}
2020-11-25 21:22:37 +00:00
/**
*
*/
export const submitCompose = (options = {}) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me) return
2019-07-02 08:10:25 +01:00
if (options.autoJoinGroup) dispatch(joinGroup(groupId))
2020-11-25 21:22:37 +00:00
let status = getState().getIn(['compose', 'text'], '')
let markdown = getState().getIn(['compose', 'markdown'], '')
2020-11-25 21:22:37 +00:00
const replacer = (match) => {
const hasProtocol = match.startsWith('https://') || match.startsWith('http://')
//Make sure not a remote mention like @someone@somewhere.com
if (!hasProtocol) {
if (status.indexOf(`@${match}`) > -1) return match
2020-06-17 18:25:10 +01:00
}
2020-11-25 21:22:37 +00:00
return hasProtocol ? match : `http://${match}`
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
// : hack :
//Prepend http:// to urls in status that don't have protocol
status = `${status}`.replace(urlRegex, replacer)
markdown = !!markdown ? `${markdown}`.replace(urlRegex, replacer) : undefined
const inReplyToId = getState().getIn(['compose', 'in_reply_to'], null)
const groupId = getState().getIn(['compose', 'group_id'], null)
const media = getState().getIn(['compose', 'media_attachments'])
const isPrivateGroup = !!groupId ? getState().getIn(['groups', groupId, 'is_private'], false) : false
const expires_at = getState().getIn(['compose', 'expires_at'], null)
2020-11-25 21:22:37 +00:00
let scheduled_at = getState().getIn(['compose', 'scheduled_at'], null)
if (scheduled_at !== null) scheduled_at = moment.utc(scheduled_at).toDate()
//
2020-11-25 21:22:37 +00:00
const id = getState().getIn(['compose', 'id'])
const endpoint = id === null ? '/api/v1/statuses' : `/api/v1/statuses/${id}`
2020-11-25 21:22:37 +00:00
const method = id === null ? 'post' : 'put'
dispatch(submitComposeRequest())
dispatch(closeModal())
if (isMobile(window.innerWidth) && options.router && options.isStandalone) {
options.router.history.goBack()
2020-11-25 21:22:37 +00:00
}
2020-05-15 04:17:31 +01:00
2020-11-25 21:22:37 +00:00
api(getState)[method](endpoint, {
status,
markdown,
expires_at,
scheduled_at,
autoJoinGroup: options.autoJoinGroup,
2020-11-25 21:22:37 +00:00
isPrivateGroup,
in_reply_to_id: inReplyToId,
quote_of_id: getState().getIn(['compose', 'quote_of_id'], null),
media_ids: media.map(item => item.get('id')),
sensitive: getState().getIn(['compose', 'sensitive']),
spoiler_text: getState().getIn(['compose', 'spoiler_text'], ''),
visibility: getState().getIn(['compose', 'privacy']),
poll: getState().getIn(['compose', 'poll'], null),
group_id: groupId || null,
}, {
headers: {
'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
},
}).then((response) => {
handleComposeSubmit(dispatch, getState, response, status)
}).catch((error) => {
dispatch(submitComposeFail(error))
})
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const submitComposeRequest = () => ({
type: COMPOSE_SUBMIT_REQUEST,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const submitComposeSuccess = (status) => ({
type: COMPOSE_SUBMIT_SUCCESS,
showToast: true,
2020-11-25 21:22:37 +00:00
status,
})
2020-11-25 21:22:37 +00:00
const submitComposeFail = (error) => ({
type: COMPOSE_SUBMIT_FAIL,
showToast: true,
2020-11-25 21:22:37 +00:00
error,
})
/**
*
*/
export const uploadCompose = (files) => (dispatch, getState) => {
if (!me) return
const uploadLimit = 4
const media = getState().getIn(['compose', 'media_attachments'])
const pending = getState().getIn(['compose', 'pending_media_attachments'])
const progress = new Array(files.length).fill(0)
let total = Array.from(files).reduce((a, v) => a + v.size, 0)
if (files.length + media.size + pending > uploadLimit) {
// dispatch(showAlert(undefined, messages.uploadErrorLimit))
return
}
if (getState().getIn(['compose', 'poll'])) {
// dispatch(showAlert(undefined, messages.uploadErrorPoll))
return
}
dispatch(uploadComposeRequest())
for (const [i, f] of Array.from(files).entries()) {
if (media.size + i > 3) break
resizeImage(f).then((file) => {
const data = new FormData()
data.append('file', file)
// Account for disparity in size of original image and resized data
total += file.size - f.size
return api(getState).post('/api/v1/media', data, {
onUploadProgress: ({ loaded }) => {
progress[i] = loaded
dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total))
},
}).then(({ data }) => dispatch(uploadComposeSuccess(data)))
}).catch((error) => dispatch(uploadComposeFail(error, true)))
2020-05-01 06:50:27 +01:00
}
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const uploadComposeRequest = () => ({
type: COMPOSE_UPLOAD_REQUEST,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const uploadComposeProgress = (loaded, total) => ({
type: COMPOSE_UPLOAD_PROGRESS,
loaded: loaded,
total: total,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const uploadComposeSuccess = (media) => ({
type: COMPOSE_UPLOAD_SUCCESS,
media: media,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const uploadComposeFail = (error) => ({
type: COMPOSE_UPLOAD_FAIL,
showToast: true,
2020-11-25 21:22:37 +00:00
error,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeUploadCompose = (id, params) => (dispatch, getState) => {
if (!me) return
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
dispatch(changeUploadComposeRequest())
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
api(getState).put(`/api/v1/media/${id}`, params).then((response) => {
dispatch(changeUploadComposeSuccess(response.data))
}).catch((error) => {
dispatch(changeUploadComposeFail(id, error))
})
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const changeUploadComposeRequest = () => ({
type: COMPOSE_UPLOAD_CHANGE_REQUEST,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const changeUploadComposeSuccess = (media) => ({
type: COMPOSE_UPLOAD_CHANGE_SUCCESS,
media: media,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const changeUploadComposeFail = (error, decrement = false) => ({
type: COMPOSE_UPLOAD_CHANGE_FAIL,
error,
showToast: true,
2020-11-25 21:22:37 +00:00
decrement: decrement,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const undoUploadCompose = (media_id) => ({
type: COMPOSE_UPLOAD_UNDO,
media_id: media_id,
})
/**
*
*/
export const clearComposeSuggestions = () => {
2019-07-02 08:10:25 +01:00
if (cancelFetchComposeSuggestionsAccounts) {
2020-11-25 21:22:37 +00:00
cancelFetchComposeSuggestionsAccounts()
2019-07-02 08:10:25 +01:00
}
2020-11-25 21:22:37 +00:00
2019-07-02 08:10:25 +01:00
return {
type: COMPOSE_SUGGESTIONS_CLEAR,
2020-11-25 21:22:37 +00:00
}
}
/**
*
*/
export const fetchComposeSuggestions = (token) => (dispatch, getState) => {
switch (token[0]) {
case ':':
fetchComposeSuggestionsEmojis(dispatch, getState, token)
break
case '#':
fetchComposeSuggestionsTags(dispatch, getState, token)
break
default:
fetchComposeSuggestionsAccounts(dispatch, getState, token)
break
}
}
2019-07-02 08:10:25 +01:00
const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) => {
if (cancelFetchComposeSuggestionsAccounts) {
2020-11-25 21:22:37 +00:00
cancelFetchComposeSuggestionsAccounts()
2019-07-02 08:10:25 +01:00
}
2020-11-25 21:22:37 +00:00
2019-07-02 08:10:25 +01:00
api(getState).get('/api/v1/accounts/search', {
cancelToken: new CancelToken(cancel => {
2020-11-25 21:22:37 +00:00
cancelFetchComposeSuggestionsAccounts = cancel
2019-07-02 08:10:25 +01:00
}),
params: {
q: token.slice(1),
resolve: false,
limit: 4,
},
2020-11-25 21:22:37 +00:00
}).then((response) => {
dispatch(importFetchedAccounts(response.data))
dispatch(readyComposeSuggestionsAccounts(token, response.data))
}).catch((error) => {
2019-07-02 08:10:25 +01:00
if (!isCancel(error)) {
2020-11-25 21:22:37 +00:00
// dispatch(showAlertForError(error))
2019-07-02 08:10:25 +01:00
}
2020-11-25 21:22:37 +00:00
})
}, 200, { leading: true, trailing: true })
2019-07-02 08:10:25 +01:00
const fetchComposeSuggestionsEmojis = (dispatch, getState, token) => {
2020-11-25 21:22:37 +00:00
const results = emojiSearch(token.replace(':', ''), { maxResults: 5 })
dispatch(readyComposeSuggestionsEmojis(token, results))
}
2019-07-02 08:10:25 +01:00
const fetchComposeSuggestionsTags = (dispatch, getState, token) => {
2020-11-25 21:22:37 +00:00
dispatch(updateSuggestionTags(token))
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const readyComposeSuggestionsEmojis = (token, emojis) => ({
type: COMPOSE_SUGGESTIONS_READY,
token,
emojis,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
const readyComposeSuggestionsAccounts = (token, accounts) => ({
2020-11-15 18:48:32 +00:00
type: COMPOSE_SUGGESTIONS_READY,
token,
accounts,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const selectComposeSuggestion = (position, token, suggestion, path) => (dispatch, getState) => {
let completion, startPosition
if (typeof suggestion === 'object' && suggestion.id) {
completion = suggestion.native || suggestion.colons
startPosition = position - 1
dispatch(useEmoji(suggestion))
} else if (suggestion[0] === '#') {
completion = suggestion
startPosition = position - 1
} else {
completion = getState().getIn(['accounts', suggestion, 'acct'])
startPosition = position
}
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
dispatch({
type: COMPOSE_SUGGESTION_SELECT,
position: startPosition,
2019-07-02 08:10:25 +01:00
token,
2020-11-25 21:22:37 +00:00
completion,
path,
})
2019-07-02 08:10:25 +01:00
}
2020-11-25 21:22:37 +00:00
/**
*
*/
export const updateSuggestionTags = (token) => ({
type: COMPOSE_SUGGESTION_TAGS_UPDATE,
token,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const updateTagHistory = (tags) => ({
type: COMPOSE_TAG_HISTORY_UPDATE,
tags,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const mountCompose = () => ({
type: COMPOSE_MOUNT,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const unmountCompose = () => ({
type: COMPOSE_UNMOUNT,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const clearCompose = () => ({
type: COMPOSE_CLEAR,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeComposeSensitivity = () => ({
type: COMPOSE_SENSITIVITY_CHANGE,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeComposeSpoilerness = () => ({
type: COMPOSE_SPOILERNESS_CHANGE,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeComposeSpoilerText = (text) => ({
type: COMPOSE_SPOILER_TEXT_CHANGE,
text,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeComposeVisibility = (value) => ({
type: COMPOSE_VISIBILITY_CHANGE,
value,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const insertEmojiCompose = (emoji, needsSpace) => ({
type: COMPOSE_EMOJI_INSERT,
emoji,
needsSpace,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changeComposing = (value) => ({
type: COMPOSE_COMPOSING_CHANGE,
value,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const addPoll = () => ({
type: COMPOSE_POLL_ADD,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const removePoll = () => ({
type: COMPOSE_POLL_REMOVE,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const addPollOption = (title) => ({
type: COMPOSE_POLL_OPTION_ADD,
title,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const changePollOption = (index, title) => ({
type: COMPOSE_POLL_OPTION_CHANGE,
index,
title,
})
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
export const removePollOption = (index) => ({
type: COMPOSE_POLL_OPTION_REMOVE,
index,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
/**
*
*/
export const changePollSettings = (expiresIn, isMultiple) => ({
type: COMPOSE_POLL_SETTINGS_CHANGE,
expiresIn,
isMultiple,
})
/**
*
*/
export const changeScheduledAt = (date) => ({
type: COMPOSE_SCHEDULED_AT_CHANGE,
date,
})
/**
*
*/
export const changeExpiresAt = (value) => ({
type: COMPOSE_EXPIRES_AT_CHANGE,
value,
})
2020-12-16 22:29:06 +00:00
/**
*
*/
export const changeComposeGroupId = (groupId) => ({
type: COMPOSE_GROUP_SET,
groupId,
})
2020-11-15 18:48:32 +00:00
/**
*
*/
export const changeRichTextEditorControlsVisibility = (open) => ({
type: COMPOSE_RICH_TEXT_EDITOR_CONTROLS_VISIBILITY,
open,
})