notifications

This commit is contained in:
boufni95 2023-10-20 18:09:32 +02:00
parent 62a2e437af
commit 0a3da2082b
8 changed files with 2168 additions and 2121 deletions

View file

@ -95,9 +95,9 @@ The nostr server will send back a message response, and inside the body there wi
- __User__: - __User__:
- expected context content - expected context content
- __user_id__: _string_
- __app_id__: _string_ - __app_id__: _string_
- __app_user_id__: _string_ - __app_user_id__: _string_
- __user_id__: _string_
- __Admin__: - __Admin__:
- expected context content - expected context content
@ -372,64 +372,14 @@ The nostr server will send back a message response, and inside the body there wi
## Messages ## Messages
### The content of requests and response from the methods ### The content of requests and response from the methods
### SetMockAppUserBalanceRequest ### LndGetInfoResponse
- __user_identifier__: _string_ - __alias__: _string_
### SendAppUserToAppUserPaymentRequest
- __from_user_identifier__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_ - __amount__: _number_
### LndGetInfoRequest
- __nodeId__: _number_
### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_
- __base_url_override__: _string_
### Product
- __id__: _string_
- __name__: _string_
- __price_sats__: _number_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
### GetUserOperationsRequest
- __latestIncomingInvoice__: _number_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
### NewAddressResponse
- __address__: _string_
### NewInvoiceRequest
- __amountSats__: _number_
- __memo__: _string_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### UserOperations
- __fromIndex__: _number_
- __toIndex__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### PayAddressResponse
- __txId__: _string_
### LnurlPayInfoResponse ### LnurlPayInfoResponse
- __tag__: _string_ - __tag__: _string_
- __callback__: _string_ - __callback__: _string_
@ -437,91 +387,12 @@ The nostr server will send back a message response, and inside the body there wi
- __minSendable__: _number_ - __minSendable__: _number_
- __metadata__: _string_ - __metadata__: _string_
### UserInfo
- __userId__: _string_
- __balance__: _number_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### SetMockAppBalanceRequest
- __amount__: _number_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### GetUserOperationsResponse
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### LiveUserOperation
- __id__: _string_
### LndGetInfoResponse
- __alias__: _string_
### AuthApp
- __app__: _[Application](#Application)_
- __auth_token__: _string_
### SendAppUserToAppUserPaymentRequest
- __from_user_identifier__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_
### AddAppInvoiceRequest
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### PayAppUserInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
### NewAddressRequest ### NewAddressRequest
- __addressType__: _[AddressType](#AddressType)_ - __addressType__: _[AddressType](#AddressType)_
### NewInvoiceResponse ### LnurlLinkResponse
- __invoice__: _string_ - __lnurl__: _string_
- __k1__: _string_
### PayInvoiceRequest
- __invoice__: _string_
- __amount__: _number_
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### Application
- __name__: _string_
- __id__: _string_
- __balance__: _number_
- __npub__: _string_
### AppUser
- __identifier__: _string_
- __info__: _[UserInfo](#UserInfo)_
- __max_withdrawable__: _number_
### GetProductBuyLinkResponse
- __link__: _string_
### PayAppUserInvoiceRequest
- __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
### LnurlWithdrawInfoResponse ### LnurlWithdrawInfoResponse
- __tag__: _string_ - __tag__: _string_
@ -533,46 +404,176 @@ The nostr server will send back a message response, and inside the body there wi
- __balanceCheck__: _string_ - __balanceCheck__: _string_
- __payLink__: _string_ - __payLink__: _string_
### UserInfo
- __userId__: _string_
- __balance__: _number_
### GetUserOperationsResponse
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### Application
- __name__: _string_
- __id__: _string_
- __balance__: _number_
- __npub__: _string_
### NewInvoiceResponse
- __invoice__: _string_
### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
### UserOperation ### UserOperation
- __paidAtUnix__: _number_ - __paidAtUnix__: _number_
- __type__: _[UserOperationType](#UserOperationType)_ - __type__: _[UserOperationType](#UserOperationType)_
- __inbound__: _boolean_ - __inbound__: _boolean_
- __amount__: _number_ - __amount__: _number_
### GetAppUserRequest
- __user_identifier__: _string_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### GetProductBuyLinkResponse
- __link__: _string_
### LndGetInfoRequest
- __nodeId__: _number_
### AuthAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional
### AuthApp
- __app__: _[Application](#Application)_
- __auth_token__: _string_
### PayInvoiceRequest
- __invoice__: _string_
- __amount__: _number_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### EncryptionExchangeRequest
- __publicKey__: _string_
- __deviceId__: _string_
### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### PayAppUserInvoiceRequest
- __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### SetMockAppUserBalanceRequest
- __user_identifier__: _string_
- __amount__: _number_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### GetUserOperationsRequest
- __latestIncomingInvoice__: _number_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
### SetMockInvoiceAsPaidRequest
- __invoice__: _string_
- __amount__: _number_
### AddAppInvoiceRequest
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_
- __base_url_override__: _string_
### SetMockAppBalanceRequest
- __amount__: _number_
### NewAddressResponse
- __address__: _string_
### DecodeInvoiceRequest
- __invoice__: _string_
### OpenChannelResponse
- __channelId__: _string_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
### Product
- __id__: _string_
- __name__: _string_
- __price_sats__: _number_
### LiveUserOperation
- __id__: _string_
- __operation__: _[UserOperation](#UserOperation)_
### Empty ### Empty
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### AddAppUserRequest ### AddAppUserRequest
- __identifier__: _string_ - __identifier__: _string_
- __fail_if_exists__: _boolean_ - __fail_if_exists__: _boolean_
- __balance__: _number_ - __balance__: _number_
### GetAppUserRequest ### AppUser
- __user_identifier__: _string_ - __identifier__: _string_
- __info__: _[UserInfo](#UserInfo)_
- __max_withdrawable__: _number_
### DecodeInvoiceRequest ### PayAppUserInvoiceResponse
- __invoice__: _string_ - __preimage__: _string_
- __amount_paid__: _number_
### PayAddressResponse
- __txId__: _string_
### NewInvoiceRequest
- __amountSats__: _number_
- __memo__: _string_
### DecodeInvoiceResponse ### DecodeInvoiceResponse
- __amount__: _number_ - __amount__: _number_
### OpenChannelResponse ### UserOperations
- __channelId__: _string_ - __fromIndex__: _number_
- __toIndex__: _number_
### LnurlLinkResponse - __operations__: ARRAY of: _[UserOperation](#UserOperation)_
- __lnurl__: _string_
- __k1__: _string_
### EncryptionExchangeRequest
- __publicKey__: _string_
- __deviceId__: _string_
### SetMockInvoiceAsPaidRequest
- __invoice__: _string_
- __amount__: _number_
### AuthAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional
## Enums ## Enums
### The enumerators used in the messages ### The enumerators used in the messages

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

Binary file not shown.

View file

@ -256,4 +256,5 @@ message GetProductBuyLinkResponse {
message LiveUserOperation { message LiveUserOperation {
string id = 1; string id = 1;
UserOperation operation = 2;
} }

View file

@ -1,3 +1,4 @@
import crypto from 'crypto'
import fetch from "node-fetch" import fetch from "node-fetch"
import Storage, { LoadStorageSettingsFromEnv } from '../storage/index.js' import Storage, { LoadStorageSettingsFromEnv } from '../storage/index.js'
import * as Types from '../../../proto/autogenerated/ts/types.js' import * as Types from '../../../proto/autogenerated/ts/types.js'
@ -45,6 +46,7 @@ export default class {
applicationManager: ApplicationManager applicationManager: ApplicationManager
appUserManager: AppUserManager appUserManager: AppUserManager
paymentManager: PaymentManager paymentManager: PaymentManager
paymentSubs: Record<string, ((op: Types.UserOperation) => void) | null> = {}
constructor(settings: MainSettings) { constructor(settings: MainSettings) {
this.settings = settings this.settings = settings
@ -66,6 +68,7 @@ export default class {
// This call will fail if the transaction is already registered // This call will fail if the transaction is already registered
const addedTx = await this.storage.paymentStorage.AddAddressReceivingTransaction(userAddress, txOutput.hash, txOutput.index, amount, fee, tx) const addedTx = await this.storage.paymentStorage.AddAddressReceivingTransaction(userAddress, txOutput.hash, txOutput.index, amount, fee, tx)
await this.storage.userStorage.IncrementUserBalance(userAddress.user.user_id, addedTx.paid_amount - fee, tx) await this.storage.userStorage.IncrementUserBalance(userAddress.user.user_id, addedTx.paid_amount - fee, tx)
this.triggerSubs(userAddress.user.user_id, { amount, paidAtUnix: Date.now() / 1000, inbound: true, type: Types.UserOperationType.INCOMING_TX })
} catch { } catch {
} }
@ -96,6 +99,7 @@ export default class {
} }
await this.triggerPaidCallback(log, userInvoice.callbackUrl) await this.triggerPaidCallback(log, userInvoice.callbackUrl)
this.triggerSubs(userInvoice.user.user_id, { amount, paidAtUnix: Date.now() / 1000, inbound: true, type: Types.UserOperationType.INCOMING_INVOICE })
log("paid invoice processed successfully") log("paid invoice processed successfully")
} catch (err: any) { } catch (err: any) {
log("ERROR", "cannot process paid invoice", err.message || "") log("ERROR", "cannot process paid invoice", err.message || "")
@ -113,4 +117,27 @@ export default class {
log("error sending paid callback for invoice", err.message || "") log("error sending paid callback for invoice", err.message || "")
} }
} }
triggerSubs(userId: string, op: Types.UserOperation) {
const sub = this.paymentSubs[userId]
if (!sub) {
return
}
sub(op)
}
async SubToPayment(ctx: Types.UserContext, cb: (res: Types.LiveUserOperation, err: Error | null) => void) {
const sub = this.paymentSubs[ctx.user_id]
if (sub) {
return
}
const app = await this.storage.applicationStorage.GetApplication(ctx.app_id)
const log = getLogger({ appName: app.name, userId: ctx.user_id })
const appUser = await this.storage.applicationStorage.GetApplicationUser(app, ctx.app_user_id)
this.paymentSubs[ctx.user_id] = (op) => {
const rand = crypto.randomBytes(32).toString('hex')
cb({ id: rand, operation: op }, null)
}
}
} }

View file

@ -176,6 +176,9 @@ export default (mainHandler: Main): Types.ServerMethods => {
}, },
SetMockAppBalance: async (ctx, req) => { SetMockAppBalance: async (ctx, req) => {
await mainHandler.applicationManager.SetMockAppBalance(ctx.app_id, req) await mainHandler.applicationManager.SetMockAppBalance(ctx.app_id, req)
},
GetLiveUserOperations: async (ctx, cb) => {
mainHandler.SubToPayment(ctx, cb)
} }
} }
} }