Fix #3665 - Refactor timelines reducer (#3686)

* Move ancestors/descendants out of timelines reducer

* Refactor timelines reducer

All types of timelines now have a flat structure and use the same
reducer functions and actions

* Reintroduce some missing behaviours

* Fix wrong import in reports

* Fix includes typo

* Fix issue related to "next" pagination in timelines and notifications

* Fix bug with timeline's initial state, expandNotifications
main
Eugen Rochko 2017-06-11 17:07:35 +02:00 committed by GitHub
parent 85d405c810
commit 47bf7a8047
21 changed files with 216 additions and 593 deletions

View File

@ -29,22 +29,6 @@ export const ACCOUNT_UNMUTE_REQUEST = 'ACCOUNT_UNMUTE_REQUEST';
export const ACCOUNT_UNMUTE_SUCCESS = 'ACCOUNT_UNMUTE_SUCCESS'; export const ACCOUNT_UNMUTE_SUCCESS = 'ACCOUNT_UNMUTE_SUCCESS';
export const ACCOUNT_UNMUTE_FAIL = 'ACCOUNT_UNMUTE_FAIL'; export const ACCOUNT_UNMUTE_FAIL = 'ACCOUNT_UNMUTE_FAIL';
export const ACCOUNT_TIMELINE_FETCH_REQUEST = 'ACCOUNT_TIMELINE_FETCH_REQUEST';
export const ACCOUNT_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_TIMELINE_FETCH_SUCCESS';
export const ACCOUNT_TIMELINE_FETCH_FAIL = 'ACCOUNT_TIMELINE_FETCH_FAIL';
export const ACCOUNT_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_TIMELINE_EXPAND_REQUEST';
export const ACCOUNT_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_TIMELINE_EXPAND_SUCCESS';
export const ACCOUNT_TIMELINE_EXPAND_FAIL = 'ACCOUNT_TIMELINE_EXPAND_FAIL';
export const ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST = 'ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST';
export const ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS';
export const ACCOUNT_MEDIA_TIMELINE_FETCH_FAIL = 'ACCOUNT_MEDIA_TIMELINE_FETCH_FAIL';
export const ACCOUNT_MEDIA_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_MEDIA_TIMELINE_EXPAND_REQUEST';
export const ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS';
export const ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL = 'ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL';
export const FOLLOWERS_FETCH_REQUEST = 'FOLLOWERS_FETCH_REQUEST'; export const FOLLOWERS_FETCH_REQUEST = 'FOLLOWERS_FETCH_REQUEST';
export const FOLLOWERS_FETCH_SUCCESS = 'FOLLOWERS_FETCH_SUCCESS'; export const FOLLOWERS_FETCH_SUCCESS = 'FOLLOWERS_FETCH_SUCCESS';
export const FOLLOWERS_FETCH_FAIL = 'FOLLOWERS_FETCH_FAIL'; export const FOLLOWERS_FETCH_FAIL = 'FOLLOWERS_FETCH_FAIL';
@ -99,99 +83,6 @@ export function fetchAccount(id) {
}; };
}; };
export function fetchAccountTimeline(id, replace = false) {
return (dispatch, getState) => {
const ids = getState().getIn(['timelines', 'accounts_timelines', id, 'items'], Immutable.List());
const newestId = ids.size > 0 ? ids.first() : null;
let params = {};
let skipLoading = false;
replace = replace || newestId === null;
if (!replace) {
params.since_id = newestId;
skipLoading = true;
}
dispatch(fetchAccountTimelineRequest(id, skipLoading));
api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(fetchAccountTimelineSuccess(id, response.data, replace, skipLoading, next));
}).catch(error => {
dispatch(fetchAccountTimelineFail(id, error, skipLoading));
});
};
};
export function fetchAccountMediaTimeline(id, replace = false) {
return (dispatch, getState) => {
const ids = getState().getIn(['timelines', 'accounts_media_timelines', id, 'items'], Immutable.List());
const newestId = ids.size > 0 ? ids.first() : null;
let params = { only_media: 'true', limit: 12 };
let skipLoading = false;
replace = replace || newestId === null;
if (!replace) {
params.since_id = newestId;
skipLoading = true;
}
dispatch(fetchAccountMediaTimelineRequest(id, skipLoading));
api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(fetchAccountMediaTimelineSuccess(id, response.data, replace, skipLoading, next));
}).catch(error => {
dispatch(fetchAccountMediaTimelineFail(id, error, skipLoading));
});
};
};
export function expandAccountTimeline(id) {
return (dispatch, getState) => {
const lastId = getState().getIn(['timelines', 'accounts_timelines', id, 'items'], Immutable.List()).last();
dispatch(expandAccountTimelineRequest(id));
api(getState).get(`/api/v1/accounts/${id}/statuses`, {
params: {
limit: 10,
max_id: lastId,
},
}).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(expandAccountTimelineSuccess(id, response.data, next));
}).catch(error => {
dispatch(expandAccountTimelineFail(id, error));
});
};
};
export function expandAccountMediaTimeline(id) {
return (dispatch, getState) => {
const lastId = getState().getIn(['timelines', 'accounts_media_timelines', id, 'items'], Immutable.List()).last();
dispatch(expandAccountMediaTimelineRequest(id));
api(getState).get(`/api/v1/accounts/${id}/statuses`, {
params: {
limit: 12,
only_media: 'true',
max_id: lastId,
},
}).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(expandAccountMediaTimelineSuccess(id, response.data, next));
}).catch(error => {
dispatch(expandAccountMediaTimelineFail(id, error));
});
};
};
export function fetchAccountRequest(id) { export function fetchAccountRequest(id) {
return { return {
type: ACCOUNT_FETCH_REQUEST, type: ACCOUNT_FETCH_REQUEST,
@ -281,112 +172,6 @@ export function unfollowAccountFail(error) {
}; };
}; };
export function fetchAccountTimelineRequest(id, skipLoading) {
return {
type: ACCOUNT_TIMELINE_FETCH_REQUEST,
id,
skipLoading,
};
};
export function fetchAccountTimelineSuccess(id, statuses, replace, skipLoading, next) {
return {
type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
id,
statuses,
replace,
skipLoading,
next,
};
};
export function fetchAccountTimelineFail(id, error, skipLoading) {
return {
type: ACCOUNT_TIMELINE_FETCH_FAIL,
id,
error,
skipLoading,
skipAlert: error.response.status === 404,
};
};
export function fetchAccountMediaTimelineRequest(id, skipLoading) {
return {
type: ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST,
id,
skipLoading,
};
};
export function fetchAccountMediaTimelineSuccess(id, statuses, replace, skipLoading, next) {
return {
type: ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS,
id,
statuses,
replace,
skipLoading,
next,
};
};
export function fetchAccountMediaTimelineFail(id, error, skipLoading) {
return {
type: ACCOUNT_MEDIA_TIMELINE_FETCH_FAIL,
id,
error,
skipLoading,
skipAlert: error.response.status === 404,
};
};
export function expandAccountTimelineRequest(id) {
return {
type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
id,
};
};
export function expandAccountTimelineSuccess(id, statuses, next) {
return {
type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
id,
statuses,
next,
};
};
export function expandAccountTimelineFail(id, error) {
return {
type: ACCOUNT_TIMELINE_EXPAND_FAIL,
id,
error,
};
};
export function expandAccountMediaTimelineRequest(id) {
return {
type: ACCOUNT_MEDIA_TIMELINE_EXPAND_REQUEST,
id,
};
};
export function expandAccountMediaTimelineSuccess(id, statuses, next) {
return {
type: ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS,
id,
statuses,
next,
};
};
export function expandAccountMediaTimelineFail(id, error) {
return {
type: ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL,
id,
error,
};
};
export function blockAccount(id) { export function blockAccount(id) {
return (dispatch, getState) => { return (dispatch, getState) => {
dispatch(blockAccountRequest(id)); dispatch(blockAccountRequest(id));

View File

@ -124,25 +124,22 @@ export function refreshNotificationsFail(error, skipLoading) {
export function expandNotifications() { export function expandNotifications() {
return (dispatch, getState) => { return (dispatch, getState) => {
const url = getState().getIn(['notifications', 'next'], null); const items = getState().getIn(['notifications', 'items'], Immutable.List());
const lastId = getState().getIn(['notifications', 'items']).last();
if (url === null || getState().getIn(['notifications', 'isLoading'])) { if (getState().getIn(['notifications', 'isLoading']) || items.size === 0) {
return; return;
} }
dispatch(expandNotificationsRequest());
const params = { const params = {
max_id: lastId, max_id: items.last().get('id'),
limit: 20, limit: 20,
exclude_types: excludeTypesFromSettings(getState()),
}; };
params.exclude_types = excludeTypesFromSettings(getState()); dispatch(expandNotificationsRequest());
api(getState).get(url, params).then(response => { api(getState).get('/api/v1/notifications', { params }).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next'); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null)); dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
fetchRelatedRelationships(dispatch, response.data); fetchRelatedRelationships(dispatch, response.data);
}).catch(error => { }).catch(error => {

View File

@ -56,91 +56,89 @@ export function deleteFromTimelines(id) {
}; };
}; };
export function refreshTimelineRequest(timeline, id, skipLoading) { export function refreshTimelineRequest(timeline, skipLoading) {
return { return {
type: TIMELINE_REFRESH_REQUEST, type: TIMELINE_REFRESH_REQUEST,
timeline, timeline,
id,
skipLoading, skipLoading,
}; };
}; };
export function refreshTimeline(timeline, id = null) { export function refreshTimeline(timelineId, path, params = {}) {
return function (dispatch, getState) { return function (dispatch, getState) {
if (getState().getIn(['timelines', timeline, 'isLoading'])) { const timeline = getState().getIn(['timelines', timelineId], Immutable.Map());
if (timeline.get('isLoading') || timeline.get('online')) {
return; return;
} }
const ids = getState().getIn(['timelines', timeline, 'items'], Immutable.List()); const ids = timeline.get('items', Immutable.List());
const newestId = ids.size > 0 ? ids.first() : null; const newestId = ids.size > 0 ? ids.first() : null;
let params = getState().getIn(['timelines', timeline, 'params'], {});
const path = getState().getIn(['timelines', timeline, 'path'])(id);
let skipLoading = false; let skipLoading = timeline.get('loaded');
if (newestId !== null && getState().getIn(['timelines', timeline, 'loaded']) && (id === null || getState().getIn(['timelines', timeline, 'id']) === id)) { if (newestId !== null) {
if (id === null && getState().getIn(['timelines', timeline, 'online'])) { params.since_id = newestId;
// Skip refreshing when timeline is live anyway
return;
}
params = { ...params, since_id: newestId };
skipLoading = true;
} else if (getState().getIn(['timelines', timeline, 'loaded'])) {
skipLoading = true;
} }
dispatch(refreshTimelineRequest(timeline, id, skipLoading)); dispatch(refreshTimelineRequest(timelineId, skipLoading));
api(getState).get(path, { params }).then(response => { api(getState).get(path, { params }).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next'); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(refreshTimelineSuccess(timeline, response.data, skipLoading, next ? next.uri : null)); dispatch(refreshTimelineSuccess(timelineId, response.data, skipLoading, next ? next.uri : null));
}).catch(error => { }).catch(error => {
dispatch(refreshTimelineFail(timeline, error, skipLoading)); dispatch(refreshTimelineFail(timelineId, error, skipLoading));
}); });
}; };
}; };
export const refreshHomeTimeline = () => refreshTimeline('home', '/api/v1/timelines/home');
export const refreshPublicTimeline = () => refreshTimeline('public', '/api/v1/timelines/public');
export const refreshCommunityTimeline = () => refreshTimeline('community', '/api/v1/timelines/public', { local: true });
export const refreshAccountTimeline = accountId => refreshTimeline(`account:${accountId}`, `/api/v1/accounts/${accountId}/statuses`);
export const refreshAccountMediaTimeline = accountId => refreshTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { only_media: true });
export const refreshHashtagTimeline = hashtag => refreshTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`);
export function refreshTimelineFail(timeline, error, skipLoading) { export function refreshTimelineFail(timeline, error, skipLoading) {
return { return {
type: TIMELINE_REFRESH_FAIL, type: TIMELINE_REFRESH_FAIL,
timeline, timeline,
error, error,
skipLoading, skipLoading,
skipAlert: error.response.status === 404,
}; };
}; };
export function expandTimeline(timeline) { export function expandTimeline(timelineId, path, params = {}) {
return (dispatch, getState) => { return (dispatch, getState) => {
if (getState().getIn(['timelines', timeline, 'isLoading'])) { const timeline = getState().getIn(['timelines', timelineId], Immutable.Map());
const ids = timeline.get('items', Immutable.List());
if (timeline.get('isLoading') || ids.size === 0) {
return; return;
} }
if (getState().getIn(['timelines', timeline, 'items']).size === 0) { params.max_id = ids.last();
return; params.limit = 10;
}
const path = getState().getIn(['timelines', timeline, 'path'])(getState().getIn(['timelines', timeline, 'id'])); dispatch(expandTimelineRequest(timelineId));
const params = getState().getIn(['timelines', timeline, 'params'], {});
const lastId = getState().getIn(['timelines', timeline, 'items']).last();
dispatch(expandTimelineRequest(timeline)); api(getState).get(path, { params }).then(response => {
api(getState).get(path, {
params: {
...params,
max_id: lastId,
limit: 10,
},
}).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next'); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(expandTimelineSuccess(timeline, response.data, next ? next.uri : null)); dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null));
}).catch(error => { }).catch(error => {
dispatch(expandTimelineFail(timeline, error)); dispatch(expandTimelineFail(timelineId, error));
}); });
}; };
}; };
export const expandHomeTimeline = () => expandTimeline('home', '/api/v1/timelines/home');
export const expandPublicTimeline = () => expandTimeline('public', '/api/v1/timelines/public');
export const expandCommunityTimeline = () => expandTimeline('community', '/api/v1/timelines/public', { local: true });
export const expandAccountTimeline = accountId => expandTimeline(`account:${accountId}`, `/api/v1/accounts/${accountId}/statuses`);
export const expandAccountMediaTimeline = accountId => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { only_media: true });
export const expandHashtagTimeline = hashtag => expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`);
export function expandTimelineRequest(timeline) { export function expandTimelineRequest(timeline) {
return { return {
type: TIMELINE_EXPAND_REQUEST, type: TIMELINE_EXPAND_REQUEST,

View File

@ -6,7 +6,7 @@ import {
refreshTimelineSuccess, refreshTimelineSuccess,
updateTimeline, updateTimeline,
deleteFromTimelines, deleteFromTimelines,
refreshTimeline, refreshHomeTimeline,
connectTimeline, connectTimeline,
disconnectTimeline, disconnectTimeline,
} from '../actions/timelines'; } from '../actions/timelines';
@ -65,7 +65,7 @@ class Mastodon extends React.PureComponent {
const setupPolling = () => { const setupPolling = () => {
this.polling = setInterval(() => { this.polling = setInterval(() => {
store.dispatch(refreshTimeline('home')); store.dispatch(refreshHomeTimeline());
store.dispatch(refreshNotifications()); store.dispatch(refreshNotifications());
}, 20000); }, 20000);
}; };
@ -104,7 +104,7 @@ class Mastodon extends React.PureComponent {
reconnected () { reconnected () {
clearPolling(); clearPolling();
store.dispatch(connectTimeline('home')); store.dispatch(connectTimeline('home'));
store.dispatch(refreshTimeline('home')); store.dispatch(refreshHomeTimeline());
store.dispatch(refreshNotifications()); store.dispatch(refreshNotifications());
}, },

View File

@ -2,11 +2,8 @@ import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { import { fetchAccount } from '../../actions/accounts';
fetchAccount, import { refreshAccountMediaTimeline, expandAccountMediaTimeline } from '../../actions/timelines';
fetchAccountMediaTimeline,
expandAccountMediaTimeline,
} from '../../actions/accounts';
import LoadingIndicator from '../../components/loading_indicator'; import LoadingIndicator from '../../components/loading_indicator';
import Column from '../ui/components/column'; import Column from '../ui/components/column';
import ColumnBackButton from '../../components/column_back_button'; import ColumnBackButton from '../../components/column_back_button';
@ -21,8 +18,8 @@ import LoadMore from '../../components/load_more';
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
medias: getAccountGallery(state, Number(props.params.accountId)), medias: getAccountGallery(state, Number(props.params.accountId)),
isLoading: state.getIn(['timelines', 'accounts_media_timelines', Number(props.params.accountId), 'isLoading']), isLoading: state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'isLoading']),
hasMore: !!state.getIn(['timelines', 'accounts_media_timelines', Number(props.params.accountId), 'next']), hasMore: !!state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'next']),
autoPlayGif: state.getIn(['meta', 'auto_play_gif']), autoPlayGif: state.getIn(['meta', 'auto_play_gif']),
}); });
@ -39,13 +36,13 @@ class AccountGallery extends ImmutablePureComponent {
componentDidMount () { componentDidMount () {
this.props.dispatch(fetchAccount(Number(this.props.params.accountId))); this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
this.props.dispatch(fetchAccountMediaTimeline(Number(this.props.params.accountId))); this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) { if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
this.props.dispatch(fetchAccount(Number(nextProps.params.accountId))); this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
this.props.dispatch(fetchAccountMediaTimeline(Number(this.props.params.accountId))); this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
} }
} }

View File

@ -2,11 +2,8 @@ import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { import { fetchAccount } from '../../actions/accounts';
fetchAccount, import { refreshAccountTimeline, expandAccountTimeline } from '../../actions/timelines';
fetchAccountTimeline,
expandAccountTimeline,
} from '../../actions/accounts';
import StatusList from '../../components/status_list'; import StatusList from '../../components/status_list';
import LoadingIndicator from '../../components/loading_indicator'; import LoadingIndicator from '../../components/loading_indicator';
import Column from '../ui/components/column'; import Column from '../ui/components/column';
@ -16,9 +13,9 @@ import Immutable from 'immutable';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
statusIds: state.getIn(['timelines', 'accounts_timelines', Number(props.params.accountId), 'items'], Immutable.List()), statusIds: state.getIn(['timelines', `account:${Number(props.params.accountId)}`, 'items'], Immutable.List()),
isLoading: state.getIn(['timelines', 'accounts_timelines', Number(props.params.accountId), 'isLoading']), isLoading: state.getIn(['timelines', `account:${Number(props.params.accountId)}`, 'isLoading']),
hasMore: !!state.getIn(['timelines', 'accounts_timelines', Number(props.params.accountId), 'next']), hasMore: !!state.getIn(['timelines', `account:${Number(props.params.accountId)}`, 'next']),
me: state.getIn(['meta', 'me']), me: state.getIn(['meta', 'me']),
}); });
@ -35,13 +32,13 @@ class AccountTimeline extends ImmutablePureComponent {
componentWillMount () { componentWillMount () {
this.props.dispatch(fetchAccount(Number(this.props.params.accountId))); this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
this.props.dispatch(fetchAccountTimeline(Number(this.props.params.accountId))); this.props.dispatch(refreshAccountTimeline(Number(this.props.params.accountId)));
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) { if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
this.props.dispatch(fetchAccount(Number(nextProps.params.accountId))); this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
this.props.dispatch(fetchAccountTimeline(Number(nextProps.params.accountId))); this.props.dispatch(refreshAccountTimeline(Number(nextProps.params.accountId)));
} }
} }

