forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type ChatBskyConvoDefs} from '@atproto/api'
2
3import {EMOJI_REACTION_LIMIT} from '#/lib/constants'
4import type * as bsky from '#/types/bsky'
5
6export function canBeMessaged(profile: bsky.profile.AnyProfileView) {
7 switch (profile.associated?.chat?.allowIncoming) {
8 case 'none':
9 return false
10 case 'all':
11 return true
12 // if unset, treat as following
13 case 'following':
14 case undefined:
15 return Boolean(profile.viewer?.followedBy)
16 // any other values are invalid according to the lexicon, so
17 // let's treat as false to be safe
18 default:
19 return false
20 }
21}
22
23export function localDateString(date: Date) {
24 // can't use toISOString because it should be in local time
25 const mm = date.getMonth()
26 const dd = date.getDate()
27 const yyyy = date.getFullYear()
28 // not padding with 0s because it's not necessary, it's just used for comparison
29 return `${yyyy}-${mm}-${dd}`
30}
31
32export function hasAlreadyReacted(
33 message: ChatBskyConvoDefs.MessageView,
34 myDid: string | undefined,
35 emoji: string,
36): boolean {
37 if (!message.reactions) {
38 return false
39 }
40 return !!message.reactions.find(
41 reaction => reaction.value === emoji && reaction.sender.did === myDid,
42 )
43}
44
45export function hasReachedReactionLimit(
46 message: ChatBskyConvoDefs.MessageView,
47 myDid: string | undefined,
48): boolean {
49 if (!message.reactions) {
50 return false
51 }
52 const myReactions = message.reactions.filter(
53 reaction => reaction.sender.did === myDid,
54 )
55 return myReactions.length >= EMOJI_REACTION_LIMIT
56}