[refactor] Notifications store

This commit is contained in:
AkiraFukushima 2023-01-02 19:15:27 +09:00
parent 3b46bda835
commit e72a7e7efe
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
9 changed files with 175 additions and 438 deletions

View File

@ -1,259 +0,0 @@
import { Entity } from 'megalodon'
import Notifications, { NotificationsState, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Notifications'
const account1: Entity.Account = {
id: '1',
username: 'h3poteto',
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://pleroma.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const account2: Entity.Account = {
id: '2',
username: 'h3poteto',
acct: 'h3poteto@mstdn.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://mstdn.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const status1: Entity.Status = {
id: '1',
uri: 'http://example.com',
url: 'http://example.com',
account: account1,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
const status2: Entity.Status = {
id: '2',
uri: 'http://example.com',
url: 'http://example.com',
account: account1,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
const rebloggedStatus: Entity.Status = {
id: '3',
uri: 'http://example.com',
url: 'http://example.com',
account: account1,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: status2,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
const notification1: Entity.Notification = {
id: '1',
account: account2,
status: status1,
type: 'favourite',
created_at: '2019-04-01T17:01:32'
}
const notification2: Entity.Notification = {
id: '2',
account: account2,
status: rebloggedStatus,
type: 'mention',
created_at: '2019-04-01T17:01:32'
}
describe('TimelineSpace/Contents/Notifications', () => {
describe('mutations', () => {
let state: NotificationsState
describe('deleteToot', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
notifications: [notification2, notification1]
}
})
describe('message is not reblogged', () => {
it('should be deleted', () => {
Notifications.mutations![MUTATION_TYPES.DELETE_TOOT](state, notification1.status!.id)
expect(state.notifications.length).toEqual(1)
})
})
describe('message is reblogged', () => {
it('should be deleted', () => {
Notifications.mutations![MUTATION_TYPES.DELETE_TOOT](state, notification2.status!.id)
expect(state.notifications.length).toEqual(1)
})
})
})
describe('appendTimeline', () => {
describe('heading', () => {
describe('normal', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
notifications: [notification1]
}
})
it('should update timeline', () => {
Notifications.mutations![MUTATION_TYPES.APPEND_NOTIFICATIONS](state, notification2)
expect(state.notifications).toEqual([notification2, notification1])
})
})
describe('duplicated status', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
notifications: [notification2, notification1]
}
})
it('should not update timeline', () => {
Notifications.mutations![MUTATION_TYPES.APPEND_NOTIFICATIONS](state, notification2)
expect(state.notifications).toEqual([notification2, notification1])
})
})
})
describe('not heading', () => {
describe('normal', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: false,
notifications: [notification1]
}
})
it('should update timeline', () => {
Notifications.mutations![MUTATION_TYPES.APPEND_NOTIFICATIONS](state, notification2)
expect(state.notifications).toEqual([notification2, notification1])
})
})
describe('duplicated status', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: false,
notifications: [notification2, notification1]
}
})
it('should not update timeline', () => {
Notifications.mutations![MUTATION_TYPES.APPEND_NOTIFICATIONS](state, notification2)
expect(state.notifications).toEqual([notification2, notification1])
})
})
})
})
})
})

View File

