forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {ChatBskyConvoDefs} from '@atproto/api'
2import {type I18n} from '@lingui/core'
3import {msg} from '@lingui/core/macro'
4
5import {createSanitizedDisplayName} from '#/lib/moderation/create-sanitized-display-name'
6
7export type UserReactionInfo = {
8 message: string
9 createdAt: string
10}
11
12export function getReactionInfo({
13 convo,
14 currentAccountDid,
15 i18n,
16}: {
17 convo: ChatBskyConvoDefs.ConvoView
18 currentAccountDid: string | undefined
19 i18n: I18n
20}): UserReactionInfo | null {
21 if (!ChatBskyConvoDefs.isMessageAndReactionView(convo.lastReaction)) {
22 return null
23 }
24
25 const {reaction, message: reactedTo} = convo.lastReaction
26 const isFromMe = reaction.sender.did === currentAccountDid
27 const senderDid = reaction.sender.did
28 const sender = convo.members.find(m => m.did === senderDid)
29 const name = sender ? createSanitizedDisplayName(sender) : null
30
31 const lastMessageText = reactedTo.text
32 const fallbackMessage = i18n._(
33 msg({
34 message: 'a message',
35 comment:
36 'If last message does not contain text, fall back to "{user} reacted to {a message}"',
37 }),
38 )
39 const target = lastMessageText ? `"${lastMessageText}"` : fallbackMessage
40
41 let message: string
42 if (isFromMe) {
43 message = i18n._(msg`You reacted ${reaction.value} to ${target}`)
44 } else if (name) {
45 message = i18n._(msg`${name} reacted ${reaction.value} to ${target}`)
46 } else {
47 message = i18n._(msg`Someone reacted ${reaction.value} to ${target}`)
48 }
49
50 return {
51 message,
52 createdAt: reaction.createdAt,
53 }
54}