1
0
elk/composables/masto/status.ts

105 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-01-08 15:21:09 +09:00
import type { mastodon } from 'masto'
2022-12-01 15:46:26 +09:00
type Action = 'reblogged' | 'favourited' | 'bookmarked' | 'pinned' | 'muted'
2022-12-01 15:46:26 +09:00
type CountField = 'reblogsCount' | 'favouritesCount'
export interface StatusActionsProps {
2023-01-08 15:21:09 +09:00
status: mastodon.v1.Status
2022-12-01 15:46:26 +09:00
}
export function useStatusActions(props: StatusActionsProps) {
const status = ref<mastodon.v1.Status>({ ...props.status })
const { client } = useMasto()
2022-12-01 15:46:26 +09:00
watch(
() => props.status,
val => status.value = { ...val },
2022-12-01 15:46:26 +09:00
{ deep: true, immediate: true },
)
// Use different states to let the user press different actions right after the other
const isLoading = ref({
2022-12-01 15:46:26 +09:00
reblogged: false,
favourited: false,
bookmarked: false,
pinned: false,
translation: false,
muted: false,
2022-12-01 15:46:26 +09:00
})
2023-01-08 15:21:09 +09:00
async function toggleStatusAction(action: Action, fetchNewStatus: () => Promise<mastodon.v1.Status>, countField?: CountField) {
2022-12-02 11:18:57 +09:00
// check login
if (!checkLogin())
return
const prevCount = countField ? status.value[countField] : undefined
isLoading.value[action] = true
const isCancel = status.value[action]
fetchNewStatus().then((newStatus) => {
// 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
Object.assign(status, newStatus)
cacheStatus(newStatus, undefined, true)
}).finally(() => {
isLoading.value[action] = false
})
2022-12-02 11:18:57 +09:00
// Optimistic update
status.value[action] = !status.value[action]
cacheStatus(status.value, undefined, true)
2022-12-01 15:46:26 +09:00
if (countField)
status.value[countField] += status.value[action] ? 1 : -1
2022-12-01 15:46:26 +09:00
}
const canReblog = computed(() =>
status.value.visibility !== 'direct'
&& (status.value.visibility !== 'private' || status.value.account.id === currentUser.value?.account.id),
)
2022-12-01 15:46:26 +09:00
const toggleReblog = () => toggleStatusAction(
'reblogged',
() => client.value.v1.statuses.$select(status.value.id)[status.value.reblogged ? 'unreblog' : 'reblog']().then((res) => {
if (status.value.reblogged)
2022-12-01 15:46:26 +09:00
// returns the original status
return res.reblog!
return res
}),
'reblogsCount',
)
const toggleFavourite = () => toggleStatusAction(
'favourited',
() => client.value.v1.statuses.$select(status.value.id)[status.value.favourited ? 'unfavourite' : 'favourite'](),
2022-12-01 15:46:26 +09:00
'favouritesCount',
)
const toggleBookmark = () => toggleStatusAction(
'bookmarked',
() => client.value.v1.statuses.$select(status.value.id)[status.value.bookmarked ? 'unbookmark' : 'bookmark'](),
2022-12-01 15:46:26 +09:00
)
const togglePin = async () => toggleStatusAction(
'pinned',
() => client.value.v1.statuses.$select(status.value.id)[status.value.pinned ? 'unpin' : 'pin'](),
2022-12-01 15:46:26 +09:00
)
const toggleMute = async () => toggleStatusAction(
'muted',
() => client.value.v1.statuses.$select(status.value.id)[status.value.muted ? 'unmute' : 'mute'](),
)
2022-12-01 15:46:26 +09:00
return {
status,
isLoading,
canReblog,
toggleMute,
2022-12-01 15:46:26 +09:00
toggleReblog,
toggleFavourite,
toggleBookmark,
togglePin,
}
}