commit: cc7f7a29cf8435cc2486122976504c38ffe75c33
parent: 8199c8e45d5dee07b358e07c13419c0cf3bb9e6d
Author: Roger Braun <roger@rogerbraun.net>
Date: Mon, 7 Nov 2016 18:36:11 +0100
Better retweet handling.
Fixes problems with liking + likes count.
Diffstat:
2 files changed, 43 insertions(+), 4 deletions(-)
diff --git a/src/modules/statuses.js b/src/modules/statuses.js
@@ -1,4 +1,4 @@
-import { map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find } from 'lodash'
+import { flatten, 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,8 +55,16 @@ 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.
- each(addedStatuses, (status) => {
+ addedStatuses = map(addedStatuses, (status) => {
const statusoid = status.retweeted_status || status
statusoid.created_at_parsed = statusoid.created_at
@@ -70,8 +78,12 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib
const nsfwRegex = /#nsfw/i
statusoid.nsfw = statusoid.text.match(nsfwRegex)
}
+
+ return splitRetweets(status)
})
+ addedStatuses = flatten(addedStatuses)
+
const newStatuses = sortBy(
unionBy(addedStatuses, statuses, 'id'),
({id}) => -id
@@ -114,6 +126,15 @@ 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)
+
+ // Set up retweets with most current status
+
+ each(state.allStatuses, (status) => {
+ if (status.retweeted_status) {
+ const retweetedStatus = find(state.allStatuses, { id: status.retweeted_status.id })
+ status.retweeted_status = retweetedStatus
+ }
+ })
},
showNewStatuses (state, { timeline }) {
const oldTimeline = (state.timelines[timeline])
diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js
@@ -1,11 +1,11 @@
import { cloneDeep } from 'lodash'
import { defaultState, mutations } from '../../../../src/modules/statuses.js'
-const makeMockStatus = ({id}) => {
+const makeMockStatus = ({id, text}) => {
return {
id,
name: 'status',
- text: `Text number ${id}`,
+ text: text || `Text number ${id}`,
fave_num: 0,
uri: ''
}
@@ -34,6 +34,24 @@ describe('The Statuses module', () => {
expect(state.timelines.public.visibleStatuses).to.eql([status])
})
+ it('splits retweets from their status and links them', () => {
+ const state = cloneDeep(defaultState)
+ const status = makeMockStatus({id: 1})
+ const retweet = makeMockStatus({id: 2})
+ const modStatus = makeMockStatus({id: 1, text: 'something else'})
+
+ retweet.retweeted_status = status
+
+ // It adds both statuses
+ mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public' })
+ expect(state.allStatuses).to.eql([retweet, status])
+
+ // It refers to the modified status.
+ mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
+ expect(state.allStatuses).to.eql([retweet, modStatus])
+ expect(retweet.retweeted_status).to.eql(modStatus)
+ })
+
it('replaces existing statuses with the same id', () => {
const state = cloneDeep(defaultState)
const status = makeMockStatus({id: 1})