diff --git a/common/notification.ts b/common/notification.ts index 47c55cc6..affa33cb 100644 --- a/common/notification.ts +++ b/common/notification.ts @@ -138,7 +138,7 @@ export const getDestinationsForUser = async ( privateUser: PrivateUser, reason: notification_reason_types | keyof notification_subscription_types ) => { - const notificationSettings = privateUser.notificationSubscriptionTypes + const notificationSettings = privateUser.notificationPreferences let destinations let subscriptionType: keyof notification_subscription_types | undefined if (Object.keys(notificationSettings).includes(reason)) { @@ -151,9 +151,11 @@ export const getDestinationsForUser = async ( ? notificationSettings[subscriptionType] : [] } + // const unsubscribeEndpoint = getFunctionUrl('unsubscribe') return { sendToEmail: destinations.includes('email'), sendToBrowser: destinations.includes('browser'), + // unsubscribeUrl: `${unsubscribeEndpoint}?id=${privateUser.id}&type=${subscriptionType}`, urlToManageThisNotification: `${DOMAIN}/notifications?tab=settings§ion=${subscriptionType}`, } } diff --git a/common/user.ts b/common/user.ts index 5d427744..7bd89906 100644 --- a/common/user.ts +++ b/common/user.ts @@ -65,9 +65,7 @@ export type PrivateUser = { initialDeviceToken?: string initialIpAddress?: string apiKey?: string - /** @deprecated - use notificationSubscriptionTypes */ - notificationPreferences?: notification_subscribe_types - notificationSubscriptionTypes: notification_subscription_types + notificationPreferences: notification_subscription_types twitchInfo?: { twitchName: string controlToken: string @@ -142,9 +140,6 @@ export const getDefaultNotificationSettings = ( privateUser?: PrivateUser, noEmails?: boolean ) => { - const prevPref = privateUser?.notificationPreferences ?? 'all' - const wantsLess = prevPref === 'less' - const wantsAll = prevPref === 'all' const { unsubscribedFromCommentEmails, unsubscribedFromAnswerEmails, @@ -161,111 +156,96 @@ export const getDefaultNotificationSettings = ( return { // Watched Markets all_comments_on_watched_markets: constructPref( - wantsAll, + true, !unsubscribedFromCommentEmails ), all_answers_on_watched_markets: constructPref( - wantsAll, + true, !unsubscribedFromAnswerEmails ), // Comments - tips_on_your_comments: constructPref( - wantsAll || wantsLess, - !unsubscribedFromCommentEmails - ), - comments_by_followed_users_on_watched_markets: constructPref( - wantsAll, - false - ), + tips_on_your_comments: constructPref(true, !unsubscribedFromCommentEmails), + comments_by_followed_users_on_watched_markets: constructPref(true, false), all_replies_to_my_comments_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromCommentEmails ), all_replies_to_my_answers_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromCommentEmails ), all_comments_on_contracts_with_shares_in_on_watched_markets: constructPref( - wantsAll, + true, !unsubscribedFromCommentEmails ), // Answers answers_by_followed_users_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromAnswerEmails ), answers_by_market_creator_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromAnswerEmails ), all_answers_on_contracts_with_shares_in_on_watched_markets: constructPref( - wantsAll, + true, !unsubscribedFromAnswerEmails ), // On users' markets your_contract_closed: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromResolutionEmails ), // High priority all_comments_on_my_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromCommentEmails ), all_answers_on_my_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromAnswerEmails ), - subsidized_your_market: constructPref(wantsAll || wantsLess, true), + subsidized_your_market: constructPref(true, true), // Market updates resolutions_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromResolutionEmails ), - market_updates_on_watched_markets: constructPref( - wantsAll || wantsLess, - false - ), + market_updates_on_watched_markets: constructPref(true, false), market_updates_on_watched_markets_with_shares_in: constructPref( - wantsAll || wantsLess, + true, false ), resolutions_on_watched_markets_with_shares_in: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromResolutionEmails ), //Balance Changes - loan_income: constructPref(wantsAll || wantsLess, false), - betting_streaks: constructPref(wantsAll || wantsLess, false), - referral_bonuses: constructPref(wantsAll || wantsLess, true), - unique_bettors_on_your_contract: constructPref( - wantsAll || wantsLess, - false - ), + loan_income: constructPref(true, false), + betting_streaks: constructPref(true, false), + referral_bonuses: constructPref(true, true), + unique_bettors_on_your_contract: constructPref(true, false), tipped_comments_on_watched_markets: constructPref( - wantsAll || wantsLess, + true, !unsubscribedFromCommentEmails ), - tips_on_your_markets: constructPref(wantsAll || wantsLess, true), - limit_order_fills: constructPref(wantsAll || wantsLess, false), + tips_on_your_markets: constructPref(true, true), + limit_order_fills: constructPref(true, false), // General - tagged_user: constructPref(wantsAll || wantsLess, true), - on_new_follow: constructPref(wantsAll || wantsLess, true), - contract_from_followed_user: constructPref(wantsAll || wantsLess, true), + tagged_user: constructPref(true, true), + on_new_follow: constructPref(true, true), + contract_from_followed_user: constructPref(true, true), trending_markets: constructPref( false, !unsubscribedFromWeeklyTrendingEmails ), profit_loss_updates: constructPref(false, true), - probability_updates_on_watched_markets: constructPref( - wantsAll || wantsLess, - false - ), + probability_updates_on_watched_markets: constructPref(true, false), thank_you_for_purchases: constructPref( false, !unsubscribedFromGenericEmails diff --git a/firestore.rules b/firestore.rules index 82392787..6f2ea90a 100644 --- a/firestore.rules +++ b/firestore.rules @@ -77,7 +77,7 @@ service cloud.firestore { allow read: if userId == request.auth.uid || isAdmin(); allow update: if (userId == request.auth.uid || isAdmin()) && request.resource.data.diff(resource.data).affectedKeys() - .hasOnly(['apiKey', 'unsubscribedFromResolutionEmails', 'unsubscribedFromCommentEmails', 'unsubscribedFromAnswerEmails', 'notificationPreferences', 'unsubscribedFromWeeklyTrendingEmails', 'notificationSubscriptionTypes', 'twitchInfo']); + .hasOnly(['apiKey', 'unsubscribedFromResolutionEmails', 'unsubscribedFromCommentEmails', 'unsubscribedFromAnswerEmails', 'unsubscribedFromWeeklyTrendingEmails', 'notificationPreferences', 'twitchInfo']); } match /private-users/{userId}/views/{viewId} { diff --git a/functions/src/create-user.ts b/functions/src/create-user.ts index 71272222..ab5f014a 100644 --- a/functions/src/create-user.ts +++ b/functions/src/create-user.ts @@ -83,7 +83,7 @@ export const createuser = newEndpoint(opts, async (req, auth) => { email, initialIpAddress: req.ip, initialDeviceToken: deviceToken, - notificationSubscriptionTypes: getDefaultNotificationSettings(auth.uid), + notificationPreferences: getDefaultNotificationSettings(auth.uid), } await firestore.collection('private-users').doc(auth.uid).create(privateUser) diff --git a/functions/src/emails.ts b/functions/src/emails.ts index e9ef9630..bb9f7195 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -214,7 +214,7 @@ export const sendOneWeekBonusEmail = async ( if ( !privateUser || !privateUser.email || - !privateUser.notificationSubscriptionTypes.onboarding_flow.includes('email') + !privateUser.notificationPreferences.onboarding_flow.includes('email') ) return @@ -247,7 +247,7 @@ export const sendCreatorGuideEmail = async ( if ( !privateUser || !privateUser.email || - !privateUser.notificationSubscriptionTypes.onboarding_flow.includes('email') + !privateUser.notificationPreferences.onboarding_flow.includes('email') ) return @@ -279,7 +279,7 @@ export const sendThankYouEmail = async ( if ( !privateUser || !privateUser.email || - !privateUser.notificationSubscriptionTypes.thank_you_for_purchases.includes( + !privateUser.notificationPreferences.thank_you_for_purchases.includes( 'email' ) ) @@ -460,9 +460,7 @@ export const sendInterestingMarketsEmail = async ( if ( !privateUser || !privateUser.email || - !privateUser.notificationSubscriptionTypes.trending_markets.includes( - 'email' - ) + !privateUser.notificationPreferences.trending_markets.includes('email') ) return diff --git a/functions/src/scripts/create-new-notification-preferences.ts b/functions/src/scripts/create-new-notification-preferences.ts index a6bd1a0b..2796f2f7 100644 --- a/functions/src/scripts/create-new-notification-preferences.ts +++ b/functions/src/scripts/create-new-notification-preferences.ts @@ -17,7 +17,7 @@ async function main() { .collection('private-users') .doc(privateUser.id) .update({ - notificationSubscriptionTypes: getDefaultNotificationSettings( + notificationPreferences: getDefaultNotificationSettings( privateUser.id, privateUser, disableEmails diff --git a/functions/src/scripts/create-private-users.ts b/functions/src/scripts/create-private-users.ts index f9b8c3a1..21e117cf 100644 --- a/functions/src/scripts/create-private-users.ts +++ b/functions/src/scripts/create-private-users.ts @@ -21,7 +21,7 @@ async function main() { id: user.id, email, username, - notificationSubscriptionTypes: getDefaultNotificationSettings(user.id), + notificationPreferences: getDefaultNotificationSettings(user.id), } if (user.totalDeposits === undefined) { diff --git a/functions/src/scripts/update-notification-preferences.ts b/functions/src/scripts/update-notification-preferences.ts new file mode 100644 index 00000000..0e2dc243 --- /dev/null +++ b/functions/src/scripts/update-notification-preferences.ts @@ -0,0 +1,29 @@ +import * as admin from 'firebase-admin' + +import { initAdmin } from './script-init' +import { getPrivateUser } from 'functions/src/utils' +import { filterDefined } from 'common/lib/util/array' +import { FieldValue } from 'firebase-admin/firestore' +initAdmin() + +const firestore = admin.firestore() + +async function main() { + // const privateUsers = await getAllPrivateUsers() + const privateUsers = filterDefined([ + await getPrivateUser('6hHpzvRG0pMq8PNJs7RZj2qlZGn2'), + ]) + await Promise.all( + privateUsers.map((privateUser) => { + if (!privateUser.id) return Promise.resolve() + return firestore.collection('private-users').doc(privateUser.id).update({ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + notificationPreferences: privateUser.notificationSubscriptionTypes, + notificationSubscriptionTypes: FieldValue.delete(), + }) + }) + ) +} + +if (require.main === module) main().then(() => process.exit()) diff --git a/web/components/notification-settings.tsx b/web/components/notification-settings.tsx index 83ebf894..d18896bd 100644 --- a/web/components/notification-settings.tsx +++ b/web/components/notification-settings.tsx @@ -183,8 +183,8 @@ export function NotificationSettings(props: { toast .promise( updatePrivateUser(privateUser.id, { - notificationSubscriptionTypes: { - ...privateUser.notificationSubscriptionTypes, + notificationPreferences: { + ...privateUser.notificationPreferences, [subscriptionTypeKey]: destinations.includes(setting) ? destinations.filter((d) => d !== setting) : uniq([...destinations, setting]), @@ -240,7 +240,7 @@ export function NotificationSettings(props: { const getUsersSavedPreference = ( key: keyof notification_subscription_types ) => { - return privateUser.notificationSubscriptionTypes[key] ?? [] + return privateUser.notificationPreferences[key] ?? [] } const Section = memo(function Section(props: {