mirror of
https://github.com/h3poteto/whalebird-desktop
synced 2024-12-09 23:46:06 +01:00
[refactor] DM store
This commit is contained in:
parent
55629be421
commit
6ed9206bb4
@ -79,7 +79,6 @@ export default defineComponent({
|
||||
|
||||
const clear = async () => {
|
||||
await store.dispatch(`${space}/${ACTION_TYPES.CLEAR_ACCOUNT}`)
|
||||
store.dispatch(`${space}/${ACTION_TYPES.CLEAR_CONTENTS_TIMELINES}`)
|
||||
await store.dispatch(`${space}/${ACTION_TYPES.REMOVE_SHORTCUT_EVENTS}`)
|
||||
await store.dispatch(`${space}/${ACTION_TYPES.CLEAR_UNREAD}`)
|
||||
return 'clear'
|
||||
|
@ -1,14 +1,16 @@
|
||||
<template>
|
||||
<div id="directmessages">
|
||||
<div></div>
|
||||
<DynamicScroller :items="timeline" :min-item-size="86" id="scroller" class="scroller" ref="scroller">
|
||||
<template v-slot="{ item, index, active }">
|
||||
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.uri]" :data-index="index" :watchData="true">
|
||||
<toot
|
||||
v-if="account.account && account.server"
|
||||
:message="item"
|
||||
:focused="item.uri + item.id === focusedId"
|
||||
:overlaid="modalOpened"
|
||||
:filters="[]"
|
||||
:account="account.account"
|
||||
:server="account.server"
|
||||
v-on:update="updateToot"
|
||||
v-on:delete="deleteToot"
|
||||
@focusRight="focusSidebar"
|
||||
@ -27,7 +29,7 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onBeforeUnmount, onUnmounted, watch } from 'vue'
|
||||
import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onBeforeUnmount, watch, reactive } from 'vue'
|
||||
import { logicAnd } from '@vueuse/math'
|
||||
import { useMagicKeys, whenever } from '@vueuse/core'
|
||||
import { useStore } from '@/store'
|
||||
@ -40,9 +42,11 @@ import Toot from '@/components/organisms/Toot.vue'
|
||||
import { EventEmitter } from '@/components/event'
|
||||
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/DirectMessages'
|
||||
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
|
||||
import { MUTATION_TYPES as TIMELINE_MUTATION, ACTION_TYPES as TIMELINE_ACTION } from '@/store/TimelineSpace'
|
||||
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
|
||||
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
|
||||
import { ACTION_TYPES as CONTENTS_ACTION } from '@/store/TimelineSpace/Contents'
|
||||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { LocalServer } from '~/src/types/localServer'
|
||||
import { MyWindow } from '~/src/types/global'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'directmessages',
|
||||
@ -55,28 +59,32 @@ 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 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 timeline = computed(() => store.state.TimelineSpace.Contents.DirectMessages.timeline)
|
||||
const lazyLoading = computed(() => store.state.TimelineSpace.Contents.DirectMessages.lazyLoading)
|
||||
const heading = computed(() => store.state.TimelineSpace.Contents.DirectMessages.heading)
|
||||
const timeline = computed(() => store.state.TimelineSpace.Contents.DirectMessages.timeline[id.value])
|
||||
const openSideBar = computed(() => store.state.TimelineSpace.Contents.SideBar.openSideBar)
|
||||
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
|
||||
const unreadNotification = computed(() => store.state.TimelineSpace.timelineSetting.unreadNotification)
|
||||
const modalOpened = computed<boolean>(() => store.getters[`TimelineSpace/Modals/modalOpened`])
|
||||
const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
|
||||
const shortcutEnabled = computed(() => !modalOpened.value)
|
||||
|
||||
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_HOME_TIMELINE}`, false)
|
||||
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
|
||||
if (!unreadNotification.value.direct) {
|
||||
store.commit(`TimelineSpace/Contents/${CONTENTS_ACTION.CHANGE_LOADING}`, true)
|
||||
await initialize().finally(() => {
|
||||
store.commit(`TimelineSpace/Contents/${CONTENTS_ACTION.CHANGE_LOADING}`, false)
|
||||
})
|
||||
}
|
||||
})
|
||||
onBeforeUpdate(() => {
|
||||
if (store.state.TimelineSpace.SideMenu.unreadDirectMessagesTimeline && heading.value) {
|
||||
@ -84,17 +92,7 @@ export default defineComponent({
|
||||
}
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
if (!unreadNotification.value.direct) {
|
||||
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.STOP_DIRECT_MESSAGES_STREAMING}`)
|
||||
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.UNBIND_DIRECT_MESSAGES_STREAMING}`)
|
||||
}
|
||||
})
|
||||
onUnmounted(() => {
|
||||
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
|
||||
store.commit(`${space}/${MUTATION_TYPES.ARCHIVE_TIMELINE}`)
|
||||
if (!unreadNotification.value.direct) {
|
||||
store.commit(`${space}/${MUTATION_TYPES.CLEAR_TIMELINE}`)
|
||||
}
|
||||
EventEmitter.off('focus-timeline')
|
||||
})
|
||||
watch(startReload, (newVal, oldVal) => {
|
||||
if (!oldVal && newVal) {
|
||||
@ -105,9 +103,9 @@ export default defineComponent({
|
||||
})
|
||||
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
|
||||
}
|
||||
})
|
||||
whenever(logicAnd(j, shortcutEnabled), () => {
|
||||
@ -124,16 +122,6 @@ export default defineComponent({
|
||||
reload()
|
||||
})
|
||||
|
||||
const initialize = async () => {
|
||||
await store.dispatch(`${space}/${ACTION_TYPES.FETCH_TIMELINE}`).catch(_ => {
|
||||
ElMessage({
|
||||
message: i18n.t('message.timeline_fetch_error'),
|
||||
type: 'error'
|
||||
})
|
||||
})
|
||||
await store.dispatch(`TimelineSpace/${TIMELINE_ACTION.BIND_DIRECT_MESSAGES_STREAMING}`)
|
||||
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.START_DIRECT_MESSAGES_STREAMING}`)
|
||||
}
|
||||
const onScroll = (event: Event) => {
|
||||
// for lazyLoading
|
||||
if (
|
||||
@ -141,25 +129,39 @@ export default defineComponent({
|
||||
document.getElementById('scroller')!.scrollHeight - 10 &&
|
||||
!lazyLoading.value
|
||||
) {
|
||||
store.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, timeline.value[timeline.value.length - 1]).catch(() => {
|
||||
ElMessage({
|
||||
message: i18n.t('message.timeline_fetch_error'),
|
||||
type: 'error'
|
||||
lazyLoading.value = true
|
||||
store
|
||||
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, {
|
||||
statuses: timeline.value[timeline.value.length - 1],
|
||||
account: account.account,
|
||||
server: account.server
|
||||
})
|
||||
.catch(() => {
|
||||
ElMessage({
|
||||
message: i18n.t('message.timeline_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
|
||||
}
|
||||
}
|
||||
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 deleteToot = (message: Entity.Status) => {
|
||||
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message.id)
|
||||
if (account.account) {
|
||||
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
|
||||
}
|
||||
}
|
||||
const reload = async () => {
|
||||
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
|
||||
@ -205,7 +207,8 @@ export default defineComponent({
|
||||
focusToot,
|
||||
openSideBar,
|
||||
heading,
|
||||
upper
|
||||
upper,
|
||||
account
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -123,6 +123,7 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
|
||||
await dispatch('TimelineSpace/Contents/Notifications/fetchNotifications', { account, server }, { root: true })
|
||||
await dispatch('TimelineSpace/Contents/Local/fetchLocalTimeline', { account, server }, { root: true })
|
||||
await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', { account, server }, { root: true })
|
||||
await dispatch('TimelineSpace/Contents/DirectMessages/fetchTimeline', { account, server }, { root: true })
|
||||
})
|
||||
},
|
||||
[ACTION_TYPES.BIND_STREAMINGS]: async ({ commit }, req: Array<[LocalAccount, LocalServer]>) => {
|
||||
@ -156,6 +157,14 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
|
||||
win.ipcRenderer.on(`delete-public-streamings-${account.id}`, (_, id: string) => {
|
||||
commit('TimelineSpace/Contents/Public/deleteToot', { statusId: id, accountId: account.id }, { root: true })
|
||||
})
|
||||
win.ipcRenderer.removeAllListeners(`update-direct-streamings-${account.id}`)
|
||||
win.ipcRenderer.on(`update-direct-streamings-${account.id}`, (_, update: Entity.Status) => {
|
||||
commit('TimelineSpace/Contents/DirectMessages/appendTimeline', { status: update, accountId: account.id }, { root: true })
|
||||
})
|
||||
win.ipcRenderer.removeAllListeners(`delete-direct-streamings-${account.id}`)
|
||||
win.ipcRenderer.on(`delete-direct-streamings-${account.id}`, (_, id: string) => {
|
||||
commit('TimelineSpace/Contents/DirectMessages/deleteToot', { statusId: id, accountId: account.id }, { root: true })
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { RootState } from '@/store'
|
||||
import { AccountLoadError } from '@/errors/load'
|
||||
import { TimelineFetchError } from '@/errors/fetch'
|
||||
import { MyWindow } from '~/src/types/global'
|
||||
import { LocalServer } from '~/src/types/localServer'
|
||||
import { Setting } from '~/src/types/setting'
|
||||
@ -84,11 +83,6 @@ export const ACTION_TYPES = {
|
||||
FETCH_EMOJIS: 'fetchEmojis',
|
||||
FETCH_FILTERS: 'fetchFilters',
|
||||
FETCH_INSTANCE: 'fetchInstance',
|
||||
FETCH_CONTENTS_TIMELINES: 'fetchContentsTimelines',
|
||||
CLEAR_CONTENTS_TIMELINES: 'clearContentsTimelines',
|
||||
BIND_STREAMINGS: 'bindStreamings',
|
||||
BIND_DIRECT_MESSAGES_STREAMING: 'bindDirectMessagesStreaming',
|
||||
UPDATE_TOOT_FOR_ALL_TIMELINES: 'updateTootForAllTimelines',
|
||||
LOAD_SETTING: 'loadSetting'
|
||||
}
|
||||
|
||||
@ -104,13 +98,9 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
|
||||
await dispatch(ACTION_TYPES.LOAD_SETTING)
|
||||
await dispatch(ACTION_TYPES.FETCH_FILTERS)
|
||||
commit(MUTATION_TYPES.CHANGE_LOADING, false)
|
||||
await dispatch(ACTION_TYPES.FETCH_CONTENTS_TIMELINES).catch(_ => {
|
||||
throw new TimelineFetchError()
|
||||
})
|
||||
return account
|
||||
},
|
||||
[ACTION_TYPES.PREPARE_SPACE]: async ({ dispatch }) => {
|
||||
await dispatch(ACTION_TYPES.BIND_STREAMINGS)
|
||||
await dispatch(ACTION_TYPES.FETCH_EMOJIS)
|
||||
await dispatch(ACTION_TYPES.FETCH_INSTANCE)
|
||||
},
|
||||
@ -158,8 +148,11 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
|
||||
/**
|
||||
* fetchEmojis
|
||||
*/
|
||||
[ACTION_TYPES.FETCH_EMOJIS]: async ({ commit, state }): Promise<Array<Entity.Emoji>> => {
|
||||
const client = generator(state.server!.sns, state.server!.baseURL, null, 'Whalebird')
|
||||
[ACTION_TYPES.FETCH_EMOJIS]: async ({ commit, state, rootState }): Promise<Array<Entity.Emoji>> => {
|
||||
if (!state.server) {
|
||||
return []
|
||||
}
|
||||
const client = generator(state.server.sns, state.server.baseURL, null, rootState.App.userAgent)
|
||||
const res = await client.getInstanceCustomEmojis()
|
||||
commit(MUTATION_TYPES.UPDATE_EMOJIS, res.data)
|
||||
return res.data
|
||||
@ -172,8 +165,11 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
|
||||
* fetchFilters
|
||||
*/
|
||||
[ACTION_TYPES.FETCH_FILTERS]: async ({ commit, state, rootState }): Promise<Array<Entity.Filter>> => {
|
||||
if (!state.server || !state.account) {
|
||||
return []
|
||||
}
|
||||
try {
|
||||
const client = generator(state.server!.sns, state.server!.baseURL, state.account!.accessToken, rootState.App.userAgent)
|
||||
const client = generator(state.server.sns, state.server.baseURL, state.account.accessToken, rootState.App.userAgent)
|
||||
const res = await client.getFilters()
|
||||
commit(MUTATION_TYPES.UPDATE_FILTERS, res.data)
|
||||
return res.data
|
||||
@ -184,8 +180,11 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
|
||||
/**
|
||||
* fetchInstance
|
||||
*/
|
||||
[ACTION_TYPES.FETCH_INSTANCE]: async ({ commit, state }) => {
|
||||
const client = generator(state.server!.sns, state.server!.baseURL, null, 'Whalebird')
|
||||
[ACTION_TYPES.FETCH_INSTANCE]: async ({ commit, state, rootState }) => {
|
||||
if (!state.server) {
|
||||
return false
|
||||
}
|
||||
const client = generator(state.server.sns, state.server.baseURL, null, rootState.App.userAgent)
|
||||
const res = await client.getInstance()
|
||||
if (res.data.max_toot_chars) {
|
||||
commit(MUTATION_TYPES.UPDATE_TOOT_MAX, res.data.max_toot_chars)
|
||||
@ -194,39 +193,6 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
|
||||
commit(MUTATION_TYPES.UPDATE_TOOT_MAX, res.data.configuration.statuses.max_characters)
|
||||
}
|
||||
return true
|
||||
},
|
||||
[ACTION_TYPES.FETCH_CONTENTS_TIMELINES]: async ({ dispatch }) => {
|
||||
await dispatch('TimelineSpace/Contents/DirectMessages/fetchTimeline', {}, { root: true })
|
||||
},
|
||||
[ACTION_TYPES.CLEAR_CONTENTS_TIMELINES]: ({ commit }) => {
|
||||
commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true })
|
||||
},
|
||||
[ACTION_TYPES.BIND_STREAMINGS]: ({ dispatch }) => {
|
||||
dispatch('bindDirectMessagesStreaming')
|
||||
},
|
||||
// ------------------------------------------------
|
||||
// Each streaming methods
|
||||
// ------------------------------------------------
|
||||
[ACTION_TYPES.BIND_DIRECT_MESSAGES_STREAMING]: ({ commit, rootState, state }) => {
|
||||
win.ipcRenderer.on(`update-direct-streamings-${state.account!.id}`, (_, update: Entity.Status) => {
|
||||
commit('TimelineSpace/Contents/DirectMessages/appendTimeline', update, { root: true })
|
||||
if (rootState.TimelineSpace.Contents.DirectMessages.heading && Math.random() > 0.8) {
|
||||
commit('TimelineSpace/Contents/DirectMessages/archiveTimeline', {}, { root: true })
|
||||
}
|
||||
commit('TimelineSpace/SideMenu/changeUnreadDirectMessagesTimeline', true, { root: true })
|
||||
})
|
||||
win.ipcRenderer.on(`delete-direct-streamings-${state.account!.id}`, (_, id: string) => {
|
||||
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 })
|
||||
commit('TimelineSpace/Contents/DirectMessages/updateToot', status, { root: true })
|
||||
commit('TimelineSpace/Contents/Local/updateToot', status, { root: true })
|
||||
commit('TimelineSpace/Contents/Public/updateToot', status, { root: true })
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,66 +1,53 @@
|
||||
import generator, { Entity } from 'megalodon'
|
||||
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { RootState } from '@/store'
|
||||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { LocalServer } from '~/src/types/localServer'
|
||||
|
||||
export type DirectMessagesState = {
|
||||
lazyLoading: boolean
|
||||
heading: boolean
|
||||
timeline: Array<Entity.Status>
|
||||
timeline: { [key: number]: Array<Entity.Status> }
|
||||
}
|
||||
|
||||
const state = (): DirectMessagesState => ({
|
||||
lazyLoading: false,
|
||||
heading: true,
|
||||
timeline: []
|
||||
timeline: {}
|
||||
})
|
||||
|
||||
export const MUTATION_TYPES = {
|
||||
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
||||
CHANGE_HEADING: 'changeHeading',
|
||||
APPEND_TIMELINE: 'appendTimeline',
|
||||
UPDATE_TIMELINE: 'updateTimeline',
|
||||
REPLACE_TIMELINE: 'replaceTimeline',
|
||||
INSERT_TIMELINE: 'insertTimeline',
|
||||
ARCHIVE_TIMELINE: 'archiveTimeline',
|
||||
CLEAR_TIMELINE: 'clearTimeline',
|
||||
UPDATE_TOOT: 'updateToot',
|
||||
DELETE_TOOT: 'deleteToot'
|
||||
}
|
||||
|
||||
const mutations: MutationTree<DirectMessagesState> = {
|
||||
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
|
||||
state.lazyLoading = value
|
||||
},
|
||||
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
|
||||
state.heading = value
|
||||
},
|
||||
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Entity.Status) => {
|
||||
// Reject duplicated status in timeline
|
||||
if (!state.timeline.find(item => item.id === update.id)) {
|
||||
state.timeline = [update].concat(state.timeline)
|
||||
[MUTATION_TYPES.APPEND_TIMELINE]: (state, obj: { status: Entity.Status; accountId: number }) => {
|
||||
if (state.timeline[obj.accountId]) {
|
||||
state.timeline[obj.accountId] = [obj.status, ...state.timeline[obj.accountId]]
|
||||
} else {
|
||||
state.timeline[obj.accountId] = [obj.status]
|
||||
}
|
||||
},
|
||||
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Entity.Status>) => {
|
||||
state.timeline = messages
|
||||
[MUTATION_TYPES.REPLACE_TIMELINE]: (state, obj: { statuses: Array<Entity.Status>; accountId: number }) => {
|
||||
state.timeline[obj.accountId] = obj.statuses
|
||||
},
|
||||
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Entity.Status>) => {
|
||||
state.timeline = state.timeline.concat(messages)
|
||||
[MUTATION_TYPES.INSERT_TIMELINE]: (state, obj: { statuses: Array<Entity.Status>; accountId: number }) => {
|
||||
if (state.timeline[obj.accountId]) {
|
||||
state.timeline[obj.accountId] = [...state.timeline[obj.accountId], ...obj.statuses]
|
||||
} else {
|
||||
state.timeline[obj.accountId] = obj.statuses
|
||||
}
|
||||
},
|
||||
[MUTATION_TYPES.ARCHIVE_TIMELINE]: state => {
|
||||
state.timeline = state.timeline.slice(0, 20)
|
||||
},
|
||||
[MUTATION_TYPES.CLEAR_TIMELINE]: state => {
|
||||
state.timeline = []
|
||||
},
|
||||
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Entity.Status) => {
|
||||
[MUTATION_TYPES.UPDATE_TOOT]: (state, obj: { status: Entity.Status; accountId: number }) => {
|
||||
// Replace target message in DirectMessagesTimeline and notifications
|
||||
state.timeline = state.timeline.map(toot => {
|
||||
if (toot.id === message.id) {
|
||||
return message
|
||||
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
|
||||
state.timeline[obj.accountId] = state.timeline[obj.accountId].map(toot => {
|
||||
if (toot.id === obj.status.id) {
|
||||
return obj.status
|
||||
} else if (toot.reblog !== null && toot.reblog.id === obj.status.id) {
|
||||
// When user reblog/favourite a reblogged toot, target message is a original toot.
|
||||
// So, a message which is received now is original toot.
|
||||
const reblog = {
|
||||
reblog: message
|
||||
reblog: obj.status
|
||||
}
|
||||
return Object.assign(toot, reblog)
|
||||
} else {
|
||||
@ -68,12 +55,12 @@ const mutations: MutationTree<DirectMessagesState> = {
|
||||
}
|
||||
})
|
||||
},
|
||||
[MUTATION_TYPES.DELETE_TOOT]: (state, id: string) => {
|
||||
state.timeline = state.timeline.filter(toot => {
|
||||
if (toot.reblog !== null && toot.reblog.id === id) {
|
||||
[MUTATION_TYPES.DELETE_TOOT]: (state, obj: { statusId: string; accountId: number }) => {
|
||||
state.timeline[obj.accountId] = state.timeline[obj.accountId].filter(toot => {
|
||||
if (toot.reblog !== null && toot.reblog.id === obj.statusId) {
|
||||
return false
|
||||
} else {
|
||||
return toot.id !== id
|
||||
return toot.id !== obj.statusId
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -85,46 +72,31 @@ export const ACTION_TYPES = {
|
||||
}
|
||||
|
||||
const actions: ActionTree<DirectMessagesState, RootState> = {
|
||||
[ACTION_TYPES.FETCH_TIMELINE]: async ({ commit, rootState }): Promise<Array<Entity.Status>> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
rootState.TimelineSpace.account!.accessToken,
|
||||
rootState.App.userAgent
|
||||
)
|
||||
[ACTION_TYPES.FETCH_TIMELINE]: async (
|
||||
{ commit, rootState },
|
||||
req: { account: LocalAccount; server: LocalServer }
|
||||
): Promise<Array<Entity.Status>> => {
|
||||
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
|
||||
try {
|
||||
const res = await client.getConversationTimeline({ limit: 20 })
|
||||
const statuses: Array<Entity.Status> = res.data.map(con => con.last_status!)
|
||||
commit(MUTATION_TYPES.UPDATE_TIMELINE, statuses)
|
||||
commit(MUTATION_TYPES.REPLACE_TIMELINE, { statuses, accountId: req.account.id }) // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
return statuses
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return []
|
||||
}
|
||||
},
|
||||
[ACTION_TYPES.LAZY_FETCH_TIMELINE]: async (
|
||||
{ state, commit, rootState },
|
||||
lastStatus: Entity.Status
|
||||
{ commit, rootState },
|
||||
req: { lastStatus: Entity.Status; account: LocalAccount; server: LocalServer }
|
||||
): Promise<Array<Entity.Status> | 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
|
||||
.getConversationTimeline({ max_id: lastStatus.id, limit: 20 })
|
||||
.then(res => {
|
||||
const statuses: Array<Entity.Status> = res.data.map(con => con.last_status!)
|
||||
commit(MUTATION_TYPES.INSERT_TIMELINE, statuses)
|
||||
return statuses
|
||||
})
|
||||
.finally(() => {
|
||||
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
|
||||
})
|
||||
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
|
||||
return client.getConversationTimeline({ max_id: req.lastStatus.id, limit: 20 }).then(res => {
|
||||
const statuses: Array<Entity.Status> = res.data.map(con => con.last_status!) // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
||||
commit(MUTATION_TYPES.INSERT_TIMELINE, { statuses, accountId: req.account.id })
|
||||
return statuses
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ export const ACTION_TYPES = {
|
||||
}
|
||||
|
||||
const actions: ActionTree<TootState, RootState> = {
|
||||
[ACTION_TYPES.REBLOG]: async ({ rootState, dispatch }, message: Entity.Status) => {
|
||||
[ACTION_TYPES.REBLOG]: async ({ rootState }, message: Entity.Status) => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -47,10 +47,9 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
// Reblog target status is in the data.reblog.
|
||||
// So I send data.reblog as status for update local timeline.
|
||||
win.ipcRenderer.send('fav-rt-action-sound')
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data.reblog, { root: true })
|
||||
return res.data.reblog
|
||||
},
|
||||
[ACTION_TYPES.UNREBLOG]: async ({ rootState, dispatch }, message: Entity.Status) => {
|
||||
[ACTION_TYPES.UNREBLOG]: async ({ rootState }, message: Entity.Status) => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -58,10 +57,9 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const res = await client.unreblogStatus(message.id)
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data, { root: true })
|
||||
return res.data
|
||||
},
|
||||
[ACTION_TYPES.ADD_FAVOURITE]: async ({ rootState, dispatch }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
[ACTION_TYPES.ADD_FAVOURITE]: async ({ rootState }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -70,10 +68,9 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
)
|
||||
const res = await client.favouriteStatus(message.id)
|
||||
win.ipcRenderer.send('fav-rt-action-sound')
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data, { root: true })
|
||||
return res.data
|
||||
},
|
||||
removeFavourite: async ({ rootState, dispatch }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
removeFavourite: async ({ rootState }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -81,10 +78,9 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const res = await client.unfavouriteStatus(message.id)
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data, { root: true })
|
||||
return res.data
|
||||
},
|
||||
addBookmark: async ({ rootState, dispatch }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
addBookmark: async ({ rootState }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -93,10 +89,9 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
)
|
||||
const res = await client.bookmarkStatus(message.id)
|
||||
win.ipcRenderer.send('fav-rt-action-sound')
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data, { root: true })
|
||||
return res.data
|
||||
},
|
||||
removeBookmark: async ({ rootState, dispatch }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
removeBookmark: async ({ rootState }, message: Entity.Status): Promise<Entity.Status> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
@ -104,7 +99,6 @@ const actions: ActionTree<TootState, RootState> = {
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const res = await client.unbookmarkStatus(message.id)
|
||||
dispatch('TimelineSpace/updateTootForAllTimelines', res.data, { root: true })
|
||||
return res.data
|
||||
},
|
||||
deleteToot: async ({ rootState }, message: Entity.Status) => {
|
||||
|
Loading…
Reference in New Issue
Block a user