feat: improve follows you grouping (#1408)

zio/stable
patak 2023-01-23 21:49:00 +01:00 committed by GitHub
parent dc23784c00
commit 66c9212a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 23 deletions

View File

@ -27,6 +27,10 @@ const groupId = (item: mastodon.v1.Notification): string => {
return JSON.stringify(id)
}
function hasHeader(account: mastodon.v1.Account) {
return !account.header.endsWith('/original/missing.png')
}
function groupItems(items: mastodon.v1.Notification[]): NotificationSlot[] {
const results: NotificationSlot[] = []
@ -44,31 +48,31 @@ function groupItems(items: mastodon.v1.Notification[]): NotificationSlot[] {
// This normally happens when you transfer an account, if not, show
// a big profile card for each follow
if (group[0].type === 'follow') {
let groups: mastodon.v1.Notification[] = []
// 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
})
function newGroup() {
if (groups.length > 0) {
results.push({
id: `grouped-${id++}`,
type: 'grouped-follow',
items: groups,
})
groups = []
}
if (processedGroup.length > 0 && hasHeader(processedGroup[0].account))
results.push(processedGroup.shift()!)
if (processedGroup.length === 1 && hasHeader(processedGroup[0].account))
results.push(processedGroup.shift()!)
if (processedGroup.length > 0) {
results.push({
id: `grouped-${id++}`,
type: 'grouped-follow',
items: processedGroup,
})
}
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))) {
newGroup()
results.push(item)
}
else {
groups.push(item)
}
}
newGroup()
return
}