-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement getSubscriptionFeatures endpoint
- Loading branch information
1 parent
d5d9667
commit 0b75704
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/bsky/src/api/app/bsky/subscription/getSubscriptionFeatures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Server } from '../../../../lexicon' | ||
import AppContext from '../../../../context' | ||
import { Features } from '../../../../lexicon/types/app/bsky/subscription/getSubscriptionFeatures' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
server.app.bsky.subscription.getSubscriptionFeatures({ | ||
auth: ctx.authVerifier.standard, | ||
handler: async ({ auth }) => { | ||
const viewer = auth.credentials.iss | ||
|
||
const features = await getFeaturesForViewerSubscription(viewer, ctx) | ||
|
||
return { | ||
encoding: 'application/json', | ||
body: { | ||
features, | ||
}, | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
const getFeaturesForViewerSubscription = async ( | ||
viewerDid: string, | ||
ctx: AppContext, | ||
) => { | ||
const { subscriptionEntitlements } = | ||
await ctx.dataplane.getSubscriptionEntitlement({ dids: [viewerDid] }) | ||
|
||
const gatedFeatures: Features = { | ||
customProfileColor: false, | ||
} | ||
|
||
if (subscriptionEntitlements?.length === 0) { | ||
return gatedFeatures | ||
} | ||
|
||
const { entitlements } = subscriptionEntitlements[0] | ||
|
||
return entitlements.reduce((acc, entitlement) => { | ||
if (!entitlementFeatures[entitlement]) { | ||
return acc | ||
} | ||
return { | ||
...acc, | ||
...entitlementFeatures[entitlement], | ||
} | ||
}, gatedFeatures) | ||
} | ||
|
||
type EntitlementFeatures = { | ||
[entitlementIdentifier: string]: { | ||
[k: keyof Features]: true | ||
} | ||
} | ||
|
||
const entitlementFeatures: EntitlementFeatures = { | ||
core: { | ||
customProfileColor: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { AtUri, AtpAgent } from '@atproto/api' | ||
import { TestNetwork, SeedClient, basicSeed } from '@atproto/dev-env' | ||
import { ids } from '../../src/lexicon/lexicons' | ||
|
||
describe('subscriptions views', () => { | ||
let network: TestNetwork | ||
let agent: AtpAgent | ||
let pdsAgent: AtpAgent | ||
let sc: SeedClient | ||
|
||
// account dids, for convenience | ||
let alice: string | ||
let bob: string | ||
|
||
beforeAll(async () => { | ||
network = await TestNetwork.create({ | ||
dbPostgresSchema: 'bsky_views_subscriptions', | ||
}) | ||
agent = network.bsky.getClient() | ||
pdsAgent = network.pds.getClient() | ||
sc = network.getSeedClient() | ||
await basicSeed(sc) | ||
await network.processAll() | ||
alice = sc.dids.alice | ||
bob = sc.dids.bob | ||
}) | ||
|
||
afterAll(async () => { | ||
await network.close() | ||
}) | ||
|
||
it('returns features for user with subscription', async () => { | ||
await network.bsky.db.db | ||
.insertInto('subscription_entitlement') | ||
.values({ did: alice, entitlements: JSON.stringify(['core']) }) | ||
.execute() | ||
|
||
const { | ||
data: { features }, | ||
} = await agent.app.bsky.subscription.getSubscriptionFeatures( | ||
{}, | ||
{ | ||
headers: await network.serviceHeaders( | ||
alice, | ||
ids.AppBskySubscriptionGetSubscriptionFeatures, | ||
), | ||
}, | ||
) | ||
|
||
expect(features).toStrictEqual({ customProfileColor: true }) | ||
}) | ||
|
||
it('returns features for user without subscription', async () => { | ||
const { | ||
data: { features }, | ||
} = await agent.app.bsky.subscription.getSubscriptionFeatures( | ||
{}, | ||
{ | ||
headers: await network.serviceHeaders( | ||
bob, | ||
ids.AppBskySubscriptionGetSubscriptionFeatures, | ||
), | ||
}, | ||
) | ||
|
||
expect(features).toStrictEqual({ customProfileColor: false }) | ||
}) | ||
}) |