Skip to content

Commit

Permalink
feat: reregister if no registration while subscribing (#406)
Browse files Browse the repository at this point in the history
* feat: reregister if no registration while subscribing

* chore: remove test code

* chore: run prettier

* Update src/w3iProxy/notifyProviders/internalNotifyProvider.ts

Co-authored-by: Enes <[email protected]>

---------

Co-authored-by: Enes <[email protected]>
  • Loading branch information
devceline and enesozturk authored Jan 16, 2024
1 parent 1a8d284 commit b843ff8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
11 changes: 4 additions & 7 deletions src/constants/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const COMING_SOON_PROJECTS: Array<INotifyApp> = [
id: 'avive',
name: 'Avive',
description:
'One of the world\'s largest UBI & DePin Projects, with over 9 million worldwide users.',
"One of the world's largest UBI & DePin Projects, with over 9 million worldwide users.",
url: 'https://www.avive.world',
isComingSoon: true,
isVerified: false,
Expand All @@ -57,8 +57,7 @@ export const COMING_SOON_PROJECTS: Array<INotifyApp> = [
{
id: 'maverick',
name: 'Maverick Protocol',
description:
'Maverick Protocol is a leading provider of smart contract solutions in DeFi.',
description: 'Maverick Protocol is a leading provider of smart contract solutions in DeFi.',
url: 'https://app.mav.xyz',
isComingSoon: true,
isVerified: false,
Expand All @@ -68,8 +67,7 @@ export const COMING_SOON_PROJECTS: Array<INotifyApp> = [
{
id: 'loopring-pro',
name: 'Loopring Pro',
description:
'Explore the Loopring DEX, Earn, and NFT Management.',
description: 'Explore the Loopring DEX, Earn, and NFT Management.',
url: 'https://loopring.io/#/pro',
isComingSoon: true,
isVerified: false,
Expand All @@ -79,8 +77,7 @@ export const COMING_SOON_PROJECTS: Array<INotifyApp> = [
{
id: 'loopring-earn',
name: 'Loopring Earn',
description:
'Access the most innovative structural products brought to the DeFi world.',
description: 'Access the most innovative structural products brought to the DeFi world.',
url: 'https://earn.loopring.io/',
isComingSoon: true,
isVerified: false,
Expand Down
19 changes: 19 additions & 0 deletions src/contexts/W3iContext/hooks/notifyHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ export const useNotifyState = (w3iProxy: Web3InboxProxy, proxyReady: boolean) =>
}
})

const notifyReregisterSub = notifyClient.observe('notify_reregister', {
next: params => {
handleRegistration(params.userPubkey).then(() => {
switch (params.nextAction.type) {
case 'subscribe':
notifyClient.subscribe(params.nextAction.params)
break
case 'deleteSubscription':
notifyClient.deleteSubscription(params.nextAction.params)
break
case 'update':
notifyClient.update(params.nextAction.params)
break
}
})
}
})

const syncUpdateSub = notifyClient.observe('sync_update', {
next: refreshNotifyState
})
Expand All @@ -165,6 +183,7 @@ export const useNotifyState = (w3iProxy: Web3InboxProxy, proxyReady: boolean) =>
notifySubsChanged.unsubscribe()
notifySignatureRequestedSub.unsubscribe()
notifySignatureRequestCancelledSub.unsubscribe()
notifyReregisterSub.unsubscribe()
}
}, [notifyClient, refreshNotifyState, setWatchSubscriptionsComplete])

Expand Down
10 changes: 9 additions & 1 deletion src/w3iProxy/listenerTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NotifyClientTypes } from '@walletconnect/notify-client'
import type { NotifyClient, NotifyClientTypes } from '@walletconnect/notify-client'

import type { ChatClientTypes } from './chatProviders/types'

Expand All @@ -21,6 +21,13 @@ export interface ChatFacadeEvents {
sync_update: never
}

type NextAction<T extends 'subscribe' | 'update' | 'deleteSubscription'> = {
type: T
params: Parameters<NotifyClient[T]>[0]
}

type NextActions = NextAction<'subscribe'> | NextAction<'update'> | NextAction<'deleteSubscription'>

export interface NotifyFacadeEvents {
notify_message: NotifyClientTypes.EventArguments['notify_message']
notify_subscription: NotifyClientTypes.EventArguments['notify_subscription']
Expand All @@ -29,5 +36,6 @@ export interface NotifyFacadeEvents {
notify_subscriptions_changed: NotifyClientTypes.EventArguments['notify_subscriptions_changed']
notify_signature_requested: { message: string }
notify_signature_request_cancelled: never
notify_reregister: { userPubkey: string; nextAction: NextActions }
sync_update: never
}
55 changes: 55 additions & 0 deletions src/w3iProxy/notifyProviders/internalNotifyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ export default class InternalNotifyProvider implements W3iNotifyProvider {
throw new Error(this.formatClientRelatedError('subscribe'))
}

if (
!this.notifyClient.isRegistered({
account: params.account,
domain: window.location.hostname,
allApps: true
})
) {
this.emitter.emit('notify_reregister', {
userPubkey: params.account,
nextAction: {
type: 'subscribe',
params
}
})
return false
}

let subscribed: boolean = false
try {
subscribed = await this.notifyClient.subscribe({
Expand All @@ -187,6 +204,27 @@ export default class InternalNotifyProvider implements W3iNotifyProvider {
throw new Error(this.formatClientRelatedError('update'))
}

const account = this.notifyClient.subscriptions.keys.includes(params.topic)
? this.notifyClient.subscriptions.get(params.topic).account
: ''

const isRegistered = this.notifyClient.isRegistered({
account,
domain: window.location.hostname,
allApps: true
})

if (!isRegistered) {
this.emitter.emit('notify_reregister', {
userPubkey: account,
nextAction: {
type: 'update',
params
}
})
return false
}

const updated = await this.notifyClient.update(params)

return updated
Expand All @@ -197,6 +235,23 @@ export default class InternalNotifyProvider implements W3iNotifyProvider {
throw new Error(this.formatClientRelatedError('deleteSubscription'))
}

const account = this.notifyClient.subscriptions.keys.includes(params.topic)
? this.notifyClient.subscriptions.get(params.topic).account
: ''

if (
!this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true })
) {
this.emitter.emit('notify_reregister', {
userPubkey: account,
nextAction: {
type: 'deleteSubscription',
params
}
})
return
}

return this.notifyClient.deleteSubscription(params)
}

Expand Down

1 comment on commit b843ff8

@vercel
Copy link

@vercel vercel bot commented on b843ff8 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.