From 24d7f9917b1a4a147b92bd31ec65511c1d528c6f Mon Sep 17 00:00:00 2001 From: jasper Date: Mon, 18 Feb 2019 11:39:35 -0800 Subject: Remove posts by blocking or following --- src/modules/statuses.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 46117fd7..57095f37 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -307,9 +307,32 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot }) } +const removeStatus = (state, { timeline, userId }) => { + const timelineObject = state.timelines[timeline] + if (userId) { + remove(timelineObject.statuses, { user: { id: userId } }) + remove(timelineObject.visibleStatuses, { user: { id: userId } }) + const statusesObject = timelineObject.statusesObject + const visibleStatusesObject = timelineObject.visibleStatusesObject + each(statusesObject, (status, key) => { + if (status.user.id === userId) { + delete statusesObject[key] + } + }) + each(visibleStatusesObject, (status, key) => { + if (status.user.id === userId) { + delete visibleStatusesObject[key] + } + }) + timelineObject.minVisibleId = (last(timeline.visibleStatuses) || {}).id + timelineObject.maxId = statuses.length > 0 ? maxBy(statuses, 'id').id : 0 + } +} + export const mutations = { addNewStatuses, addNewNotifications, + removeStatus, showNewStatuses (state, { timeline }) { const oldTimeline = (state.timelines[timeline]) -- cgit v1.2.3-70-g09d2 From 4e79300232288fd9b7c4456e8b461f7c1cbac6fa Mon Sep 17 00:00:00 2001 From: jasper Date: Mon, 25 Feb 2019 14:35:47 -0800 Subject: Remove posts by blocking or unfollowing --- .../user_card_content/user_card_content.js | 51 ---------------------- src/modules/statuses.js | 18 ++------ src/services/api/api.service.js | 6 +-- 3 files changed, 5 insertions(+), 70 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/components/user_card_content/user_card_content.js b/src/components/user_card_content/user_card_content.js index 8bc01941..d2a97a84 100644 --- a/src/components/user_card_content/user_card_content.js +++ b/src/components/user_card_content/user_card_content.js @@ -1,5 +1,4 @@ import UserAvatar from '../user_avatar/user_avatar.vue' -import apiService from '../../services/api/api.service.js' import { hex2rgb } from '../../services/color_convert/color_convert.js' import { requestFollow, requestUnfollow } from '../../services/follow_manipulate/follow_manipulate' import generateProfileLink from 'src/services/user_profile_link_generator/user_profile_link_generator' @@ -99,25 +98,6 @@ export default { requestFollow(this.user, store).then(({sent}) => { this.followRequestInProgress = false this.followRequestSent = sent - - const rootState = store.rootState || store.state - const credentials = store.state.users.currentUser.credentials - const timelineData = rootState.statuses.timelines['friends'] - apiService.fetchTimeline({ - store, - credentials, - userId: this.user.id, - timeline: 'user', - between: true, - until: timelineData.maxId, - since: timelineData.minVisibleId - }).then((statuses) => { - store.dispatch('addNewStatuses', { - timeline: 'friends', - statuses, - showImmediately: true - }) - }, () => store.dispatch('setError', { value: true })) }) }, unfollowUser () { @@ -145,37 +125,6 @@ export default { store.state.api.backendInteractor.unblockUser(this.user.id) .then((unblockedUser) => { store.commit('addNewUsers', [unblockedUser]) - - const rootState = store.rootState || store.state - const credentials = store.state.users.currentUser.credentials - const timelineData = rootState.statuses.timelines['friends'] - apiService.fetchTimeline({ - store, - credentials, - userId: this.user.id, - timeline: 'user', - between: true, - until: timelineData.maxId, - since: timelineData.minVisibleId - }).then((statuses) => { - store.dispatch('addNewStatuses', { - timeline: 'public', - statuses, - showImmediately: true - }) - store.dispatch('addNewStatuses', { - timeline: 'publicAndExternal', - statuses, - showImmediately: true - }) - if (this.user.follows_you) { - store.dispatch('addNewStatuses', { - timeline: 'friends', - statuses, - showImmediately: true - }) - } - }, () => store.dispatch('setError', { value: true })) }) }, toggleMute () { diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 57095f37..d144702e 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash' +import { remove, slice, each, find, maxBy, minBy, merge, first, last, isArray } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -312,20 +312,8 @@ const removeStatus = (state, { timeline, userId }) => { if (userId) { remove(timelineObject.statuses, { user: { id: userId } }) remove(timelineObject.visibleStatuses, { user: { id: userId } }) - const statusesObject = timelineObject.statusesObject - const visibleStatusesObject = timelineObject.visibleStatusesObject - each(statusesObject, (status, key) => { - if (status.user.id === userId) { - delete statusesObject[key] - } - }) - each(visibleStatusesObject, (status, key) => { - if (status.user.id === userId) { - delete visibleStatusesObject[key] - } - }) - timelineObject.minVisibleId = (last(timeline.visibleStatuses) || {}).id - timelineObject.maxId = statuses.length > 0 ? maxBy(statuses, 'id').id : 0 + timelineObject.minVisibleId = timelineObject.visibleStatuses.length > 0 ? last(timelineObject.visibleStatuses).id : 0 + timelineObject.maxId = timelineObject.statuses.length > 0 ? first(timelineObject.statuses).id : 0 } } diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index d35c72b1..c5fdcb72 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -329,7 +329,7 @@ const setUserMute = ({id, credentials, muted = true}) => { }) } -const fetchTimeline = ({timeline, credentials, since = false, until = false, between = false, count = 20, userId = false, tag = false}) => { +const fetchTimeline = ({timeline, credentials, since = false, until = false, userId = false, tag = false}) => { const timelineUrls = { public: PUBLIC_TIMELINE_URL, friends: FRIENDS_TIMELINE_URL, @@ -362,9 +362,7 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, bet if (timeline === 'media') { params.push(['only_media', 1]) } - if (!between) { - params.push(['count', count]) - } + params.push(['count', 20]) const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join('&') url += `?${queryString}` -- cgit v1.2.3-70-g09d2 From 9aec49bacb033cdcc118f821706c419d384bf5fe Mon Sep 17 00:00:00 2001 From: jasper Date: Wed, 27 Feb 2019 17:45:08 -0800 Subject: Fetch activites by last id --- src/modules/statuses.js | 19 ++++++++++--------- .../timeline_fetcher/timeline_fetcher.service.js | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 826b544c..4002f282 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -10,6 +10,7 @@ const emptyTl = (userId = 0) => ({ visibleStatusesObject: {}, newStatusCount: 0, maxId: 0, + minId: 0, minVisibleId: 0, loading: false, followers: [], @@ -117,11 +118,16 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us const timelineObject = state.timelines[timeline] const maxNew = statuses.length > 0 ? maxBy(statuses, 'id').id : 0 - const older = timeline && maxNew < timelineObject.maxId + const minNew = statuses.length > 0 ? minBy(statuses, 'id').id : 0 + const newer = timeline && maxNew > timelineObject.maxId && statuses.length > 0 + const older = timeline && (minNew < timelineObject.minId || timelineObject.minId === 0) && statuses.length > 0 - if (timeline && !noIdUpdate && statuses.length > 0 && !older) { + if (!noIdUpdate && newer) { timelineObject.maxId = maxNew } + if (!noIdUpdate && older) { + timelineObject.minId = minNew + } // This makes sure that user timeline won't get data meant for other // user. I.e. opening different user profiles makes request which could @@ -255,13 +261,8 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us processor(status) }) - // Keep the visible statuses sorted - if (timeline) { - sortTimeline(timelineObject) - if ((older || timelineObject.minVisibleId <= 0) && statuses.length > 0) { - timelineObject.minVisibleId = minBy(statuses, 'id').id - } - } + // Keep the visible statuses sorted + if (timeline) sortTimeline(timelineObject) } const addNewNotifications = (state, { dispatch, notifications, older, visibleNotificationTypes }) => { diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index 64f8f468..6f99616f 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -21,7 +21,7 @@ const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false const timelineData = rootState.statuses.timelines[camelCase(timeline)] if (older) { - args['until'] = until || timelineData.minVisibleId + args['until'] = until || timelineData.minId } else { args['since'] = timelineData.maxId } -- cgit v1.2.3-70-g09d2 From ba2e05bc63e0b36d5a126b422c9f87a7ace43305 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:03:44 -0500 Subject: #392: stale data served to new user account --- src/components/notifications/notifications.js | 3 ++- src/modules/statuses.js | 15 ++++++++++++--- src/modules/users.js | 4 ++++ 3 files changed, 18 insertions(+), 4 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js index 5e95631a..9fc5e38a 100644 --- a/src/components/notifications/notifications.js +++ b/src/components/notifications/notifications.js @@ -11,7 +11,8 @@ const Notifications = { const store = this.$store const credentials = store.state.users.currentUser.credentials - notificationsFetcher.startFetching({ store, credentials }) + const fetcherId = notificationsFetcher.startFetching({ store, credentials }) + this.$store.commit('setNotificationFetcher', { fetcherId }) }, data () { return { diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 826b544c..2d1313a5 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash' +import { remove, slice, each, find, maxBy, minBy, merge, last, isArray, cloneDeep } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -29,7 +29,8 @@ export const defaultState = { data: [], idStore: {}, loading: false, - error: false + error: false, + fetcherId: null }, favorites: new Set(), error: false, @@ -321,6 +322,14 @@ export const mutations = { oldTimeline.visibleStatusesObject = {} each(oldTimeline.visibleStatuses, (status) => { oldTimeline.visibleStatusesObject[status.id] = status }) }, + setNotificationFetcher (state, { fetcherId }) { + state.notifications.fetcherId = fetcherId + }, + resetStatuses (state) { + Object.keys(state).forEach(key => { + state[key] = cloneDeep(defaultState[key]) + }) + }, clearTimeline (state, { timeline }) { state.timelines[timeline] = emptyTl(state.timelines[timeline].userId) }, @@ -371,7 +380,7 @@ export const mutations = { } const statuses = { - state: defaultState, + state: cloneDeep(defaultState), actions: { addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) { commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId }) diff --git a/src/modules/users.js b/src/modules/users.js index 77df7168..84fe039b 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -290,6 +290,10 @@ const users = { store.commit('setToken', false) store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) + if (store.rootState.statuses.notifications.fetcherId) { + window.clearInterval(store.rootState.statuses.notifications.fetcherId) + } + store.commit('resetStatuses') }, loginUser (store, accessToken) { return new Promise((resolve, reject) => { -- cgit v1.2.3-70-g09d2 From f5adb62e2e6efdf6bd9a9c7a8c2677c618de53f9 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:27:47 -0500 Subject: #392: update defaultState into a function --- src/modules/statuses.js | 11 ++++++----- test/unit/specs/modules/statuses.spec.js | 27 +++++++++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 2d1313a5..2af23a9d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -1,4 +1,4 @@ -import { remove, slice, each, find, maxBy, minBy, merge, last, isArray, cloneDeep } from 'lodash' +import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash' import apiService from '../services/api/api.service.js' // import parse from '../services/status_parser/status_parser.js' @@ -18,7 +18,7 @@ const emptyTl = (userId = 0) => ({ flushMarker: 0 }) -export const defaultState = { +export const defaultState = () => ({ allStatuses: [], allStatusesObject: {}, maxId: 0, @@ -45,7 +45,7 @@ export const defaultState = { tag: emptyTl(), dms: emptyTl() } -} +}) export const prepareStatus = (status) => { // Set deleted flag @@ -326,8 +326,9 @@ export const mutations = { state.notifications.fetcherId = fetcherId }, resetStatuses (state) { + const emptyState = defaultState() Object.keys(state).forEach(key => { - state[key] = cloneDeep(defaultState[key]) + state[key] = emptyState[key] }) }, clearTimeline (state, { timeline }) { @@ -380,7 +381,7 @@ export const mutations = { } const statuses = { - state: cloneDeep(defaultState), + state: defaultState(), actions: { addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) { commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId }) diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 01d2ce06..864b798d 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -1,4 +1,3 @@ -import { cloneDeep } from 'lodash' import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js' // eslint-disable-next-line camelcase @@ -24,7 +23,7 @@ describe('Statuses.prepareStatus', () => { describe('The Statuses module', () => { it('adds the status to allStatuses and to the given timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) @@ -36,7 +35,7 @@ describe('The Statuses module', () => { }) it('counts the status as new if it has not been seen on this timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) @@ -54,7 +53,7 @@ describe('The Statuses module', () => { }) it('add the statuses to allStatuses if no timeline is given', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status] }) @@ -66,7 +65,7 @@ describe('The Statuses module', () => { }) it('adds the status to allStatuses and to the given timeline, directly visible', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) @@ -78,7 +77,7 @@ describe('The Statuses module', () => { }) it('removes statuses by tag on deletion', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const otherStatus = makeMockStatus({id: '3'}) status.uri = 'xxx' @@ -96,7 +95,7 @@ describe('The Statuses module', () => { }) it('does not update the maxId when the noIdUpdate flag is set', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const secondStatus = makeMockStatus({id: '2'}) @@ -110,7 +109,7 @@ describe('The Statuses module', () => { }) it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const nonVisibleStatus = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '3'}) const statusTwo = makeMockStatus({id: '2'}) @@ -130,7 +129,7 @@ describe('The Statuses module', () => { }) it('splits retweets from their status and links them', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const retweet = makeMockStatus({id: '2', type: 'retweet'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) @@ -155,7 +154,7 @@ describe('The Statuses module', () => { }) it('replaces existing statuses with the same id', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) @@ -172,7 +171,7 @@ describe('The Statuses module', () => { }) it('replaces existing statuses with the same id, coming from a retweet', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const modStatus = makeMockStatus({id: '1', text: 'something else'}) const retweet = makeMockStatus({id: '2', type: 'retweet'}) @@ -193,7 +192,7 @@ describe('The Statuses module', () => { }) it('handles favorite actions', () => { - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const favorite = { @@ -241,7 +240,7 @@ describe('The Statuses module', () => { }) it('keeps userId when clearing user timeline', () => { - const state = cloneDeep(defaultState) + const state = defaultState() state.timelines.user.userId = 123 mutations.clearTimeline(state, { timeline: 'user' }) @@ -252,7 +251,7 @@ describe('The Statuses module', () => { describe('notifications', () => { it('removes a notification when the notice gets removed', () => { const user = { id: '1' } - const state = cloneDeep(defaultState) + const state = defaultState() const status = makeMockStatus({id: '1'}) const otherStatus = makeMockStatus({id: '3'}) const mentionedStatus = makeMockStatus({id: '2'}) -- cgit v1.2.3-70-g09d2 From bbe1821be7c098f3a23bd0dd54b4ec89a159e9c1 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 28 Feb 2019 14:44:43 -0500 Subject: #392: update object.entries --- src/modules/statuses.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 2af23a9d..028053cf 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -327,8 +327,8 @@ export const mutations = { }, resetStatuses (state) { const emptyState = defaultState() - Object.keys(state).forEach(key => { - state[key] = emptyState[key] + Object.entries(emptyState).forEach(([key, value]) => { + state[key] = value }) }, clearTimeline (state, { timeline }) { -- cgit v1.2.3-70-g09d2 From f3f9fbe3027bf5284adc05387e89d904884490b2 Mon Sep 17 00:00:00 2001 From: dave Date: Fri, 1 Mar 2019 11:59:50 -0500 Subject: #392: clean up notification stopping section --- src/modules/statuses.js | 6 ++++++ src/modules/users.js | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 028053cf..77a8ec72 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -401,6 +401,12 @@ const statuses = { setNotificationsSilence ({ rootState, commit }, { value }) { commit('setNotificationsSilence', { value }) }, + stopFetchingNotifications ({ rootState, commit }) { + if (rootState.statuses.notifications.fetcherId) { + window.clearInterval(rootState.statuses.notifications.fetcherId) + } + commit('setNotificationFetcher', { fetcherId: null }) + }, deleteStatus ({ rootState, commit }, status) { commit('setDeleted', { status }) apiService.deleteStatus({ id: status.id, credentials: rootState.users.currentUser.credentials }) diff --git a/src/modules/users.js b/src/modules/users.js index 84fe039b..0e1dd166 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -290,9 +290,7 @@ const users = { store.commit('setToken', false) store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) - if (store.rootState.statuses.notifications.fetcherId) { - window.clearInterval(store.rootState.statuses.notifications.fetcherId) - } + store.dispatch('stopFetchingNotifications') store.commit('resetStatuses') }, loginUser (store, accessToken) { -- cgit v1.2.3-70-g09d2 From 81e89fed3d6cc247346611e1b58aae9d87667934 Mon Sep 17 00:00:00 2001 From: jasper Date: Fri, 1 Mar 2019 12:53:24 -0800 Subject: Fetch activites by last id --- src/modules/statuses.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 4002f282..96a3549d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -262,7 +262,9 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us }) // Keep the visible statuses sorted - if (timeline) sortTimeline(timelineObject) + if (timeline) { + sortTimeline(timelineObject) + } } const addNewNotifications = (state, { dispatch, notifications, older, visibleNotificationTypes }) => { -- cgit v1.2.3-70-g09d2 From 70d7ed36076081f22368bceaa42dd0548fd1c89a Mon Sep 17 00:00:00 2001 From: shpuld Date: Sun, 10 Mar 2019 22:40:48 +0200 Subject: Make minId reset with minVisibleId to prevent gaps when showing new --- src/modules/statuses.js | 1 + test/unit/specs/modules/statuses.spec.js | 418 ++++++++++++++++--------------- 2 files changed, 220 insertions(+), 199 deletions(-) (limited to 'src/modules/statuses.js') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 7571b62a..6b512fa3 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -333,6 +333,7 @@ export const mutations = { oldTimeline.newStatusCount = 0 oldTimeline.visibleStatuses = slice(oldTimeline.statuses, 0, 50) oldTimeline.minVisibleId = last(oldTimeline.visibleStatuses).id + oldTimeline.minId = oldTimeline.minVisibleId oldTimeline.visibleStatusesObject = {} each(oldTimeline.visibleStatuses, (status) => { oldTimeline.visibleStatusesObject[status.id] = status }) }, diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 864b798d..0bbcb25a 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -14,238 +14,258 @@ const makeMockStatus = ({id, text, type = 'status'}) => { } } -describe('Statuses.prepareStatus', () => { - it('sets deleted flag to false', () => { - const aStatus = makeMockStatus({id: '1', text: 'Hello oniichan'}) - expect(prepareStatus(aStatus).deleted).to.eq(false) +describe('Statuses module', () => { + describe('prepareStatus', () => { + it('sets deleted flag to false', () => { + const aStatus = makeMockStatus({id: '1', text: 'Hello oniichan'}) + expect(prepareStatus(aStatus).deleted).to.eq(false) + }) }) -}) -describe('The Statuses module', () => { - it('adds the status to allStatuses and to the given timeline', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) + describe('addNewStatuses', () => { + it('adds the status to allStatuses and to the given timeline', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) - mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) - expect(state.allStatuses).to.eql([status]) - expect(state.timelines.public.statuses).to.eql([status]) - expect(state.timelines.public.visibleStatuses).to.eql([]) - expect(state.timelines.public.newStatusCount).to.equal(1) - }) + expect(state.allStatuses).to.eql([status]) + expect(state.timelines.public.statuses).to.eql([status]) + expect(state.timelines.public.visibleStatuses).to.eql([]) + expect(state.timelines.public.newStatusCount).to.equal(1) + }) - it('counts the status as new if it has not been seen on this timeline', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) + it('counts the status as new if it has not been seen on this timeline', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) - mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) - mutations.addNewStatuses(state, { statuses: [status], timeline: 'friends' }) + mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [status], timeline: 'friends' }) - expect(state.allStatuses).to.eql([status]) - expect(state.timelines.public.statuses).to.eql([status]) - expect(state.timelines.public.visibleStatuses).to.eql([]) - expect(state.timelines.public.newStatusCount).to.equal(1) + expect(state.allStatuses).to.eql([status]) + expect(state.timelines.public.statuses).to.eql([status]) + expect(state.timelines.public.visibleStatuses).to.eql([]) + expect(state.timelines.public.newStatusCount).to.equal(1) - expect(state.allStatuses).to.eql([status]) - expect(state.timelines.friends.statuses).to.eql([status]) - expect(state.timelines.friends.visibleStatuses).to.eql([]) - expect(state.timelines.friends.newStatusCount).to.equal(1) - }) + expect(state.allStatuses).to.eql([status]) + expect(state.timelines.friends.statuses).to.eql([status]) + expect(state.timelines.friends.visibleStatuses).to.eql([]) + expect(state.timelines.friends.newStatusCount).to.equal(1) + }) - it('add the statuses to allStatuses if no timeline is given', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) + it('add the statuses to allStatuses if no timeline is given', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) - mutations.addNewStatuses(state, { statuses: [status] }) + mutations.addNewStatuses(state, { statuses: [status] }) - expect(state.allStatuses).to.eql([status]) - expect(state.timelines.public.statuses).to.eql([]) - expect(state.timelines.public.visibleStatuses).to.eql([]) - expect(state.timelines.public.newStatusCount).to.equal(0) - }) + expect(state.allStatuses).to.eql([status]) + expect(state.timelines.public.statuses).to.eql([]) + expect(state.timelines.public.visibleStatuses).to.eql([]) + expect(state.timelines.public.newStatusCount).to.equal(0) + }) - it('adds the status to allStatuses and to the given timeline, directly visible', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) + it('adds the status to allStatuses and to the given timeline, directly visible', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - expect(state.allStatuses).to.eql([status]) - expect(state.timelines.public.statuses).to.eql([status]) - expect(state.timelines.public.visibleStatuses).to.eql([status]) - expect(state.timelines.public.newStatusCount).to.equal(0) - }) + expect(state.allStatuses).to.eql([status]) + expect(state.timelines.public.statuses).to.eql([status]) + expect(state.timelines.public.visibleStatuses).to.eql([status]) + expect(state.timelines.public.newStatusCount).to.equal(0) + }) - it('removes statuses by tag on deletion', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - const otherStatus = makeMockStatus({id: '3'}) - status.uri = 'xxx' - const deletion = makeMockStatus({id: '2', type: 'deletion'}) - deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.' - deletion.uri = 'xxx' - - mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' }) - mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' }) - - expect(state.allStatuses).to.eql([otherStatus]) - expect(state.timelines.public.statuses).to.eql([otherStatus]) - expect(state.timelines.public.visibleStatuses).to.eql([otherStatus]) - expect(state.timelines.public.maxId).to.eql('3') - }) + it('removes statuses by tag on deletion', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + const otherStatus = makeMockStatus({id: '3'}) + status.uri = 'xxx' + const deletion = makeMockStatus({id: '2', type: 'deletion'}) + deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.' + deletion.uri = 'xxx' - it('does not update the maxId when the noIdUpdate flag is set', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - const secondStatus = makeMockStatus({id: '2'}) + mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' }) - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - expect(state.timelines.public.maxId).to.eql('1') + expect(state.allStatuses).to.eql([otherStatus]) + expect(state.timelines.public.statuses).to.eql([otherStatus]) + expect(state.timelines.public.visibleStatuses).to.eql([otherStatus]) + expect(state.timelines.public.maxId).to.eql('3') + }) - mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true }) - expect(state.timelines.public.statuses).to.eql([secondStatus, status]) - expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status]) - expect(state.timelines.public.maxId).to.eql('1') - }) + it('does not update the maxId when the noIdUpdate flag is set', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + const secondStatus = makeMockStatus({id: '2'}) - it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => { - const state = defaultState() - const nonVisibleStatus = makeMockStatus({id: '1'}) - const status = makeMockStatus({id: '3'}) - const statusTwo = makeMockStatus({id: '2'}) - const statusThree = makeMockStatus({id: '4'}) + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + expect(state.timelines.public.maxId).to.eql('1') - mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true }) + expect(state.timelines.public.statuses).to.eql([secondStatus, status]) + expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status]) + expect(state.timelines.public.maxId).to.eql('1') + }) - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' }) + it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => { + const state = defaultState() + const nonVisibleStatus = makeMockStatus({id: '1'}) + const status = makeMockStatus({id: '3'}) + const statusTwo = makeMockStatus({id: '2'}) + const statusThree = makeMockStatus({id: '4'}) - expect(state.timelines.public.minVisibleId).to.equal('2') + mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' }) - mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' }) - expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus]) - expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo]) - }) + expect(state.timelines.public.minVisibleId).to.equal('2') - it('splits retweets from their status and links them', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - const retweet = makeMockStatus({id: '2', type: 'retweet'}) - const modStatus = makeMockStatus({id: '1', text: 'something else'}) - - retweet.retweeted_status = 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.timelines.public.statuses).to.have.length(1) - expect(state.allStatuses).to.have.length(2) - expect(state.allStatuses[0].id).to.equal('1') - expect(state.allStatuses[1].id).to.equal('2') - - // It refers to the modified status. - mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' }) - expect(state.allStatuses).to.have.length(2) - expect(state.allStatuses[0].id).to.equal('1') - expect(state.allStatuses[0].text).to.equal(modStatus.text) - expect(state.allStatuses[1].id).to.equal('2') - expect(retweet.retweeted_status.text).to.eql(modStatus.text) - }) + mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' }) - it('replaces existing statuses with the same id', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - const modStatus = makeMockStatus({id: '1', text: 'something else'}) - - // Add original status - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - expect(state.timelines.public.visibleStatuses).to.have.length(1) - expect(state.allStatuses).to.have.length(1) - - // Add new version of status - mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' }) - expect(state.timelines.public.visibleStatuses).to.have.length(1) - expect(state.allStatuses).to.have.length(1) - expect(state.allStatuses[0].text).to.eql(modStatus.text) - }) + expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus]) + expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo]) + }) - it('replaces existing statuses with the same id, coming from a retweet', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - const modStatus = makeMockStatus({id: '1', text: 'something else'}) - const retweet = makeMockStatus({id: '2', type: 'retweet'}) - retweet.retweeted_status = modStatus - - // Add original status - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - expect(state.timelines.public.visibleStatuses).to.have.length(1) - expect(state.allStatuses).to.have.length(1) - - // Add new version of status - mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' }) - expect(state.timelines.public.visibleStatuses).to.have.length(1) - // Don't add the retweet itself if the tweet is visible - expect(state.timelines.public.statuses).to.have.length(1) - expect(state.allStatuses).to.have.length(2) - expect(state.allStatuses[0].text).to.eql(modStatus.text) + it('splits retweets from their status and links them', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + const retweet = makeMockStatus({id: '2', type: 'retweet'}) + const modStatus = makeMockStatus({id: '1', text: 'something else'}) + + retweet.retweeted_status = 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.timelines.public.statuses).to.have.length(1) + expect(state.allStatuses).to.have.length(2) + expect(state.allStatuses[0].id).to.equal('1') + expect(state.allStatuses[1].id).to.equal('2') + + // It refers to the modified status. + mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' }) + expect(state.allStatuses).to.have.length(2) + expect(state.allStatuses[0].id).to.equal('1') + expect(state.allStatuses[0].text).to.equal(modStatus.text) + expect(state.allStatuses[1].id).to.equal('2') + expect(retweet.retweeted_status.text).to.eql(modStatus.text) + }) + + it('replaces existing statuses with the same id', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + const modStatus = makeMockStatus({id: '1', text: 'something else'}) + + // Add original status + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + expect(state.timelines.public.visibleStatuses).to.have.length(1) + expect(state.allStatuses).to.have.length(1) + + // Add new version of status + mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' }) + expect(state.timelines.public.visibleStatuses).to.have.length(1) + expect(state.allStatuses).to.have.length(1) + expect(state.allStatuses[0].text).to.eql(modStatus.text) + }) + + it('replaces existing statuses with the same id, coming from a retweet', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + const modStatus = makeMockStatus({id: '1', text: 'something else'}) + const retweet = makeMockStatus({id: '2', type: 'retweet'}) + retweet.retweeted_status = modStatus + + // Add original status + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + expect(state.timelines.public.visibleStatuses).to.have.length(1) + expect(state.allStatuses).to.have.length(1) + + // Add new version of status + mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' }) + expect(state.timelines.public.visibleStatuses).to.have.length(1) + // Don't add the retweet itself if the tweet is visible + expect(state.timelines.public.statuses).to.have.length(1) + expect(state.allStatuses).to.have.length(2) + expect(state.allStatuses[0].text).to.eql(modStatus.text) + }) + + it('handles favorite actions', () => { + const state = defaultState() + const status = makeMockStatus({id: '1'}) + + const favorite = { + id: '2', + type: 'favorite', + 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: { id: '99' } + } + + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + 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) + + // 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 but does not increment counter to avoid over-counting. Counter is incremented (updated, really) via response to the favorite request. + const user = { + id: '1' + } + + const ownFavorite = { + id: '3', + type: 'favorite', + 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(1) + expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true) + }) }) - it('handles favorite actions', () => { - const state = defaultState() - const status = makeMockStatus({id: '1'}) - - const favorite = { - id: '2', - type: 'favorite', - 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: { id: '99' } - } - - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) - 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) - - // 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 but does not increment counter to avoid over-counting. Counter is incremented (updated, really) via response to the favorite request. - const user = { - id: '1' - } - - const ownFavorite = { - id: '3', - type: 'favorite', - 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(1) - expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true) + describe('showNewStatuses', () => { + it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => { + const state = defaultState() + const status = makeMockStatus({ id: '10' }) + mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + const newStatus = makeMockStatus({ id: '20' }) + mutations.addNewStatuses(state, { statuses: [newStatus], showImmediately: false, timeline: 'public' }) + state.timelines.public.minId = '5' + mutations.showNewStatuses(state, { timeline: 'public' }) + + expect(state.timelines.public.visibleStatuses.length).to.eql(2) + expect(state.timelines.public.minVisibleId).to.eql('10') + expect(state.timelines.public.minId).to.eql('10') + }) }) - it('keeps userId when clearing user timeline', () => { - const state = defaultState() - state.timelines.user.userId = 123 + describe('clearTimeline', () => { + it('keeps userId when clearing user timeline', () => { + const state = defaultState() + state.timelines.user.userId = 123 - mutations.clearTimeline(state, { timeline: 'user' }) + mutations.clearTimeline(state, { timeline: 'user' }) - expect(state.timelines.user.userId).to.eql(123) + expect(state.timelines.user.userId).to.eql(123) + }) }) describe('notifications', () => { -- cgit v1.2.3-70-g09d2