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

563 lines
15 KiB
JavaScript
Raw Normal View History

2020-11-25 21:22:37 +00:00
import api, { getLinks } from '../api'
import {
importFetchedAccounts,
importFetchedStatus,
} from './importer'
import { fetchRelationships } from './accounts'
2020-11-15 18:48:32 +00:00
import { updateStatusStats } from './statuses'
import { me } from '../initial_state'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +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-11-15 18:48:32 +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-11-15 18:48:32 +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-11-15 18:48:32 +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-11-15 18:48:32 +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
2020-11-25 21:22:37 +00:00
export const REPOSTS_EXPAND_REQUEST = 'REPOSTS_EXPAND_REQUEST'
export const REPOSTS_EXPAND_SUCCESS = 'REPOSTS_EXPAND_SUCCESS'
export const REPOSTS_EXPAND_FAIL = 'REPOSTS_EXPAND_FAIL'
2020-11-15 18:48:32 +00:00
export const PIN_REQUEST = 'PIN_REQUEST'
export const PIN_SUCCESS = 'PIN_SUCCESS'
export const PIN_FAIL = 'PIN_FAIL'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const UNPIN_REQUEST = 'UNPIN_REQUEST'
export const UNPIN_SUCCESS = 'UNPIN_SUCCESS'
export const UNPIN_FAIL = 'UNPIN_FAIL'
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
export const IS_PIN_REQUEST = 'IS_PIN_REQUEST'
export const IS_PIN_SUCCESS = 'IS_PIN_SUCCESS'
export const IS_PIN_FAIL = 'IS_PIN_FAIL'
export const BOOKMARK_REQUEST = 'BOOKMARK_REQUEST'
export const BOOKMARK_SUCCESS = 'BOOKMARK_SUCCESS'
export const BOOKMARK_FAIL = 'BOOKMARK_FAIL'
export const UNBOOKMARK_REQUEST = 'UNBOOKMARK_REQUEST'
export const UNBOOKMARK_SUCCESS = 'UNBOOKMARK_SUCCESS'
export const UNBOOKMARK_FAIL = 'UNBOOKMARK_FAIL'
2020-11-25 21:22:37 +00:00
export const IS_BOOKMARK_REQUEST = 'IS_BOOKMARK_REQUEST'
export const IS_BOOKMARK_SUCCESS = 'IS_BOOKMARK_SUCCESS'
export const IS_BOOKMARK_FAIL = 'IS_BOOKMARK_FAIL'
2020-11-15 18:48:32 +00: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-11-25 21:22:37 +00:00
export const LIKES_EXPAND_REQUEST = 'LIKES_EXPAND_REQUEST'
export const LIKES_EXPAND_SUCCESS = 'LIKES_EXPAND_SUCCESS'
export const LIKES_EXPAND_FAIL = 'LIKES_EXPAND_FAIL'
2020-11-15 18:48:32 +00:00
/**
2020-11-25 21:22:37 +00:00
* @description Repost the given status. Set status to status.reblogged:true and
* increment status.reblogs_count by 1 on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const repost = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(repostRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/reblog`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
2020-11-15 18:48:32 +00:00
dispatch(repostSuccess(status))
}).catch((error) => {
dispatch(repostFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const repostRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: REPOST_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const repostSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: REPOST_SUCCESS,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const repostFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: REPOST_FAIL,
2020-11-25 21:22:37 +00:00
status,
error,
2020-11-15 18:48:32 +00:00
})
/**
2020-11-25 21:22:37 +00:00
* @description Unrepost the given status. Set status to status.reblogged:false and
* decrement status.reblogs_count by 1 on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const unrepost = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(unrepostRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/unreblog`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
2020-11-15 18:48:32 +00:00
dispatch(unrepostSuccess(status))
}).catch((error) => {
dispatch(unrepostFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const unrepostRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNREPOST_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unrepostSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNREPOST_SUCCESS,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unrepostFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: UNREPOST_FAIL,
2020-11-25 21:22:37 +00:00
status,
error,
2020-11-15 18:48:32 +00:00
})
/**
2020-11-25 21:22:37 +00:00
* @description Favorite the given status. Set status to status.favourited:true and
* increment status.favourites_count by 1 on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const favorite = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(favoriteRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/favourite`).then((response) => {
dispatch(updateStatusStats(response.data))
2020-11-25 21:22:37 +00:00
dispatch(favoriteSuccess(response.data))
2020-11-15 18:48:32 +00:00
}).catch((error) => {
dispatch(favoriteFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const favoriteRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: FAVORITE_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const favoriteSuccess = (data) => ({
2020-11-15 18:48:32 +00:00
type: FAVORITE_SUCCESS,
2020-11-25 21:22:37 +00:00
data,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const favoriteFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: FAVORITE_FAIL,
2020-11-25 21:22:37 +00:00
status,
error,
2020-11-15 18:48:32 +00:00
})
/**
2020-11-25 21:22:37 +00:00
* @description Unfavorite the given status. Set status to status.favourited:false and
* decrement status.favourites_count by 1 on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const unfavorite = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(unfavoriteRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/unfavourite`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
2020-11-15 18:48:32 +00:00
dispatch(unfavoriteSuccess(status))
}).catch((error) => {
dispatch(unfavoriteFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const unfavoriteRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNFAVORITE_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unfavoriteSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNFAVORITE_SUCCESS,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unfavoriteFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: UNFAVORITE_FAIL,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
error,
})
/**
2020-11-25 21:22:37 +00:00
* @description Pin the given status to your profile. Set status to status.pinned:true
* on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const pin = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(pinRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/pin`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
2020-11-15 18:48:32 +00:00
dispatch(pinSuccess(status))
}).catch((error) => {
dispatch(pinFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const pinRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: PIN_REQUEST,
status,
})
2020-11-25 21:22:37 +00:00
const pinSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: PIN_SUCCESS,
status,
})
2020-11-25 21:22:37 +00:00
const pinFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: PIN_FAIL,
status,
error,
})
/**
2020-11-25 21:22:37 +00:00
* @description Unpin the given status from your profile. Set status to status.pinned:false
* on success and remove from account pins in timeline reducer.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const unpin = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(unpinRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/unpin`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
dispatch(unpinSuccess(status, response.data.account_id))
2020-11-15 18:48:32 +00:00
}).catch((error) => {
dispatch(unpinFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const unpinRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNPIN_REQUEST,
status,
})
2020-11-25 21:22:37 +00:00
const unpinSuccess = (status, accountId) => ({
2020-11-15 18:48:32 +00:00
type: UNPIN_SUCCESS,
2020-11-25 21:22:37 +00:00
accountId,
2020-11-15 18:48:32 +00:00
status,
})
2020-11-25 21:22:37 +00:00
const unpinFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: UNPIN_FAIL,
status,
error,
})
/**
2020-11-25 21:22:37 +00:00
* @description Check if a status is pinned to the current user account.
* @param {String} statusId
2020-11-15 18:48:32 +00:00
*/
2020-11-25 21:22:37 +00:00
export const isPin = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
2020-11-15 18:48:32 +00:00
2020-11-25 21:22:37 +00:00
dispatch(isPinRequest(statusId))
api(getState).get(`/api/v1/statuses/${statusId}/pin`).then((response) => {
dispatch(updateStatusStats(response.data))
dispatch(isPinSuccess(statusId))
2020-11-15 18:48:32 +00:00
}).catch((error) => {
2020-11-25 21:22:37 +00:00
dispatch(isPinFail(statusId, error))
2020-11-15 18:48:32 +00:00
})
}
2020-11-25 21:22:37 +00:00
const isPinRequest = (statusId) => ({
type: IS_PIN_REQUEST,
statusId,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const isPinSuccess = (statusId) => ({
type: IS_PIN_SUCCESS,
statusId,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const isPinFail = (statusId, error) => ({
type: IS_PIN_FAIL,
statusId,
2020-11-15 18:48:32 +00:00
error,
})
/**
2020-11-25 21:22:37 +00:00
* @description Bookmark the given status in your profile if PRO. Set status to
* status.bookmarked:true on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const bookmark = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(bookmarkRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/bookmark`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
dispatch(bookmarkSuccess(status))
2020-11-15 18:48:32 +00:00
}).catch((error) => {
dispatch(bookmarkFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const bookmarkRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARK_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const bookmarkSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARK_SUCCESS,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const bookmarkFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARK_FAIL,
2020-11-25 21:22:37 +00:00
status,
error,
2020-11-15 18:48:32 +00:00
})
/**
2020-11-25 21:22:37 +00:00
* @description Unbookmark the given status in your profile if PRO. Set status to
* status.bookmarked:false on success.
* @param {ImmutableMap} status
2020-11-15 18:48:32 +00:00
*/
export const unbookmark = (status) => (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
if (!me || !status) return
2020-11-15 18:48:32 +00:00
dispatch(unbookmarkRequest(status))
api(getState).post(`/api/v1/statuses/${status.get('id')}/unbookmark`).then((response) => {
2020-11-25 21:22:37 +00:00
dispatch(updateStatusStats(response.data))
dispatch(unbookmarkSuccess(status))
2020-11-15 18:48:32 +00:00
}).catch((error) => {
dispatch(unbookmarkFail(status, error))
})
}
2020-11-25 21:22:37 +00:00
const unbookmarkRequest = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNBOOKMARK_REQUEST,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unbookmarkSuccess = (status) => ({
2020-11-15 18:48:32 +00:00
type: UNBOOKMARK_SUCCESS,
2020-11-25 21:22:37 +00:00
status,
2020-11-15 18:48:32 +00:00
})
2020-11-25 21:22:37 +00:00
const unbookmarkFail = (status, error) => ({
2020-11-15 18:48:32 +00:00
type: UNBOOKMARK_FAIL,
2020-11-25 21:22:37 +00:00
status,
error,
})
/**
* @description Check if a status is bookmarked to the current user account.
* @param {String} statusId
*/
export const isBookmark = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
dispatch(isBookmarkRequest(statusId))
api(getState).get(`/api/v1/statuses/${statusId}/bookmark`).then((response) => {
dispatch(updateStatusStats(response.data))
dispatch(isBookmarkSuccess(statusId))
}).catch((error) => {
dispatch(isBookmarkFail(statusId, error))
})
}
const isBookmarkRequest = (statusId) => ({
type: IS_BOOKMARK_REQUEST,
statusId,
})
const isBookmarkSuccess = (statusId) => ({
type: IS_BOOKMARK_SUCCESS,
statusId,
})
const isBookmarkFail = (statusId, error) => ({
type: IS_BOOKMARK_FAIL,
statusId,
error,
})
/**
* @description Fetch reposts for the given statusId and imports paginated accounts
* and sets in user_lists reducer.
* @param {String} statusId
*/
export const fetchReposts = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
dispatch(fetchRepostsRequest(statusId))
api(getState).get(`/api/v1/statuses/${statusId}/reblogged_by`).then((response) => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedAccounts(response.data))
dispatch(fetchRepostsSuccess(statusId, response.data, next ? next.uri : null))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch((error) => {
dispatch(fetchRepostsFail(statusId, error))
})
}
const fetchRepostsRequest = (statusId) => ({
type: REPOSTS_FETCH_REQUEST,
statusId,
})
const fetchRepostsSuccess = (statusId, accounts, next) => ({
type: REPOSTS_FETCH_SUCCESS,
statusId,
accounts,
next,
})
const fetchRepostsFail = (statusId, error) => ({
type: REPOSTS_FETCH_FAIL,
statusId,
error,
})
/**
* @description Expand reposts for the given statusId and imports paginated accounts
* and sets in user_lists reducer.
* @param {String} statusId
*/
export const expandReposts = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
const url = getState().getIn(['user_lists', 'reblogged_by', statusId, 'next'])
const isLoading = getState().getIn(['user_lists', 'reblogged_by', statusId, 'isLoading'])
if (url === null || isLoading) return
dispatch(expandRepostsRequest(statusId))
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedAccounts(response.data))
dispatch(expandRepostsSuccess(statusId, response.data, next ? next.uri : null))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch(error => dispatch(expandRepostsFail(error)))
}
const expandRepostsRequest = (statusId) => ({
type: REPOSTS_EXPAND_REQUEST,
statusId,
})
const expandRepostsSuccess = (statusId, accounts, next) => ({
type: REPOSTS_EXPAND_SUCCESS,
statusId,
accounts,
next,
})
const expandRepostsFail = (statusId, error) => ({
type: REPOSTS_EXPAND_FAIL,
statusId,
error,
})
/**
* @description Fetch likes for the given statusId and imports paginated accounts
* and sets in user_lists reducer.
* @param {String} statusId
*/
export const fetchLikes = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
dispatch(fetchLikesRequest(statusId))
api(getState).get(`/api/v1/statuses/${statusId}/favourited_by`).then((response) => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedAccounts(response.data))
dispatch(fetchLikesSuccess(statusId, response.data, next ? next.uri : null))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch((error) => {
dispatch(fetchLikesFail(statusId, error))
})
}
const fetchLikesRequest = (statusId) => ({
type: LIKES_FETCH_REQUEST,
statusId,
})
const fetchLikesSuccess = (statusId, accounts, next) => ({
type: LIKES_FETCH_SUCCESS,
statusId,
accounts,
next,
})
const fetchLikesFail = (statusId, error) => ({
type: LIKES_FETCH_FAIL,
statusId,
error,
})
/**
* @description Expand likes for the given statusId and imports paginated accounts
* and sets in user_lists reducer.
* @param {String} statusId
*/
export const expandLikes = (statusId) => (dispatch, getState) => {
if (!me || !statusId) return
const url = getState().getIn(['user_lists', 'liked_by', statusId, 'next'])
const isLoading = getState().getIn(['user_lists', 'liked_by', statusId, 'isLoading'])
if (url === null || isLoading) return
dispatch(expandLikesRequest(statusId))
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedAccounts(response.data))
dispatch(expandLikesSuccess(statusId, response.data, next ? next.uri : null))
dispatch(fetchRelationships(response.data.map(item => item.id)))
}).catch(error => dispatch(expandLikesFail(error)))
}
const expandLikesRequest = (statusId) => ({
type: LIKES_EXPAND_REQUEST,
statusId,
})
const expandLikesSuccess = (statusId, accounts, next) => ({
type: LIKES_EXPAND_SUCCESS,
statusId,
accounts,
next,
})
const expandLikesFail = (statusId, error) => ({
type: LIKES_EXPAND_FAIL,
statusId,
error,
})