2020-03-15 09:47:56 +01:00
|
|
|
import generator, { Entity } from 'megalodon'
|
2019-04-14 09:42:00 +02:00
|
|
|
import { Module, MutationTree, ActionTree } from 'vuex'
|
|
|
|
import { RootState } from '@/store'
|
2019-12-04 15:03:21 +01:00
|
|
|
import { MyWindow } from '~/src/types/global'
|
|
|
|
|
|
|
|
const win = window as MyWindow
|
2019-04-14 09:42:00 +02:00
|
|
|
|
2019-06-06 16:44:50 +02:00
|
|
|
export type NotificationsState = {
|
2019-05-18 15:28:53 +02:00
|
|
|
lazyLoading: boolean
|
|
|
|
heading: boolean
|
2020-03-15 09:47:56 +01:00
|
|
|
notifications: Array<Entity.Notification>
|
|
|
|
unreadNotifications: Array<Entity.Notification>
|
2019-04-14 09:42:00 +02:00
|
|
|
filter: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = (): NotificationsState => ({
|
|
|
|
lazyLoading: false,
|
|
|
|
heading: true,
|
|
|
|
notifications: [],
|
|
|
|
unreadNotifications: [],
|
|
|
|
filter: ''
|
|
|
|
})
|
|
|
|
|
|
|
|
export const MUTATION_TYPES = {
|
|
|
|
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
|
|
|
CHANGE_HEADING: 'changeHeading',
|
|
|
|
APPEND_NOTIFICATIONS: 'appendNotifications',
|
|
|
|
UPDATE_NOTIFICATIONS: 'updateNotifications',
|
|
|
|
MERGE_NOTIFICATIONS: 'mergeNotifications',
|
|
|
|
INSERT_NOTIFICATIONS: 'insertNotifications',
|
|
|
|
UPDATE_TOOT: 'updateToot',
|
2019-05-29 16:32:16 +02:00
|
|
|
DELETE_TOOT: 'deleteToot',
|
2019-04-14 09:42:00 +02:00
|
|
|
CLEAR_NOTIFICATIONS: 'clearNotifications',
|
|
|
|
ARCHIVE_NOTIFICATIONS: 'archiveNotifications',
|
|
|
|
CHANGE_FILTER: 'changeFilter'
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations: MutationTree<NotificationsState> = {
|
|
|
|
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
|
|
|
|
state.lazyLoading = value
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
|
|
|
|
state.heading = value
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.APPEND_NOTIFICATIONS]: (state, notification: Entity.Notification) => {
|
2019-11-13 15:42:50 +01:00
|
|
|
// Reject duplicated status in timeline
|
|
|
|
if (
|
|
|
|
!state.notifications.find(item => item.id === notification.id) &&
|
|
|
|
!state.unreadNotifications.find(item => item.id === notification.id)
|
|
|
|
) {
|
|
|
|
if (state.heading) {
|
|
|
|
state.notifications = [notification].concat(state.notifications)
|
|
|
|
} else {
|
|
|
|
state.unreadNotifications = [notification].concat(state.unreadNotifications)
|
|
|
|
}
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.UPDATE_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification>) => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = notifications
|
|
|
|
},
|
2019-05-18 15:28:53 +02:00
|
|
|
[MUTATION_TYPES.MERGE_NOTIFICATIONS]: state => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = state.unreadNotifications.slice(0, 80).concat(state.notifications)
|
|
|
|
state.unreadNotifications = []
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.INSERT_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification>) => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = state.notifications.concat(notifications)
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Entity.Status) => {
|
2019-05-18 15:28:53 +02:00
|
|
|
state.notifications = state.notifications.map(notification => {
|
2019-04-14 09:42:00 +02:00
|
|
|
// I want to update toot only mention.
|
|
|
|
// Because Toot component don't use status information when other patterns.
|
2019-05-18 15:28:53 +02:00
|
|
|
if (notification.type === 'mention' && notification.status && notification.status.id === message.id) {
|
2019-04-14 09:42:00 +02:00
|
|
|
const status = {
|
|
|
|
status: message
|
|
|
|
}
|
|
|
|
return Object.assign(notification, status)
|
|
|
|
} else {
|
|
|
|
return notification
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2019-05-29 16:32:16 +02:00
|
|
|
[MUTATION_TYPES.DELETE_TOOT]: (state, id: string) => {
|
|
|
|
state.notifications = state.notifications.filter(notification => {
|
|
|
|
if (notification.status) {
|
|
|
|
if (notification.status.reblog && notification.status.reblog.id === id) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return notification.status.id !== id
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2019-05-18 15:28:53 +02:00
|
|
|
[MUTATION_TYPES.CLEAR_NOTIFICATIONS]: state => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = []
|
|
|
|
},
|
2019-05-18 15:28:53 +02:00
|
|
|
[MUTATION_TYPES.ARCHIVE_NOTIFICATIONS]: state => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = state.notifications.slice(0, 30)
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
|
|
|
|
state.filter = filter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions: ActionTree<NotificationsState, RootState> = {
|
2020-03-15 09:47:56 +01:00
|
|
|
fetchNotifications: async ({ commit, rootState }): Promise<Array<Entity.Notification>> => {
|
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2019-10-27 06:41:03 +01:00
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2020-03-15 09:47:56 +01:00
|
|
|
const res = await client.getNotifications({ limit: 30 })
|
2019-04-14 09:42:00 +02:00
|
|
|
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, res.data)
|
|
|
|
return res.data
|
|
|
|
},
|
2020-03-15 09:47:56 +01:00
|
|
|
lazyFetchNotifications: (
|
|
|
|
{ state, commit, rootState },
|
|
|
|
lastNotification: Entity.Notification
|
|
|
|
): Promise<Array<Entity.Notification> | null> => {
|
2019-04-14 09:42:00 +02:00
|
|
|
if (state.lazyLoading) {
|
|
|
|
return Promise.resolve(null)
|
|
|
|
}
|
|
|
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
|
2020-03-15 09:47:56 +01:00
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
2019-10-27 06:41:03 +01:00
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2019-05-18 15:28:53 +02:00
|
|
|
return client
|
2020-03-15 09:47:56 +01:00
|
|
|
.getNotifications({ max_id: lastNotification.id, limit: 30 })
|
2019-04-14 09:42:00 +02:00
|
|
|
.then(res => {
|
|
|
|
commit(MUTATION_TYPES.INSERT_NOTIFICATIONS, res.data)
|
|
|
|
return res.data
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
resetBadge: () => {
|
2019-12-04 15:03:21 +01:00
|
|
|
win.ipcRenderer.send('reset-badge')
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Notifications: Module<NotificationsState, RootState> = {
|
|
|
|
namespaced: true,
|
|
|
|
state: state,
|
|
|
|
mutations: mutations,
|
|
|
|
actions: actions
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notifications
|