2021-05-22 07:33:57 +02:00
|
|
|
import generator, { Entity, FilterContext, NotificationType } from 'megalodon'
|
2020-12-25 17:26:20 +01:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
|
2019-04-14 09:42:00 +02:00
|
|
|
import { RootState } from '@/store'
|
2021-06-10 08:27:04 +02:00
|
|
|
import { LocalMarker } from '~/src/types/localMarker'
|
2019-12-04 15:03:21 +01:00
|
|
|
import { MyWindow } from '~/src/types/global'
|
|
|
|
|
2020-04-11 11:22:49 +02:00
|
|
|
const win = (window as any) 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>
|
2021-08-28 17:41:07 +02:00
|
|
|
scrolling: boolean
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const state = (): NotificationsState => ({
|
|
|
|
lazyLoading: false,
|
|
|
|
heading: true,
|
2021-08-28 17:41:07 +02:00
|
|
|
notifications: [],
|
|
|
|
scrolling: false
|
2019-04-14 09:42:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export const MUTATION_TYPES = {
|
|
|
|
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
|
|
|
CHANGE_HEADING: 'changeHeading',
|
|
|
|
APPEND_NOTIFICATIONS: 'appendNotifications',
|
|
|
|
UPDATE_NOTIFICATIONS: 'updateNotifications',
|
|
|
|
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',
|
2021-08-28 17:41:07 +02:00
|
|
|
ARCHIVE_NOTIFICATIONS: 'archiveNotifications',
|
|
|
|
CHANGE_SCROLLING: 'changeScrolling'
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-07-25 14:51:51 +02:00
|
|
|
if (!state.notifications.find(item => item.id === notification.id)) {
|
|
|
|
state.notifications = [notification].concat(state.notifications)
|
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
|
|
|
|
},
|
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)
|
2021-08-28 17:41:07 +02:00
|
|
|
},
|
|
|
|
[MUTATION_TYPES.CHANGE_SCROLLING]: (state, value: boolean) => {
|
|
|
|
state.scrolling = value
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-06-30 17:07:55 +02:00
|
|
|
rootState.App.userAgent
|
2019-10-27 06:41:03 +01:00
|
|
|
)
|
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,
|
2020-06-30 17:07:55 +02:00
|
|
|
rootState.App.userAgent
|
2019-10-27 06:41:03 +01:00
|
|
|
)
|
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')
|
2021-06-10 08:27:04 +02:00
|
|
|
},
|
|
|
|
saveMarker: async ({ rootState }, id: string) => {
|
|
|
|
await win.ipcRenderer.invoke('save-marker', {
|
|
|
|
owner_id: rootState.TimelineSpace.account._id,
|
|
|
|
timeline: 'notifications',
|
|
|
|
last_read_id: id
|
|
|
|
} as LocalMarker)
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 17:26:20 +01:00
|
|
|
const getters: GetterTree<NotificationsState, RootState> = {
|
|
|
|
handledNotifications: state => {
|
|
|
|
return state.notifications.filter(n => {
|
|
|
|
switch (n.type) {
|
2021-02-16 16:48:14 +01:00
|
|
|
case NotificationType.Follow:
|
|
|
|
case NotificationType.Favourite:
|
|
|
|
case NotificationType.Reblog:
|
|
|
|
case NotificationType.Mention:
|
|
|
|
case NotificationType.EmojiReaction:
|
|
|
|
case NotificationType.FollowRequest:
|
2021-03-02 02:59:41 +01:00
|
|
|
case NotificationType.Status:
|
2021-03-23 16:46:31 +01:00
|
|
|
case NotificationType.PollVote:
|
2021-03-23 17:14:54 +01:00
|
|
|
case NotificationType.PollExpired:
|
2020-12-25 17:26:20 +01:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
2021-05-22 07:33:57 +02:00
|
|
|
},
|
|
|
|
filters: (_state, _getters, rootState) => {
|
|
|
|
return rootState.TimelineSpace.filters.filter(f => f.context.includes(FilterContext.Notifications) && !f.irreversible)
|
2020-12-25 17:26:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 09:42:00 +02:00
|
|
|
const Notifications: Module<NotificationsState, RootState> = {
|
|
|
|
namespaced: true,
|
|
|
|
state: state,
|
|
|
|
mutations: mutations,
|
2020-12-25 17:26:20 +01:00
|
|
|
actions: actions,
|
|
|
|
getters: getters
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Notifications
|