View File

@ -5,7 +5,8 @@ import StatusListContainer from '../ui/containers/status_list_container';
import Column from '../../components/column'; import Column from '../../components/column';
import ColumnHeader from '../../components/column_header'; import ColumnHeader from '../../components/column_header';
import { import {
refreshTimeline, refreshCommunityTimeline,
expandCommunityTimeline,
updateTimeline, updateTimeline,
deleteFromTimelines, deleteFromTimelines,
connectTimeline, connectTimeline,
@ -61,7 +62,7 @@ class CommunityTimeline extends React.PureComponent {
componentDidMount () { componentDidMount () {
const { dispatch, streamingAPIBaseURL, accessToken } = this.props; const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
dispatch(refreshTimeline('community')); dispatch(refreshCommunityTimeline());
if (typeof this._subscription !== 'undefined') { if (typeof this._subscription !== 'undefined') {
return; return;
@ -106,6 +107,10 @@ class CommunityTimeline extends React.PureComponent {
this.column = c; this.column = c;
} }
handleLoadMore = () => {
this.props.dispatch(expandCommunityTimeline());
}
render () { render () {
const { intl, hasUnread, columnId, multiColumn } = this.props; const { intl, hasUnread, columnId, multiColumn } = this.props;
const pinned = !!columnId; const pinned = !!columnId;
@ -126,10 +131,10 @@ class CommunityTimeline extends React.PureComponent {
</ColumnHeader> </ColumnHeader>
<StatusListContainer <StatusListContainer
{...this.props}
trackScroll={!pinned} trackScroll={!pinned}
scrollKey={`community_timeline-${columnId}`} scrollKey={`community_timeline-${columnId}`}
type='community' timelineId='community'
loadMore={this.handleLoadMore}
emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />} emptyMessage={<FormattedMessage id='empty_column.community' defaultMessage='The local timeline is empty. Write something publicly to get the ball rolling!' />}
/> />
</Column> </Column>

View File

@ -5,7 +5,8 @@ import StatusListContainer from '../ui/containers/status_list_container';
import Column from '../../components/column'; import Column from '../../components/column';
import ColumnHeader from '../../components/column_header'; import ColumnHeader from '../../components/column_header';
import { import {
refreshTimeline, refreshHashtagTimeline,
expandHashtagTimeline,
updateTimeline, updateTimeline,
deleteFromTimelines, deleteFromTimelines,
} from '../../actions/timelines'; } from '../../actions/timelines';
@ -81,13 +82,13 @@ class HashtagTimeline extends React.PureComponent {
const { dispatch } = this.props; const { dispatch } = this.props;
const { id } = this.props.params; const { id } = this.props.params;
dispatch(refreshTimeline('tag', id)); dispatch(refreshHashtagTimeline(id));
this._subscribe(dispatch, id); this._subscribe(dispatch, id);
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (nextProps.params.id !== this.props.params.id) { if (nextProps.params.id !== this.props.params.id) {
this.props.dispatch(refreshTimeline('tag', nextProps.params.id)); this.props.dispatch(refreshHashtagTimeline(nextProps.params.id));
this._unsubscribe(); this._unsubscribe();
this._subscribe(this.props.dispatch, nextProps.params.id); this._subscribe(this.props.dispatch, nextProps.params.id);
} }
@ -101,6 +102,10 @@ class HashtagTimeline extends React.PureComponent {
this.column = c; this.column = c;
} }
handleLoadMore = () => {
this.props.dispatch(expandHashtagTimeline(this.props.params.id));
}
render () { render () {
const { hasUnread, columnId, multiColumn } = this.props; const { hasUnread, columnId, multiColumn } = this.props;
const { id } = this.props.params; const { id } = this.props.params;
@ -123,8 +128,8 @@ class HashtagTimeline extends React.PureComponent {
<StatusListContainer <StatusListContainer
trackScroll={!pinned} trackScroll={!pinned}
scrollKey={`hashtag_timeline-${columnId}`} scrollKey={`hashtag_timeline-${columnId}`}
type='tag' timelineId={`hashtag:${id}`}
id={id} loadMore={this.handleLoadMore}
emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />} emptyMessage={<FormattedMessage id='empty_column.hashtag' defaultMessage='There is nothing in this hashtag yet.' />}
/> />
</Column> </Column>

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { expandHomeTimeline } from '../../actions/timelines';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import StatusListContainer from '../ui/containers/status_list_container'; import StatusListContainer from '../ui/containers/status_list_container';
import Column from '../../components/column'; import Column from '../../components/column';
@ -52,6 +53,10 @@ class HomeTimeline extends React.PureComponent {
this.column = c; this.column = c;
} }
handleLoadMore = () => {
this.props.dispatch(expandHomeTimeline());
}
render () { render () {
const { intl, hasUnread, hasFollows, columnId, multiColumn } = this.props; const { intl, hasUnread, hasFollows, columnId, multiColumn } = this.props;
const pinned = !!columnId; const pinned = !!columnId;
@ -80,10 +85,10 @@ class HomeTimeline extends React.PureComponent {
</ColumnHeader> </ColumnHeader>
<StatusListContainer <StatusListContainer
{...this.props}
trackScroll={!pinned} trackScroll={!pinned}
scrollKey={`home_timeline-${columnId}`} scrollKey={`home_timeline-${columnId}`}
type='home' loadMore={this.handleLoadMore}
timelineId='home'
emptyMessage={emptyMessage} emptyMessage={emptyMessage}
/> />
</Column> </Column>

View File

@ -5,7 +5,8 @@ import StatusListContainer from '../ui/containers/status_list_container';
import Column from '../../components/column'; import Column from '../../components/column';
import ColumnHeader from '../../components/column_header'; import ColumnHeader from '../../components/column_header';
import { import {
refreshTimeline, refreshPublicTimeline,
expandPublicTimeline,
updateTimeline, updateTimeline,
deleteFromTimelines, deleteFromTimelines,
connectTimeline, connectTimeline,
@ -61,7 +62,7 @@ class PublicTimeline extends React.PureComponent {
componentDidMount () { componentDidMount () {
const { dispatch, streamingAPIBaseURL, accessToken } = this.props; const { dispatch, streamingAPIBaseURL, accessToken } = this.props;
dispatch(refreshTimeline('public')); dispatch(refreshPublicTimeline());
if (typeof this._subscription !== 'undefined') { if (typeof this._subscription !== 'undefined') {
return; return;
@ -106,6 +107,10 @@ class PublicTimeline extends React.PureComponent {
this.column = c; this.column = c;
} }
handleLoadMore = () => {
this.props.dispatch(expandPublicTimeline());
}
render () { render () {
const { intl, columnId, hasUnread, multiColumn } = this.props; const { intl, columnId, hasUnread, multiColumn } = this.props;
const pinned = !!columnId; const pinned = !!columnId;
@ -126,8 +131,8 @@ class PublicTimeline extends React.PureComponent {
</ColumnHeader> </ColumnHeader>
<StatusListContainer <StatusListContainer
{...this.props} timelineId='public'
type='public' loadMore={this.handleLoadMore}
trackScroll={!pinned} trackScroll={!pinned}
scrollKey={`public_timeline-${columnId}`} scrollKey={`public_timeline-${columnId}`}
emptyMessage={<FormattedMessage id='empty_column.public' defaultMessage='There is nothing here! Write something publicly, or manually follow users from other instances to fill it up' />} emptyMessage={<FormattedMessage id='empty_column.public' defaultMessage='There is nothing here! Write something publicly, or manually follow users from other instances to fill it up' />}

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { cancelReport, changeReportComment, submitReport } from '../../actions/reports'; import { cancelReport, changeReportComment, submitReport } from '../../actions/reports';
import { fetchAccountTimeline } from '../../actions/accounts'; import { refreshAccountTimeline } from '../../actions/timelines';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import Column from '../ui/components/column'; import Column from '../ui/components/column';
@ -28,7 +28,7 @@ const makeMapStateToProps = () => {
isSubmitting: state.getIn(['reports', 'new', 'isSubmitting']), isSubmitting: state.getIn(['reports', 'new', 'isSubmitting']),
account: getAccount(state, accountId), account: getAccount(state, accountId),
comment: state.getIn(['reports', 'new', 'comment']), comment: state.getIn(['reports', 'new', 'comment']),
statusIds: Immutable.OrderedSet(state.getIn(['timelines', 'accounts_timelines', accountId, 'items'])).union(state.getIn(['reports', 'new', 'status_ids'])), statusIds: Immutable.OrderedSet(state.getIn(['timelines', `account:${accountId}`, 'items'])).union(state.getIn(['reports', 'new', 'status_ids'])),
}; };
}; };
@ -61,12 +61,12 @@ class Report extends React.PureComponent {
return; return;
} }
this.props.dispatch(fetchAccountTimeline(this.props.account.get('id'))); this.props.dispatch(refreshAccountTimeline(this.props.account.get('id')));
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (this.props.account !== nextProps.account && nextProps.account) { if (this.props.account !== nextProps.account && nextProps.account) {
this.props.dispatch(fetchAccountTimeline(nextProps.account.get('id'))); this.props.dispatch(refreshAccountTimeline(nextProps.account.get('id')));
} }
} }

View File

@ -44,8 +44,8 @@ const makeMapStateToProps = () => {
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
status: getStatus(state, Number(props.params.statusId)), status: getStatus(state, Number(props.params.statusId)),
ancestorsIds: state.getIn(['timelines', 'ancestors', Number(props.params.statusId)]), ancestorsIds: state.getIn(['contexts', 'ancestors', Number(props.params.statusId)]),
descendantsIds: state.getIn(['timelines', 'descendants', Number(props.params.statusId)]), descendantsIds: state.getIn(['contexts', 'descendants', Number(props.params.statusId)]),
me: state.getIn(['meta', 'me']), me: state.getIn(['meta', 'me']),
boostModal: state.getIn(['meta', 'boost_modal']), boostModal: state.getIn(['meta', 'boost_modal']),
deleteModal: state.getIn(['meta', 'delete_modal']), deleteModal: state.getIn(['meta', 'delete_modal']),

View File

@ -1,6 +1,6 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import StatusList from '../../../components/status_list'; import StatusList from '../../../components/status_list';
import { expandTimeline, scrollTopTimeline } from '../../../actions/timelines'; import { scrollTopTimeline } from '../../../actions/timelines';
import Immutable from 'immutable'; import Immutable from 'immutable';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
@ -39,31 +39,29 @@ const makeGetStatusIds = () => createSelector([
const makeMapStateToProps = () => { const makeMapStateToProps = () => {
const getStatusIds = makeGetStatusIds(); const getStatusIds = makeGetStatusIds();
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, { timelineId }) => ({
scrollKey: props.scrollKey, statusIds: getStatusIds(state, { type: timelineId }),
shouldUpdateScroll: props.shouldUpdateScroll, isLoading: state.getIn(['timelines', timelineId, 'isLoading'], true),
statusIds: getStatusIds(state, props), isUnread: state.getIn(['timelines', timelineId, 'unread']) > 0,
isLoading: state.getIn(['timelines', props.type, 'isLoading'], true), hasMore: !!state.getIn(['timelines', timelineId, 'next']),
isUnread: state.getIn(['timelines', props.type, 'unread']) > 0,
hasMore: !!state.getIn(['timelines', props.type, 'next']),
}); });
return mapStateToProps; return mapStateToProps;
}; };
const mapDispatchToProps = (dispatch, { type, id }) => ({ const mapDispatchToProps = (dispatch, { timelineId, loadMore }) => ({
onScrollToBottom: debounce(() => { onScrollToBottom: debounce(() => {
dispatch(scrollTopTimeline(type, false)); dispatch(scrollTopTimeline(timelineId, false));
dispatch(expandTimeline(type, id)); loadMore();
}, 300, { leading: true }), }, 300, { leading: true }),
onScrollToTop: debounce(() => { onScrollToTop: debounce(() => {
dispatch(scrollTopTimeline(type, true)); dispatch(scrollTopTimeline(timelineId, true));
}, 100), }, 100),
onScroll: debounce(() => { onScroll: debounce(() => {
dispatch(scrollTopTimeline(type, false)); dispatch(scrollTopTimeline(timelineId, false));
}, 100), }, 100),
}); });

View File

@ -8,7 +8,7 @@ import { connect } from 'react-redux';
import { isMobile } from '../../is_mobile'; import { isMobile } from '../../is_mobile';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { uploadCompose } from '../../actions/compose'; import { uploadCompose } from '../../actions/compose';
import { refreshTimeline } from '../../actions/timelines'; import { refreshHomeTimeline } from '../../actions/timelines';
import { refreshNotifications } from '../../actions/notifications'; import { refreshNotifications } from '../../actions/notifications';
import UploadArea from './components/upload_area'; import UploadArea from './components/upload_area';
import ColumnsAreaContainer from './containers/columns_area_container'; import ColumnsAreaContainer from './containers/columns_area_container';
@ -95,7 +95,7 @@ class UI extends React.PureComponent {
document.addEventListener('dragleave', this.handleDragLeave, false); document.addEventListener('dragleave', this.handleDragLeave, false);
document.addEventListener('dragend', this.handleDragEnd, false); document.addEventListener('dragend', this.handleDragEnd, false);
this.props.dispatch(refreshTimeline('home')); this.props.dispatch(refreshHomeTimeline());
this.props.dispatch(refreshNotifications()); this.props.dispatch(refreshNotifications());
} }

View File

@ -4,8 +4,6 @@ import {
FOLLOWERS_EXPAND_SUCCESS, FOLLOWERS_EXPAND_SUCCESS,
FOLLOWING_FETCH_SUCCESS, FOLLOWING_FETCH_SUCCESS,
FOLLOWING_EXPAND_SUCCESS, FOLLOWING_EXPAND_SUCCESS,
ACCOUNT_TIMELINE_FETCH_SUCCESS,
ACCOUNT_TIMELINE_EXPAND_SUCCESS,
FOLLOW_REQUESTS_FETCH_SUCCESS, FOLLOW_REQUESTS_FETCH_SUCCESS,
FOLLOW_REQUESTS_EXPAND_SUCCESS, FOLLOW_REQUESTS_EXPAND_SUCCESS,
} from '../actions/accounts'; } from '../actions/accounts';
@ -113,8 +111,6 @@ export default function accounts(state = initialState, action) {
return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses); return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
case TIMELINE_REFRESH_SUCCESS: case TIMELINE_REFRESH_SUCCESS:
case TIMELINE_EXPAND_SUCCESS: case TIMELINE_EXPAND_SUCCESS:
case ACCOUNT_TIMELINE_FETCH_SUCCESS:
case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
case CONTEXT_FETCH_SUCCESS: case CONTEXT_FETCH_SUCCESS:
case FAVOURITED_STATUSES_FETCH_SUCCESS: case FAVOURITED_STATUSES_FETCH_SUCCESS:
case FAVOURITED_STATUSES_EXPAND_SUCCESS: case FAVOURITED_STATUSES_EXPAND_SUCCESS:

View File

@ -4,8 +4,6 @@ import {
FOLLOWERS_EXPAND_SUCCESS, FOLLOWERS_EXPAND_SUCCESS,
FOLLOWING_FETCH_SUCCESS, FOLLOWING_FETCH_SUCCESS,
FOLLOWING_EXPAND_SUCCESS, FOLLOWING_EXPAND_SUCCESS,
ACCOUNT_TIMELINE_FETCH_SUCCESS,
ACCOUNT_TIMELINE_EXPAND_SUCCESS,
FOLLOW_REQUESTS_FETCH_SUCCESS, FOLLOW_REQUESTS_FETCH_SUCCESS,
FOLLOW_REQUESTS_EXPAND_SUCCESS, FOLLOW_REQUESTS_EXPAND_SUCCESS,
ACCOUNT_FOLLOW_SUCCESS, ACCOUNT_FOLLOW_SUCCESS,
@ -115,8 +113,6 @@ export default function accountsCounters(state = initialState, action) {
return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses); return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
case TIMELINE_REFRESH_SUCCESS: case TIMELINE_REFRESH_SUCCESS:
case TIMELINE_EXPAND_SUCCESS: case TIMELINE_EXPAND_SUCCESS:
case ACCOUNT_TIMELINE_FETCH_SUCCESS:
case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
case CONTEXT_FETCH_SUCCESS: case CONTEXT_FETCH_SUCCESS:
case FAVOURITED_STATUSES_FETCH_SUCCESS: case FAVOURITED_STATUSES_FETCH_SUCCESS:
case FAVOURITED_STATUSES_EXPAND_SUCCESS: case FAVOURITED_STATUSES_EXPAND_SUCCESS:

View File

@ -0,0 +1,43 @@
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
import { TIMELINE_DELETE } from '../actions/timelines';
import Immutable from 'immutable';
const initialState = Immutable.Map({
ancestors: Immutable.Map(),
descendants: Immutable.Map(),
});
const normalizeContext = (state, id, ancestors, descendants) => {
const ancestorsIds = ancestors.map(ancestor => ancestor.get('id'));
const descendantsIds = descendants.map(descendant => descendant.get('id'));
return state.withMutations(map => {
map.setIn(['ancestors', id], ancestorsIds);
map.setIn(['descendants', id], descendantsIds);
});
};
const deleteFromContexts = (state, id) => {
state.getIn(['descendants', id], Immutable.List()).forEach(descendantId => {
state = state.updateIn(['ancestors', descendantId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
});
state.getIn(['ancestors', id], Immutable.List()).forEach(ancestorId => {
state = state.updateIn(['descendants', ancestorId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
});
state = state.deleteIn(['descendants', id]).deleteIn(['ancestors', id]);
return state;
};
export default function contexts(state = initialState, action) {
switch(action.type) {
case CONTEXT_FETCH_SUCCESS:
return normalizeContext(state, action.id, Immutable.fromJS(action.ancestors), Immutable.fromJS(action.descendants));
case TIMELINE_DELETE:
return deleteFromContexts(state, action.id);
default:
return state;
}
};

View File

@ -17,6 +17,7 @@ import settings from './settings';
import status_lists from './status_lists'; import status_lists from './status_lists';
import cards from './cards'; import cards from './cards';
import reports from './reports'; import reports from './reports';
import contexts from './contexts';
export default combineReducers({ export default combineReducers({
timelines, timelines,
@ -37,4 +38,5 @@ export default combineReducers({
settings, settings,
cards, cards,
reports, reports,
contexts,
}); });

View File

@ -21,10 +21,6 @@ import {
TIMELINE_EXPAND_SUCCESS, TIMELINE_EXPAND_SUCCESS,
} from '../actions/timelines'; } from '../actions/timelines';
import { import {
ACCOUNT_TIMELINE_FETCH_SUCCESS,
ACCOUNT_TIMELINE_EXPAND_SUCCESS,
ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS,
ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS,
ACCOUNT_BLOCK_SUCCESS, ACCOUNT_BLOCK_SUCCESS,
} from '../actions/accounts'; } from '../actions/accounts';
import { import {
@ -113,10 +109,6 @@ export default function statuses(state = initialState, action) {
return state.setIn([action.id, 'muted'], false); return state.setIn([action.id, 'muted'], false);
case TIMELINE_REFRESH_SUCCESS: case TIMELINE_REFRESH_SUCCESS:
case TIMELINE_EXPAND_SUCCESS: case TIMELINE_EXPAND_SUCCESS:
case ACCOUNT_TIMELINE_FETCH_SUCCESS:
case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
case ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS:
case ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS:
case CONTEXT_FETCH_SUCCESS: case CONTEXT_FETCH_SUCCESS:
case NOTIFICATIONS_REFRESH_SUCCESS: case NOTIFICATIONS_REFRESH_SUCCESS:
case NOTIFICATIONS_EXPAND_SUCCESS: case NOTIFICATIONS_EXPAND_SUCCESS:

View File

@ -18,228 +18,79 @@ import {
UNFAVOURITE_SUCCESS, UNFAVOURITE_SUCCESS,
} from '../actions/interactions'; } from '../actions/interactions';
import { import {
ACCOUNT_TIMELINE_FETCH_REQUEST,
ACCOUNT_TIMELINE_FETCH_SUCCESS,
ACCOUNT_TIMELINE_FETCH_FAIL,
ACCOUNT_TIMELINE_EXPAND_REQUEST,
ACCOUNT_TIMELINE_EXPAND_SUCCESS,
ACCOUNT_TIMELINE_EXPAND_FAIL,
ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST,
ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS,
ACCOUNT_MEDIA_TIMELINE_FETCH_FAIL,
ACCOUNT_MEDIA_TIMELINE_EXPAND_REQUEST,
ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS,
ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL,
ACCOUNT_BLOCK_SUCCESS, ACCOUNT_BLOCK_SUCCESS,
ACCOUNT_MUTE_SUCCESS, ACCOUNT_MUTE_SUCCESS,
} from '../actions/accounts'; } from '../actions/accounts';
import {
CONTEXT_FETCH_SUCCESS,
} from '../actions/statuses';
import Immutable from 'immutable'; import Immutable from 'immutable';
const initialState = Immutable.Map({ const initialState = Immutable.Map();
home: Immutable.Map({
path: () => '/api/v1/timelines/home',
next: null,
isLoading: false,
online: false,
loaded: false,
top: true,
unread: 0,
items: Immutable.List(),
}),
public: Immutable.Map({ const initialTimeline = Immutable.Map({
path: () => '/api/v1/timelines/public', unread: 0,
next: null, online: false,
isLoading: false, top: true,
online: false, loaded: false,
loaded: false, isLoading: false,
top: true, next: false,
unread: 0, items: Immutable.List(),
items: Immutable.List(),
}),
community: Immutable.Map({
path: () => '/api/v1/timelines/public',
next: null,
params: { local: true },
isLoading: false,
online: false,
loaded: false,
top: true,
unread: 0,
items: Immutable.List(),
}),
tag: Immutable.Map({
path: (id) => `/api/v1/timelines/tag/${id}`,
next: null,
isLoading: false,
id: null,
loaded: false,
top: true,
unread: 0,
items: Immutable.List(),
}),
accounts_timelines: Immutable.Map(),
accounts_media_timelines: Immutable.Map(),
ancestors: Immutable.Map(),
descendants: Immutable.Map(),
}); });
const normalizeStatus = (state, status) => {
return state;
};
const normalizeTimeline = (state, timeline, statuses, next) => { const normalizeTimeline = (state, timeline, statuses, next) => {
let ids = Immutable.List(); const ids = Immutable.List(statuses.map(status => status.get('id')));
const loaded = state.getIn([timeline, 'loaded']); const wasLoaded = state.getIn([timeline, 'loaded']);
const hadNext = state.getIn([timeline, 'next']);
const oldIds = state.getIn([timeline, 'items'], Immutable.List());
statuses.forEach((status, i) => { return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
state = normalizeStatus(state, status); mMap.set('loaded', true);
ids = ids.set(i, status.get('id')); mMap.set('isLoading', false);
}); if (!hadNext) mMap.set('next', next);
mMap.set('items', wasLoaded ? ids.concat(oldIds) : ids);
state = state.setIn([timeline, 'loaded'], true); }));
state = state.setIn([timeline, 'isLoading'], false);
if (state.getIn([timeline, 'next']) === null) {
state = state.setIn([timeline, 'next'], next);
}
return state.updateIn([timeline, 'items'], Immutable.List(), list => (loaded ? ids.concat(list) : ids));
}; };
const appendNormalizedTimeline = (state, timeline, statuses, next) => { const appendNormalizedTimeline = (state, timeline, statuses, next) => {
let moreIds = Immutable.List(); const ids = Immutable.List(statuses.map(status => status.get('id')));
const oldIds = state.getIn([timeline, 'items'], Immutable.List());
statuses.forEach((status, i) => { return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
state = normalizeStatus(state, status); mMap.set('isLoading', false);
moreIds = moreIds.set(i, status.get('id')); mMap.set('next', next);
}); mMap.set('items', oldIds.concat(ids));
}));
state = state.setIn([timeline, 'isLoading'], false);
state = state.setIn([timeline, 'next'], next);
return state.updateIn([timeline, 'items'], Immutable.List(), list => list.concat(moreIds));
};
const normalizeAccountTimeline = (state, accountId, statuses, replace, next) => {
let ids = Immutable.List();
statuses.forEach((status, i) => {
state = normalizeStatus(state, status);
ids = ids.set(i, status.get('id'));
});
return state.updateIn(['accounts_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false)
.set('loaded', true)
.update('next', null, v => replace ? next : v)
.update('items', Immutable.List(), list => (replace ? ids : ids.concat(list))));
};
const normalizeAccountMediaTimeline = (state, accountId, statuses, replace, next) => {
let ids = Immutable.List();
statuses.forEach((status, i) => {
state = normalizeStatus(state, status);
ids = ids.set(i, status.get('id'));
});
return state.updateIn(['accounts_media_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false)
.update('next', null, v => replace ? next : v)
.update('items', Immutable.List(), list => (replace ? ids : ids.concat(list))));
};
const appendNormalizedAccountTimeline = (state, accountId, statuses, next) => {
let moreIds = Immutable.List([]);
statuses.forEach((status, i) => {
state = normalizeStatus(state, status);
moreIds = moreIds.set(i, status.get('id'));
});
return state.updateIn(['accounts_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false)
.set('next', next)
.update('items', list => list.concat(moreIds)));
};
const appendNormalizedAccountMediaTimeline = (state, accountId, statuses, next) => {
let moreIds = Immutable.List([]);
statuses.forEach((status, i) => {
state = normalizeStatus(state, status);
moreIds = moreIds.set(i, status.get('id'));
});
return state.updateIn(['accounts_media_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false)
.set('next', next)
.update('items', list => list.concat(moreIds)));
}; };
const updateTimeline = (state, timeline, status, references) => { const updateTimeline = (state, timeline, status, references) => {
const top = state.getIn([timeline, 'top']); const top = state.getIn([timeline, 'top']);
const ids = state.getIn([timeline, 'items'], Immutable.List());
const includesId = ids.includes(status.get('id'));
const unread = state.getIn([timeline, 'unread'], 0);
state = normalizeStatus(state, status); if (includesId) {
return state;
if (!top) {
state = state.updateIn([timeline, 'unread'], unread => unread + 1);
} }
state = state.updateIn([timeline, 'items'], Immutable.List(), list => { let newIds = ids;
if (top && list.size > 40) {
list = list.take(20);
}
if (list.includes(status.get('id'))) { return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
return list; if (!top) mMap.set('unread', unread + 1);
} if (top && ids.size > 40) newIds = newIds.take(20);
if (status.getIn(['reblog', 'id'], null) !== null) newIds = newIds.filterNot(item => references.includes(item));
const reblogOfId = status.getIn(['reblog', 'id'], null); mMap.set('items', newIds.unshift(status.get('id')));
}));
if (reblogOfId !== null) {
list = list.filterNot(itemId => references.includes(itemId));
}
return list.unshift(status.get('id'));
});
return state;
}; };
const deleteStatus = (state, id, accountId, references, reblogOf) => { const deleteStatus = (state, id, accountId, references, reblogOf) => {
if (reblogOf) { state.keySeq().forEach(timeline => {
// If we are deleting a reblog, just replace reblog with its original state = state.updateIn([timeline, 'items'], list => {
return state.updateIn(['home', 'items'], list => list.map(item => item === id ? reblogOf : item)); if (reblogOf && !list.includes(reblogOf)) {
} return list.map(item => item === id ? reblogOf : item);
} else {
// Remove references from timelines return list.filterNot(item => item === id);
['home', 'public', 'community', 'tag'].forEach(function (timeline) { }
state = state.updateIn([timeline, 'items'], list => list.filterNot(item => item === id)); });
}); });
// Remove references from account timelines
state = state.updateIn(['accounts_timelines', accountId, 'items'], Immutable.List([]), list => list.filterNot(item => item === id));
state = state.updateIn(['accounts_media_timelines', accountId, 'items'], Immutable.List([]), list => list.filterNot(item => item === id));
// Remove references from context
state.getIn(['descendants', id], Immutable.List()).forEach(descendantId => {
state = state.updateIn(['ancestors', descendantId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
});
state.getIn(['ancestors', id], Immutable.List()).forEach(ancestorId => {
state = state.updateIn(['descendants', ancestorId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
});
state = state.deleteIn(['descendants', id]).deleteIn(['ancestors', id]);
// Remove reblogs of deleted status // Remove reblogs of deleted status
references.forEach(ref => { references.forEach(ref => {
state = deleteStatus(state, ref[0], ref[1], []); state = deleteStatus(state, ref[0], ref[1], []);
@ -257,54 +108,27 @@ const filterTimelines = (state, relationship, statuses) => {
} }
references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => [item.get('id'), item.get('account')]); references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => [item.get('id'), item.get('account')]);
state = deleteStatus(state, status.get('id'), status.get('account'), references); state = deleteStatus(state, status.get('id'), status.get('account'), references);
}); });
return state; return state;
}; };
const normalizeContext = (state, id, ancestors, descendants) => {
const ancestorsIds = ancestors.map(ancestor => ancestor.get('id'));
const descendantsIds = descendants.map(descendant => descendant.get('id'));
return state.withMutations(map => {
map.setIn(['ancestors', id], ancestorsIds);
map.setIn(['descendants', id], descendantsIds);
});
};
const resetTimeline = (state, timeline, id) => {
if (timeline === 'tag' && typeof id !== 'undefined' && state.getIn([timeline, 'id']) !== id) {
state = state.update(timeline, map => map
.set('id', id)
.set('isLoading', true)
.set('loaded', false)
.set('next', null)
.set('top', true)
.update('items', list => list.clear()));
} else {
state = state.setIn([timeline, 'isLoading'], true);
}
return state;
};
const updateTop = (state, timeline, top) => { const updateTop = (state, timeline, top) => {
if (top) { return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
state = state.setIn([timeline, 'unread'], 0); if (top) mMap.set('unread', 0);
} mMap.set('top', top);
}));
return state.setIn([timeline, 'top'], top);
}; };
export default function timelines(state = initialState, action) { export default function timelines(state = initialState, action) {
switch(action.type) { switch(action.type) {
case TIMELINE_REFRESH_REQUEST: case TIMELINE_REFRESH_REQUEST:
case TIMELINE_EXPAND_REQUEST: case TIMELINE_EXPAND_REQUEST:
return resetTimeline(state, action.timeline, action.id); return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
case TIMELINE_REFRESH_FAIL: case TIMELINE_REFRESH_FAIL:
case TIMELINE_EXPAND_FAIL: case TIMELINE_EXPAND_FAIL:
return state.setIn([action.timeline, 'isLoading'], false); return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
case TIMELINE_REFRESH_SUCCESS: case TIMELINE_REFRESH_SUCCESS:
return normalizeTimeline(state, action.timeline, Immutable.fromJS(action.statuses), action.next); return normalizeTimeline(state, action.timeline, Immutable.fromJS(action.statuses), action.next);
case TIMELINE_EXPAND_SUCCESS: case TIMELINE_EXPAND_SUCCESS:
@ -313,37 +137,15 @@ export default function timelines(state = initialState, action) {
return updateTimeline(state, action.timeline, Immutable.fromJS(action.status), action.references); return updateTimeline(state, action.timeline, Immutable.fromJS(action.status), action.references);
case TIMELINE_DELETE: case TIMELINE_DELETE:
return deleteStatus(state, action.id, action.accountId, action.references, action.reblogOf); return deleteStatus(state, action.id, action.accountId, action.references, action.reblogOf);
case CONTEXT_FETCH_SUCCESS:
return normalizeContext(state, action.id, Immutable.fromJS(action.ancestors), Immutable.fromJS(action.descendants));
case ACCOUNT_TIMELINE_FETCH_REQUEST:
case ACCOUNT_TIMELINE_EXPAND_REQUEST:
return state.updateIn(['accounts_timelines', action.id], Immutable.Map(), map => map.set('isLoading', true));
case ACCOUNT_TIMELINE_FETCH_FAIL:
case ACCOUNT_TIMELINE_EXPAND_FAIL:
return state.updateIn(['accounts_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false));
case ACCOUNT_TIMELINE_FETCH_SUCCESS:
return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace, action.next);
case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
return appendNormalizedAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next);
case ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST:
case ACCOUNT_MEDIA_TIMELINE_EXPAND_REQUEST:
return state.updateIn(['accounts_media_timelines', action.id], Immutable.Map(), map => map.set('isLoading', true));
case ACCOUNT_MEDIA_TIMELINE_FETCH_FAIL:
case ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL:
return state.updateIn(['accounts_media_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false));
case ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS:
return normalizeAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace, action.next);
case ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS:
return appendNormalizedAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next);
case ACCOUNT_BLOCK_SUCCESS: case ACCOUNT_BLOCK_SUCCESS:
case ACCOUNT_MUTE_SUCCESS: case ACCOUNT_MUTE_SUCCESS:
return filterTimelines(state, action.relationship, action.statuses); return filterTimelines(state, action.relationship, action.statuses);
case TIMELINE_SCROLL_TOP: case TIMELINE_SCROLL_TOP:
return updateTop(state, action.timeline, action.top); return updateTop(state, action.timeline, action.top);
case TIMELINE_CONNECT: case TIMELINE_CONNECT:
return state.setIn([action.timeline, 'online'], true); return state.update(action.timeline, initialTimeline, map => map.set('online', true));
case TIMELINE_DISCONNECT: case TIMELINE_DISCONNECT:
return state.setIn([action.timeline, 'online'], false); return state.update(action.timeline, initialTimeline, map => map.set('online', false));
default: default:
return state; return state;
} }

View File

@ -76,7 +76,7 @@ export const makeGetNotification = () => {
}; };
export const getAccountGallery = createSelector([ export const getAccountGallery = createSelector([
(state, id) => state.getIn(['timelines', 'accounts_media_timelines', id, 'items'], Immutable.List()), (state, id) => state.getIn(['timelines', `account:${id}:media`, 'items'], Immutable.List()),
state => state.get('statuses'), state => state.get('statuses'),
], (statusIds, statuses) => { ], (statusIds, statuses) => {
let medias = Immutable.List(); let medias = Immutable.List();