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

349 lines
7.7 KiB
JavaScript
Raw Normal View History

2019-07-02 08:10:25 +01:00
import api from '../api';
import { importFetchedAccounts, importFetchedStatus } from './importer';
2020-01-29 16:45:17 +00:00
import { me } from '../initial_state';
2019-07-02 08:10:25 +01:00
2020-03-04 22:26:01 +00:00
export const REPOST_REQUEST = 'REPOST_REQUEST';
export const REPOST_SUCCESS = 'REPOST_SUCCESS';
export const REPOST_FAIL = 'REPOST_FAIL';
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
export const FAVORITE_REQUEST = 'FAVORITE_REQUEST';
export const FAVORITE_SUCCESS = 'FAVORITE_SUCCESS';
export const FAVORITE_FAIL = 'FAVORITE_FAIL';
2019-07-02 08:10:25 +01:00
2020-03-04 22:26:01 +00:00
export const UNREPOST_REQUEST = 'UNREPOST_REQUEST';
export const UNREPOST_SUCCESS = 'UNREPOST_SUCCESS';
export const UNREPOST_FAIL = 'UNREPOST_FAIL';
2019-07-02 08:10:25 +01:00
2020-03-02 22:26:25 +00:00
export const UNFAVORITE_REQUEST = 'UNFAVORITE_REQUEST';
export const UNFAVORITE_SUCCESS = 'UNFAVORITE_SUCCESS';
export const UNFAVORITE_FAIL = 'UNFAVORITE_FAIL';
2019-07-02 08:10:25 +01:00
2020-03-04 22:26:01 +00:00
export const REPOSTS_FETCH_REQUEST = 'REPOSTS_FETCH_REQUEST';
export const REPOSTS_FETCH_SUCCESS = 'REPOSTS_FETCH_SUCCESS';
export const REPOSTS_FETCH_FAIL = 'REPOSTS_FETCH_FAIL';
2019-07-02 08:10:25 +01:00
export const PIN_REQUEST = 'PIN_REQUEST';
export const PIN_SUCCESS = 'PIN_SUCCESS';
export const PIN_FAIL = 'PIN_FAIL';
export const UNPIN_REQUEST = 'UNPIN_REQUEST';
export const UNPIN_SUCCESS = 'UNPIN_SUCCESS';
export const UNPIN_FAIL = 'UNPIN_FAIL';
2020-04-28 06:33:58 +01:00
export const LIKES_FETCH_REQUEST = 'LIKES_FETCH_REQUEST';
export const LIKES_FETCH_SUCCESS = 'LIKES_FETCH_SUCCESS';
export const LIKES_FETCH_FAIL = 'LIKES_FETCH_FAIL';
2020-03-04 22:26:01 +00:00
export function repost(status) {
2019-07-02 08:10:25 +01:00
return function (dispatch, getState) {
if (!me) return;
2020-03-04 22:26:01 +00:00
dispatch(repostRequest(status));
2019-07-02 08:10:25 +01:00
api(getState).post(`/api/v1/statuses/${status.get('id')}/reblog`).then(function (response) {
// The reblog API method returns a new status wrapped around the original. In this case we are only
// interested in how the original is modified, hence passing it skipping the wrapper
dispatch(importFetchedStatus(response.data.reblog));
2020-03-04 22:26:01 +00:00
dispatch(repostSuccess(status));
2019-07-02 08:10:25 +01:00
}).catch(function (error) {
2020-03-04 22:26:01 +00:00
dispatch(repostFail(status, error));
2019-07-02 08:10:25 +01:00
});
};
};
2020-03-04 22:26:01 +00:00
export function unrepost(status) {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
if (!me) return;
2020-03-04 22:26:01 +00:00
dispatch(unrepostRequest(status));
2019-07-02 08:10:25 +01:00
api(getState).post(`/api/v1/statuses/${status.get('id')}/unreblog`).then(response => {
dispatch(importFetchedStatus(response.data));
2020-03-04 22:26:01 +00:00
dispatch(unrepostSuccess(status));
2019-07-02 08:10:25 +01:00
}).catch(error => {
2020-03-04 22:26:01 +00:00
dispatch(unrepostFail(status, error));
2019-07-02 08:10:25 +01:00
});
};
};
2020-03-04 22:26:01 +00:00
export function repostRequest(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOST_REQUEST,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function repostSuccess(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOST_SUCCESS,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function repostFail(status, error) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOST_FAIL,
2019-07-02 08:10:25 +01:00
status: status,
error: error,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unrepostRequest(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: UNREPOST_REQUEST,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unrepostSuccess(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: UNREPOST_SUCCESS,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unrepostFail(status, error) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: UNREPOST_FAIL,
2019-07-02 08:10:25 +01:00
status: status,
error: error,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function favorite(status) {
2019-07-02 08:10:25 +01:00
return function (dispatch, getState) {
if (!me) return;
2020-03-04 22:26:01 +00:00
dispatch(favoriteRequest(status));
2019-07-02 08:10:25 +01:00
api(getState).post(`/api/v1/statuses/${status.get('id')}/favourite`).then(function (response) {
dispatch(importFetchedStatus(response.data));
2020-03-04 22:26:01 +00:00
dispatch(favoriteSuccess(status));
2019-07-02 08:10:25 +01:00
}).catch(function (error) {
2020-03-04 22:26:01 +00:00
dispatch(favoriteFail(status, error));
2019-07-02 08:10:25 +01:00
});
};
};
2020-03-04 22:26:01 +00:00
export function unfavorite(status) {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
if (!me) return;
2020-03-04 22:26:01 +00:00
dispatch(unfavoriteRequest(status));
2019-07-02 08:10:25 +01:00
api(getState).post(`/api/v1/statuses/${status.get('id')}/unfavourite`).then(response => {
dispatch(importFetchedStatus(response.data));
2020-03-04 22:26:01 +00:00
dispatch(unfavoriteSuccess(status));
2019-07-02 08:10:25 +01:00
}).catch(error => {
2020-03-04 22:26:01 +00:00
dispatch(unfavoriteFail(status, error));
2019-07-02 08:10:25 +01:00
});
};
};
2020-03-04 22:26:01 +00:00
export function favoriteRequest(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: FAVORITE_REQUEST,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function favoriteSuccess(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: FAVORITE_SUCCESS,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function favoriteFail(status, error) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: FAVORITE_FAIL,
2019-07-02 08:10:25 +01:00
status: status,
error: error,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unfavoriteRequest(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: UNFAVORITE_REQUEST,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unfavoriteSuccess(status) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: UNFAVORITE_SUCCESS,
2019-07-02 08:10:25 +01:00
status: status,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function unfavoriteFail(status, error) {
2019-07-02 08:10:25 +01:00
return {
2020-03-02 22:26:25 +00:00
type: UNFAVORITE_FAIL,
2019-07-02 08:10:25 +01:00
status: status,
error: error,
skipLoading: true,
};
};
2020-03-04 22:26:01 +00:00
export function fetchReposts(id) {
2019-07-02 08:10:25 +01:00
return (dispatch, getState) => {
if (!me) return;
2020-03-04 22:26:01 +00:00
dispatch(fetchRepostsRequest(id));
2019-07-02 08:10:25 +01:00
api(getState).get(`/api/v1/statuses/${id}/reblogged_by`).then(response => {
dispatch(importFetchedAccounts(response.data));
2020-03-04 22:26:01 +00:00
dispatch(fetchRepostsSuccess(id, response.data));
2019-07-02 08:10:25 +01:00
}).catch(error => {
2020-03-04 22:26:01 +00:00
dispatch(fetchRepostsFail(id, error));
2019-07-02 08:10:25 +01:00
});
};
};
2020-03-04 22:26:01 +00:00
export function fetchRepostsRequest(id) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOSTS_FETCH_REQUEST,
2019-07-02 08:10:25 +01:00
id,
};
};
2020-03-04 22:26:01 +00:00
export function fetchRepostsSuccess(id, accounts) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOSTS_FETCH_SUCCESS,
2019-07-02 08:10:25 +01:00
id,
accounts,
};
};
2020-03-04 22:26:01 +00:00
export function fetchRepostsFail(id, error) {
2019-07-02 08:10:25 +01:00
return {
2020-03-04 22:26:01 +00:00
type: REPOSTS_FETCH_FAIL,
2019-07-02 08:10:25 +01:00
error,
};
};
export function pin(status) {
return (dispatch, getState) => {
if (!me) return;
dispatch(pinRequest(status));
api(getState).post(`/api/v1/statuses/${status.get('id')}/pin`).then(response => {
dispatch(importFetchedStatus(response.data));
dispatch(pinSuccess(status));
}).catch(error => {
dispatch(pinFail(status, error));
});
};
};
export function pinRequest(status) {
return {
type: PIN_REQUEST,
status,
skipLoading: true,
};
};
export function pinSuccess(status) {
return {
type: PIN_SUCCESS,
status,
skipLoading: true,
};
};
export function pinFail(status, error) {
return {
type: PIN_FAIL,
status,
error,
skipLoading: true,
};
};
export function unpin (status) {
return (dispatch, getState) => {
if (!me) return;
dispatch(unpinRequest(status));
api(getState).post(`/api/v1/statuses/${status.get('id')}/unpin`).then(response => {
dispatch(importFetchedStatus(response.data));
dispatch(unpinSuccess(status));
}).catch(error => {
dispatch(unpinFail(status, error));
});
};
};
export function unpinRequest(status) {
return {
type: UNPIN_REQUEST,
status,
skipLoading: true,
};
};
export function unpinSuccess(status) {
return {
type: UNPIN_SUCCESS,
status,
skipLoading: true,
};
};
export function unpinFail(status, error) {
return {
type: UNPIN_FAIL,
status,
error,
skipLoading: true,
};
};
2020-04-28 06:33:58 +01:00
export function fetchLikes(id) {
return (dispatch, getState) => {
dispatch(fetchLikesRequest(id));
api(getState).get(`/api/v1/statuses/${id}/favourited_by`).then(response => {
dispatch(importFetchedAccounts(response.data));
dispatch(fetchLikesSuccess(id, response.data));
}).catch(error => {
dispatch(fetchLikesFail(id, error));
});
};
};
export function fetchLikesRequest(id) {
return {
type: LIKES_FETCH_REQUEST,
id,
};
};
export function fetchLikesSuccess(id, accounts) {
return {
type: LIKES_FETCH_SUCCESS,
id,
accounts,
};
};
export function fetchLikesFail(id, error) {
return {
type: LIKES_FETCH_FAIL,
error,
};
};