Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Df continous payments #250

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0e1617c
Tag listener
Mar 26, 2024
f54983a
Monetization tag
Apr 1, 2024
700535e
Add stop & resume
Apr 2, 2024
2fc6efa
Merge branch 'main' of github.com:interledger/web-monetization-extens…
ionutanin Apr 2, 2024
2e925af
merge and resolve conflicts
ionutanin Apr 2, 2024
1e2f8b1
merge conflicts
ionutanin Apr 2, 2024
df6cfad
update monetization event payload
ionutanin Apr 2, 2024
5b7702d
Monetization service progress
raducristianpopa Apr 11, 2024
a40e256
Update monetization event (include frameId as well)
raducristianpopa Apr 15, 2024
a626d87
Merge branch 'main' into ia--monetization-flow
raducristianpopa Apr 15, 2024
1c93e84
Remove unused code
raducristianpopa Apr 15, 2024
822ecac
Merge branch 'main' into ia--monetization-flow
raducristianpopa Apr 16, 2024
eace6b8
Move message handler in contentScript
Apr 16, 2024
c914915
Add contentScript file changes
Apr 16, 2024
a883ad9
Clear payment session on closed tab
raducristianpopa Apr 16, 2024
7afd263
Fix merge issues
Apr 16, 2024
0df6286
Listen for tab events, resume & pause monetization
raducristianpopa Apr 17, 2024
5f770fe
Merge branch 'df--tag-listener' into ia--monetization-flow
raducristianpopa Apr 17, 2024
b2cff50
Update icon
Apr 18, 2024
4f52a9f
remove logs
Apr 18, 2024
b568d62
Updated icon
Apr 22, 2024
c2a919f
Remove null validation
Apr 30, 2024
c3411b3
Merge branch 'main' of https://github.com/interledger/web-monetizatio…
Apr 30, 2024
7a51918
Update icon logic
May 9, 2024
ab5a539
Update bulk at start
May 10, 2024
f86bef9
Fix build error
May 10, 2024
759ad7d
Fix comflicts
May 10, 2024
a830e57
Minor code updated
May 10, 2024
84a1f45
Add contiunous payments
May 10, 2024
d47a9d1
Merge branch 'df--update-icon' of https://github.com/interledger/web-…
May 10, 2024
69810ed
Emit continous payment to content
May 10, 2024
accd4ad
Remove global WM
May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/background/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ export const emitToggleWM = async (
payload
})
}

