2022-11-16 00:21:54 +03:00
|
|
|
<script setup lang="ts">
|
2023-01-08 09:21:09 +03:00
|
|
|
import { mastodon } from 'masto'
|
|
|
|
import type { Paginator, WsEvents } from 'masto'
|
2023-01-05 19:48:20 +03:00
|
|
|
// type used in <template>
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
2023-01-09 00:25:25 +03:00
|
|
|
import type { GroupedAccountLike, GroupedLikeNotifications, GroupedNotifications, NotificationSlot } from '~/types'
|
2022-11-16 00:21:54 +03:00
|
|
|
|
2022-12-02 05:21:10 +03:00
|
|
|
const { paginator, stream } = defineProps<{
|
2023-01-09 00:25:25 +03:00
|
|
|
paginator: Paginator<NotificationSlot[], mastodon.v1.ListNotificationsParams>
|
2022-12-29 00:43:46 +03:00
|
|
|
stream?: Promise<WsEvents>
|
2022-11-16 00:21:54 +03:00
|
|
|
}>()
|
2022-11-30 03:47:54 +03:00
|
|
|
|
2022-12-12 01:40:40 +03:00
|
|
|
const groupCapacity = Number.MAX_VALUE // No limit
|
|
|
|
|
|
|
|
// Group by type (and status when applicable)
|
2023-01-08 09:21:09 +03:00
|
|
|
const groupId = (item: mastodon.v1.Notification): string => {
|
2022-12-12 01:40:40 +03:00
|
|
|
// If the update is related to an status, group notifications from the same account (boost + favorite the same status)
|
|
|
|
const id = item.status
|
|
|
|
? {
|
|
|
|
status: item.status?.id,
|
|
|
|
type: (item.type === 'reblog' || item.type === 'favourite') ? 'like' : item.type,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
type: item.type,
|
|
|
|
}
|
|
|
|
return JSON.stringify(id)
|
|
|
|
}
|
|
|
|
|
2023-01-08 09:21:09 +03:00
|
|
|
function groupItems(items: mastodon.v1.Notification[]): NotificationSlot[] {
|
2022-12-12 01:40:40 +03:00
|
|
|
const results: NotificationSlot[] = []
|
2022-11-30 03:47:54 +03:00
|
|
|
|
|
|
|
let id = 0
|
2022-12-12 01:40:40 +03:00
|
|
|
let currentGroupId = ''
|
2023-01-08 09:21:09 +03:00
|
|
|
let currentGroup: mastodon.v1.Notification[] = []
|
2022-12-12 01:40:40 +03:00
|
|
|
const processGroup = () => {
|
|
|
|
if (currentGroup.length === 0)
|
|
|
|
return
|
|
|
|
|
|
|
|
const group = currentGroup
|
|
|
|
currentGroup = []
|
2022-11-30 03:47:54 +03:00
|
|
|
|
2022-12-12 01:40:40 +03:00
|
|
|
// Only group follow notifications when there are too many in a row
|
|
|
|
// This normally happens when you transfer an account, if not, show
|
|
|
|
// a big profile card for each follow
|
2022-12-14 02:06:53 +03:00
|
|
|
if (group[0].type === 'follow') {
|
|
|
|
const toGroup = []
|
|
|
|
for (const item of group) {
|
|
|
|
const hasHeader = !item.account.header.endsWith('/original/missing.png')
|
|
|
|
if (hasHeader && (item.account.followersCount > 250 || (group.length === 1 && item.account.followersCount > 25)))
|
|
|
|
results.push(item)
|
|
|
|
else
|
|
|
|
toGroup.push(item)
|
|
|
|
}
|
|
|
|
if (toGroup.length > 0) {
|
|
|
|
results.push({
|
|
|
|
id: `grouped-${id++}`,
|
|
|
|
type: `grouped-${group[0].type}`,
|
|
|
|
items: toGroup,
|
|
|
|
})
|
|
|
|
}
|
2022-12-12 01:40:40 +03:00
|
|
|
return
|
2022-11-30 03:47:54 +03:00
|
|
|
}
|
2022-12-12 01:40:40 +03:00
|
|
|
|
|
|
|
const { status } = group[0]
|
|
|
|
if (status && group.length > 1 && (group[0].type === 'reblog' || group[0].type === 'favourite')) {
|
|
|
|
// All notifications in these group are reblogs or favourites of the same status
|
|
|
|
const likes: GroupedAccountLike[] = []
|
|
|
|
for (const notification of group) {
|
|
|
|
let like = likes.find(like => like.account.id === notification.account.id)
|
|
|
|
if (!like) {
|
|
|
|
like = { account: notification.account }
|
|
|
|
likes.push(like)
|
|
|
|
}
|
|
|
|
like[notification.type === 'reblog' ? 'reblog' : 'favourite'] = notification
|
|
|
|
}
|
2022-12-27 23:49:04 +03:00
|
|
|
likes.sort((a, b) => a.reblog ? !b.reblog || (a.favourite && !b.favourite) ? -1 : 0 : 0)
|
2022-11-30 03:47:54 +03:00
|
|
|
results.push({
|
|
|
|
id: `grouped-${id++}`,
|
2022-12-12 01:40:40 +03:00
|
|
|
type: 'grouped-reblogs-and-favourites',
|
|
|
|
status,
|
|
|
|
likes,
|
2022-11-30 03:47:54 +03:00
|
|
|
})
|
2022-12-12 01:40:40 +03:00
|
|
|
return
|
2022-11-30 03:47:54 +03:00
|
|
|
}
|
2022-12-12 01:40:40 +03:00
|
|
|
|
|
|
|
results.push(...group)
|
2022-11-30 03:47:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const item of items) {
|
2022-12-12 01:40:40 +03:00
|
|
|
const itemId = groupId(item)
|
|
|
|
// Finalize group if it already has too many notifications
|
|
|
|
if (currentGroupId !== itemId || currentGroup.length >= groupCapacity)
|
|
|
|
processGroup()
|
2022-11-30 03:47:54 +03:00
|
|
|
|
2022-12-12 01:40:40 +03:00
|
|
|
currentGroup.push(item)
|
|
|
|
currentGroupId = itemId
|
|
|
|
}
|
|
|
|
// Finalize remaining groups
|
|
|
|
processGroup()
|
2022-11-30 03:47:54 +03:00
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2022-12-02 05:21:10 +03:00
|
|
|
|
2023-01-09 00:25:25 +03:00
|
|
|
function preprocess(items: NotificationSlot[]): NotificationSlot[] {
|
|
|
|
const flattenedNotifications: mastodon.v1.Notification[] = []
|
|
|
|
for (const item of items) {
|
|
|
|
if (item.type === 'grouped-reblogs-and-favourites') {
|
|
|
|
const group = item as GroupedLikeNotifications
|
|
|
|
for (const like of group.likes) {
|
|
|
|
if (like.reblog)
|
|
|
|
flattenedNotifications.push(like.reblog)
|
|
|
|
if (like.favourite)
|
|
|
|
flattenedNotifications.push(like.favourite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (item.type.startsWith('grouped-')) {
|
|
|
|
flattenedNotifications.push(...(item as GroupedNotifications).items)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
flattenedNotifications.push(item as mastodon.v1.Notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groupItems(flattenedNotifications)
|
|
|
|
}
|
|
|
|
|
2022-12-02 05:21:10 +03:00
|
|
|
const { clearNotifications } = useNotifications()
|
2023-01-01 17:29:11 +03:00
|
|
|
const { formatNumber } = useHumanReadableNumber()
|
2022-11-16 00:21:54 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-09 00:25:25 +03:00
|
|
|
<CommonPaginator :paginator="paginator" :preprocess="preprocess" :stream="stream" :eager="3" event-type="notification">
|
2022-12-02 05:21:10 +03:00
|
|
|
<template #updater="{ number, update }">
|
|
|
|
<button py-4 border="b base" flex="~ col" p-3 w-full text-primary font-bold @click="() => { update(); clearNotifications() }">
|
2023-01-01 17:29:11 +03:00
|
|
|
{{ $t('timeline.show_new_items', number, { named: { v: formatNumber(number) } }) }}
|
2022-12-02 05:21:10 +03:00
|
|
|
</button>
|
|
|
|
</template>
|
2022-11-30 03:47:54 +03:00
|
|
|
<template #items="{ items }">
|
2023-01-09 00:25:25 +03:00
|
|
|
<template v-for="item of items" :key="item.id">
|
2022-11-30 03:47:54 +03:00
|
|
|
<NotificationGroupedFollow
|
|
|
|
v-if="item.type === 'grouped-follow'"
|
|
|
|
:items="item"
|
2022-12-06 14:07:17 +03:00
|
|
|
border="b base"
|
2022-11-30 03:47:54 +03:00
|
|
|
/>
|
2022-12-12 01:40:40 +03:00
|
|
|
<NotificationGroupedLikes
|
|
|
|
v-else-if="item.type === 'grouped-reblogs-and-favourites'"
|
2023-01-05 19:48:20 +03:00
|
|
|
:group="item as GroupedLikeNotifications"
|
2022-12-12 01:40:40 +03:00
|
|
|
border="b base"
|
|
|
|
/>
|
2022-11-30 03:47:54 +03:00
|
|
|
<NotificationCard
|
|
|
|
v-else
|
2023-01-08 09:21:09 +03:00
|
|
|
:notification="item as mastodon.v1.Notification"
|
2022-11-30 03:47:54 +03:00
|
|
|
hover:bg-active
|
2022-12-06 14:07:17 +03:00
|
|
|
border="b base"
|
2022-11-30 03:47:54 +03:00
|
|
|
/>
|
|
|
|
</template>
|
2022-11-16 19:11:08 +03:00
|
|
|
</template>
|
|
|
|
</CommonPaginator>
|
2022-11-16 00:21:54 +03:00
|
|
|
</template>
|