gab-social/app/javascript/gabsocial/containers/status_container.js

315 lines
8.0 KiB
JavaScript
Raw Normal View History

import React from 'react'
import { connect } from 'react-redux'
2020-05-02 07:25:55 +01:00
import { FormattedMessage } from 'react-intl'
import {
Map as ImmutableMap,
List as ImmutableList,
fromJS,
} from 'immutable';
2019-07-02 08:10:25 +01:00
import {
replyCompose,
mentionCompose,
quoteCompose,
2019-07-02 08:10:25 +01:00
} from '../actions/compose';
import {
2020-03-14 17:31:29 +00:00
repost,
2020-03-04 22:26:01 +00:00
favorite,
2020-03-14 17:31:29 +00:00
unrepost,
2020-03-04 22:26:01 +00:00
unfavorite,
2019-07-02 08:10:25 +01:00
} from '../actions/interactions';
import {
hideStatus,
revealStatus,
2020-04-22 06:00:11 +01:00
fetchComments,
2020-05-05 06:16:01 +01:00
fetchContext,
2019-07-02 08:10:25 +01:00
} from '../actions/statuses';
import { openModal } from '../actions/modal';
2020-04-22 06:00:11 +01:00
import { openPopover } from '../actions/popover';
import {
me,
boostModal,
} from '../initial_state'
import {
MODAL_BOOST,
MODAL_CONFIRM,
POPOVER_COMMENT_SORTING_OPTIONS,
COMMENT_SORTING_TYPE_OLDEST,
COMMENT_SORTING_TYPE_NEWEST,
COMMENT_SORTING_TYPE_TOP,
} from '../constants'
2020-05-07 00:40:54 +01:00
import { makeGetStatus } from '../selectors'
import Status from '../components/status';
2019-07-02 08:10:25 +01:00
const sortReplies = (replyIds, state, type) => {
if (!replyIds) return replyIds
if (type === COMMENT_SORTING_TYPE_OLDEST || !type) {
return replyIds // default
} else if (type === COMMENT_SORTING_TYPE_NEWEST) {
return replyIds.reverse()
} else if (type === COMMENT_SORTING_TYPE_TOP) {
let statusList = []
replyIds.forEach((replyId) => {
const status = state.getIn(['statuses', replyId])
if (status) {
statusList.push({
id: replyId,
likeCount: status.get('favourites_count'),
})
}
})
statusList.sort((a, b) => parseFloat(b.likeCount) - parseFloat(a.likeCount))
let newReplyIds = ImmutableList()
for (let i = 0; i < statusList.length; i++) {
const block = statusList[i];
newReplyIds = newReplyIds.set(i, block.id)
}
return newReplyIds
}
return replyIds
}
const getDescendants = (state, status, highlightStatusId, commentSortingType) => {
2020-05-05 06:16:01 +01:00
let descendantsIds = ImmutableList()
let index = 0
const MAX_INDENT = 2
2020-05-05 06:16:01 +01:00
descendantsIds = descendantsIds.withMutations((mutable) => {
const ids = [{
id: status.get('id'),
indent: -1,
}]
2020-05-05 06:16:01 +01:00
while (ids.length > 0) {
let block = ids.shift()
let id = block.id
let indent = block.indent
let replies = state.getIn(['contexts', 'replies', id])
// Sort only Top-level replies (if original status not comment)
if (index === 0) {
replies = sortReplies(replies, state, commentSortingType)
}
2020-05-05 06:16:01 +01:00
if (status.get('id') !== id) {
mutable.push(ImmutableMap({
statusId: id,
indent: indent,
isHighlighted: !!highlightStatusId && highlightStatusId === id,
2020-05-05 06:16:01 +01:00
}))
}
if (replies) {
indent++
indent = Math.min(MAX_INDENT, indent)
replies.reverse().forEach((reply) => {
ids.unshift({
id: reply,
indent: indent,
})
})
2020-05-05 06:16:01 +01:00
}
index++
2020-05-05 06:16:01 +01:00
}
})
return descendantsIds
}
2019-07-02 08:10:25 +01:00
const makeMapStateToProps = () => {
2020-05-02 07:25:55 +01:00
const getStatus = makeGetStatus()
2019-07-02 08:10:25 +01:00
2020-04-22 06:00:11 +01:00
const mapStateToProps = (state, props) => {
2020-04-29 23:32:49 +01:00
const statusId = props.id || props.params.statusId
const username = props.params ? props.params.username : undefined
const commentSortingType = state.getIn(['settings', 'commentSorting'])
2020-04-29 23:32:49 +01:00
const status = getStatus(state, {
id: statusId,
username: username,
})
2019-07-02 08:10:25 +01:00
2020-05-06 05:33:54 +01:00
let fetchedContext = false
2020-04-22 06:00:11 +01:00
let descendantsIds = ImmutableList()
2020-05-04 19:44:37 +01:00
let ancestorStatus
2020-05-05 06:16:01 +01:00
2020-05-06 05:33:54 +01:00
//
if (status && status.get('in_reply_to_account_id') && !props.isChild) {
2020-05-05 06:16:01 +01:00
fetchedContext = true
let inReplyTos = state.getIn(['contexts', 'inReplyTos'])
let ancestorsIds = ImmutableList()
ancestorsIds = ancestorsIds.withMutations(mutable => {
let id = statusId;
while (id) {
mutable.unshift(id)
id = inReplyTos.get(id)
2020-04-22 06:00:11 +01:00
}
})
2020-05-05 06:16:01 +01:00
const ancestorStatusId = ancestorsIds.get(0)
2020-05-06 05:33:54 +01:00
if (ancestorStatusId !== statusId) {
ancestorStatus = getStatus(state, {
id: ancestorStatusId,
})
if (!!ancestorStatus) descendantsIds = getDescendants(state, ancestorStatus, statusId)
2020-05-06 05:33:54 +01:00
}
2020-04-22 06:00:11 +01:00
}
2020-05-05 06:16:01 +01:00
2020-05-06 05:33:54 +01:00
//
2020-05-05 06:16:01 +01:00
if (status && status.get('replies_count') > 0 && !fetchedContext) {
descendantsIds = getDescendants(state, status, null, commentSortingType)
2020-05-04 19:44:37 +01:00
}
2020-05-05 06:16:01 +01:00
const isComment = !!status ? !!status.get('in_reply_to_id') : false
2020-04-22 06:00:11 +01:00
return {
status,
2020-05-04 19:44:37 +01:00
ancestorStatus,
2020-05-05 06:16:01 +01:00
descendantsIds,
isComment,
commentSortingType,
2020-05-07 00:40:54 +01:00
isComposeModalOpen: state.getIn(['modal', 'modalType']) === 'COMPOSE',
2020-04-22 06:00:11 +01:00
}
}
return mapStateToProps
2019-07-02 08:10:25 +01:00
};
2020-05-02 07:25:55 +01:00
const mapDispatchToProps = (dispatch) => ({
onReply (status, router, showModal) {
2020-04-22 06:00:11 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2019-07-02 08:10:25 +01:00
dispatch((_, getState) => {
2020-04-08 02:06:59 +01:00
const state = getState();
2019-07-02 08:10:25 +01:00
if (state.getIn(['compose', 'text']).trim().length !== 0) {
dispatch(openModal('CONFIRM', {
2020-05-02 07:25:55 +01:00
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' />,
2019-07-02 08:10:25 +01:00
onConfirm: () => dispatch(replyCompose(status, router)),
2020-05-02 07:25:55 +01:00
}))
2019-07-02 08:10:25 +01:00
} else {
2020-05-02 07:25:55 +01:00
dispatch(replyCompose(status, router, showModal));
2019-07-02 08:10:25 +01:00
}
2020-05-02 07:25:55 +01:00
})
2019-07-02 08:10:25 +01:00
},
2019-09-17 16:23:51 +01:00
onShowRevisions (status) {
2020-04-22 06:00:11 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2019-09-17 16:23:51 +01:00
dispatch(openModal('STATUS_REVISION', { status }));
},
2020-03-04 22:26:01 +00:00
onFavorite (status) {
2020-04-22 06:00:11 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2019-07-02 08:10:25 +01:00
if (status.get('favourited')) {
2020-03-04 22:26:01 +00:00
dispatch(unfavorite(status));
2019-07-02 08:10:25 +01:00
} else {
2020-03-04 22:26:01 +00:00
dispatch(favorite(status));
2019-07-02 08:10:25 +01:00
}
},
onMention (account, router) {
2020-04-22 06:00:11 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2019-07-02 08:10:25 +01:00
dispatch(mentionCompose(account, router));
},
onOpenMedia (media, index) {
dispatch(openModal('MEDIA', { media, index }));
},
onOpenVideo (media, time) {
dispatch(openModal('VIDEO', { media, time }));
},
onToggleHidden (status) {
2020-04-22 06:00:11 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2019-07-02 08:10:25 +01:00
if (status.get('hidden')) {
dispatch(revealStatus(status.get('id')));
} else {
dispatch(hideStatus(status.get('id')));
}
},
2020-05-03 06:22:49 +01:00
onOpenLikes(status) {
2020-05-09 03:17:19 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2020-05-03 06:22:49 +01:00
dispatch(openModal('STATUS_LIKES', { status }))
},
onOpenReposts(status) {
2020-05-09 03:17:19 +01:00
if (!me) return dispatch(openModal('UNAUTHORIZED'))
2020-05-03 06:22:49 +01:00
dispatch(openModal('STATUS_REPOSTS', { status }))
},
2020-05-09 03:17:19 +01:00
onFetchComments(statusId) {
dispatch(fetchComments(statusId))
},
onFetchContext(statusId) {
dispatch(fetchContext(statusId))
},
2020-05-03 06:22:49 +01:00
onQuote (status, router) {
if (!me) return dispatch(openModal('UNAUTHORIZED'))
dispatch((_, getState) => {
const state = getState()
if (state.getIn(['compose', 'text']).trim().length !== 0) {
dispatch(openModal(MODAL_CONFIRM, {
message: <FormattedMessage id='confirmations.quote.message' defaultMessage='Quoting now will overwrite the message you are currently composing. Are you sure you want to proceed?' />,
confirm: <FormattedMessage id='confirmations.quote.confirm' defaultMessage='Quote' />,
onConfirm: () => dispatch(quoteCompose(status, router)),
}))
} else {
dispatch(quoteCompose(status, router))
}
})
},
onRepost (status) {
if (!me) return dispatch(openModal('UNAUTHORIZED'))
const alreadyReposted = status.get('reblogged')
if (boostModal && !alreadyReposted) {
dispatch(openModal(MODAL_BOOST, {
status,
onRepost: () => dispatch(repost(status)),
}))
} else {
if (alreadyReposted) {
dispatch(unrepost(status))
} else {
dispatch(repost(status))
}
}
},
onCommentSortOpen(targetRef, callback) {
dispatch(openPopover(POPOVER_COMMENT_SORTING_OPTIONS, {
targetRef,
callback,
position: 'top',
}))
}
2019-07-02 08:10:25 +01:00
});
2020-04-23 07:13:29 +01:00
export default connect(makeMapStateToProps, mapDispatchToProps)(Status);