@ -1,7 +1,6 @@
<template>
<div id="notifications">
<div></div>
<DynamicScroller :items="handledNotifications" :min-item-size="20" id="scroller" class="scroller" ref="scroller">
<DynamicScroller :items="notifications" :min-item-size="20" id="scroller" class="scroller" ref="scroller">
<template v-slot="{ item, index, active }">
<template v-if="item.id === 'loading-card'">
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.id]" :data-index="index" :watchData="true">
@ -11,10 +10,13 @@
<template v-else>
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.url]" :data-index="index" :watchData="true">
<notification
v-if="account.account && account.server"
:message="item"
:focused="item.id === focusedId"
:overlaid="modalOpened"
:filters="filters"
:account="account.account"
:server="account.server"
v-on:update="updateToot"
@focusRight="focusSidebar"
@selectNotification="focusNotification(item)"
@ -33,7 +35,7 @@
</template>
<script lang="ts">
import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onUnmounted, watch } from 'vue'
import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onUnmounted, watch, reactive } from 'vue'
import { logicAnd } from '@vueuse/math'
import { useMagicKeys, whenever } from '@vueuse/core'
import { ElMessage } from 'element-plus'
@ -49,6 +51,9 @@ import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Not
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
import { MyWindow } from '~/src/types/global'
export default defineComponent({
name: 'notifications',
@ -62,28 +67,39 @@ export default defineComponent({
const { reloadable } = useReloadable(store, route, i18n)
const { j, k, Ctrl_r } = useMagicKeys()
const win = (window as any) as MyWindow
const id = computed(() => parseInt(route.params.id as string))
const focusedId = ref<string | null>(null)
const loadingMore = ref(false)
const scroller = ref<any>()
const lazyLoading = ref(false)
const heading = ref(true)
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
const notifications = computed(() => store.state.TimelineSpace.Contents.Notifications.notifications)
const lazyLoading = computed(() => store.state.TimelineSpace.Contents.Notifications.lazyLoading)
const heading = computed(() => store.state.TimelineSpace.Contents.Notifications.heading)
const notifications = computed(() => store.state.TimelineSpace.Contents.Notifications.notifications[id.value])
const openSideBar = computed(() => store.state.TimelineSpace.Contents.SideBar.openSideBar)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
const modalOpened = computed<boolean>(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const filters = computed(() => store.getters[`${space}/filters}`])
const handledNotifications = computed(() => store.getters[`${space}/handledNotifications`])
const currentFocusedIndex = computed(() => notifications.value.findIndex(notification => focusedId.value === notification.id))
const shortcutEnabled = computed(() => !modalOpened.value)
onMounted(() => {
onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_NOTIFICATIONS}`, false)
store.dispatch(`${space}/${ACTION_TYPES.RESET_BADGE}`)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
if (heading.value && handledNotifications.value.length > 0) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`)
if (heading.value && notifications.value.length > 0) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`, account)
}
})
onBeforeUpdate(() => {
@ -92,8 +108,6 @@ export default defineComponent({
}
})
onUnmounted(() => {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
store.commit(`${space}/${MUTATION_TYPES.ARCHIVE_NOTIFICATIONS}`)
const el = document.getElementById('scroller')
if (el !== undefined && el !== null) {
el.removeEventListener('scroll', onScroll)
@ -110,23 +124,23 @@ export default defineComponent({
watch(
notifications,
(newState, _oldState) => {
if (heading.value && newState.length > 0) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`)
if (heading.value && newState.length > 0 && account.account && account.server) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`, account)
}
},
{ deep: true }
)
watch(focusedId, (newVal, _oldVal) => {
if (newVal && heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
heading.value = false
} else if (newVal === null && !heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
heading.value = true
store.commit(`${space}/${ACTION_TYPES.RESET_BADGE}`)
}
})
whenever(logicAnd(j, shortcutEnabled), () => {
if (focusedId.value === null) {
focusedId.value = handledNotifications.value[0].id
focusedId.value = notifications.value[0].id
} else {
focusNext()
}
@ -144,30 +158,46 @@ export default defineComponent({
document.getElementById('scroller')!.scrollHeight - 10 &&
!lazyLoading.value
) {
lazyLoading.value = true
store
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_NOTIFICATIONS}`, handledNotifications.value[handledNotifications.value.length - 1])
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_NOTIFICATIONS}`, {
lastNotification: notifications.value[notifications.value.length - 1],
account: account.account,
server: account.server
})
.catch(() => {
ElMessage({
message: i18n.t('message.notification_fetch_error'),
type: 'error'
})
})
.finally(() => {
lazyLoading.value = false
})
}
if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
heading.value = false
} else if ((event.target as HTMLElement)!.scrollTop <= 10 && !heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
heading.value = true
store.dispatch(`${space}/${ACTION_TYPES.RESET_BADGE}`)
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`)
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`, account)
}
}
const updateToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
const fetchNotificationsSince = (since_id: string) => {
loadingMore.value = true
store.dispatch(`${space}/${ACTION_TYPES.FETCH_NOTIFICATIONS_SINCE}`, since_id).finally(() => {
store
.dispatch(`${space}/${ACTION_TYPES.FETCH_NOTIFICATIONS_SINCE}`, {
sinceId: since_id,
account: account.account,
server: account.server
})
.finally(() => {
setTimeout(() => {
loadingMore.value = false
}, 500)
@ -188,16 +218,16 @@ export default defineComponent({
}
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
focusedId.value = handledNotifications.value[0].id
} else if (currentFocusedIndex.value < handledNotifications.value.length) {
focusedId.value = handledNotifications.value[currentFocusedIndex.value + 1].id
focusedId.value = notifications.value[0].id
} else if (currentFocusedIndex.value < notifications.value.length) {
focusedId.value = notifications.value[currentFocusedIndex.value + 1].id
}
}
const focusPrev = () => {
if (currentFocusedIndex.value === 0) {
focusedId.value = null
} else if (currentFocusedIndex.value > 0) {
focusedId.value = handledNotifications.value[currentFocusedIndex.value - 1].id
focusedId.value = notifications.value[currentFocusedIndex.value - 1].id
}
}
const focusNotification = (notification: Entity.Notification) => {
@ -208,7 +238,7 @@ export default defineComponent({
}
return {
handledNotifications,
notifications,
loadingMore,
fetchNotificationsSince,
focusedId,
@ -221,7 +251,8 @@ export default defineComponent({
focusNotification,
openSideBar,
heading,
upper
upper,
account
}
}
})

View File

@ -35,6 +35,8 @@
:filters="filters"
:focused="focused"
:overlaid="overlaid"
:account="account"
:server="server"
@update="updateToot"
@delete="deleteToot"
@focus-right="$emit('focusRight')"
@ -69,6 +71,8 @@
:filters="filters"
:focused="focused"
:overlaid="overlaid"
:account="account"
:server="server"
@focus-right="$emit('focusRight')"
@select="$emit('selectNotification')"
>
@ -117,6 +121,8 @@ import Follow from './Notification/Follow.vue'
import FollowRequest from './Notification/FollowRequest.vue'
import Mention from './Notification/Mention.vue'
import Status from './Notification/Status.vue'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
export default defineComponent({
name: 'Notification',
@ -143,6 +149,14 @@ export default defineComponent({
overlaid: {
type: Boolean,
default: () => false
},
account: {
type: Object as PropType<LocalAccount>,
required: true
},
server: {
type: Object as PropType<LocalServer>,
required: true
}
},
emits: ['focusRight', 'selectNotification', 'update', 'delete'],

View File

@ -5,6 +5,8 @@
:filters="filters"
:focused="focused"
:overlaid="overlaid"
:account="account"
:server="server"
v-on:update="updateToot"
v-on:delete="deleteToot"
@focusRight="$emit('focusRight')"
@ -18,6 +20,8 @@
import { defineComponent, PropType } from 'vue'
import { Entity } from 'megalodon'
import Toot from '../Toot.vue'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
export default defineComponent({
name: 'mention',
@ -37,6 +41,14 @@ export default defineComponent({
overlaid: {
type: Boolean,
default: false
},
account: {
type: Object as PropType<LocalAccount>,
required: true
},
server: {
type: Object as PropType<LocalServer>,
required: true
}
},
components: { Toot },

View File

@ -26,6 +26,8 @@
:filters="filters"
:focused="focused"
:overlaid="overlaid"
:account="account"
:server="server"
v-on:update="updateToot"
v-on:delete="deleteToot"
@focusRight="$emit('focusRight')"
@ -45,6 +47,8 @@ import Toot from '../Toot.vue'
import { usernameWithStyle } from '@/utils/username'
import { MUTATION_TYPES as SIDEBAR_MUTATION, ACTION_TYPES as SIDEBAR_ACTION } from '@/store/TimelineSpace/Contents/SideBar'
import { ACTION_TYPES as PROFILE_ACTION } from '@/store/TimelineSpace/Contents/SideBar/AccountProfile'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
export default defineComponent({
name: 'mention',
@ -64,6 +68,14 @@ export default defineComponent({
overlaid: {
type: Boolean,
default: false
},
account: {
type: Object as PropType<LocalAccount>,
required: true
},
server: {
type: Object as PropType<LocalServer>,
required: true
}
},
components: { Toot, FailoverImg },

View File

@ -683,8 +683,6 @@ export default defineComponent({
displayNameStyle,
timeFormat,
language,
server,
account,
originalMessage,
timestamp,
readableTimestamp,

View File

@ -120,6 +120,7 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
[ACTION_TYPES.LOAD_TIMELINES]: async ({ dispatch }, req: Array<[LocalAccount, LocalServer]>) => {
req.forEach(async ([account, server]) => {
await dispatch('TimelineSpace/Contents/Home/fetchTimeline', { account, server }, { root: true })
await dispatch('TimelineSpace/Contents/Notifications/fetchNotifications', { account, server }, { root: true })
})
},
[ACTION_TYPES.BIND_STREAMINGS]: async ({ commit }, req: Array<[LocalAccount, LocalServer]>) => {
@ -128,6 +129,15 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
win.ipcRenderer.on(`update-user-streamings-${account.id}`, (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Home/appendTimeline', { status: update, accountId: account.id }, { root: true })
})
win.ipcRenderer.removeAllListeners(`notification-user-streamings-${account.id}`)
win.ipcRenderer.on(`notification-user-streamings-${account.id}`, (_, notification: Entity.Notification) => {
commit('TimelineSpace/Contents/Notifications/appendNotifications', { notification, accountId: account.id }, { root: true })
})
win.ipcRenderer.removeAllListeners(`delete-user-streamings-${account.id}`)
win.ipcRenderer.on(`delete-user-streamings-${account.id}`, (_, id: string) => {
commit('TimelineSpace/Contents/Home/deleteToot', { statusId: id, accountId: account.id }, { root: true })
commit('TimelineSpace/Contents/Notifications/deleteToot', { statusId: id, accountId: account.id }, { root: true })
})
})
}
}

View File

@ -87,7 +87,6 @@ export const ACTION_TYPES = {
FETCH_CONTENTS_TIMELINES: 'fetchContentsTimelines',
CLEAR_CONTENTS_TIMELINES: 'clearContentsTimelines',
BIND_STREAMINGS: 'bindStreamings',
BIND_USER_STREAMING: 'bindUserStreaming',
BIND_LOCAL_STREAMING: 'bindLocalStreaming',
BIND_PUBLIC_STREAMING: 'bindPublicStreaming',
BIND_DIRECT_MESSAGES_STREAMING: 'bindDirectMessagesStreaming',
@ -199,7 +198,6 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
return true
},
[ACTION_TYPES.FETCH_CONTENTS_TIMELINES]: async ({ dispatch }) => {
await dispatch('TimelineSpace/Contents/Notifications/fetchNotifications', {}, { root: true })
await dispatch('TimelineSpace/Contents/DirectMessages/fetchTimeline', {}, { root: true })
await dispatch('TimelineSpace/Contents/Local/fetchLocalTimeline', {}, { root: true })
await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', {}, { root: true })
@ -207,11 +205,9 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
[ACTION_TYPES.CLEAR_CONTENTS_TIMELINES]: ({ commit }) => {
commit('TimelineSpace/Contents/Local/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Notifications/clearNotifications', {}, { root: true })
commit('TimelineSpace/Contents/Public/clearTimeline', {}, { root: true })
},
[ACTION_TYPES.BIND_STREAMINGS]: ({ dispatch }) => {
dispatch('bindUserStreaming')
dispatch('bindDirectMessagesStreaming')
dispatch('bindLocalStreaming')
dispatch('bindPublicStreaming')
@ -219,23 +215,6 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// ------------------------------------------------
// Each streaming methods
// ------------------------------------------------
[ACTION_TYPES.BIND_USER_STREAMING]: async ({ commit, state, rootState }) => {
if (!state.account) {
throw new Error('Account is not set')
}
win.ipcRenderer.on(`notification-user-streamings-${state.account!.id}`, (_, notification: Entity.Notification) => {
commit('TimelineSpace/Contents/Notifications/appendNotifications', notification, { root: true })
if (rootState.TimelineSpace.Contents.Notifications.heading && Math.random() > 0.8) {
commit('TimelineSpace/Contents/Notifications/archiveNotifications', null, { root: true })
}
commit('TimelineSpace/SideMenu/changeUnreadNotifications', true, { root: true })
})
win.ipcRenderer.on(`delete-user-streamings-${state.account!.id}`, (_, id: string) => {
commit('TimelineSpace/Contents/Home/deleteToot', id, { root: true })
commit('TimelineSpace/Contents/Notifications/deleteToot', id, { root: true })
})
},
[ACTION_TYPES.BIND_LOCAL_STREAMING]: ({ commit, rootState, state }) => {
win.ipcRenderer.on(`update-local-streamings-${state.account!.id}`, (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Local/appendTimeline', update, { root: true })
@ -272,7 +251,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/DirectMessages/deleteToot', id, { root: true })
})
},
// todo
[ACTION_TYPES.UPDATE_TOOT_FOR_ALL_TIMELINES]: ({ commit }, status: Entity.Status): boolean => {
commit('TimelineSpace/Contents/Home/updateToot', status, { root: true })
commit('TimelineSpace/Contents/Notifications/updateToot', status, { root: true })

View File

@ -1,62 +1,55 @@
import generator, { Entity, FilterContext, NotificationType } from 'megalodon'
import generator, { Entity, FilterContext } from 'megalodon'
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
import { RootState } from '@/store'
import { MyWindow } from '~/src/types/global'
import { LoadingCard } from '@/types/loading-card'
import { LocalServer } from '~/src/types/localServer'
import { LocalAccount } from '~/src/types/localAccount'
const win = (window as any) as MyWindow
export type NotificationsState = {
lazyLoading: boolean
heading: boolean
notifications: Array<Entity.Notification | LoadingCard>
notifications: { [key: number]: Array<Entity.Notification | LoadingCard> }
}
const state = (): NotificationsState => ({
lazyLoading: false,
heading: true,
notifications: []
notifications: {}
})
export const MUTATION_TYPES = {
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_HEADING: 'changeHeading',
APPEND_NOTIFICATIONS: 'appendNotifications',
UPDATE_NOTIFICATIONS: 'updateNotifications',
REPLACE_NOTIFICATIONS: 'updateNotifications',
INSERT_NOTIFICATIONS: 'insertNotifications',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CLEAR_NOTIFICATIONS: 'clearNotifications',
ARCHIVE_NOTIFICATIONS: 'archiveNotifications',
APPEND_NOTIFICATIONS_AFTER_LOADING_CARD: 'appendNotificationsAfterLoadingCard'
}
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
},
[MUTATION_TYPES.APPEND_NOTIFICATIONS]: (state, notification: Entity.Notification) => {
// Reject duplicated status in timeline
if (!state.notifications.find(item => item.id === notification.id)) {
state.notifications = ([notification] as Array<Entity.Notification | LoadingCard>).concat(state.notifications)
[MUTATION_TYPES.APPEND_NOTIFICATIONS]: (state, obj: { notification: Entity.Notification; accountId: number }) => {
if (state.notifications[obj.accountId]) {
state.notifications[obj.accountId] = [obj.notification, ...state.notifications[obj.accountId]]
} else {
state.notifications[obj.accountId] = [obj.notification]
}
},
[MUTATION_TYPES.UPDATE_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
state.notifications = notifications
[MUTATION_TYPES.REPLACE_NOTIFICATIONS]: (state, obj: { notifications: Array<Entity.Notification | LoadingCard>; accountId: number }) => {
state.notifications[obj.accountId] = obj.notifications
},
[MUTATION_TYPES.INSERT_NOTIFICATIONS]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
state.notifications = state.notifications.concat(notifications)
[MUTATION_TYPES.INSERT_NOTIFICATIONS]: (state, obj: { notifications: Array<Entity.Notification | LoadingCard>; accountId: number }) => {
if (state.notifications[obj.accountId]) {
state.notifications[obj.accountId] = [...state.notifications[obj.accountId], ...obj.notifications]
} else {
state.notifications[obj.accountId] = obj.notifications
}
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Entity.Status) => {
state.notifications = state.notifications.map(notification => {
[MUTATION_TYPES.UPDATE_TOOT]: (state, obj: { status: Entity.Status; accountId: number }) => {
state.notifications[obj.accountId] = state.notifications[obj.accountId].map(notification => {
// I want to update toot only mention.
// Because Toot component don't use status information when other patterns.
if (notification.type === 'mention' && notification.status && notification.status.id === message.id) {
if (notification.type === 'mention' && notification.status && notification.status.id === obj.status.id) {
const status = {
status: message
status: obj.status
}
return Object.assign(notification, status)
} else {
@ -64,39 +57,36 @@ const mutations: MutationTree<NotificationsState> = {
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, id: string) => {
state.notifications = state.notifications.filter(notify => {
[MUTATION_TYPES.DELETE_TOOT]: (state, obj: { statusId: string; accountId: number }) => {
state.notifications[obj.accountId] = state.notifications[obj.accountId].filter(notify => {
if (notify.id === 'loading-card') {
return true
}
const notification = notify as Entity.Notification
if (notification.status) {
if (notification.status.reblog && notification.status.reblog.id === id) {
if (notification.status.reblog && notification.status.reblog.id === obj.statusId) {
return false
} else {
return notification.status.id !== id
return notification.status.id !== obj.statusId
}
} else {
return true
}
})
},
[MUTATION_TYPES.CLEAR_NOTIFICATIONS]: state => {
state.notifications = []
},
[MUTATION_TYPES.ARCHIVE_NOTIFICATIONS]: state => {
state.notifications = state.notifications.slice(0, 30)
},
[MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD]: (state, notifications: Array<Entity.Notification | LoadingCard>) => {
const n = state.notifications.flatMap(notify => {
[MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD]: (
state,
obj: { notifications: Array<Entity.Notification | LoadingCard>; accountId: number }
) => {
const n = state.notifications[obj.accountId].flatMap(notify => {
if (notify.id !== 'loading-card') {
return notify
} else {
return notifications
return obj.notifications
}
})
// Reject duplicated status in timeline
state.notifications = Array.from(new Set(n))
state.notifications[obj.accountId] = Array.from(new Set(n))
}
}
@ -110,15 +100,13 @@ export const ACTION_TYPES = {
}
const actions: ActionTree<NotificationsState, RootState> = {
[ACTION_TYPES.FETCH_NOTIFICATIONS]: async ({ dispatch, commit, rootState }): Promise<Array<Entity.Notification>> => {
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
[ACTION_TYPES.FETCH_NOTIFICATIONS]: async (
{ dispatch, commit, rootState },
req: { account: LocalAccount; server: LocalServer }
): Promise<Array<Entity.Notification>> => {
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
const marker: Entity.Marker | null = await dispatch('getMarker').catch(err => {
const marker: Entity.Marker | null = await dispatch(ACTION_TYPES.GET_MARKER, req).catch(err => {
console.error(err)
})
@ -143,50 +131,31 @@ const actions: ActionTree<NotificationsState, RootState> = {
let notifications: Array<Entity.Notification | LoadingCard> = [card]
const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id })
notifications = notifications.concat(res.data)
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, notifications)
commit(MUTATION_TYPES.REPLACE_NOTIFICATIONS, { notifications, accountId: req.account.id })
commit('TimelineSpace/SideMenu/changeUnreadNotifications', true, { root: true })
return res.data
}
}
const res = await client.getNotifications({ limit: 30 })
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, res.data)
commit(MUTATION_TYPES.REPLACE_NOTIFICATIONS, { notifications: res.data, accountId: req.account.id })
return res.data
},
[ACTION_TYPES.LAZY_FETCH_NOTIFICATIONS]: async (
{ state, commit, rootState },
lastNotification: Entity.Notification
{ commit, rootState },
req: { lastNotification: Entity.Notification; account: LocalAccount; server: LocalServer }
): Promise<Array<Entity.Notification> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
return client
.getNotifications({ max_id: lastNotification.id, limit: 30 })
.then(res => {
commit(MUTATION_TYPES.INSERT_NOTIFICATIONS, res.data)
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
return client.getNotifications({ max_id: req.lastNotification.id, limit: 30 }).then(res => {
commit(MUTATION_TYPES.INSERT_NOTIFICATIONS, { notifications: res.data, accountId: req.account.id })
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
},
[ACTION_TYPES.FETCH_NOTIFICATIONS_SINCE]: async (
{ state, rootState, commit },
since_id: string
req: { sinceId: string; account: LocalAccount; server: LocalServer }
): Promise<Array<Entity.Notification> | null> => {
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
const cardIndex = state.notifications.findIndex(s => {
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
const cardIndex = state.notifications[req.account.id].findIndex(s => {
if (s.id === 'loading-card') {
return true
}
@ -194,9 +163,9 @@ const actions: ActionTree<NotificationsState, RootState> = {
})
let maxID: string | null = null
if (cardIndex > 0) {
maxID = state.notifications[cardIndex - 1].id
maxID = state.notifications[req.account.id][cardIndex - 1].id
}
let params = { min_id: since_id, limit: 30 }
let params = { min_id: req.sinceId, limit: 30 }
if (maxID !== null) {
params = Object.assign({}, params, {
max_id: maxID
@ -214,25 +183,20 @@ const actions: ActionTree<NotificationsState, RootState> = {
}
let notifications: Array<Entity.Notification | LoadingCard> = [card]
notifications = notifications.concat(res.data)
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, notifications)
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, { notifications, accountId: req.account.id })
} else {
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, res.data)
commit(MUTATION_TYPES.APPEND_NOTIFICATIONS_AFTER_LOADING_CARD, { notifications: res.data, accountId: req.account.id })
}
return res.data
},
[ACTION_TYPES.RESET_BADGE]: () => {
win.ipcRenderer.send('reset-badge')
},
[ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<Entity.Marker | null> => {
[ACTION_TYPES.GET_MARKER]: async ({ rootState }, req: { account: LocalAccount; server: LocalServer }): Promise<Entity.Marker | null> => {
if (!rootState.TimelineSpace.setting.markerNotifications) {
return null
}
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
let serverMarker: Entity.Marker | {} = {}
try {
const res = await client.getMarkers(['notifications'])
@ -242,21 +206,16 @@ const actions: ActionTree<NotificationsState, RootState> = {
}
return serverMarker
},
[ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }) => {
const notifications = state.notifications
[ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }, req: { account: LocalAccount; server: LocalServer }) => {
const notifications = state.notifications[req.account.id]
if (notifications.length === 0 || notifications[0].id === 'loading-card') {
return
}
if (rootState.TimelineSpace.server!.sns === 'misskey') {
if (req.server.sns === 'misskey') {
return
}
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
const res = await client.saveMarkers({ notifications: { last_read_id: notifications[0].id } })
if (rootState.TimelineSpace.server!.sns === 'pleroma') {
await client.readNotifications({ max_id: notifications[0].id })
@ -266,25 +225,6 @@ const actions: ActionTree<NotificationsState, RootState> = {
}
const getters: GetterTree<NotificationsState, RootState> = {
handledNotifications: state => {
return state.notifications.filter(n => {
switch (n.type) {
case 'middle-load':
case NotificationType.Follow:
case NotificationType.Favourite:
case NotificationType.Reblog:
case NotificationType.Mention:
case NotificationType.EmojiReaction:
case NotificationType.FollowRequest:
case NotificationType.Status:
case NotificationType.PollVote:
case NotificationType.PollExpired:
return true
default:
return false
}
})
},
filters: (_state, _getters, rootState) => {
return rootState.TimelineSpace.filters.filter(f => f.context.includes(FilterContext.Notifications) && !f.irreversible)
}