forked from AkkomaGang/akkoma-fe
Add first notification.
This adds a favorite notification.
This commit is contained in:
parent
4cef86f2a8
commit
e7637e4196
2 changed files with 37 additions and 3 deletions
|
@ -6,6 +6,7 @@ import apiService from '../services/api/api.service.js'
|
||||||
export const defaultState = {
|
export const defaultState = {
|
||||||
allStatuses: [],
|
allStatuses: [],
|
||||||
maxId: 0,
|
maxId: 0,
|
||||||
|
notifications: [],
|
||||||
timelines: {
|
timelines: {
|
||||||
public: {
|
public: {
|
||||||
statuses: [],
|
statuses: [],
|
||||||
|
@ -99,7 +100,7 @@ const mergeOrAdd = (arr, item) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mutations = {
|
export const mutations = {
|
||||||
addNewStatuses (state, { statuses, showImmediately = false, timeline }) {
|
addNewStatuses (state, { statuses, showImmediately = false, timeline, user = {} }) {
|
||||||
const allStatuses = state.allStatuses
|
const allStatuses = state.allStatuses
|
||||||
const timelineObject = state.timelines[timeline]
|
const timelineObject = state.timelines[timeline]
|
||||||
|
|
||||||
|
@ -134,10 +135,17 @@ export const mutations = {
|
||||||
return status
|
return status
|
||||||
}
|
}
|
||||||
|
|
||||||
const favoriteStatus = (favorite) => {
|
const addNotification = (type, status) => {
|
||||||
|
state.notifications.push({type, status})
|
||||||
|
}
|
||||||
|
|
||||||
|
const favoriteStatus = (favorite, user) => {
|
||||||
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
|
||||||
|
if (status.user.id === user.id) {
|
||||||
|
addNotification('favorite', status)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return status
|
return status
|
||||||
}
|
}
|
||||||
|
@ -164,7 +172,7 @@ export const mutations = {
|
||||||
},
|
},
|
||||||
'favorite': (favorite) => {
|
'favorite': (favorite) => {
|
||||||
updateMaxId(favorite)
|
updateMaxId(favorite)
|
||||||
favoriteStatus(favorite)
|
favoriteStatus(favorite, user)
|
||||||
},
|
},
|
||||||
'deletion': ({uri}) => {
|
'deletion': ({uri}) => {
|
||||||
remove(allStatuses, { tag: uri })
|
remove(allStatuses, { tag: uri })
|
||||||
|
@ -216,6 +224,9 @@ export const mutations = {
|
||||||
const statuses = {
|
const statuses = {
|
||||||
state: defaultState,
|
state: defaultState,
|
||||||
actions: {
|
actions: {
|
||||||
|
addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline }) {
|
||||||
|
commit('addNewStatuses', { statuses, showImmediately, timeline, user: rootState.users.currentUser })
|
||||||
|
},
|
||||||
favorite ({ rootState, commit }, status) {
|
favorite ({ rootState, commit }, status) {
|
||||||
// Optimistic favoriting...
|
// Optimistic favoriting...
|
||||||
commit('setFavorited', { status, value: true })
|
commit('setFavorited', { status, value: true })
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { defaultState, mutations, findMaxId, prepareStatus } from '../../../../s
|
||||||
const makeMockStatus = ({id, text, is_post_verb = true}) => {
|
const makeMockStatus = ({id, text, is_post_verb = true}) => {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
|
user: {id: 0},
|
||||||
name: 'status',
|
name: 'status',
|
||||||
text: text || `Text number ${id}`,
|
text: text || `Text number ${id}`,
|
||||||
fave_num: 0,
|
fave_num: 0,
|
||||||
|
@ -193,4 +194,26 @@ describe('The Statuses module', () => {
|
||||||
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)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('notifications', () => {
|
||||||
|
it('adds a notfication when one of the user\'s status is favorited', () => {
|
||||||
|
const state = cloneDeep(defaultState)
|
||||||
|
const status = makeMockStatus({id: 1})
|
||||||
|
const user = {id: 1}
|
||||||
|
status.user = user
|
||||||
|
|
||||||
|
const favorite = {
|
||||||
|
id: 2,
|
||||||
|
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'
|
||||||
|
}
|
||||||
|
|
||||||
|
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public', user })
|
||||||
|
mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public', user })
|
||||||
|
|
||||||
|
expect(state.notifications).to.have.length(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue