wip emoji reactions

This commit is contained in:
john 2025-05-26 22:49:15 +02:00
parent 48ea06294e
commit 72389136a7
2 changed files with 98 additions and 0 deletions

View file

@ -1,12 +1,18 @@
import { Temporal } from '@js-temporal/polyfill'
import { components } from '../../api/schema.ts'
export interface EmojiReaction {
emoji: string
count: number
}
export class Post {
public readonly postId: string
public readonly content: string
public readonly media: PostMedia[]
public readonly createdAt: Temporal.Instant
public readonly authorName: string
public readonly reactions: EmojiReaction[]
constructor(
postId: string,
@ -14,12 +20,51 @@ export class Post {
media: PostMedia[],
createdAt: string | Temporal.Instant,
authorName: string,
reactions: EmojiReaction[] = [],
) {
this.postId = postId
this.content = content
this.media = media
this.createdAt = Temporal.Instant.from(createdAt)
this.authorName = authorName
this.reactions = reactions.length > 0 ? reactions : this.generateRandomReactions()
}
private generateRandomReactions(): EmojiReaction[] {
// List of popular emojis
const emojis = [
'👍',
'❤️',
'😂',
'🎉',
'🔥',
'👏',
'🙏',
'💯',
'🤔',
'😍',
'🥰',
'😮',
'😢',
'😡',
'🤩',
]
// Randomly select 5 unique emojis
const selectedEmojis: string[] = []
while (selectedEmojis.length < 5) {
const randomIndex = Math.floor(Math.random() * emojis.length)
const emoji = emojis[randomIndex]
if (!selectedEmojis.includes(emoji!)) {
selectedEmojis.push(emoji!)
}
}
// Create reaction objects with random counts
return selectedEmojis.map((emoji) => ({
emoji,
count: Math.floor(Math.random() * 50), // Random count between 0 and 49
}))
}
public static fromDto(dto: components['schemas']['PostDto']): Post {