export const emitToggleContinousPayment = async (
payload: BackgroundToContentActionPayload[BackgroundToContentAction.EMIT_TOGGLE_CONTINOUS_PAYMENT]
) => {
return await message.sendToActiveTab({
action: BackgroundToContentAction.EMIT_TOGGLE_CONTINOUS_PAYMENT,
payload
})
}
9 changes: 9 additions & 0 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class Background {
this.tabEvents.onUpdatedTab()
return

case PopupToBackgroundAction.TOGGLE_CONTINOUS_PAYMENT:
await this.monetizationService.toggleContinousPayment()

this.tabEvents.onUpdatedTab()
return

case PopupToBackgroundAction.PAY_WEBSITE:
return success(
await this.monetizationService.pay(message.payload.amount)
Expand Down Expand Up @@ -105,6 +111,9 @@ export class Background {
case ContentToBackgroundAction.IS_WM_ENABLED:
return success(await this.storage.getWMState())

case ContentToBackgroundAction.IS_CONTINOUS_PAYMENT_ENABLED:
return success(await this.storage.getContinousPaymentState())

default:
return
}
Expand Down
30 changes: 24 additions & 6 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StopMonetizationPayload
} from '@/shared/messages'
import { PaymentSession } from './paymentSession'
import { emitToggleWM } from '../lib/messages'
import { emitToggleWM, emitToggleContinousPayment } from '../lib/messages'
import { getCurrentActiveTab, getSender, getTabId } from '../utils'

export class MonetizationService {
Expand All @@ -28,10 +28,12 @@ export class MonetizationService {
payload: StartMonetizationPayload,
sender: Runtime.MessageSender
) {
const { connected, enabled } = await this.storage.get([
'enabled',
'connected'
])
const { connected, enabled, enabledContinousPayment } =
await this.storage.get([
'enabled',
'connected',
'enabledContinousPayment'
])

const { requestId, walletAddress } = payload
const { tabId, frameId } = getSender(sender)
Expand Down Expand Up @@ -61,7 +63,11 @@ export class MonetizationService {

this.sessions[tabId].set(requestId, session)

if (connected === true && enabled === true) {
if (
connected === true &&
enabled === true &&
enabledContinousPayment === true
) {
void session.start()
}
}
Expand Down Expand Up @@ -104,6 +110,18 @@ export class MonetizationService {
emitToggleWM({ enabled: !enabled })
}

async toggleContinousPayment() {
const { enabledContinousPayment } = await this.storage.get([
'enabledContinousPayment'
])
await this.storage.set({
enabledContinousPayment: !enabledContinousPayment
})
emitToggleContinousPayment({
enabledContinousPayment: !enabledContinousPayment
})
}

clearTabSessions(tabId: number) {
this.logger.debug(`Attempting to clear sessions for tab ${tabId}.`)
const sessions = this.sessions[tabId]
Expand Down
14 changes: 12 additions & 2 deletions src/background/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const defaultStorage = {
grant: null,
rateOfPay: null,
minRateOfPay: null,
maxRateOfPay: null
maxRateOfPay: null,
enabledContinousPayment: true
} satisfies Omit<Storage, 'publicKey' | 'privateKey' | 'keyId'>

// TODO: Emit events when certain values are updated:
Expand Down Expand Up @@ -65,7 +66,8 @@ export class StorageService extends EventEmitter {
'minRateOfPay',
'maxRateOfPay',
'walletAddress',
'publicKey'
'publicKey',
'enabledContinousPayment'
])

const tab = await getCurrentActiveTab(this.browser)
Expand All @@ -91,6 +93,14 @@ export class StorageService extends EventEmitter {
return enabled
}

async getContinousPaymentState(): Promise<boolean> {
const { enabledContinousPayment } = await this.get([
'enabledContinousPayment'
])

return enabledContinousPayment
}

async keyPairExists(): Promise<boolean> {
const keys = await this.get(['privateKey', 'publicKey', 'keyId'])
if (
Expand Down
25 changes: 18 additions & 7 deletions src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ export class TabEvents {
}

private changeIcon = async () => {
const { enabled } = await this.storage.get(['enabled'])
console.log('changeIcon')

const { enabledContinousPayment } = await this.storage.get([
'enabledContinousPayment'
])

console.log('enabledContinousPayment', enabledContinousPayment)

const iconData = {
'34': enabled ? icon34 : iconWarning34,
'128': enabled ? icon128 : iconWarning128
'34': enabledContinousPayment ? icon34 : iconWarning34,
'128': enabledContinousPayment ? icon128 : iconWarning128
}

if (this.browser.action) {
Expand All @@ -49,14 +55,19 @@ export class TabEvents {
}

onUpdatedTab = async (payload?: IsTabMonetizedPayload) => {
const { enabled } = await this.storage.get(['enabled'])
console.log('onUpdatedTab')

const { enabledContinousPayment } = await this.storage.get([
'enabledContinousPayment'
])
console.log('enabledContinousPayment', enabledContinousPayment)

let iconData = {
'34': enabled ? icon34 : iconWarning34,
'128': enabled ? icon34 : iconWarning128
'34': enabledContinousPayment ? icon34 : iconWarning34,
'128': enabledContinousPayment ? icon34 : iconWarning128
}

if (enabled) {
if (enabledContinousPayment) {
if (payload) {
const { value } = payload

Expand Down
6 changes: 6 additions & 0 deletions src/content/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ export const isWMEnabled = async () => {
action: ContentToBackgroundAction.IS_WM_ENABLED
})
}

export const isContinousPaymentEnabled = async () => {
return await message.send<boolean>({
action: ContentToBackgroundAction.IS_CONTINOUS_PAYMENT_ENABLED
})
}
5 changes: 5 additions & 0 deletions src/content/services/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export class ContentScript {

case BackgroundToContentAction.EMIT_TOGGLE_WM:
this.monetizationTagManager.toggleWM(message.payload)
return

case BackgroundToContentAction.EMIT_TOGGLE_CONTINOUS_PAYMENT:
this.monetizationTagManager.toggleContinousPayment(
message.payload
)
return

default:
Expand Down
24 changes: 21 additions & 3 deletions src/content/services/monetizationTagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { WalletAddress } from '@interledger/open-payments/dist/types'
import { checkWalletAddressUrlFormat } from '../utils'
import {
checkWalletAddressUrlCall,
isContinousPaymentEnabled,
isTabMonetized,
isWMEnabled,
resumeMonetization,
startMonetization,
stopMonetization
} from '../lib/messages'
import {
EmitToggleContinousPaymentPayload,
EmitToggleWMPayload,
MonetizationEventPayload
} from '@/shared/messages'
Expand Down Expand Up @@ -66,9 +68,15 @@ export class MonetizationTagManager extends EventEmitter {
}

private async resumeAllMonetization() {
const response = await isWMEnabled()
const isWMEnabledResponse = await isWMEnabled()
const isContinousPaymentEnabledResponse = await isContinousPaymentEnabled()

if (response.success && response.payload) {
if (
isWMEnabledResponse.success &&
isWMEnabledResponse.payload &&
isContinousPaymentEnabledResponse.success &&
isContinousPaymentEnabledResponse.payload
) {
let validTagsCount = 0

this.monetizationTags.forEach((value) => {
Expand Down Expand Up @@ -396,7 +404,17 @@ export class MonetizationTagManager extends EventEmitter {
if (enabled) {
await this.resumeAllMonetization()
} else {
await this.stopAllMonetization()
this.stopAllMonetization()
}
}

async toggleContinousPayment({
enabledContinousPayment
}: EmitToggleContinousPaymentPayload) {
if (enabledContinousPayment) {
await this.resumeAllMonetization()
} else {
this.stopAllMonetization()
}
}
}
19 changes: 10 additions & 9 deletions src/popup/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useContext, useMemo } from 'react'
import { Link, useLocation } from 'react-router-dom'
import browser from 'webextension-polyfill'
import { ArrowBack, Settings } from '../Icons'
import { Switch } from '../ui/Switch'
// import { Switch } from '../ui/Switch'
import { ROUTES_PATH } from '@/popup/Popup'
import { PopupStateContext, ReducerActionType } from '@/popup/lib/context'
import { toggleWM } from '@/popup/lib/messages'
import { PopupStateContext } from '@/popup/lib/context'
// import { toggleWM } from '@/popup/lib/messages'

const Logo = browser.runtime.getURL('assets/images/logo.svg')

Expand All @@ -30,11 +30,12 @@ const NavigationButton = () => {
}

export const Header = () => {
const { state, dispatch } = useContext(PopupStateContext)
const onChange = async () => {
await toggleWM()
dispatch({ type: ReducerActionType.TOGGLE_WM, data: {} })
}
// TO DO - uncomment when decision about global monetization vs continous payment made
// const { state, dispatch } = useContext(PopupStateContext)
// const onChange = async () => {
// await toggleWM()
// dispatch({ type: ReducerActionType.TOGGLE_WM, data: {} })
// }
return (
<header className="flex h-8 flex-row items-center justify-between">
<div className="flex flex-row items-center gap-3">
Expand All @@ -43,7 +44,7 @@ export const Header = () => {
</div>
<div className="flex flex-row items-center gap-3">
<NavigationButton />
<Switch checked={state.enabled} onChange={onChange} />
{/* <Switch checked={state.enabled} onChange={onChange} /> */}
</div>
</header>
)
Expand Down
14 changes: 13 additions & 1 deletion src/popup/lib/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { DeepNonNullable, PopupStore } from '@/shared/types'
export enum ReducerActionType {
SET_DATA = 'SET_DATA',
TOGGLE_WM = 'TOGGLE_WM',
UPDATE_RATE_OF_PAY = 'UPDATE_RATE_OF_PAY'
UPDATE_RATE_OF_PAY = 'UPDATE_RATE_OF_PAY',
TOGGLE_CONTINOUS_PAYMENT = 'TOGGLE_CONTINOUS_PAYMENT'
}

export type PopupState = Required<DeepNonNullable<PopupStore>>
Expand All @@ -29,6 +30,10 @@ interface ToggleWMAction extends ReducerActionMock {
type: ReducerActionType.TOGGLE_WM
}

interface ToggleContinousPaymentAction extends ReducerActionMock {
type: ReducerActionType.TOGGLE_CONTINOUS_PAYMENT
}

interface UpdateRateOfPayAction extends ReducerActionMock {
type: ReducerActionType.UPDATE_RATE_OF_PAY
data: {
Expand All @@ -40,6 +45,7 @@ export type ReducerActions =
| SetDataAction
| ToggleWMAction
| UpdateRateOfPayAction
| ToggleContinousPaymentAction

export const PopupStateContext = React.createContext<PopupContext>(
{} as PopupContext
Expand All @@ -62,6 +68,12 @@ const reducer = (state: PopupState, action: ReducerActions): PopupState => {
rateOfPay: action.data.rateOfPay
}
}
case ReducerActionType.TOGGLE_CONTINOUS_PAYMENT: {
return {
...state,
enabledContinousPayment: !state.enabledContinousPayment
}
}
default:
return state
}
Expand Down
6 changes: 6 additions & 0 deletions src/popup/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export const toggleWM = async () => {
})
}

export const toggleContinousPayment = async () => {
return await message.send({
action: PopupToBackgroundAction.TOGGLE_CONTINOUS_PAYMENT
})
}

export const updateRateOfPay = async (
payload: PopupToBackgroundActionPayload[PopupToBackgroundAction.UPDATE_RATE_OF_PAY]
) => {
Expand Down
Loading
Loading