From 9fe60eb860299dbc84c6f238a8fd25b9ef51dd92 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 3 Sep 2026 16:55:41 +0300 Subject: [PATCH] review --- src/components/emoji_input/suggestor.js | 2 +- src/components/notification/notification.js | 4 +- src/components/side_drawer/side_drawer.js | 2 +- src/components/side_drawer/side_drawer.vue | 4 +- src/stores/drafts.js | 6 +-- src/stores/follow_requests.js | 7 +-- src/stores/instance_capabilities.js | 10 +++++ src/stores/shout.js | 3 +- test/unit/specs/stores/users.spec.js | 49 +++++++++++++-------- 9 files changed, 56 insertions(+), 31 deletions(-) diff --git a/src/components/emoji_input/suggestor.js b/src/components/emoji_input/suggestor.js index 72e376648..7d71f2978 100644 --- a/src/components/emoji_input/suggestor.js +++ b/src/components/emoji_input/suggestor.js @@ -15,7 +15,7 @@ import { useUsersStore } from 'src/stores/users.js' export default (data) => { const emojiCurry = suggestEmoji(data.emoji) - const usersCurry = data.store && suggestUsers(data.store) + const usersCurry = suggestUsers() return (input, nameKeywordLocalizer) => { const firstChar = input[0] if (firstChar === ':' && data.emoji) { diff --git a/src/components/notification/notification.js b/src/components/notification/notification.js index 51b60fb29..65acfed5c 100644 --- a/src/components/notification/notification.js +++ b/src/components/notification/notification.js @@ -165,7 +165,9 @@ const Notification = { id: this.user.id, credentials: useOAuthStore().token, }).then(() => { - useNotificationsStore().dismissNotificationLocal(this.notification.id) + useNotificationsStore().markSingleNotificationAsSeen( + this.notification.id, + ) useFollowRequestsStore().remove(this.user.id) }) this.hideDenyConfirmDialog() diff --git a/src/components/side_drawer/side_drawer.js b/src/components/side_drawer/side_drawer.js index 6b3fad7fa..02c6d63d0 100644 --- a/src/components/side_drawer/side_drawer.js +++ b/src/components/side_drawer/side_drawer.js @@ -110,7 +110,7 @@ const SideDrawer = { hideSitename: (store) => store.instanceIdentity.hideSitename, }), ...mapState(useChatsStore, ['unreadChatsCount']), - ...mapState(useDraftsStore, ['draftCount']), + ...mapState(useDraftsStore, ['draftsCount']), }, methods: { toggleDrawer() { diff --git a/src/components/side_drawer/side_drawer.vue b/src/components/side_drawer/side_drawer.vue index f7b0563ba..8bd3b1336 100644 --- a/src/components/side_drawer/side_drawer.vue +++ b/src/components/side_drawer/side_drawer.vue @@ -269,10 +269,10 @@ icon="file-pen" /> {{ $t('nav.drafts') }} - {{ draftCount }} + {{ draftsCount }} diff --git a/src/stores/drafts.js b/src/stores/drafts.js index bf707cbe4..0d2986e3c 100644 --- a/src/stores/drafts.js +++ b/src/stores/drafts.js @@ -57,7 +57,7 @@ export const useDraftsStore = defineStore('drafts', { await deleteDraftFromStorage([id]) }, async loadDrafts() { - const currentData = await getStorageData() + const currentData = (await getStorageData()) ?? {} this.drafts = new Map(Object.entries(currentData)) }, async addOrSaveDraft(draft) { @@ -67,9 +67,9 @@ export const useDraftsStore = defineStore('drafts', { await saveDraftToStorage(draftWithId) return id }, - async abandonAllDrafts(store) { + async abandonAllDrafts() { const ids = [...this.drafts.keys()] - ids.forEach((id) => this.abandonDraft(id)) + ids.forEach((id) => this.drafts.delete(id)) await deleteDraftFromStorage(ids) }, }, diff --git a/src/stores/follow_requests.js b/src/stores/follow_requests.js index 27f8cbc00..e30d7d4ca 100644 --- a/src/stores/follow_requests.js +++ b/src/stores/follow_requests.js @@ -15,7 +15,7 @@ export const useFollowRequestsStore = defineStore('followRequests', { }, actions: { startFetching() { - if (this.fetcher) throw 'Fetcher already exists!' + if (this.fetcher) throw new Error('Fetcher already exists!') this.fetcher = followRequestFetcher({ credentials: useOAuthStore().token, @@ -24,8 +24,9 @@ export const useFollowRequestsStore = defineStore('followRequests', { this.fetcher.startFetching() }, stopFetching() { - if (!this.fetcher) throw "Fetcher doesn't exists!" - this.fetcher.stopFetching(), (this.fetcher = null) + if (!this.fetcher) throw new Error("Fetcher doesn't exists!") + this.fetcher.stopFetching() + this.fetcher = null }, setFollowRequests(requests) { this.requests = new Map(requests.map((user) => [user.id, user])) diff --git a/src/stores/instance_capabilities.js b/src/stores/instance_capabilities.js index a794ea21b..3e7ae3f0b 100644 --- a/src/stores/instance_capabilities.js +++ b/src/stores/instance_capabilities.js @@ -1,5 +1,8 @@ import { defineStore } from 'pinia' +import { useShoutStore } from 'src/stores/shout.js' +import { useUsersStore } from 'src/stores/users.js' + const defaultState = { postFormats: [], mailerEnabled: false, @@ -38,6 +41,13 @@ export const useInstanceCapabilitiesStore = defineStore( } this[capability] = value + + if ( + capability === 'shoutAvailable' && + useUsersStore().currentUser?.token + ) { + useShoutStore().initializeSocket() + } }, }, }, diff --git a/src/stores/shout.js b/src/stores/shout.js index 487b04c4d..05cd340be 100644 --- a/src/stores/shout.js +++ b/src/stores/shout.js @@ -10,7 +10,6 @@ export const useShoutStore = defineStore('shout', { messages: [], channel: { state: '' }, joined: false, - token: null, socket: null, }), getters: { @@ -20,7 +19,7 @@ export const useShoutStore = defineStore('shout', { initializeSocket() { if (this.token === null) return if (!useInstanceCapabilitiesStore().shoutAvailable) return - if (this.socket !== null) throw new Error('Shout socket already exist!') + if (this.socket !== null) return this.socket = new Socket('/socket', { params: { token: this.token } }) this.socket.connect() diff --git a/test/unit/specs/stores/users.spec.js b/test/unit/specs/stores/users.spec.js index 702f3b31b..6743ae8cc 100644 --- a/test/unit/specs/stores/users.spec.js +++ b/test/unit/specs/stores/users.spec.js @@ -5,7 +5,9 @@ import { setActivePinia } from 'pinia' import { useAnnouncementsStore } from 'src/stores/announcements.js' import { useBookmarkFoldersStore } from 'src/stores/bookmark_folders.js' import { useChatsStore } from 'src/stores/chats.js' +import { useDraftsStore } from 'src/stores/drafts.js' import { useEmojiStore } from 'src/stores/emoji.js' +import { useFollowRequestsStore } from 'src/stores/follow_requests.js' import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js' import { useInterfaceStore } from 'src/stores/interface.js' import { useListsStore } from 'src/stores/lists.js' @@ -53,12 +55,14 @@ const mockMastoAPIUser = ({ name = userName, url = userUrl, id = userId, + locked = true, } = {}) => ({ id, acct: screen_name, display_name: name, fields: [], avatar: '', + locked, url, }) @@ -67,17 +71,20 @@ const mockUser = ({ id = userId, name = userName, url = userUrl, + locked = true, } = {}) => ({ _original: mockMastoAPIUser({ screen_name, id, name, url, + locked, }), id, name, screen_name, url, + locked, relationship: undefined, }) @@ -631,22 +638,24 @@ describe('Users store', () => { const spies = [ // Misc initialization - vi.spyOn(useSyncConfigStore(), 'initSyncConfig'), - vi.spyOn(useUserHighlightStore(), 'initUserHighlight'), - vi.spyOn(useInterfaceStore(), 'applyTheme'), - vi.spyOn(useInterfaceStore(), 'onLogin'), - vi.spyOn(useEmojiStore(), 'fetchEmoji'), + /* 0 */ vi.spyOn(useSyncConfigStore(), 'initSyncConfig'), + /* 1 */ vi.spyOn(useUserHighlightStore(), 'initUserHighlight'), + /* 2 */ vi.spyOn(useInterfaceStore(), 'applyTheme'), + /* 3 */ vi.spyOn(useInterfaceStore(), 'onLogin'), + /* 4 */ vi.spyOn(useEmojiStore(), 'fetchEmoji'), + /* 5 */ vi.spyOn(useDraftsStore(), 'loadDrafts'), // Timeline / Notifications - vi.spyOn(useNotificationsStore(), 'activate'), - vi.spyOn(useTimelinesStore(), 'activatePersistents'), + /* 6 */ vi.spyOn(useNotificationsStore(), 'activate'), + /* 7 */ vi.spyOn(useTimelinesStore(), 'activatePersistents'), // Fetchers - vi.spyOn(useChatsStore(), 'startFetching'), - vi.spyOn(useListsStore(), 'startFetching'), - vi.spyOn(useAnnouncementsStore(), 'startFetching'), - vi.spyOn(useBookmarkFoldersStore(), 'startFetching'), - vi.spyOn(useStreamingStore(), 'initSocket'), + /* 8 */ vi.spyOn(useChatsStore(), 'startFetching'), + /* 9 */ vi.spyOn(useListsStore(), 'startFetching'), + /* 10 */ vi.spyOn(useAnnouncementsStore(), 'startFetching'), + /* 11 */ vi.spyOn(useBookmarkFoldersStore(), 'startFetching'), + /* 12 */ vi.spyOn(useFollowRequestsStore(), 'startFetching'), + /* 13 */ vi.spyOn(useStreamingStore(), 'initSocket'), ] spies.forEach((spy) => { @@ -758,6 +767,7 @@ describe('Users store', () => { /* 11 */ vi.spyOn(useAnnouncementsStore(), 'stopFetching'), /* 12 */ vi.spyOn(useBookmarkFoldersStore(), 'stopFetching'), /* 13 */ vi.spyOn(useStreamingStore(), 'stopSocket'), + /* 14 */ vi.spyOn(useFollowRequestsStore(), 'stopFetching'), ] spies.forEach((spy) => { @@ -771,6 +781,7 @@ describe('Users store', () => { const store = useUsersStore() store.currentUser = mockUser() + store.currentUser.locked = true // Adding some users to verify they are getting cleaned afterwards store.addNewUsers({ @@ -832,17 +843,19 @@ describe('Users store', () => { /* 3 */ vi.spyOn(useChatsStore(), 'stopFetching'), /* 4 */ vi.spyOn(useAnnouncementsStore(), 'stopFetching'), /* 5 */ vi.spyOn(useBookmarkFoldersStore(), 'stopFetching'), + /* 6 */ vi.spyOn(useFollowRequestsStore(), 'stopFetching'), // ## RESUME ## // Timeline / Notifications - /* 6 */ vi.spyOn(useNotificationsStore(), 'resume'), - /* 7 */ vi.spyOn(useTimelinesStore(), 'resumeAll'), + /* 7 */ vi.spyOn(useNotificationsStore(), 'resume'), + /* 8 */ vi.spyOn(useTimelinesStore(), 'resumeAll'), // Fetchers (Pauseless) - /* 8 */ vi.spyOn(useListsStore(), 'startFetching'), - /* 9 */ vi.spyOn(useChatsStore(), 'startFetching'), - /* 10 */ vi.spyOn(useAnnouncementsStore(), 'startFetching'), - /* 11 */ vi.spyOn(useBookmarkFoldersStore(), 'startFetching'), + /* 9 */ vi.spyOn(useListsStore(), 'startFetching'), + /* 10 */ vi.spyOn(useChatsStore(), 'startFetching'), + /* 11 */ vi.spyOn(useAnnouncementsStore(), 'startFetching'), + /* 12 */ vi.spyOn(useBookmarkFoldersStore(), 'startFetching'), + /* 13 */ vi.spyOn(useFollowRequestsStore(), 'startFetching'), ] spies.forEach((spy) => {