2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2022-12-01 09:46:26 +03:00
|
|
|
|
2022-12-29 00:44:23 +03:00
|
|
|
type Action = 'reblogged' | 'favourited' | 'bookmarked' | 'pinned' | 'muted'
|
2022-12-01 09:46:26 +03:00
|
|
|
type CountField = 'reblogsCount' | 'favouritesCount'
|
|
|
|
|
|
|
|
export interface StatusActionsProps {
|
2023-01-08 09:21:09 +03:00
|
|
|
status: mastodon.v1.Status
|
2022-12-01 09:46:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useStatusActions(props: StatusActionsProps) {
|
2024-02-21 18:20:08 +03:00
|
|
|
const status = ref<mastodon.v1.Status>({ ...props.status })
|
|
|
|
const { client } = useMasto()
|
2022-12-01 09:46:26 +03:00
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.status,
|
2024-02-21 18:20:08 +03:00
|
|
|
val => status.value = { ...val },
|
2022-12-01 09:46:26 +03:00
|
|
|
{ deep: true, immediate: true },
|
|
|
|
)
|
|
|
|
|
|
|
|
// Use different states to let the user press different actions right after the other
|
2024-02-21 18:20:08 +03:00
|
|
|
const isLoading = ref({
|
2022-12-01 09:46:26 +03:00
|
|
|
reblogged: false,
|
|
|
|
favourited: false,
|
|
|
|
bookmarked: false,
|
|
|
|
pinned: false,
|
|
|
|
translation: false,
|
2022-12-29 00:44:23 +03:00
|
|
|
muted: false,
|
2022-12-01 09:46:26 +03:00
|
|
|
})
|
|
|
|
|
2023-01-08 09:21:09 +03:00
|
|
|
async function toggleStatusAction(action: Action, fetchNewStatus: () => Promise<mastodon.v1.Status>, countField?: CountField) {
|
2022-12-02 05:18:57 +03:00
|
|
|
// check login
|
|
|
|
if (!checkLogin())
|
|
|
|
return
|
2023-01-08 16:24:59 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const prevCount = countField ? status.value[countField] : undefined
|
2023-01-12 21:48:52 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
isLoading.value[action] = true
|
|
|
|
const isCancel = status.value[action]
|
2022-12-02 19:55:52 +03:00
|
|
|
fetchNewStatus().then((newStatus) => {
|
2023-01-12 21:48:52 +03:00
|
|
|
// when the action is cancelled, the count is not updated highly likely (if they're the same)
|
|
|
|
// issue of Mastodon API
|
|
|
|
if (isCancel && countField && prevCount === newStatus[countField])
|
|
|
|
newStatus[countField] -= 1
|
|
|
|
|
2022-12-02 19:55:52 +03:00
|
|
|
Object.assign(status, newStatus)
|
2022-12-25 17:52:37 +03:00
|
|
|
cacheStatus(newStatus, undefined, true)
|
2022-12-02 19:55:52 +03:00
|
|
|
}).finally(() => {
|
2024-02-21 18:20:08 +03:00
|
|
|
isLoading.value[action] = false
|
2022-12-02 19:55:52 +03:00
|
|
|
})
|
2022-12-02 05:18:57 +03:00
|
|
|
// Optimistic update
|
2024-02-21 18:20:08 +03:00
|
|
|
status.value[action] = !status.value[action]
|
|
|
|
cacheStatus(status.value, undefined, true)
|
2022-12-01 09:46:26 +03:00
|
|
|
if (countField)
|
2024-02-21 18:20:08 +03:00
|
|
|
status.value[countField] += status.value[action] ? 1 : -1
|
2022-12-01 09:46:26 +03:00
|
|
|
}
|
2023-01-08 16:24:59 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const canReblog = computed(() =>
|
|
|
|
status.value.visibility !== 'direct'
|
|
|
|
&& (status.value.visibility !== 'private' || status.value.account.id === currentUser.value?.account.id),
|
2023-01-08 16:24:59 +03:00
|
|
|
)
|
|
|
|
|
2022-12-01 09:46:26 +03:00
|
|
|
const toggleReblog = () => toggleStatusAction(
|
|
|
|
'reblogged',
|
2024-02-21 18:20:08 +03:00
|
|
|
() => client.value.v1.statuses.$select(status.value.id)[status.value.reblogged ? 'unreblog' : 'reblog']().then((res) => {
|
|
|
|
if (status.value.reblogged)
|
2022-12-01 09:46:26 +03:00
|
|
|
// returns the original status
|
|
|
|
return res.reblog!
|
|
|
|
return res
|
|
|
|
}),
|
|
|
|
'reblogsCount',
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggleFavourite = () => toggleStatusAction(
|
|
|
|
'favourited',
|
2024-02-21 18:20:08 +03:00
|
|
|
() => client.value.v1.statuses.$select(status.value.id)[status.value.favourited ? 'unfavourite' : 'favourite'](),
|
2022-12-01 09:46:26 +03:00
|
|
|
'favouritesCount',
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggleBookmark = () => toggleStatusAction(
|
|
|
|
'bookmarked',
|
2024-02-21 18:20:08 +03:00
|
|
|
() => client.value.v1.statuses.$select(status.value.id)[status.value.bookmarked ? 'unbookmark' : 'bookmark'](),
|
2022-12-01 09:46:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const togglePin = async () => toggleStatusAction(
|
|
|
|
'pinned',
|
2024-02-21 18:20:08 +03:00
|
|
|
() => client.value.v1.statuses.$select(status.value.id)[status.value.pinned ? 'unpin' : 'pin'](),
|
2022-12-01 09:46:26 +03:00
|
|
|
)
|
|
|
|
|
2022-12-29 00:44:23 +03:00
|
|
|
const toggleMute = async () => toggleStatusAction(
|
|
|
|
'muted',
|
2024-02-21 18:20:08 +03:00
|
|
|
() => client.value.v1.statuses.$select(status.value.id)[status.value.muted ? 'unmute' : 'mute'](),
|
2022-12-29 00:44:23 +03:00
|
|
|
)
|
|
|
|
|
2022-12-01 09:46:26 +03:00
|
|
|
return {
|
2024-02-21 18:20:08 +03:00
|
|
|
status,
|
|
|
|
isLoading,
|
|
|
|
canReblog,
|
2022-12-29 00:44:23 +03:00
|
|
|
toggleMute,
|
2022-12-01 09:46:26 +03:00
|
|
|
toggleReblog,
|
|
|
|
toggleFavourite,
|
|
|
|
toggleBookmark,
|
|
|
|
togglePin,
|
|
|
|
}
|
|
|
|
}
|