mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api'
2
3export type ThreadgateSetting =
4 | {type: 'nobody'}
5 | {type: 'mention'}
6 | {type: 'following'}
7 | {type: 'list'; list: unknown}
8
9export function threadgateViewToSettings(
10 threadgate: AppBskyFeedDefs.ThreadgateView | undefined,
11): ThreadgateSetting[] {
12 const record =
13 threadgate &&
14 AppBskyFeedThreadgate.isRecord(threadgate.record) &&
15 AppBskyFeedThreadgate.validateRecord(threadgate.record).success
16 ? threadgate.record
17 : null
18 if (!record) {
19 return []
20 }
21 if (!record.allow?.length) {
22 return [{type: 'nobody'}]
23 }
24 const settings: ThreadgateSetting[] = record.allow
25 .map(allow => {
26 let setting: ThreadgateSetting | undefined
27 if (allow.$type === 'app.bsky.feed.threadgate#mentionRule') {
28 setting = {type: 'mention'}
29 } else if (allow.$type === 'app.bsky.feed.threadgate#followingRule') {
30 setting = {type: 'following'}
31 } else if (allow.$type === 'app.bsky.feed.threadgate#listRule') {
32 setting = {type: 'list', list: allow.list}
33 }
34 return setting
35 })
36 .filter(n => !!n)
37 return settings
38}