From 5888697c0dccb02eb3fdefba28bc359c32ec264e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 13 Nov 2016 22:09:27 +0100 Subject: [PATCH] Better maxId calculation. --- src/modules/statuses.js | 16 +++++++++++++--- test/unit/specs/modules/statuses.spec.js | 20 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 8238644d..6fe2558c 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { reduce, map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find } from 'lodash' +import { reduce, map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find, flatten, maxBy } from 'lodash' import moment from 'moment' import apiService from '../services/api/api.service.js' import parse from '../services/status_parser/status_parser.js' @@ -41,7 +41,7 @@ const statusType = (status) => { return !status.is_post_verb && status.uri.match(/fave/) ? 'fave' : 'status' } -const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visibleStatuses, newStatusCount, faves, loading }) => { +const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visibleStatuses, newStatusCount, faves, loading, maxId }) => { const statusesAndFaves = groupBy(addedStatuses, statusType) const addedFaves = statusesAndFaves['fave'] || [] const unseenFaves = differenceBy(addedFaves, faves, 'id') @@ -92,9 +92,9 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib statuses: newStatuses, visibleStatuses: newVisibleStatuses, newStatusCount: newNewStatusCount, - maxId: newStatuses[0].id, minVisibleId: (last(newVisibleStatuses) || { id: undefined }).id, faves: unionBy(faves, addedFaves, 'id'), + maxId, loading } } @@ -109,8 +109,18 @@ const updateTimestampsInStatuses = (statuses) => { }) } + +export const findMaxId = (...args) => { + return (maxBy(flatten(args), 'id') || {}).id +} + export const mutations = { addNewStatuses (state, { statuses, showImmediately = false, timeline }) { + const timelineObject = state.timelines[timeline] + + // Set new maxId + const maxId = findMaxId(statuses, timelineObject.statuses) + timelineObject.maxId = maxId state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline]) state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses, 'id') diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 27936fb1..372ab3ca 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -1,5 +1,5 @@ import { cloneDeep } from 'lodash' -import { defaultState, mutations } from '../../../../src/modules/statuses.js' +import { defaultState, mutations, findMaxId } from '../../../../src/modules/statuses.js' const makeMockStatus = ({id, text}) => { return { @@ -11,6 +11,21 @@ const makeMockStatus = ({id, text}) => { } } +describe('findMaxId', () => { + it('returns the largest id in any of the given arrays', () => { + const statusesOne = [{ id: 100 }, { id: 2 }] + const statusesTwo = [{ id: 3 }] + + const maxId = findMaxId(statusesOne, statusesTwo) + expect(maxId).to.eq(100) + }) + + it('returns undefined for empty arrays', () => { + const maxId = findMaxId([], []) + expect(maxId).to.eq(undefined) + }) +}) + describe('The Statuses module', () => { it('adds the status to allStatuses and to the given timeline', () => { const state = cloneDeep(defaultState) @@ -96,7 +111,7 @@ describe('The Statuses module', () => { const favorite = { id: 2, is_post_verb: false, - in_reply_to_status_id: 1, // The API uses strings here... + in_reply_to_status_id: '1', // The API uses strings here... uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00', text: 'a favorited something by b' } @@ -106,6 +121,7 @@ describe('The Statuses module', () => { expect(state.timelines.public.visibleStatuses.length).to.eql(1) expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1) + expect(state.timelines.public.maxId).to.eq(favorite.id) // Adding again shouldn't change anything mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })