2022-11-15 21:21:54 +00:00
|
|
|
<script setup lang="ts">
|
2023-01-11 09:59:47 +00:00
|
|
|
// @ts-expect-error missing types
|
|
|
|
import { DynamicScrollerItem } from 'vue-virtual-scroller'
|
2023-01-09 12:23:15 +00:00
|
|
|
import type { Paginator, WsEvents, mastodon } from 'masto'
|
|
|
|
import type { GroupedAccountLike, NotificationSlot } from '~/types'
|
2022-11-15 21:21:54 +00:00
|
|
|
|
2022-12-02 02:21:10 +00:00
|
|
|
const { paginator, stream } = defineProps<{
|
2023-01-09 12:23:15 +00:00
|
|
|
paginator: Paginator<mastodon.v1.Notification[], mastodon.v1.ListNotificationsParams>
|
2022-12-28 21:43:46 +00:00
|
|
|
stream?: Promise<WsEvents>
|
2022-11-15 21:21:54 +00:00
|
|
|
}>()
|
2022-11-30 00:47:54 +00:00
|
|
|
|
2023-01-12 23:05:09 +00:00
|
|
|
const virtualScroller = false // TODO: fix flickering issue with virtual scroll
|
2023-01-11 09:59:47 +00:00
|
|
|
|
2022-12-11 22:40:40 +00:00
|
|
|
const groupCapacity = Number.MAX_VALUE // No limit
|
|
|
|
|
2023-10-09 21:33:02 +01:00
|
|
|
const includeNotificationTypes: mastodon.v1.NotificationType[] = ['update', 'mention', 'poll', 'status']
|
|
|
|
|
|
|
|
function includeNotificationsForStatusCard({ type, status }: mastodon.v1.Notification) {
|
|
|
|
// Exclude update, mention, pool and status notifications without the status entry:
|
|
|
|
// no makes sense to include them
|
|
|
|
// Those notifications will be shown using StatusCard SFC:
|
|
|
|
// check NotificationCard SFC L68 and L81 => :status="notification.status!"
|
|
|
|
return status || !includeNotificationTypes.includes(type)
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:40:40 +00:00
|
|
|
// Group by type (and status when applicable)
|
2023-03-30 20:01:24 +01:00
|
|
|
function groupId(item: mastodon.v1.Notification): string {
|
2022-12-11 22:40:40 +00: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-23 20:49:00 +00:00
|
|
|
function hasHeader(account: mastodon.v1.Account) {
|
|
|
|
return !account.header.endsWith('/original/missing.png')
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:39:59 +00:00
|
|
|
function groupItems(items: mastodon.v1.Notification[]): NotificationSlot[] {
|
2022-12-11 22:40:40 +00:00
|
|
|
const results: NotificationSlot[] = []
|
2022-11-30 00:47:54 +00:00
|
|
|
|
|
|
|
let id = 0
|
2022-12-11 22:40:40 +00:00
|
|
|
let currentGroupId = ''
|
2023-01-08 06:21:09 +00:00
|
|
|
let currentGroup: mastodon.v1.Notification[] = []
|
2022-12-11 22:40:40 +00:00
|
|
|
const processGroup = () => {
|
|
|
|
if (currentGroup.length === 0)
|
|
|
|
return
|
|
|
|
|
|
|
|
const group = currentGroup
|
|
|
|
currentGroup = []
|
2022-11-30 00:47:54 +00:00
|
|
|
|
2022-12-11 22:40:40 +00: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-13 23:06:53 +00:00
|
|
|
if (group[0].type === 'follow') {
|
2023-01-23 20:49:00 +00:00
|
|
|
// Order group by followers count
|
|
|
|
const processedGroup = [...group]
|
|
|
|
processedGroup.sort((a, b) => {
|
|
|
|
const aHasHeader = hasHeader(a.account)
|
|
|
|
const bHasHeader = hasHeader(b.account)
|
|
|
|
if (bHasHeader && !aHasHeader)
|
|
|
|
return 1
|
|
|
|
if (aHasHeader && !bHasHeader)
|
|
|
|
return -1
|
|
|
|
return b.account.followersCount - a.account.followersCount
|
|
|
|
})
|
2023-01-09 12:23:15 +00:00
|
|
|
|
2023-01-23 20:49:00 +00:00
|
|
|
if (processedGroup.length > 0 && hasHeader(processedGroup[0].account))
|
|
|
|
results.push(processedGroup.shift()!)
|
2023-01-09 12:23:15 +00:00
|
|
|
|
2023-01-23 20:49:00 +00:00
|
|
|
if (processedGroup.length === 1 && hasHeader(processedGroup[0].account))
|
|
|
|
results.push(processedGroup.shift()!)
|
2023-01-09 12:23:15 +00:00
|
|
|
|
2023-01-23 20:49:00 +00:00
|
|
|
if (processedGroup.length > 0) {
|
|
|
|
results.push({
|
|
|
|
id: `grouped-${id++}`,
|
|
|
|
type: 'grouped-follow',
|
|
|
|
items: processedGroup,
|
|
|
|
})
|
|
|
|
}
|
2022-12-11 22:40:40 +00:00
|
|
|
return
|
2022-11-30 00:47:54 +00:00
|
|
|
}
|
2023-04-21 20:35:39 +01:00
|
|
|
else if (group.length && (group[0].type === 'reblog' || group[0].type === 'favourite')) {
|
|
|
|
if (!group[0].status) {
|
|
|
|
// Ignore favourite or reblog if status is null, sometimes the API is sending these
|
|
|
|
// notifications
|
|
|
|
return
|
|
|
|
}
|
2022-12-11 22:40:40 +00:00
|
|
|
// 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
|
|
|
|
}
|
2023-03-19 12:12:20 +00:00
|
|
|
likes.sort((a, b) => a.reblog
|
|
|
|
? (!b.reblog || (a.favourite && !b.favourite))
|
|
|
|
? -1
|
|
|
|
: 0
|
|
|
|
: 0)
|
2022-11-30 00:47:54 +00:00
|
|
|
results.push({
|
|
|
|
id: `grouped-${id++}`,
|
2022-12-11 22:40:40 +00:00
|
|
|
type: 'grouped-reblogs-and-favourites',
|
2023-03-19 20:55:19 +00:00
|
|
|
status: group[0].status,
|
2022-12-11 22:40:40 +00:00
|
|
|
likes,
|
2022-11-30 00:47:54 +00:00
|
|
|
})
|
2022-12-11 22:40:40 +00:00
|
|
|
return
|
2022-11-30 00:47:54 +00:00
|
|
|
}
|
2022-12-11 22:40:40 +00:00
|
|
|
|
|
|
|
results.push(...group)
|
2022-11-30 00:47:54 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 21:33:02 +01:00
|
|
|
for (const item of items.filter(includeNotificationsForStatusCard)) {
|
2022-12-11 22:40:40 +00:00
|
|
|
const itemId = groupId(item)
|
2023-10-09 21:33:02 +01:00
|
|
|
// Finalize the group if it already has too many notifications
|
2022-12-11 22:40:40 +00:00
|
|
|
if (currentGroupId !== itemId || currentGroup.length >= groupCapacity)
|
|
|
|
processGroup()
|
2022-11-30 00:47:54 +00:00
|
|
|
|
2022-12-11 22:40:40 +00:00
|
|
|
currentGroup.push(item)
|
|
|
|
currentGroupId = itemId
|
|
|
|
}
|
|
|
|
// Finalize remaining groups
|
|
|
|
processGroup()
|
2022-11-30 00:47:54 +00:00
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2022-12-02 02:21:10 +00:00
|
|
|
|
2023-01-23 19:33:21 +00:00
|
|
|
function removeFiltered(items: mastodon.v1.Notification[]): mastodon.v1.Notification[] {
|
|
|
|
return items.filter(item => !item.status?.filtered?.find(
|
|
|
|
filter => filter.filter.filterAction === 'hide' && filter.filter.context.includes('notifications'),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:39:59 +00: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
|
|
|
|
for (const like of group.likes) {
|
|
|
|
if (like.reblog)
|
|
|
|
flattenedNotifications.push(like.reblog)
|
|
|
|
if (like.favourite)
|
|
|
|
flattenedNotifications.push(like.favourite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (item.type === 'grouped-follow') {
|
|
|
|
flattenedNotifications.push(...item.items)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
flattenedNotifications.push(item)
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 19:33:21 +00:00
|
|
|
return groupItems(removeFiltered(flattenedNotifications))
|
2023-01-09 15:39:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 02:21:10 +00:00
|
|
|
const { clearNotifications } = useNotifications()
|
2023-01-01 14:29:11 +00:00
|
|
|
const { formatNumber } = useHumanReadableNumber()
|
2022-11-15 21:21:54 +00:00
|
|
|
</script>
|
|
|
|
|
2023-05-29 08:16:34 +01:00
|
|
|
<!-- eslint-disable vue/attribute-hyphenation -->
|
2022-11-15 21:21:54 +00:00
|
|
|
<template>
|
2023-01-11 09:59:47 +00:00
|
|
|
<CommonPaginator
|
|
|
|
:paginator="paginator"
|
|
|
|
:preprocess="preprocess"
|
|
|
|
:stream="stream"
|
2023-05-29 08:16:34 +01:00
|
|
|
:virtualScroller="virtualScroller"
|
|
|
|
eventType="notification"
|
2023-01-11 09:59:47 +00:00
|
|
|
>
|
2022-12-02 02:21:10 +00: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 14:29:11 +00:00
|
|
|
{{ $t('timeline.show_new_items', number, { named: { v: formatNumber(number) } }) }}
|
2022-12-02 02:21:10 +00:00
|
|
|
</button>
|
|
|
|
</template>
|
2023-01-11 09:59:47 +00:00
|
|
|
<template #default="{ item, active }">
|
|
|
|
<template v-if="virtualScroller">
|
|
|
|
<DynamicScrollerItem :item="item" :active="active" tag="div">
|
|
|
|
<NotificationGroupedFollow
|
|
|
|
v-if="item.type === 'grouped-follow'"
|
|
|
|
:items="item"
|
|
|
|
border="b base"
|
|
|
|
/>
|
|
|
|
<NotificationGroupedLikes
|
|
|
|
v-else-if="item.type === 'grouped-reblogs-and-favourites'"
|
|
|
|
:group="item"
|
|
|
|
border="b base"
|
|
|
|
/>
|
|
|
|
<NotificationCard
|
|
|
|
v-else
|
|
|
|
:notification="item"
|
|
|
|
hover:bg-active
|
|
|
|
border="b base"
|
|
|
|
/>
|
|
|
|
</DynamicScrollerItem>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
2022-11-30 00:47:54 +00:00
|
|
|
<NotificationGroupedFollow
|
|
|
|
v-if="item.type === 'grouped-follow'"
|
|
|
|
:items="item"
|
2022-12-06 11:07:17 +00:00
|
|
|
border="b base"
|
2022-11-30 00:47:54 +00:00
|
|
|
/>
|
2022-12-11 22:40:40 +00:00
|
|
|
<NotificationGroupedLikes
|
|
|
|
v-else-if="item.type === 'grouped-reblogs-and-favourites'"
|
2023-01-09 12:23:15 +00:00
|
|
|
:group="item"
|
2022-12-11 22:40:40 +00:00
|
|
|
border="b base"
|
|
|
|
/>
|
2022-11-30 00:47:54 +00:00
|
|
|
<NotificationCard
|
|
|
|
v-else
|
2023-01-09 12:23:15 +00:00
|
|
|
:notification="item"
|
2022-11-30 00:47:54 +00:00
|
|
|
hover:bg-active
|
2022-12-06 11:07:17 +00:00
|
|
|
border="b base"
|
2022-11-30 00:47:54 +00:00
|
|
|
/>
|
|
|
|
</template>
|
2022-11-16 16:11:08 +00:00
|
|
|
</template>
|
|
|
|
</CommonPaginator>
|
2022-11-15 21:21:54 +00:00
|
|
|
</template>
|