Better handling of favorites.

This commit is contained in:
Roger Braun 2016-11-25 16:56:08 +01:00
parent 08393b8580
commit 1d8c813135
2 changed files with 45 additions and 4 deletions

View file

@ -7,6 +7,7 @@ export const defaultState = {
allStatuses: [], allStatuses: [],
maxId: 0, maxId: 0,
notifications: [], notifications: [],
favorites: new Set(),
timelines: { timelines: {
public: { public: {
statuses: [], 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) }) const status = find(allStatuses, { id: toInteger(favorite.in_reply_to_status_id) })
if (status) { if (status) {
status.fave_num += 1 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) { if (status.user.id === user.id) {
addNotification({type: 'favorite', status, action: favorite}) addNotification({type: 'favorite', status, action: favorite})
} }
@ -175,8 +183,12 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us
retweet.retweeted_status = retweetedStatus retweet.retweeted_status = retweetedStatus
}, },
'favorite': (favorite) => { 'favorite': (favorite) => {
// Only update if this is a new favorite.
if (!state.favorites.has(favorite.id)) {
state.favorites.add(favorite.id)
updateMaxId(favorite) updateMaxId(favorite)
favoriteStatus(favorite) favoriteStatus(favorite)
}
}, },
'deletion': (deletion) => { 'deletion': (deletion) => {
const uri = deletion.uri const uri = deletion.uri

View file

@ -197,7 +197,8 @@ describe('The Statuses module', () => {
is_post_verb: false, 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', 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' }) 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.length).to.eql(1)
expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1) expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
expect(state.timelines.public.maxId).to.eq(favorite.id) 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', () => { describe('notifications', () => {
@ -220,7 +248,8 @@ describe('The Statuses module', () => {
is_post_verb: false, 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', 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 }) mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public', user })