From f8a3afc9d14428d05115105030af1f1d300dde8e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 7 Nov 2016 22:09:34 +0100 Subject: [PATCH] Don't put statuses we received from retweets into timelines. They should be in allStatuses to keep only one reference, but they should not get into the timeline, or it will mess up old status fetching, because they have a very low id. --- src/modules/statuses.js | 26 ++++++++++++------------ test/unit/specs/modules/statuses.spec.js | 7 ++++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 7913c289..8ed2890f 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { flatten, 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 } from 'lodash' import moment from 'moment' import apiService from '../services/api/api.service.js' @@ -55,14 +55,6 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib addedStatuses = statusesAndFaves['status'] || [] - const splitRetweets = (status) => { - if (status.retweeted_status) { - return [status, status.retweeted_status] - } else { - return status - } - } - // Add some html and nsfw to the statuses. addedStatuses = map(addedStatuses, (status) => { const statusoid = status.retweeted_status || status @@ -79,11 +71,9 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib statusoid.nsfw = statusoid.text.match(nsfwRegex) } - return splitRetweets(status) + return status }) - addedStatuses = flatten(addedStatuses) - const newStatuses = sortBy( unionBy(addedStatuses, statuses, 'id'), ({id}) => -id @@ -125,9 +115,19 @@ const updateTimestampsInStatuses = (statuses) => { export const mutations = { addNewStatuses (state, { statuses, showImmediately = false, timeline }) { state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline]) - state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses.id) + state.allStatuses = unionBy(state.timelines[timeline].statuses, state.allStatuses, 'id') // Set up retweets with most current status + const getRetweets = (result, status) => { + if (status.retweeted_status) { + result.push(status.retweeted_status) + } + return result + } + + const retweets = reduce(statuses, getRetweets, []) + + state.allStatuses = unionBy(retweets, state.allStatuses, 'id') each(state.allStatuses, (status) => { if (status.retweeted_status) { diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 75736133..fe8ed0b8 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -42,9 +42,10 @@ describe('The Statuses module', () => { retweet.retweeted_status = status - // It adds both statuses - mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public' }) - expect(state.allStatuses).to.eql([retweet, status]) + // It adds both statuses, but only the retweet to visible. + mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public', showImmediately: true }) + expect(state.timelines.public.visibleStatuses).to.have.length(1) + expect(state.allStatuses).to.eql([status, retweet]) // It refers to the modified status. mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })