From 5ec2a485711014b8d88401e6f1b711b3c00e9707 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Sun, 29 Jan 2023 21:57:54 +0900
Subject: [PATCH 1/7] [refactor] Remove public timeline store
---
.../TimelineSpace/Contents/DirectMessages.vue | 10 +-
.../TimelineSpace/Contents/Favourites.vue | 10 +-
.../TimelineSpace/Contents/Home.vue | 10 +-
.../TimelineSpace/Contents/Public.vue | 100 +++++++++------
src/renderer/components/organisms/Toot.vue | 4 +-
src/renderer/store/GlobalHeader.ts | 9 --
src/renderer/store/TimelineSpace/Contents.ts | 3 -
.../store/TimelineSpace/Contents/Public.ts | 114 ------------------
8 files changed, 82 insertions(+), 178 deletions(-)
delete mode 100644 src/renderer/store/TimelineSpace/Contents/Public.ts
diff --git a/src/renderer/components/TimelineSpace/Contents/DirectMessages.vue b/src/renderer/components/TimelineSpace/Contents/DirectMessages.vue
index 9f3118c0ce..84465c8f34 100644
--- a/src/renderer/components/TimelineSpace/Contents/DirectMessages.vue
+++ b/src/renderer/components/TimelineSpace/Contents/DirectMessages.vue
@@ -11,9 +11,9 @@
:filters="[]"
:account="account.account"
:server="account.server"
- v-on:update="updateToot"
- v-on:delete="deleteToot"
- @selectToot="focusToot(item)"
+ @update="updateToot"
+ @delete="deleteToot"
+ @select-toot="focusToot(item)"
>
@@ -132,9 +132,9 @@ export default defineComponent({
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
- const deleteToot = (message: Entity.Status) => {
+ const deleteToot = (id: string) => {
if (account.account) {
- store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
+ store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: id, accountId: account.account.id })
}
}
const upper = () => {
diff --git a/src/renderer/components/TimelineSpace/Contents/Favourites.vue b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
index 0f25e0f617..85dc88b3b9 100644
--- a/src/renderer/components/TimelineSpace/Contents/Favourites.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
@@ -12,9 +12,9 @@
:filters="[]"
:account="account.account"
:server="account.server"
- v-on:update="updateToot"
- v-on:delete="deleteToot"
- @selectToot="focusToot(item)"
+ @update="updateToot"
+ @delete="deleteToot"
+ @select-toot="focusToot(item)"
>
@@ -150,8 +150,8 @@ export default defineComponent({
const updateToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
}
- const deleteToot = (message: Entity.Status) => {
- store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message)
+ const deleteToot = (id: string) => {
+ store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, id)
}
const reload = async () => {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
diff --git a/src/renderer/components/TimelineSpace/Contents/Home.vue b/src/renderer/components/TimelineSpace/Contents/Home.vue
index 21507e6896..67490fca6d 100644
--- a/src/renderer/components/TimelineSpace/Contents/Home.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Home.vue
@@ -17,9 +17,9 @@
:filters="filters"
:account="account.account"
:server="account.server"
- v-on:update="updateToot"
- v-on:delete="deleteToot"
- @selectToot="focusToot(item)"
+ @update="updateToot"
+ delete="deleteToot"
+ @select-toot="focusToot(item)"
>
@@ -180,9 +180,9 @@ export default defineComponent({
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
- const deleteToot = (message: Entity.Status) => {
+ const deleteToot = (id: string) => {
if (account.account) {
- store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
+ store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: id, accountId: account.account.id })
}
}
const fetchTimelineSince = (since_id: string) => {
diff --git a/src/renderer/components/TimelineSpace/Contents/Public.vue b/src/renderer/components/TimelineSpace/Contents/Public.vue
index 4735b9b262..293ba4a80e 100644
--- a/src/renderer/components/TimelineSpace/Contents/Public.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Public.vue
@@ -1,6 +1,7 @@
-
+
+
@@ -27,13 +28,12 @@ import { computed, defineComponent, onBeforeUpdate, onMounted, onUnmounted, ref,
import { logicAnd } from '@vueuse/math'
import { useMagicKeys, whenever } from '@vueuse/core'
import { ElMessage } from 'element-plus'
-import { Entity } from 'megalodon'
+import generator, { Entity, MegalodonInterface } from 'megalodon'
import { useI18next } from 'vue3-i18next'
import { useRoute } from 'vue-router'
import { useStore } from '@/store'
import Toot from '@/components/organisms/Toot.vue'
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
-import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Public'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
import { MyWindow } from '~/src/types/global'
@@ -53,23 +53,44 @@ export default defineComponent({
const focusedId = ref(null)
const scroller = ref(null)
- const lazyLoading = ref(false)
+ const loading = ref(false)
const heading = ref(true)
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
+ const client = ref(null)
- const timeline = computed(() => store.state.TimelineSpace.Contents.Public.timeline[id.value])
+ const statuses = ref>([])
const modalOpened = computed(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const filters = computed(() => store.getters[`${space}/filters`])
- const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
+ const currentFocusedIndex = computed(() => statuses.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
const shortcutEnabled = computed(() => !modalOpened.value)
+ const userAgent = computed(() => store.state.App.userAgent)
+ const backgroundColor = computed(() => store.state.App.theme.background_color)
onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
+ client.value = generator(s.sns, s.baseURL, a.accessToken, userAgent.value)
+
+ loading.value = true
+ try {
+ const res = await client.value.getPublicTimeline({ limit: 20 })
+ statuses.value = res.data
+ } finally {
+ loading.value = false
+ }
+
+ win.ipcRenderer.removeAllListeners(`update-public-streamings-${a.id}`)
+ win.ipcRenderer.on(`update-public-streamings-${a.id}`, (_, update: Entity.Status) => {
+ statuses.value = [update, ...statuses.value]
+ })
+ win.ipcRenderer.removeAllListeners(`delete-public-streamings-${a.id}`)
+ win.ipcRenderer.on(`delete-public-streamings-${a.id}`, (_, id: string) => {
+ deleteToot(id)
+ })
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_PUBLIC_TIMELINE}`, false)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
@@ -96,7 +117,7 @@ export default defineComponent({
})
whenever(logicAnd(j, shortcutEnabled), () => {
if (focusedId.value === null) {
- focusedId.value = timeline.value[0].uri + timeline.value[0].id
+ focusedId.value = statuses.value[0].uri + statuses.value[0].id
} else {
focusNext()
}
@@ -108,25 +129,21 @@ export default defineComponent({
const onScroll = (event: Event) => {
if (
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
- document.getElementById('scroller')!.scrollHeight - 10 &&
- !lazyLoading.value
+ document.getElementById('scroller')!.scrollHeight - 10
) {
- lazyLoading.value = true
- store
- .dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, {
- statuses: timeline.value[timeline.value.length - 1],
- account: account.account,
- server: account.server
+ const lastStatus = statuses.value[statuses.value.length - 1]
+ client.value
+ ?.getPublicTimeline({ max_id: lastStatus.id, limit: 20 })
+ .then(res => {
+ statuses.value = [...statuses.value, ...res.data]
})
- .catch(() => {
+ .catch(err => {
+ console.error(err)
ElMessage({
message: i18n.t('message.timeline_fetch_error'),
type: 'error'
})
})
- .finally(() => {
- lazyLoading.value = false
- })
}
if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
@@ -137,14 +154,25 @@ export default defineComponent({
}
const updateToot = (message: Entity.Status) => {
- if (account.account) {
- store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
- }
+ statuses.value = statuses.value.map(status => {
+ if (status.id === message.id) {
+ return message
+ } else if (status.reblog && status.reblog.id === message.id) {
+ return Object.assign(status, {
+ reblog: message
+ })
+ }
+ return status
+ })
}
- const deleteToot = (message: Entity.Status) => {
- if (account.account) {
- store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
- }
+ const deleteToot = (id: string) => {
+ statuses.value = statuses.value.filter(status => {
+ if (status.reblog !== null && status.reblog.id === id) {
+ return false
+ } else {
+ return status.id !== id
+ }
+ })
}
const upper = () => {
scroller.value.scrollToItem(0)
@@ -152,16 +180,16 @@ export default defineComponent({
}
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
- focusedId.value = timeline.value[0].uri + timeline.value[0].id
- } else if (currentFocusedIndex.value < timeline.value.length) {
- focusedId.value = timeline.value[currentFocusedIndex.value + 1].uri + timeline.value[currentFocusedIndex.value + 1].id
+ focusedId.value = statuses.value[0].uri + statuses.value[0].id
+ } else if (currentFocusedIndex.value < statuses.value.length) {
+ focusedId.value = statuses.value[currentFocusedIndex.value + 1].uri + statuses.value[currentFocusedIndex.value + 1].id
}
}
const focusPrev = () => {
if (currentFocusedIndex.value === 0) {
focusedId.value = null
} else if (currentFocusedIndex.value > 0) {
- focusedId.value = timeline.value[currentFocusedIndex.value - 1].uri + timeline.value[currentFocusedIndex.value - 1].id
+ focusedId.value = statuses.value[currentFocusedIndex.value - 1].uri + statuses.value[currentFocusedIndex.value - 1].id
}
}
const focusToot = (message: Entity.Status) => {
@@ -169,7 +197,8 @@ export default defineComponent({
}
return {
- timeline,
+ loading,
+ statuses,
scroller,
focusedId,
modalOpened,
@@ -179,7 +208,8 @@ export default defineComponent({
focusToot,
heading,
upper,
- account
+ account,
+ backgroundColor
}
}
})
diff --git a/src/renderer/components/organisms/Toot.vue b/src/renderer/components/organisms/Toot.vue
index 432c5c2096..eb46b05877 100644
--- a/src/renderer/components/organisms/Toot.vue
+++ b/src/renderer/components/organisms/Toot.vue
@@ -633,8 +633,8 @@ export default defineComponent({
const deleteToot = (message: Entity.Status) => {
client.value
?.deleteStatus(message.id)
- .then(message => {
- ctx.emit('delete', message)
+ .then(() => {
+ ctx.emit('delete', message.id)
})
.catch(() => {
ElMessage({
diff --git a/src/renderer/store/GlobalHeader.ts b/src/renderer/store/GlobalHeader.ts
index 5f1e2c6a96..348c437357 100644
--- a/src/renderer/store/GlobalHeader.ts
+++ b/src/renderer/store/GlobalHeader.ts
@@ -122,7 +122,6 @@ const actions: ActionTree = {
await dispatch('TimelineSpace/Contents/Home/fetchTimeline', { account, server }, { root: true })
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 })
})
},
@@ -149,14 +148,6 @@ const actions: ActionTree = {
win.ipcRenderer.on(`delete-local-streamings-${account.id}`, (_, id: string) => {
commit('TimelineSpace/Contents/Local/deleteToot', { statusId: id, accountId: account.id }, { root: true })
})
- win.ipcRenderer.removeAllListeners(`update-public-streamings-${account.id}`)
- win.ipcRenderer.on(`update-public-streamings-${account.id}`, (_, update: Entity.Status) => {
- commit('TimelineSpace/Contents/Public/appendTimeline', { status: update, accountId: account.id }, { root: true })
- })
- win.ipcRenderer.removeAllListeners(`delete-public-streamings-${account.id}`)
- 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 })
diff --git a/src/renderer/store/TimelineSpace/Contents.ts b/src/renderer/store/TimelineSpace/Contents.ts
index e91eae7475..718b5e7b64 100644
--- a/src/renderer/store/TimelineSpace/Contents.ts
+++ b/src/renderer/store/TimelineSpace/Contents.ts
@@ -3,7 +3,6 @@ import Notifications, { NotificationsState } from './Contents/Notifications'
import Favourites, { FavouritesState } from './Contents/Favourites'
import Bookmarks, { BookmarksState } from './Contents/Bookmarks'
import Local, { LocalState } from './Contents/Local'
-import Public, { PublicState } from './Contents/Public'
import Search, { SearchModuleState } from './Contents/Search'
import Lists, { ListsModuleState } from './Contents/Lists'
import Hashtag, { HashtagModuleState } from './Contents/Hashtag'
@@ -23,7 +22,6 @@ type ContentsModule = {
Favourites: FavouritesState
Bookmarks: BookmarksState
Local: LocalState
- Public: PublicState
Search: SearchModuleState
Hashtag: HashtagModuleState
FollowRequests: FollowRequestsState
@@ -66,7 +64,6 @@ const Contents: Module = {
Bookmarks,
Local,
DirectMessages,
- Public,
Search,
Lists,
Hashtag,
diff --git a/src/renderer/store/TimelineSpace/Contents/Public.ts b/src/renderer/store/TimelineSpace/Contents/Public.ts
deleted file mode 100644
index 8111a8e044..0000000000
--- a/src/renderer/store/TimelineSpace/Contents/Public.ts
+++ /dev/null
@@ -1,114 +0,0 @@
-import generator, { Entity, FilterContext } from 'megalodon'
-import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
-import { RootState } from '@/store'
-import { LocalAccount } from '~/src/types/localAccount'
-import { LocalServer } from '~/src/types/localServer'
-
-export type PublicState = {
- timeline: { [key: number]: Array }
-}
-
-const state = (): PublicState => ({
- timeline: {}
-})
-
-export const MUTATION_TYPES = {
- APPEND_TIMELINE: 'appendTimeline',
- REPLACE_TIMELINE: 'replaceTimeline',
- INSERT_TIMELINE: 'insertTimeline',
- UPDATE_TOOT: 'updateToot',
- DELETE_TOOT: 'deleteToot'
-}
-
-const mutations: MutationTree = {
- [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.REPLACE_TIMELINE]: (state, obj: { statuses: Array; accountId: number }) => {
- state.timeline[obj.accountId] = obj.statuses
- },
- [MUTATION_TYPES.INSERT_TIMELINE]: (state, obj: { statuses: Array; 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.UPDATE_TOOT]: (state, obj: { status: Entity.Status; accountId: number }) => {
- state.timeline[obj.accountId] = state.timeline[obj.accountId].map(toot => {
- if (toot.id === obj.status.id) {
- return obj.status
- } else if (toot.reblog && 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: obj.status
- }
- return Object.assign(toot, reblog)
- } else {
- return toot
- }
- })
- },
- [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 !== obj.statusId
- }
- })
- }
-}
-
-export const ACTION_TYPES = {
- FETCH_PUBLIC_TIMELINE: 'fetchPublicTimeline',
- LAZY_FETCH_TIMELINE: 'lazyFetchTimeline'
-}
-
-const actions: ActionTree = {
- [ACTION_TYPES.FETCH_PUBLIC_TIMELINE]: async (
- { commit, rootState },
- req: { account: LocalAccount; server: LocalServer }
- ): Promise> => {
- const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
- try {
- const res = await client.getPublicTimeline({ limit: 20 })
- commit(MUTATION_TYPES.REPLACE_TIMELINE, { statuses: res.data, accountId: req.account.id })
- return res.data
- } catch (err) {
- console.error(err)
- return []
- }
- },
- [ACTION_TYPES.LAZY_FETCH_TIMELINE]: async (
- { commit, rootState },
- req: { lastStatus: Entity.Status; account: LocalAccount; server: LocalServer }
- ): Promise | null> => {
- const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
- return client.getPublicTimeline({ max_id: req.lastStatus.id, limit: 20 }).then(res => {
- commit(MUTATION_TYPES.INSERT_TIMELINE, { statuses: res.data, accountId: req.account.id })
- return res.data
- })
- }
-}
-
-const getters: GetterTree = {
- filters: (_state, _getters, rootState) => {
- return rootState.TimelineSpace.filters.filter(f => f.context.includes(FilterContext.Public) && !f.irreversible)
- }
-}
-
-const Public: Module = {
- namespaced: true,
- state: state,
- mutations: mutations,
- actions: actions,
- getters: getters
-}
-
-export default Public
From 273b3c7d13fe9c9c219bcda999ee3c8e1fdbdc71 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Sun, 29 Jan 2023 22:15:25 +0900
Subject: [PATCH 2/7] [refactor] Remove favourites timeline store
---
.../TimelineSpace/Contents/Favourites.vue | 115 +++++++++-------
.../TimelineSpace/Contents/Public.vue | 13 +-
src/renderer/store/TimelineSpace/Contents.ts | 3 -
.../TimelineSpace/Contents/Favourites.ts | 123 ------------------
4 files changed, 78 insertions(+), 176 deletions(-)
delete mode 100644 src/renderer/store/TimelineSpace/Contents/Favourites.ts
diff --git a/src/renderer/components/TimelineSpace/Contents/Favourites.vue b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
index 85dc88b3b9..2d6c5cc77e 100644
--- a/src/renderer/components/TimelineSpace/Contents/Favourites.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
@@ -1,7 +1,7 @@
-
-
+
+
(false)
const focusedId = ref(null)
const scroller = ref()
const { j, k, Ctrl_r } = useMagicKeys()
@@ -58,41 +55,53 @@ export default defineComponent({
const win = (window as any) as MyWindow
const id = computed(() => parseInt(route.params.id as string))
+ const loading = ref(false)
const lazyLoading = ref(false)
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
+ const client = ref(null)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
- const favourites = computed(() => store.state.TimelineSpace.Contents.Favourites.favourites)
+ const favourites = ref>([])
+ const nextMaxId = ref(null)
const modalOpened = computed(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const currentFocusedIndex = computed(() => favourites.value.findIndex(status => focusedId.value === status.uri))
const shortcutEnabled = computed(() => !modalOpened.value)
+ const userAgent = computed(() => store.state.App.userAgent)
+ const backgroundColor = computed(() => store.state.App.theme.background_color)
onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
+ client.value = generator(s.sns, s.baseURL, a.accessToken, userAgent.value)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
- store.commit(`TimelineSpace/Contents/${CONTENTS_MUTATION.CHANGE_LOADING}`, true)
- store
- .dispatch(`${space}/${ACTION_TYPES.FETCH_FAVOURITES}`, account)
- .catch(() => {
- ElMessage({
- message: i18n.t('message.favourite_fetch_error'),
- type: 'error'
- })
- })
- .finally(() => {
- store.commit(`TimelineSpace/Contents/${CONTENTS_MUTATION.CHANGE_LOADING}`, false)
+ loading.value = true
+ try {
+ const res = await client.value.getFavourites({ limit: 20 })
+ favourites.value = res.data
+ const link = parse(res.headers.link)
+ if (link !== null && link.next) {
+ nextMaxId.value = link.next.max_id
+ } else {
+ nextMaxId.value = null
+ }
+ } catch (err) {
+ console.error(err)
+ ElMessage({
+ message: i18n.t('message.favourite_fetch_error'),
+ type: 'error'
})
+ } finally {
+ loading.value = false
+ }
})
onUnmounted(() => {
- store.commit(`${space}/${MUTATION_TYPES.UPDATE_FAVOURITES}`, [])
const el = document.getElementById('scroller')
if (el !== undefined && el !== null) {
el.removeEventListener('scroll', onScroll)
@@ -125,12 +134,23 @@ export default defineComponent({
if (
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
document.getElementById('scroller')!.scrollHeight - 10 &&
- !lazyLoading.value
+ !lazyLoading.value &&
+ nextMaxId.value
) {
lazyLoading.value = true
- store
- .dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_FAVOURITES}`, account)
- .catch(() => {
+ client.value
+ ?.getFavourites({ limit: 20, max_id: nextMaxId.value })
+ .then(res => {
+ favourites.value = [...favourites.value, ...res.data]
+ const link = parse(res.headers.link)
+ if (link !== null && link.next) {
+ nextMaxId.value = link.next.max_id
+ } else {
+ nextMaxId.value = null
+ }
+ })
+ .catch(err => {
+ console.error(err)
ElMessage({
message: i18n.t('message.favourite_fetch_error'),
type: 'error'
@@ -140,36 +160,43 @@ export default defineComponent({
lazyLoading.value = false
})
}
- // for upper
- if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
- heading.value = false
- } else if ((event.target as HTMLElement)!.scrollTop <= 10 && !heading.value) {
- heading.value = true
- }
}
const updateToot = (message: Entity.Status) => {
- store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
+ favourites.value = favourites.value.map(status => {
+ if (status.id === message.id) {
+ return message
+ } else if (status.reblog && status.reblog.id === message.id) {
+ return Object.assign(status, {
+ reblog: message
+ })
+ }
+ return status
+ })
}
const deleteToot = (id: string) => {
- store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, id)
+ favourites.value = favourites.value.filter(status => {
+ if (status.reblog !== null && status.reblog.id === id) {
+ return false
+ } else {
+ return status.id !== id
+ }
+ })
}
const reload = async () => {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
try {
- await store.dispatch(`${space}/${ACTION_TYPES.FETCH_FAVOURITES}`, account).catch(() => {
- ElMessage({
- message: i18n.t('message.favourite_fetch_error'),
- type: 'error'
- })
- })
+ const res = await client.value!.getFavourites({ limit: 20 })
+ favourites.value = res.data
+ const link = parse(res.headers.link)
+ if (link !== null && link.next) {
+ nextMaxId.value = link.next.max_id
+ } else {
+ nextMaxId.value = null
+ }
} finally {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, false)
}
}
- const upper = () => {
- scroller.value.scrollToItem(0)
- focusedId.value = null
- }
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
focusedId.value = favourites.value[0].uri
@@ -189,15 +216,15 @@ export default defineComponent({
}
return {
+ loading,
favourites,
+ backgroundColor,
scroller,
focusedId,
modalOpened,
updateToot,
deleteToot,
focusToot,
- heading,
- upper,
account
}
}
diff --git a/src/renderer/components/TimelineSpace/Contents/Public.vue b/src/renderer/components/TimelineSpace/Contents/Public.vue
index 293ba4a80e..f4a9b461d9 100644
--- a/src/renderer/components/TimelineSpace/Contents/Public.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Public.vue
@@ -54,6 +54,7 @@ export default defineComponent({
const focusedId = ref(null)
const scroller = ref(null)
const loading = ref(false)
+ const lazyLoading = ref(false)
const heading = ref(true)
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
@@ -129,9 +130,11 @@ export default defineComponent({
const onScroll = (event: Event) => {
if (
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
- document.getElementById('scroller')!.scrollHeight - 10
+ document.getElementById('scroller')!.scrollHeight - 10 &&
+ !lazyLoading.value
) {
const lastStatus = statuses.value[statuses.value.length - 1]
+ lazyLoading.value = true
client.value
?.getPublicTimeline({ max_id: lastStatus.id, limit: 20 })
.then(res => {
@@ -144,6 +147,9 @@ export default defineComponent({
type: 'error'
})
})
+ .finally(() => {
+ lazyLoading.value = false
+ })
}
if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
@@ -174,10 +180,6 @@ export default defineComponent({
}
})
}
- const upper = () => {
- scroller.value.scrollToItem(0)
- focusedId.value = null
- }
const focusNext = () => {
if (currentFocusedIndex.value === -1) {
focusedId.value = statuses.value[0].uri + statuses.value[0].id
@@ -207,7 +209,6 @@ export default defineComponent({
deleteToot,
focusToot,
heading,
- upper,
account,
backgroundColor
}
diff --git a/src/renderer/store/TimelineSpace/Contents.ts b/src/renderer/store/TimelineSpace/Contents.ts
index 718b5e7b64..27bd847f77 100644
--- a/src/renderer/store/TimelineSpace/Contents.ts
+++ b/src/renderer/store/TimelineSpace/Contents.ts
@@ -1,6 +1,5 @@
import Home, { HomeState } from './Contents/Home'
import Notifications, { NotificationsState } from './Contents/Notifications'
-import Favourites, { FavouritesState } from './Contents/Favourites'
import Bookmarks, { BookmarksState } from './Contents/Bookmarks'
import Local, { LocalState } from './Contents/Local'
import Search, { SearchModuleState } from './Contents/Search'
@@ -19,7 +18,6 @@ type ContentsModule = {
Home: HomeState
Notifications: NotificationsState
DirectMessages: DirectMessagesState
- Favourites: FavouritesState
Bookmarks: BookmarksState
Local: LocalState
Search: SearchModuleState
@@ -60,7 +58,6 @@ const Contents: Module = {
modules: {
Home,
Notifications,
- Favourites,
Bookmarks,
Local,
DirectMessages,
diff --git a/src/renderer/store/TimelineSpace/Contents/Favourites.ts b/src/renderer/store/TimelineSpace/Contents/Favourites.ts
deleted file mode 100644
index 909c229d58..0000000000
--- a/src/renderer/store/TimelineSpace/Contents/Favourites.ts
+++ /dev/null
@@ -1,123 +0,0 @@
-import generator, { Entity } from 'megalodon'
-import parse from 'parse-link-header'
-import { Module, MutationTree, ActionTree } from 'vuex'
-import { RootState } from '@/store'
-import { LocalAccount } from '~/src/types/localAccount'
-import { LocalServer } from '~/src/types/localServer'
-
-export type FavouritesState = {
- favourites: Array
- maxId: string | null
-}
-
-const state = (): FavouritesState => ({
- favourites: [],
- maxId: null
-})
-
-export const MUTATION_TYPES = {
- UPDATE_FAVOURITES: 'updateFavourites',
- INSERT_FAVOURITES: 'insertFavourites',
- UPDATE_TOOT: 'updateToot',
- DELETE_TOOT: 'deleteToot',
- CHANGE_MAX_ID: 'changeMaxId'
-}
-
-const mutations: MutationTree = {
- [MUTATION_TYPES.UPDATE_FAVOURITES]: (state, favourites: Array) => {
- state.favourites = favourites
- },
- [MUTATION_TYPES.INSERT_FAVOURITES]: (state, favourites: Array) => {
- state.favourites = state.favourites.concat(favourites)
- },
- [MUTATION_TYPES.UPDATE_TOOT]: (state, message: Entity.Status) => {
- state.favourites = state.favourites.map(toot => {
- if (toot.id === message.id) {
- return message
- } else if (toot.reblog !== null && toot.reblog.id === message.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
- }
- return Object.assign(toot, reblog)
- } else {
- return toot
- }
- })
- },
- [MUTATION_TYPES.DELETE_TOOT]: (state, message: Entity.Status) => {
- state.favourites = state.favourites.filter(toot => {
- if (toot.reblog !== null && toot.reblog.id === message.id) {
- return false
- } else {
- return toot.id !== message.id
- }
- })
- },
- [MUTATION_TYPES.CHANGE_MAX_ID]: (state, id: string | null) => {
- state.maxId = id
- }
-}
-
-export const ACTION_TYPES = {
- FETCH_FAVOURITES: 'fetchFavourites',
- LAZY_FETCH_FAVOURITES: 'lazyFetchFavourites'
-}
-
-const actions: ActionTree = {
- [ACTION_TYPES.FETCH_FAVOURITES]: async (
- { commit, rootState },
- req: { account: LocalAccount; server: LocalServer }
- ): Promise> => {
- const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
- const res = await client.getFavourites({ limit: 20 })
- commit(MUTATION_TYPES.UPDATE_FAVOURITES, res.data)
- // Parse link header
- try {
- const link = parse(res.headers.link)
- if (link !== null && link.next) {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, link.next.max_id)
- } else {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
- }
- } catch (err) {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
- console.error(err)
- }
- return res.data
- },
- lazyFetchFavourites: async (
- { state, commit, rootState },
- req: { account: LocalAccount; server: LocalServer }
- ): Promise | null> => {
- if (!state.maxId) {
- return Promise.resolve(null)
- }
- const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
- const res = await client.getFavourites({ max_id: state.maxId, limit: 20 })
- commit(MUTATION_TYPES.INSERT_FAVOURITES, res.data)
- // Parse link header
- try {
- const link = parse(res.headers.link)
- if (link !== null && link.next) {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, link.next.max_id)
- } else {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
- }
- } catch (err) {
- commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
- console.error(err)
- }
- return res.data
- }
-}
-
-const Favourites: Module = {
- namespaced: true,
- state: state,
- mutations: mutations,
- actions: actions
-}
-
-export default Favourites
From 2ed4e8d9e9bb30049a1492fa87c078eb99c412d4 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Sun, 29 Jan 2023 22:24:45 +0900
Subject: [PATCH 3/7] [refactor] Remove follow request store
---
.../TimelineSpace/Contents/Favourites.vue | 3 +-
.../TimelineSpace/Contents/FollowRequests.vue | 55 +++++++++------
src/renderer/store/TimelineSpace/Contents.ts | 5 +-
.../TimelineSpace/Contents/FollowRequests.ts | 68 -------------------
4 files changed, 37 insertions(+), 94 deletions(-)
delete mode 100644 src/renderer/store/TimelineSpace/Contents/FollowRequests.ts
diff --git a/src/renderer/components/TimelineSpace/Contents/Favourites.vue b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
index 2d6c5cc77e..2460b876cc 100644
--- a/src/renderer/components/TimelineSpace/Contents/Favourites.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Favourites.vue
@@ -184,8 +184,9 @@ export default defineComponent({
}
const reload = async () => {
store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
+ if (!client.value) return
try {
- const res = await client.value!.getFavourites({ limit: 20 })
+ const res = await client.value.getFavourites({ limit: 20 })
favourites.value = res.data
const link = parse(res.headers.link)
if (link !== null && link.next) {
diff --git a/src/renderer/components/TimelineSpace/Contents/FollowRequests.vue b/src/renderer/components/TimelineSpace/Contents/FollowRequests.vue
index 21f43f4cc9..a726abd38a 100644
--- a/src/renderer/components/TimelineSpace/Contents/FollowRequests.vue
+++ b/src/renderer/components/TimelineSpace/Contents/FollowRequests.vue
@@ -7,13 +7,12 @@
diff --git a/src/renderer/components/TimelineSpace/Contents/Search/Tag.vue b/src/renderer/components/TimelineSpace/Contents/Search/Tag.vue
index 54f224815a..01445a1314 100644
--- a/src/renderer/components/TimelineSpace/Contents/Search/Tag.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Search/Tag.vue
@@ -11,24 +11,17 @@