Skip to content

Commit

Permalink
feat(core): proxy notification filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Jan 16, 2024
1 parent 73546ba commit 0bb1332
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export BRIA_HOST=${DOCKER_HOST_IP}
export BRIA_PORT=2742
export BRIA_API_KEY="bria_dev_000000000000000000000"

export NOTIFICATIONS_HOST=${DOCKER_HOST_IP}
export NOTIFICATIONS_PORT=6685

export PRICE_HOST=${DOCKER_HOST_IP}
export PRICE_PORT=50051

Expand Down
3 changes: 3 additions & 0 deletions core/api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export BRIA_HOST=${DOCKER_HOST_IP}
export BRIA_PORT=2742
export BRIA_API_KEY="bria_dev_000000000000000000000"

export NOTIFICATIONS_HOST=${DOCKER_HOST_IP}
export NOTIFICATIONS_PORT=6685

export PRICE_HOST=${DOCKER_HOST_IP}
export PRICE_PORT=50051

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const sendDefaultWalletBalanceToAccounts = async () => {
deviceTokens: user.deviceTokens,
displayBalanceAmount: displayAmount,
recipientLanguage: user.language,
notificationSettings: account.notificationSettings,
recipientUserId: user.id,
})

if (result instanceof DeviceTokensNotRegisteredNotificationsServiceError) {
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/app/admin/send-admin-push-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const sendAdminPushNotification = async ({
body,
data,
notificationCategory: checkedNotificationCategory,
notificationSettings: account.notificationSettings,
userId: kratosUserId,
})

return success
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/app/payments/send-intraledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ const executePaymentViaIntraledger = async <
walletId: recipientWalletDescriptor.id,
deviceTokens: recipientUser.deviceTokens,
language: recipientUser.language,
notificationSettings: recipientAccount.notificationSettings,
userId: recipientUser.id,
level: recipientAccount.level,
},
transaction: recipientWalletTransaction,
Expand Down Expand Up @@ -385,7 +385,7 @@ const executePaymentViaIntraledger = async <
walletId: senderWalletDescriptor.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
userId: senderUser.id,
level: senderAccount.level,
},
transaction: senderWalletTransaction,
Expand Down
6 changes: 3 additions & 3 deletions core/api/src/app/payments/send-lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ const executePaymentViaIntraledger = async <
walletId: recipientWalletDescriptor.id,
deviceTokens: recipientUser.deviceTokens,
language: recipientUser.language,
notificationSettings: recipientAccount.notificationSettings,
userId: recipientUser.id,
level: recipientAccount.level,
},
transaction: recipientWalletTransaction,
Expand Down Expand Up @@ -578,7 +578,7 @@ const executePaymentViaIntraledger = async <
walletId: senderWalletDescriptor.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
userId: senderUser.id,
level: senderAccount.level,
},
transaction: senderWalletTransaction,
Expand Down Expand Up @@ -819,7 +819,7 @@ const executePaymentViaLn = async ({
walletId: senderWallet.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
userId: senderUser.id,
level: senderAccount.level,
},
transaction: walletTransaction,
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/app/payments/send-on-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ const executePaymentViaIntraledger = async <
walletId: recipientWallet.id,
deviceTokens: recipientUser.deviceTokens,
language: recipientUser.language,
notificationSettings: recipientAccount.notificationSettings,
userId: recipientUser.id,
level: recipientAccount.level,
},
transaction: recipientWalletTransaction,
Expand Down Expand Up @@ -468,7 +468,7 @@ const executePaymentViaIntraledger = async <
walletId: senderWalletDescriptor.id,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
userId: senderUser.id,
level: senderAccount.level,
},
transaction: senderWalletTransaction,
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/app/payments/update-pending-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const updatePendingPayment = wrapAsyncToRunInSpan({
walletId,
deviceTokens: senderUser.deviceTokens,
language: senderUser.language,
notificationSettings: senderAccount.notificationSettings,
userId: senderUser.id,
level: senderAccount.level,
},
transaction: walletTransaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const addPendingTransaction = async ({
walletId: wallet.id,
deviceTokens: recipientUser.deviceTokens,
language: recipientUser.language,
notificationSettings: account.notificationSettings,
userId: recipientUser.id,
level: account.level,
},
transaction: res,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const addSettledTransactionBeforeFinally = async ({
walletId: wallet.id,
deviceTokens: user.deviceTokens,
language: user.language,
notificationSettings: account.notificationSettings,
userId: user.id,
level: account.level,
},
transaction: walletTransaction,
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/app/wallets/settle-payout-txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const settlePayout = async (
walletId: wallet.id,
deviceTokens: user.deviceTokens,
language: user.language,
notificationSettings: account.notificationSettings,
userId: user.id,
level: account.level,
},
transaction: walletTransaction,
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/app/wallets/update-single-pending-invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ const lockedUpdatePendingInvoiceSteps = async ({
walletId: recipientWalletDescriptor.id,
deviceTokens: recipientUser.deviceTokens,
language: recipientUser.language,
notificationSettings: recipientAccount.notificationSettings,
userId: recipientUser.id,
level: recipientAccount.level,
},
transaction: walletTransaction,
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/domain/notifications/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type NotificationType =
type SendBalanceArgs = {
balanceAmount: BalanceAmount<WalletCurrency>
deviceTokens: DeviceToken[]
notificationSettings: NotificationSettings
recipientUserId: UserId
displayBalanceAmount?: DisplayAmount<DisplayCurrency>
recipientLanguage: UserLanguageOrEmpty
}
Expand All @@ -29,9 +29,9 @@ type NotificationChannelSettings = {

type NotificationRecipient = {
accountId: AccountId
userId: UserId
walletId: WalletId
deviceTokens: DeviceToken[]
notificationSettings: NotificationSettings
language: UserLanguageOrEmpty
level: AccountLevel
}
Expand Down
10 changes: 5 additions & 5 deletions core/api/src/services/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const NotificationsService = (): INotificationsService => {
title,
body,
notificationCategory: GaloyNotificationCategories.Payments,
notificationSettings: recipient.notificationSettings,
userId: recipient.userId,
})

if (result instanceof Error) return result
Expand Down Expand Up @@ -291,7 +291,7 @@ export const NotificationsService = (): INotificationsService => {
const sendBalance = async ({
balanceAmount,
deviceTokens,
notificationSettings,
recipientUserId,
displayBalanceAmount,
recipientLanguage,
}: SendBalanceArgs): Promise<true | NotificationsServiceError> => {
Expand All @@ -313,7 +313,7 @@ export const NotificationsService = (): INotificationsService => {
title,
body,
notificationCategory,
notificationSettings,
userId: recipientUserId,
})

if (result instanceof NotificationsServiceError) {
Expand Down Expand Up @@ -352,7 +352,7 @@ export const NotificationsService = (): INotificationsService => {
body,
data,
deviceTokens,
notificationSettings,
userId,
notificationCategory,
}: SendFilteredPushNotificationArgs): Promise<true | NotificationsServiceError> => {
const hasDeviceTokens = deviceTokens && deviceTokens.length > 0
Expand All @@ -364,8 +364,8 @@ export const NotificationsService = (): INotificationsService => {
title,
body,
data,
notificationSettings,
notificationCategory,
userId,
})

if (result instanceof NotificationsServiceError) {
Expand Down
59 changes: 47 additions & 12 deletions core/api/src/services/notifications/push-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ import * as admin from "firebase-admin"

import { Messaging } from "firebase-admin/lib/messaging/messaging"

import {
ShouldSendNotificationRequest,
NotificationChannel as GrpcNotificationChannel,
} from "./proto/notifications_pb"

import {
shouldSendNotification as grpcShouldSendNotification,
notificationsMetadata,
} from "./grpc-client"

import { notificationCategoryToGrpcNotificationCategory } from "./convert"

import {
DeviceTokensNotRegisteredNotificationsServiceError,
InvalidDeviceNotificationsServiceError,
NotificationChannel,
NotificationsServiceError,
NotificationsServiceUnreachableServerError,
UnknownNotificationsServiceError,
shouldSendNotification,
} from "@/domain/notifications"
import { ErrorLevel, parseErrorMessageFromUnknown } from "@/domain/shared"
import { baseLogger } from "@/services/logger"
Expand Down Expand Up @@ -116,17 +126,42 @@ export const PushNotificationsService = (): IPushNotificationsService => {
})()
}

const checkShouldSendNotification = async ({
userId,
notificationCategory,
}: {
userId: UserId
notificationCategory: NotificationCategory
}): Promise<boolean | UnknownNotificationsServiceError> => {
try {
const request = new ShouldSendNotificationRequest()
request.setUserId(userId)
request.setChannel(GrpcNotificationChannel.PUSH)
request.setCategory(
notificationCategoryToGrpcNotificationCategory(notificationCategory),
)

const response = await grpcShouldSendNotification(request, notificationsMetadata)

return response.getShouldSend()
} catch (err) {
return new UnknownNotificationsServiceError(err)
}
}

const sendFilteredNotification = async (args: SendFilteredPushNotificationArgs) => {
const { notificationSettings, notificationCategory, data, ...sendNotificationArgs } =
args

if (
!shouldSendNotification({
notificationCategory,
notificationSettings,
notificationChannel: NotificationChannel.Push,
})
) {
const { notificationCategory, data, ...sendNotificationArgs } = args

const shouldSendNotification = await checkShouldSendNotification({
userId: args.userId,
notificationCategory,
})

if (shouldSendNotification instanceof UnknownNotificationsServiceError) {
return shouldSendNotification
}

if (!shouldSendNotification) {
return {
status: SendFilteredPushNotificationStatus.Filtered,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type SendFilteredPushNotificationArgs = {
title: string
body: string
data?: { [key: string]: string }
notificationSettings: NotificationSettings
userId: UserId
notificationCategory: NotificationCategory
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const walletId = "walletId" as WalletId
const paymentHash = "paymentHash" as PaymentHash
const txHash = "txHash" as OnChainTxHash
const deviceTokens = ["token" as DeviceToken]
const userId = "UserId" as UserId
const language = "" as UserLanguageOrEmpty
const paymentAmount = {
amount: 1000n,
Expand Down Expand Up @@ -68,13 +69,6 @@ const crcSettlementDisplayPrice = <S extends WalletCurrency>({
walletCurrency,
})

const unfilteredNotificationSettings: NotificationSettings = {
push: {
enabled: true,
disabledCategories: [],
},
}

beforeAll(async () => {
const usdDisplayPriceRatio = await getCurrentPriceAsDisplayPriceRatio({
currency: UsdDisplayCurrency,
Expand Down Expand Up @@ -247,7 +241,7 @@ describe("notification", () => {
walletId,
deviceTokens,
language,
notificationSettings: unfilteredNotificationSettings,
userId,
level: AccountLevel.One,
},
transaction: {
Expand Down Expand Up @@ -319,7 +313,7 @@ describe("notification", () => {
walletId,
deviceTokens,
language,
notificationSettings: unfilteredNotificationSettings,
userId,
level: AccountLevel.One,
},
transaction: {
Expand Down Expand Up @@ -392,7 +386,7 @@ describe("notification", () => {
walletId,
deviceTokens,
language,
notificationSettings: unfilteredNotificationSettings,
userId,
level: AccountLevel.One,
},
transaction: {
Expand Down Expand Up @@ -462,7 +456,7 @@ describe("notification", () => {
walletId,
deviceTokens,
language,
notificationSettings: unfilteredNotificationSettings,
userId,
level: AccountLevel.One,
},
transaction: {
Expand Down Expand Up @@ -532,7 +526,7 @@ describe("notification", () => {
walletId,
deviceTokens,
language,
notificationSettings: unfilteredNotificationSettings,
userId,
level: AccountLevel.One,
},
transaction: {
Expand Down

This file was deleted.

0 comments on commit 0bb1332

Please sign in to comment.