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'
|
2021-12-26 11:58:24 +01:00
|
|
|
import { LoadingCard } from '@/types/loading-card'
|
2019-12-04 15:03:21 +01:00
|
|
|
|
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
|
2021-12-26 11:58:24 +01:00
|
|
|
notifications: Array<Entity.Notification | LoadingCard>
|
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',
|
2021-12-26 11:58:24 +01:00
|
|
|
CHANGE_SCROLLING: 'changeScrolling',
|
|
|
|
APPEND_NOTIFICATIONS_AFTER_LOADING_CARD: 'appendNotificationsAfterLoadingCard'
|
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)) {
|
2021-12-26 11:58:24 +01:00
|
|
|
state.notifications = ([notification] as Array<Entity.Notification | LoadingCard>).concat(state.notifications)
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
},
|
2021-12-26 11:58:24 +01:00
|
|
|
[MUTATION_TYPES.UPDATE_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
|
2019-04-14 09:42:00 +02:00
|
|
|
state.notifications = notifications
|
|
|
|
},
|
2021-12-26 11:58:24 +01:00
|
|
|
[MUTATION_TYPES.INSERT_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
|
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) => {
|
2021-12-26 11:58:24 +01:00
|
|
|
state.notifications = state.notifications.filter(notify => {
|
|
|
|
if (notify.id === 'loading-card') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
const notification = notify as Entity.Notification
|
2019-05-29 16:32:16 +02:00
|
|
|
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
|
2021-12-26 11:58:24 +01:00
|
|
|
},
|
|
|
|
[MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
|
|
|
|
const n = state.notifications.flatMap(notify => {
|
|
|
|
if (notify.id !== 'loading-card') {
|
|
|
|
return notify
|
|
|
|
} else {
|
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Reject duplicated status in timeline
|
|
|
|
state.notifications = Array.from(new Set(n))
|
2019-04-14 09:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions: ActionTree<NotificationsState, RootState> = {
|
2021-12-26 11:58:24 +01:00
|
|
|
fetchNotifications: async ({ dispatch, commit, rootState }): Promise<Array<Entity.Notification>> => {
|
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
|
|
|
)
|
2021-12-26 11:58:24 +01:00
|
|
|
|
|
|
|
const localMarker: LocalMarker | null = await dispatch('getMarker').catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
|
|
|
|
2021-12-26 17:11:20 +01:00
|
|
|
if (rootState.App.useMarkerTimeline.includes('notifications') && localMarker !== null) {
|
2021-12-26 11:58:24 +01:00
|
|
|
// The result does not contain max_id's notification, when we specify max_id parameter in get notifications.
|
|
|
|
// So we need to get max_id's notification.
|
|
|
|
const last = await client.getNotification(localMarker.last_read_id)
|
|
|
|
const lastReadNotification = last.data
|
|
|
|
|
|
|
|
let notifications: Array<Entity.Notification | LoadingCard> = [lastReadNotification]
|
|
|
|
const card: LoadingCard = {
|
|
|
|
type: 'middle-load',
|
|
|
|
since_id: lastReadNotification.id,
|
|
|
|
// We don't need to fill this field in the first fetcing.
|
|
|
|
// Because in most cases there is no new statuses at the first fetching.
|
|
|
|
// After new statuses are received, if the number of unread statuses is more than 30, max_id is not necessary.
|
|
|
|
// We can fill max_id when calling fetchTimelineSince.
|
|
|
|
// If the number of unread statuses is less than 30, max_id is necessary, but it is enough to reject duplicated statuses.
|
|
|
|
// So we do it in mutation.
|
|
|
|
max_id: null,
|
|
|
|
id: 'loading-card'
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await client.getNotifications({ limit: 30, max_id: localMarker.last_read_id })
|
|
|
|
// Make sure whether new notifications exist or not
|
|
|
|
const nextResponse = await client.getNotifications({ limit: 1, min_id: lastReadNotification.id })
|
|
|
|
if (nextResponse.data.length > 0) {
|
|
|
|
notifications = ([card] as Array<Entity.Notification | LoadingCard>).concat(notifications).concat(res.data)
|
|
|
|
} else {
|
|
|
|
notifications = notifications.concat(res.data)
|
|
|
|
}
|
|
|
|
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, notifications)
|
|
|
|
return res.data
|
|
|
|
} else {
|
|
|
|
const res = await client.getNotifications({ limit: 30 })
|
|
|
|
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, res.data)
|
|
|
|
return res.data
|
|
|
|
}
|
2019-04-14 09:42:00 +02:00
|
|
|
},
|
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)
|
|
|
|
})
|
|
|
|
},
|
2021-12-26 11:58:24 +01:00
|
|
|
fetchNotificationsSince: async ({ state, rootState, commit }, since_id: string): Promise<Array<Entity.Notification> | null> => {
|
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
|
|
|
rootState.App.userAgent
|
|
|
|
)
|
|
|
|
const cardIndex = state.notifications.findIndex(s => {
|
|
|
|
if (s.id === 'loading-card') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
let maxID: string | null = null
|
|
|
|
if (cardIndex > 0) {
|
|
|
|
maxID = state.notifications[cardIndex - 1].id
|
|
|
|
}
|
2021-12-26 15:15:31 +01:00
|
|
|
let params = { min_id: since_id, limit: 30 }
|
|
|
|
if (maxID !== null) {
|
|
|
|
params = Object.assign({}, params, {
|
|
|
|
max_id: maxID
|
|
|
|
})
|
|
|
|
}
|
2021-12-26 11:58:24 +01:00
|
|
|
|
2021-12-26 15:15:31 +01:00
|
|
|
const res = await client.getNotifications(params)
|
2021-12-26 11:58:24 +01:00
|
|
|
if (res.data.length >= 30) {
|
|
|
|
const card: LoadingCard = {
|
|
|
|
type: 'middle-load',
|
|
|
|
since_id: res.data[0].id,
|
|
|
|
max_id: maxID,
|
|
|
|
id: 'loading-card'
|
|
|
|
}
|
|
|
|
let notifications: Array<Entity.Notification | LoadingCard> = [card]
|
|
|
|
notifications = notifications.concat(res.data)
|
|
|
|
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, notifications)
|
|
|
|
} else {
|
|
|
|
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, res.data)
|
|
|
|
}
|
|
|
|
return res.data
|
|
|
|
},
|
2019-04-14 09:42:00 +02:00
|
|
|
resetBadge: () => {
|
2019-12-04 15:03:21 +01:00
|
|
|
win.ipcRenderer.send('reset-badge')
|
2021-06-10 08:27:04 +02:00
|
|
|
},
|
2021-12-26 11:58:24 +01:00
|
|
|
getMarker: async ({ rootState }): Promise<LocalMarker | null> => {
|
2021-12-26 17:11:20 +01:00
|
|
|
if (!rootState.App.useMarkerTimeline.includes('notifications')) {
|
2021-12-26 11:58:24 +01:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
|
|
|
rootState.App.userAgent
|
|
|
|
)
|
|
|
|
let serverMarker: Entity.Marker | {} = {}
|
|
|
|
try {
|
|
|
|
const res = await client.getMarkers(['notifications'])
|
|
|
|
serverMarker = res.data
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
|
|
|
}
|
|
|
|
if ((serverMarker as Entity.Marker).notifications !== undefined) {
|
|
|
|
return {
|
|
|
|
timeline: 'notifications',
|
|
|
|
last_read_id: (serverMarker as Entity.Marker).notifications.last_read_id
|
|
|
|
} as LocalMarker
|
|
|
|
}
|
|
|
|
const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-notifications-marker', rootState.TimelineSpace.account._id)
|
|
|
|
return localMarker
|
|
|
|
},
|
|
|
|
saveMarker: async ({ state, rootState }) => {
|
|
|
|
const notifications = state.notifications
|
|
|
|
if (notifications.length === 0 || notifications[0].id === 'loading-card') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
win.ipcRenderer.send('save-marker', {
|
2021-06-10 08:27:04 +02:00
|
|
|
owner_id: rootState.TimelineSpace.account._id,
|
|
|
|
timeline: 'notifications',
|
2021-12-26 11:58:24 +01:00
|
|
|
last_read_id: notifications[0].id
|
2021-06-10 08:27:04 +02:00
|
|
|
} as LocalMarker)
|
2021-12-26 11:58:24 +01:00
|
|
|
|
|
|
|
if (rootState.TimelineSpace.sns === 'misskey') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const client = generator(
|
|
|
|
rootState.TimelineSpace.sns,
|
|
|
|
rootState.TimelineSpace.account.baseURL,
|
|
|
|
rootState.TimelineSpace.account.accessToken,
|
|
|
|
rootState.App.userAgent
|
|
|
|
)
|
|
|
|
const res = await client.saveMarkers({ notifications: { last_read_id: notifications[0].id } })
|
|
|
|
return res.data
|
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-12-26 11:58:24 +01:00
|
|
|
case 'middle-load':
|
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
|