More use of next link header on account (media) timelines (#3311)

This will reduce requests on who have only few statuses.

- Use next link header to detect more items from first request
- Omit next link header if result items are fewer than requested count
(It had omit it only if result was empty before)
This commit is contained in:
unarist 2017-05-26 00:09:13 +09:00 committed by Eugen Rochko
parent 2241a15ee9
commit cf4fe6cab8
3 changed files with 16 additions and 12 deletions

View file

@ -62,7 +62,7 @@ class Api::V1::AccountsController < ApiController
set_maps(@statuses) set_maps(@statuses)
next_path = statuses_api_v1_account_url(statuses_pagination_params(max_id: @statuses.last.id)) unless @statuses.empty? next_path = statuses_api_v1_account_url(statuses_pagination_params(max_id: @statuses.last.id)) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
prev_path = statuses_api_v1_account_url(statuses_pagination_params(since_id: @statuses.first.id)) unless @statuses.empty? prev_path = statuses_api_v1_account_url(statuses_pagination_params(since_id: @statuses.first.id)) unless @statuses.empty?
set_pagination_headers(next_path, prev_path) set_pagination_headers(next_path, prev_path)

View file

@ -115,7 +115,8 @@ export function fetchAccountTimeline(id, replace = false) {
dispatch(fetchAccountTimelineRequest(id, skipLoading)); dispatch(fetchAccountTimelineRequest(id, skipLoading));
api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => { api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => {
dispatch(fetchAccountTimelineSuccess(id, response.data, replace, skipLoading)); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(fetchAccountTimelineSuccess(id, response.data, replace, skipLoading, next));
}).catch(error => { }).catch(error => {
dispatch(fetchAccountTimelineFail(id, error, skipLoading)); dispatch(fetchAccountTimelineFail(id, error, skipLoading));
}); });
@ -138,7 +139,8 @@ export function fetchAccountMediaTimeline(id, replace = false) {
dispatch(fetchAccountMediaTimelineRequest(id, skipLoading)); dispatch(fetchAccountMediaTimelineRequest(id, skipLoading));
api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => { api(getState).get(`/api/v1/accounts/${id}/statuses`, { params }).then(response => {
dispatch(fetchAccountMediaTimelineSuccess(id, response.data, replace, skipLoading)); const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(fetchAccountMediaTimelineSuccess(id, response.data, replace, skipLoading, next));
}).catch(error => { }).catch(error => {
dispatch(fetchAccountMediaTimelineFail(id, error, skipLoading)); dispatch(fetchAccountMediaTimelineFail(id, error, skipLoading));
}); });
@ -283,13 +285,14 @@ export function fetchAccountTimelineRequest(id, skipLoading) {
}; };
}; };
export function fetchAccountTimelineSuccess(id, statuses, replace, skipLoading) { export function fetchAccountTimelineSuccess(id, statuses, replace, skipLoading, next) {
return { return {
type: ACCOUNT_TIMELINE_FETCH_SUCCESS, type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
id, id,
statuses, statuses,
replace, replace,
skipLoading, skipLoading,
next,
}; };
}; };
@ -311,13 +314,14 @@ export function fetchAccountMediaTimelineRequest(id, skipLoading) {
}; };
}; };
export function fetchAccountMediaTimelineSuccess(id, statuses, replace, skipLoading) { export function fetchAccountMediaTimelineSuccess(id, statuses, replace, skipLoading, next) {
return { return {
type: ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS, type: ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS,
id, id,
statuses, statuses,
replace, replace,
skipLoading, skipLoading,
next,
}; };
}; };

View file

@ -140,7 +140,7 @@ const appendNormalizedTimeline = (state, timeline, statuses, next) => {
return state.updateIn([timeline, 'items'], Immutable.List(), list => list.concat(moreIds)); return state.updateIn([timeline, 'items'], Immutable.List(), list => list.concat(moreIds));
}; };
const normalizeAccountTimeline = (state, accountId, statuses, replace = false) => { const normalizeAccountTimeline = (state, accountId, statuses, replace, next) => {
let ids = Immutable.List(); let ids = Immutable.List();
statuses.forEach((status, i) => { statuses.forEach((status, i) => {
@ -151,11 +151,11 @@ const normalizeAccountTimeline = (state, accountId, statuses, replace = false) =
return state.updateIn(['accounts_timelines', accountId], Immutable.Map(), map => map return state.updateIn(['accounts_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false) .set('isLoading', false)
.set('loaded', true) .set('loaded', true)
.set('next', true) .set('next', next)
.update('items', Immutable.List(), list => (replace ? ids : ids.concat(list)))); .update('items', Immutable.List(), list => (replace ? ids : ids.concat(list))));
}; };
const normalizeAccountMediaTimeline = (state, accountId, statuses) => { const normalizeAccountMediaTimeline = (state, accountId, statuses, replace, next) => {
let ids = Immutable.List(); let ids = Immutable.List();
statuses.forEach((status, i) => { statuses.forEach((status, i) => {
@ -165,8 +165,8 @@ const normalizeAccountMediaTimeline = (state, accountId, statuses) => {
return state.updateIn(['accounts_media_timelines', accountId], Immutable.Map(), map => map return state.updateIn(['accounts_media_timelines', accountId], Immutable.Map(), map => map
.set('isLoading', false) .set('isLoading', false)
.set('next', true) .set('next', next)
.update('items', Immutable.List(), list => ids.concat(list))); .update('items', Immutable.List(), list => (replace ? ids : ids.concat(list))));
}; };
const appendNormalizedAccountTimeline = (state, accountId, statuses, next) => { const appendNormalizedAccountTimeline = (state, accountId, statuses, next) => {
@ -335,7 +335,7 @@ export default function timelines(state = initialState, action) {
case ACCOUNT_TIMELINE_EXPAND_FAIL: case ACCOUNT_TIMELINE_EXPAND_FAIL:
return state.updateIn(['accounts_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false)); return state.updateIn(['accounts_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false));
case ACCOUNT_TIMELINE_FETCH_SUCCESS: case ACCOUNT_TIMELINE_FETCH_SUCCESS:
return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace); return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace, action.next);
case ACCOUNT_TIMELINE_EXPAND_SUCCESS: case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
return appendNormalizedAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next); return appendNormalizedAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next);
case ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST: case ACCOUNT_MEDIA_TIMELINE_FETCH_REQUEST:
@ -345,7 +345,7 @@ export default function timelines(state = initialState, action) {
case ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL: case ACCOUNT_MEDIA_TIMELINE_EXPAND_FAIL:
return state.updateIn(['accounts_media_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false)); return state.updateIn(['accounts_media_timelines', action.id], Immutable.Map(), map => map.set('isLoading', false));
case ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS: case ACCOUNT_MEDIA_TIMELINE_FETCH_SUCCESS:
return normalizeAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses)); return normalizeAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace, action.next);
case ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS: case ACCOUNT_MEDIA_TIMELINE_EXPAND_SUCCESS:
return appendNormalizedAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next); return appendNormalizedAccountMediaTimeline(state, action.id, Immutable.fromJS(action.statuses), action.next);
case ACCOUNT_BLOCK_SUCCESS: case ACCOUNT_BLOCK_SUCCESS: