2020-11-25 21:22:37 +00:00
|
|
|
import api from '../api'
|
|
|
|
import openDB from '../storage/db'
|
|
|
|
import { evictStatus } from '../storage/modifier'
|
|
|
|
import { deleteFromTimelines } from './timelines'
|
|
|
|
import { importFetchedStatus, importFetchedStatuses, importAccount, importStatus } from './importer'
|
|
|
|
import { openModal } from './modal'
|
|
|
|
import { me } from '../initial_state'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST'
|
|
|
|
export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS'
|
|
|
|
export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST'
|
|
|
|
export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS'
|
|
|
|
export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST'
|
|
|
|
export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS'
|
|
|
|
export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const COMMENTS_FETCH_REQUEST = 'COMMENTS_FETCH_REQUEST'
|
|
|
|
export const COMMENTS_FETCH_SUCCESS = 'COMMENTS_FETCH_SUCCESS'
|
|
|
|
export const COMMENTS_FETCH_FAIL = 'COMMENTS_FETCH_FAIL'
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const STATUS_REVEAL = 'STATUS_REVEAL'
|
|
|
|
export const STATUS_HIDE = 'STATUS_HIDE'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
export const STATUS_EDIT = 'STATUS_EDIT'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const UPDATE_STATUS_STATS = 'UPDATE_STATUS_STATS'
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
function getFromDB(dispatch, getState, accountIndex, index, id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-25 21:22:37 +00:00
|
|
|
const request = index.get(id)
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
request.onerror = reject
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
request.onsuccess = () => {
|
2020-11-25 21:22:37 +00:00
|
|
|
const promises = []
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (!request.result) {
|
2020-11-25 21:22:37 +00:00
|
|
|
reject()
|
|
|
|
return
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
dispatch(importStatus(request.result))
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
if (getState().getIn(['accounts', request.result.account], null) === null) {
|
|
|
|
promises.push(new Promise((accountResolve, accountReject) => {
|
2020-11-25 21:22:37 +00:00
|
|
|
const accountRequest = accountIndex.get(request.result.account)
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
accountRequest.onerror = accountReject
|
2019-07-02 08:10:25 +01:00
|
|
|
accountRequest.onsuccess = () => {
|
|
|
|
if (!request.result) {
|
2020-11-25 21:22:37 +00:00
|
|
|
accountReject()
|
|
|
|
return
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
dispatch(importAccount(accountRequest.result))
|
|
|
|
accountResolve()
|
|
|
|
}
|
|
|
|
}))
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (request.result.reblog && getState().getIn(['statuses', request.result.reblog], null) === null) {
|
2020-11-25 21:22:37 +00:00
|
|
|
promises.push(getFromDB(dispatch, getState, accountIndex, index, request.result.reblog))
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
resolve(Promise.all(promises))
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
2020-11-25 21:22:37 +00:00
|
|
|
})
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchStatus = (id) => (dispatch, getState) => {
|
2020-12-31 22:49:25 +00:00
|
|
|
if (!id) return
|
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
const skipLoading = getState().getIn(['statuses', id], null) !== null
|
|
|
|
if (skipLoading) return
|
|
|
|
|
|
|
|
dispatch(fetchStatusRequest(id, skipLoading))
|
|
|
|
|
|
|
|
openDB().then((db) => {
|
|
|
|
const transaction = db.transaction(['accounts', 'statuses'], 'read')
|
|
|
|
const accountIndex = transaction.objectStore('accounts').index('id')
|
|
|
|
const index = transaction.objectStore('statuses').index('id')
|
|
|
|
|
|
|
|
return getFromDB(dispatch, getState, accountIndex, index, id).then(() => {
|
|
|
|
db.close()
|
|
|
|
}, (error) => {
|
|
|
|
db.close()
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
dispatch(fetchStatusSuccess(skipLoading))
|
|
|
|
}, () => api(getState).get(`/api/v1/statuses/${id}`).then((response) => {
|
|
|
|
dispatch(importFetchedStatus(response.data))
|
|
|
|
dispatch(fetchStatusSuccess(skipLoading))
|
|
|
|
})).catch((error) => {
|
|
|
|
dispatch(fetchStatusFail(id, error, skipLoading))
|
|
|
|
})
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
const fetchStatusRequest = (id, skipLoading) => ({
|
|
|
|
type: STATUS_FETCH_REQUEST,
|
|
|
|
id,
|
|
|
|
skipLoading,
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchStatusSuccess = (skipLoading) => ({
|
|
|
|
type: STATUS_FETCH_SUCCESS,
|
|
|
|
skipLoading,
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchStatusFail = (id, error, skipLoading) => ({
|
|
|
|
type: STATUS_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
|
|
|
skipLoading,
|
|
|
|
skipAlert: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const editStatus = (status) => (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: STATUS_EDIT,
|
|
|
|
status,
|
|
|
|
})
|
|
|
|
|
|
|
|
dispatch(openModal('COMPOSE'))
|
|
|
|
}
|
2020-05-15 03:31:24 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const deleteStatus = (id, routerHistory) => (dispatch, getState) => {
|
|
|
|
if (!me) return
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
let status = getState().getIn(['statuses', id])
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
if (status.get('poll')) {
|
|
|
|
status = status.set('poll', getState().getIn(['polls', status.get('poll')]))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
dispatch(deleteStatusRequest(id))
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
api(getState).delete(`/api/v1/statuses/${id}`).then((response) => {
|
|
|
|
evictStatus(id)
|
|
|
|
dispatch(deleteStatusSuccess(id))
|
|
|
|
dispatch(deleteFromTimelines(id))
|
|
|
|
}).catch((error) => {
|
|
|
|
dispatch(deleteStatusFail(id, error))
|
|
|
|
})
|
|
|
|
}
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
const deleteStatusRequest = (id) => ({
|
|
|
|
type: STATUS_DELETE_REQUEST,
|
|
|
|
id: id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const deleteStatusSuccess = (id) => ({
|
|
|
|
type: STATUS_DELETE_SUCCESS,
|
|
|
|
id: id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const deleteStatusFail = (id, error) => ({
|
|
|
|
type: STATUS_DELETE_FAIL,
|
|
|
|
id: id,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchContext = (id, ensureIsReply) => (dispatch, getState) => {
|
|
|
|
if (ensureIsReply) {
|
|
|
|
const isReply = !!getState().getIn(['statuses', id, 'in_reply_to_id'], null)
|
|
|
|
if (!isReply) return
|
|
|
|
}
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
dispatch(fetchContextRequest(id))
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
api(getState).get(`/api/v1/statuses/${id}/context`).then((response) => {
|
|
|
|
dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)))
|
|
|
|
dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants))
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response && error.response.status === 404) {
|
|
|
|
dispatch(deleteFromTimelines(id))
|
|
|
|
}
|
|
|
|
dispatch(fetchContextFail(id, error))
|
|
|
|
})
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const fetchContextRequest = (id) => ({
|
|
|
|
type: CONTEXT_FETCH_REQUEST,
|
|
|
|
id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchContextSuccess = (id, ancestors, descendants) => ({
|
|
|
|
type: CONTEXT_FETCH_SUCCESS,
|
|
|
|
id,
|
|
|
|
ancestors,
|
|
|
|
descendants,
|
|
|
|
statuses: ancestors.concat(descendants),
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchContextFail = (id, error) => ({
|
|
|
|
type: CONTEXT_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchComments = (id) => (dispatch, getState) => {
|
|
|
|
dispatch(fetchCommentsRequest(id))
|
|
|
|
|
|
|
|
api(getState).get(`/api/v1/statuses/${id}/comments`).then((response) => {
|
|
|
|
dispatch(importFetchedStatuses(response.data.descendants))
|
|
|
|
dispatch(fetchCommentsSuccess(id, response.data.descendants))
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response && error.response.status === 404) {
|
|
|
|
dispatch(deleteFromTimelines(id))
|
|
|
|
}
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
dispatch(fetchCommentsFail(id, error))
|
|
|
|
})
|
|
|
|
}
|
2020-04-22 06:00:11 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
const fetchCommentsRequest = (id) => ({
|
|
|
|
type: COMMENTS_FETCH_REQUEST,
|
|
|
|
id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchCommentsSuccess = (id, descendants) => ({
|
|
|
|
type: COMMENTS_FETCH_SUCCESS,
|
|
|
|
id,
|
|
|
|
descendants,
|
|
|
|
})
|
|
|
|
|
|
|
|
const fetchCommentsFail = (id, error) => ({
|
|
|
|
type: COMMENTS_FETCH_FAIL,
|
|
|
|
id,
|
|
|
|
error,
|
|
|
|
skipAlert: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const hideStatus = (ids) => {
|
2019-07-02 08:10:25 +01:00
|
|
|
if (!Array.isArray(ids)) {
|
2020-11-25 21:22:37 +00:00
|
|
|
ids = [ids]
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: STATUS_HIDE,
|
|
|
|
ids,
|
2020-11-25 21:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const revealStatus = (ids) => {
|
2019-07-02 08:10:25 +01:00
|
|
|
if (!Array.isArray(ids)) {
|
2020-11-25 21:22:37 +00:00
|
|
|
ids = [ids]
|
2019-07-02 08:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: STATUS_REVEAL,
|
|
|
|
ids,
|
2020-11-25 21:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 18:48:32 +00:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const updateStatusStats = (data) => ({
|
|
|
|
type: UPDATE_STATUS_STATS,
|
|
|
|
data,
|
|
|
|
})
|