From 1d8c8131352d05d796a6baf65a09fbdd1726fedf Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 25 Nov 2016 16:56:08 +0100 Subject: [PATCH] Better handling of favorites. --- src/modules/statuses.js | 16 ++++++++++-- test/unit/specs/modules/statuses.spec.js | 33 ++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 734ffc8aaa..37115506b4 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -7,6 +7,7 @@ export const defaultState = { allStatuses: [], maxId: 0, notifications: [], + favorites: new Set(), timelines: { public: { statuses: [], @@ -147,6 +148,13 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us const status = find(allStatuses, { id: toInteger(favorite.in_reply_to_status_id) }) if (status) { status.fave_num += 1 + + // This is our favorite, so the relevant bit. + if (favorite.user.id === user.id) { + status.favorited = true + } + + // Add a notification if the user's status is favorited if (status.user.id === user.id) { addNotification({type: 'favorite', status, action: favorite}) } @@ -175,8 +183,12 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us retweet.retweeted_status = retweetedStatus }, 'favorite': (favorite) => { - updateMaxId(favorite) - favoriteStatus(favorite) + // Only update if this is a new favorite. + if (!state.favorites.has(favorite.id)) { + state.favorites.add(favorite.id) + updateMaxId(favorite) + favoriteStatus(favorite) + } }, 'deletion': (deletion) => { const uri = deletion.uri diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index a50e4f9cd7..f068bb92dd 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -197,7 +197,8 @@ describe('The Statuses module', () => { is_post_verb: false, 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' + text: 'a favorited something by b', + user: {} } mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) @@ -206,6 +207,33 @@ 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 it again does nothing + mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' }) + + 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) + + // If something is favorited by the current user, it also sets the 'favorited' property + const user = { + id: 1 + } + + const ownFavorite = { + id: 3, + is_post_verb: false, + 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', + user + } + + mutations.addNewStatuses(state, { statuses: [ownFavorite], showImmediately: true, timeline: 'public', user }) + + expect(state.timelines.public.visibleStatuses.length).to.eql(1) + expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(2) + expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true) }) describe('notifications', () => { @@ -220,7 +248,8 @@ describe('The Statuses module', () => { is_post_verb: false, 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' + text: 'a favorited something by b', + user: {} } mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public', user })