forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {AppBskyEmbedRecord, 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'
6import {
7 postUriToRelativePath,
8 toBskyAppUrl,
9 toShortUrl,
10} from '#/lib/strings/url-helpers'
11
12export type UserMessageInfo = {
13 message: string | null
14 sentAt: string
15 reportableMessage?: ChatBskyConvoDefs.MessageView
16}
17
18export function getMessageInfo({
19 convo,
20 currentAccountDid,
21 i18n,
22}: {
23 convo: ChatBskyConvoDefs.ConvoView
24 currentAccountDid: string | undefined
25 i18n: I18n
26}): UserMessageInfo | null {
27 if (!ChatBskyConvoDefs.isMessageView(convo.lastMessage)) {
28 return null
29 }
30
31 const lastMessage = convo.lastMessage
32 const isFromMe = lastMessage.sender?.did === currentAccountDid
33 const senderDid = lastMessage.sender?.did
34 const sender = convo.members.find(m => m.did === senderDid)
35 const name = sender ? createSanitizedDisplayName(sender) : null
36 const isGroup = ChatBskyConvoDefs.isGroupConvo(convo.kind)
37
38 const reportableMessage = isFromMe ? undefined : lastMessage
39
40 const prefix = (message: string) => {
41 if (isFromMe) {
42 return i18n._(
43 msg({
44 message: `You: ${message}`,
45 comment: 'When the last message in a chat was made by you.',
46 }),
47 )
48 } else if (isGroup && name) {
49 return i18n._(
50 msg({
51 message: `${name}: ${message}`,
52 comment:
53 'When the last message in a group chat came from someone other than you.',
54 }),
55 )
56 }
57 return message
58 }
59
60 let message: string | null = null
61
62 if (lastMessage.text) {
63 message = prefix(lastMessage.text)
64 } else if (lastMessage.embed) {
65 const defaultEmbeddedContentMessage = i18n._(
66 msg`(contains embedded content)`,
67 )
68
69 if (AppBskyEmbedRecord.isView(lastMessage.embed)) {
70 const embed = lastMessage.embed
71
72 if (AppBskyEmbedRecord.isViewRecord(embed.record)) {
73 const record = embed.record
74 const path = postUriToRelativePath(record.uri, {
75 handle: record.author.handle,
76 })
77 const href = path ? toBskyAppUrl(path) : undefined
78 const short = href ? toShortUrl(href) : defaultEmbeddedContentMessage
79 message = prefix(short)
80 } else {
81 message = prefix(defaultEmbeddedContentMessage)
82 }
83 } else {
84 message = prefix(defaultEmbeddedContentMessage)
85 }
86 }
87
88 return {
89 message,
90 sentAt: lastMessage.sentAt,
91 reportableMessage,
92 }
93}