mirror of
https://github.com/sky-systems/sky_phone.git
synced 2026-09-04 17:23:25 +00:00
ENH - prioritize recent SMS contacts
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import type { SmsConversation } from '@/types/messages'
|
||||
import {
|
||||
sortContactsByMessageRecency,
|
||||
sortConversationsByRecency,
|
||||
} from '@/utils/messages'
|
||||
|
||||
function conversation(
|
||||
phoneNumber: string,
|
||||
lastMessageAt: string,
|
||||
): SmsConversation {
|
||||
return {
|
||||
lastMessage: phoneNumber,
|
||||
lastMessageAt,
|
||||
lastMessageType: 'text',
|
||||
phoneNumber,
|
||||
unread: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const conversations = [
|
||||
conversation('5550000001', '2026-08-12 10:00:00'),
|
||||
conversation('5550000002', '2026-08-12 12:00:00'),
|
||||
]
|
||||
|
||||
describe('SMS recency ordering', () => {
|
||||
it('places the conversation with the newest message first', () => {
|
||||
expect(
|
||||
sortConversationsByRecency(conversations).map(
|
||||
(item) => item.phoneNumber,
|
||||
),
|
||||
).toEqual(['5550000002', '5550000001'])
|
||||
})
|
||||
|
||||
it('places recently messaged contacts before unused contacts', () => {
|
||||
const contacts = [
|
||||
{ name: 'Unused', phone_number: '5550000003' },
|
||||
{ name: 'Older', phone_number: '5550000001' },
|
||||
{ name: 'Newest', phone_number: '5550000002' },
|
||||
]
|
||||
|
||||
expect(
|
||||
sortContactsByMessageRecency(contacts, conversations).map(
|
||||
(item) => item.name,
|
||||
),
|
||||
).toEqual(['Newest', 'Older', 'Unused'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { SmsConversation } from '@/types/messages'
|
||||
import { parseDatabaseDate } from '@/utils/date'
|
||||
|
||||
function conversationTime(conversation: SmsConversation): number {
|
||||
const timestamp = parseDatabaseDate(conversation.lastMessageAt).getTime()
|
||||
return Number.isNaN(timestamp) ? 0 : timestamp
|
||||
}
|
||||
|
||||
export function sortConversationsByRecency(
|
||||
conversations: SmsConversation[],
|
||||
): SmsConversation[] {
|
||||
return conversations
|
||||
.map((conversation, index) => ({ conversation, index }))
|
||||
.sort(
|
||||
(left, right) =>
|
||||
conversationTime(right.conversation) -
|
||||
conversationTime(left.conversation) || left.index - right.index,
|
||||
)
|
||||
.map(({ conversation }) => conversation)
|
||||
}
|
||||
|
||||
export function sortContactsByMessageRecency<
|
||||
T extends { phone_number: string },
|
||||
>(contacts: T[], conversations: SmsConversation[]): T[] {
|
||||
const activity = new Map(
|
||||
conversations.map((conversation) => [
|
||||
conversation.phoneNumber,
|
||||
conversationTime(conversation),
|
||||
]),
|
||||
)
|
||||
return contacts
|
||||
.map((contact, index) => ({ contact, index }))
|
||||
.sort((left, right) => {
|
||||
const leftActivity = activity.get(left.contact.phone_number)
|
||||
const rightActivity = activity.get(right.contact.phone_number)
|
||||
if (leftActivity === undefined && rightActivity === undefined)
|
||||
return left.index - right.index
|
||||
if (leftActivity === undefined) return 1
|
||||
if (rightActivity === undefined) return -1
|
||||
return rightActivity - leftActivity || left.index - right.index
|
||||
})
|
||||
.map(({ contact }) => contact)
|
||||
}
|
||||
Reference in New Issue
Block a user