From 18d5fd4804569ce6ce0b393c0f96b1bbc4d469a4 Mon Sep 17 00:00:00 2001 From: Chris <1633711653@qq.com> Date: Fri, 2 Dec 2022 10:18:57 +0800 Subject: [PATCH] fix: checkLogin in StatusCard (#270) --- components/status/StatusActionButton.vue | 2 ++ components/status/StatusActions.vue | 2 ++ composables/status.ts | 17 ++++++++++------- composables/users.ts | 8 ++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/components/status/StatusActionButton.vue b/components/status/StatusActionButton.vue index 1c79f536..8e2d2a81 100644 --- a/components/status/StatusActionButton.vue +++ b/components/status/StatusActionButton.vue @@ -29,6 +29,8 @@ useCommand({ icon: () => props.icon, onActivate() { + if (!checkLogin()) + return const clickEvent = new MouseEvent('click', { view: window, bubbles: true, diff --git a/components/status/StatusActions.vue b/components/status/StatusActions.vue index de9ca77d..5731f084 100644 --- a/components/status/StatusActions.vue +++ b/components/status/StatusActions.vue @@ -18,6 +18,8 @@ const { } = $(useStatusActions(props)) const reply = () => { + if (!checkLogin()) + return if (details) { // TODO focus to editor } diff --git a/composables/status.ts b/composables/status.ts index 9f771a53..a559cf77 100644 --- a/composables/status.ts +++ b/composables/status.ts @@ -25,15 +25,18 @@ export function useStatusActions(props: StatusActionsProps) { translation: false, }) - async function toggleStatusAction(action: Action, newStatus: Promise, countField?: CountField) { - // Optimistic update + async function toggleStatusAction(action: Action, newStatus: () => Promise, countField?: CountField) { + // check login + if (!checkLogin()) + return + // Optimistic update status[action] = !status[action] if (countField) status[countField] += status[action] ? 1 : -1 try { isLoading[action] = true - Object.assign(status, await newStatus) + Object.assign(status, await newStatus()) } finally { isLoading[action] = false @@ -41,7 +44,7 @@ export function useStatusActions(props: StatusActionsProps) { } const toggleReblog = () => toggleStatusAction( 'reblogged', - useMasto().statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => { + () => useMasto().statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => { if (status.reblogged) // returns the original status return res.reblog! @@ -52,18 +55,18 @@ export function useStatusActions(props: StatusActionsProps) { const toggleFavourite = () => toggleStatusAction( 'favourited', - useMasto().statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id), + () => useMasto().statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id), 'favouritesCount', ) const toggleBookmark = () => toggleStatusAction( 'bookmarked', - useMasto().statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id), + () => useMasto().statuses[status.bookmarked ? 'unbookmark' : 'bookmark'](status.id), ) const togglePin = async () => toggleStatusAction( 'pinned', - useMasto().statuses[status.pinned ? 'unpin' : 'pin'](status.id), + () => useMasto().statuses[status.pinned ? 'unpin' : 'pin'](status.id), ) return { diff --git a/composables/users.ts b/composables/users.ts index 0f0a78a5..ff7d32df 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -96,3 +96,11 @@ export async function signout() { await loginTo(currentUser.value) } + +export function checkLogin() { + if (!currentUser.value) { + openSigninDialog() + return false + } + return true +}