use reactions from post

This commit is contained in:
john 2025-05-28 19:56:30 +02:00
parent 72389136a7
commit 83835d374b
3 changed files with 80 additions and 70 deletions

View file

@ -4,6 +4,7 @@ import { components } from '../../api/schema.ts'
export interface EmojiReaction {
emoji: string
count: number
didReact: boolean
}
export class Post {
@ -13,6 +14,7 @@ export class Post {
public readonly createdAt: Temporal.Instant
public readonly authorName: string
public readonly reactions: EmojiReaction[]
public readonly possibleReactions: string[]
constructor(
postId: string,
@ -21,50 +23,15 @@ export class Post {
createdAt: string | Temporal.Instant,
authorName: string,
reactions: EmojiReaction[] = [],
possibleReactions: string[] = [],
) {
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
}))
this.reactions = reactions
this.possibleReactions = possibleReactions
}
public static fromDto(dto: components['schemas']['PostDto']): Post {
@ -74,6 +41,12 @@ export class Post {
dto.media.map((m) => new PostMediaImpl(new URL(m.url), m.width, m.height)),
Temporal.Instant.from(dto.createdAt),
dto.author.username,
dto.reactions.map((r) => ({
emoji: r.emoji,
count: r.count,
didReact: r.didReact
})),
dto.possibleReactions
)
}
}