Skip to content

Instantly share code, notes, and snippets.

@shenders13
Last active September 20, 2023 23:48
Show Gist options
  • Save shenders13/4ecde74a16cfacd2f65f7d27a565e042 to your computer and use it in GitHub Desktop.
Save shenders13/4ecde74a16cfacd2f65f7d27a565e042 to your computer and use it in GitHub Desktop.
Backfill to set EmojiAccess.travelingEnabled for existing data
.mod backfills
.mod billing
.mod emojis
const { z } = await import('zod')
function run() {
const EmojiAccessParser = z.object({
accessCategory: z.union([
z.literal('followersOnly'),
z.literal('tier-1'),
z.literal('tier-2'),
z.literal('tier-3'),
]),
actionText: z.string(),
archivedAt: z.date().nullable(),
channelID: z.string(),
filePath: z.string(),
id: z.string(),
storageEngine: z.literal('r2'),
borrowerID: z.string(),
lenderID: z.string(),
travelingEnabled: z.boolean().default(false),
})
function isTierGreaterOrEqual({
benchMarkTier,
testTier,
}) {
const tiers = ['tier-1', 'tier-2', 'tier-3'];
return tiers.indexOf(testTier) >= tiers.indexOf(benchMarkTier);
}
backfills.runBackfill({
filter: backfills.fb.beginsWith('PK', 'EmojiAccess#'),
parser: EmojiAccessParser,
}, async emojiAccess => {
// Define who is the lender and who is the borrower.
const lenderID = emojiAccess.lenderID
const borrowerID = emojiAccess.borrowerID
const isOwner = lenderID === borrowerID
// Determine if the borrower is subscribed to the lender.
const borrowerSubscription =
(
await billing.api.findChannelSubscriptions([
{
subscriberID: borrowerID,
subscribedToID: lenderID,
},
])
).get(`${lenderID}:${borrowerID}`);
const isBorrowerSubscribed = borrowerSubscription?.state === 'active';
// Determine what type of emoji this is.
const isFollowerEmoji = emojiAccess.accessCategory === 'followersOnly'
const isTierEmoji = !isFollowerEmoji
// Build the input for the update.
const intialTravelingEnabled = emojiAccess.travelingEnabled
let input = { ...emojiAccess }
// Determine if traveling should be enabled.
if (isOwner) {
// 1. Streamer owns the emoji. Enabling traveling.
input.travelingEnabled = true
} else if (isFollowerEmoji && isBorrowerSubscribed) {
// 2. Follower emoji and the borrower is subscribed. Enabling traveling.
input.travelingEnabled = true
} else if (isFollowerEmoji && !isBorrowerSubscribed) {
// 3. Follower emoji and the borrower is not subscribed. Disabling traveling.
input.travelingEnabled = false
} else if (isTierEmoji && isBorrowerSubscribed && isTierGreaterOrEqual({
benchMarkTier: emojiAccess.accessCategory,
testTier: borrowerSubscription.tier,
})) {
// 4. Tier emoji and borrower is subscribed at the right level. Enabling traveling.
input.travelingEnabled = true
} else {
// 5. Tier emoji and borrower is not subscribed at the right level. Disabling traveling.
input.travelingEnabled = false
}
const combined = {
lenderID,
borrowerID,
id: input.id
}
if (input.travelingEnabled === intialTravelingEnabled) {
console.log('No update for: ', combined)
return
}
console.log('Updating travelingEnabled for', combined, ' from ', intialTravelingEnabled,' to ', input.travelingEnabled)
await emojis.api.upsertEmojiAccess({
emojiAccess: input,
});
});
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment