Skip to content

Commit

Permalink
Filter out originating tab from list create+delete broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
poltak committed Jun 3, 2024
1 parent 04cfc51 commit c4dc584
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 168 deletions.
6 changes: 3 additions & 3 deletions src/authentication/components/AccountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import QRCanvas from 'src/common-ui/components/qr-canvas'
import { PopoutBox } from '@worldbrain/memex-common/lib/common-ui/components/popout-box'
import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field'
import UpgradeModal from '../upgrade-modal'
import { RemoteBGScriptInterface } from 'src/background-script/types'
import { Browser } from 'webextension-polyfill'
import type { RemoteBGScriptInterface } from 'src/background-script/types'
import type { Browser } from 'webextension-polyfill'

const styles = require('./styles.css')

Expand All @@ -30,7 +30,7 @@ const DisplayNameBox = styled.div`
export interface Props extends Dependencies {
setAuthMode: (mode) => void
getRootElement: () => HTMLElement
bgScriptBG: RemoteBGScriptInterface
bgScriptBG: RemoteBGScriptInterface<'caller'>
browserAPIs: Browser
}

Expand Down
6 changes: 2 additions & 4 deletions src/authentication/components/UserScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import Logic from './logic'
import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section'
import { LoadingContainer } from 'src/dashboard-refactor/styled-components'
import LoadingIndicator from '@worldbrain/memex-common/lib/common-ui/components/loading-indicator'
import { RemoteBGScriptInterface } from 'src/background-script/types'
import { LOGIN_URL } from 'src/constants'
import checkBrowser from 'src/util/check-browser'
import type { RemoteBGScriptInterface } from 'src/background-script/types'
import browser, { Browser } from 'webextension-polyfill'

// interface Props {
Expand All @@ -23,7 +21,7 @@ import browser, { Browser } from 'webextension-polyfill'
export interface Props extends Dependencies {
refreshUser?: boolean
getRootElement: () => HTMLElement
bgScriptBG: RemoteBGScriptInterface
bgScriptBG: RemoteBGScriptInterface<'caller'>
browserAPIs: Browser
}

Expand Down
8 changes: 4 additions & 4 deletions src/authentication/upgrade-modal/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export default class PromptTemplatesLogic extends UILogic<
newSelection.push(event)
}

const upgradeResponse = await this.dependencies.createCheckOutLink(
billingPeriod,
newSelection,
const upgradeResponse = await this.dependencies.createCheckOutLink({
doNotOpen,
)
billingPeriod,
selectedPremiumPlans: newSelection,
})

if (upgradeResponse === 'error') {
this.emitMutation({
Expand Down
17 changes: 7 additions & 10 deletions src/authentication/upgrade-modal/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { UITaskState } from '../../../external/@worldbrain/memex-common/ts/main-ui/types'
import {
import type { UITaskState } from '../../../external/@worldbrain/memex-common/ts/main-ui/types'
import type {
UIEvent,
UISignal,
} from '../../../external/@worldbrain/memex-common/ts/main-ui/classes/logic'
import { PremiumPlans } from '../../../external/@worldbrain/memex-common/ts/subscriptions/availablePowerups'
import { AuthRemoteFunctionsInterface } from '../background/types'
import { Browser } from 'webextension-polyfill'
import type { PremiumPlans } from '../../../external/@worldbrain/memex-common/ts/subscriptions/availablePowerups'
import type { AuthRemoteFunctionsInterface } from '../background/types'
import type { Browser } from 'webextension-polyfill'
import type { RemoteBGScriptInterface } from 'src/background-script/types'

export interface PromptTemplatesDependencies {
powerUpType: PowerUpModalVersion
limitReachedNotif?: PowerUpModalVersion
createCheckOutLink: (
billingPeriod: 'monthly' | 'yearly',
selectedPremiumPlans: PremiumPlans[],
doNotOpen: boolean,
) => Promise<'error' | 'success'>
createCheckOutLink: RemoteBGScriptInterface<'caller'>['createCheckoutLink']
componentVariant: 'Modal' | 'PricingList' | 'AccountPage' | 'OnboardingStep'
getRootElement?: () => HTMLElement
closeComponent?: () => void
Expand Down
68 changes: 42 additions & 26 deletions src/background-script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { Browser, Runtime, Storage, Tabs } from 'webextension-polyfill'
import type { URLNormalizer } from '@worldbrain/memex-common/lib/url-utils/normalize/types'

import * as utils from './utils'
import { makeRemotelyCallable, runInTab } from '../util/webextensionRPC'
import {
registerRemoteFunctions,
remoteFunctionWithExtraArgs,
remoteFunctionWithoutExtraArgs,
runInTab,
} from '../util/webextensionRPC'
import type { StorageChangesManager } from '../util/storage-changes'
import { migrations, MIGRATION_PREFIX } from './quick-and-dirty-migrations'
import { generateUserId } from 'src/analytics/utils'
Expand Down Expand Up @@ -35,9 +40,7 @@ import type { InPageUIContentScriptRemoteInterface } from 'src/in-page-ui/conten
import { captureException } from 'src/util/raven'
import { checkStripePlan } from '@worldbrain/memex-common/lib/subscriptions/storage'
import type { AnalyticsCoreInterface } from '@worldbrain/memex-common/lib/analytics/types'
import { trackOnboardingPath } from '@worldbrain/memex-common/lib/analytics/events'
import { CLOUDFLARE_WORKER_URLS } from '@worldbrain/memex-common/lib/content-sharing/storage/constants'
import { PremiumPlans } from '@worldbrain/memex-common/lib/subscriptions/availablePowerups'
import checkBrowser from 'src/util/check-browser'

interface Dependencies {
Expand Down Expand Up @@ -72,18 +75,23 @@ interface Dependencies {
}

class BackgroundScript {
remoteFunctions: RemoteBGScriptInterface
remoteFunctions: RemoteBGScriptInterface<'provider'>

constructor(public deps: Dependencies) {
this.remoteFunctions = {
openOptionsTab: this.openOptionsPage,
openOverviewTab: this.openDashboardPage,
createCheckoutLink: this.createCheckoutLink,
broadcastListChangeToAllTabs: this.broadcastSpaceChangeToAllTabs,
openOptionsTab: remoteFunctionWithoutExtraArgs(
this.openOptionsPage,
),
openOverviewTab: remoteFunctionWithoutExtraArgs(
this.openDashboardPage,
),
createCheckoutLink: remoteFunctionWithoutExtraArgs(
this.createCheckoutLink,
),
broadcastListChangeToAllTabs: remoteFunctionWithExtraArgs(
this.broadcastSpaceChangeToAllTabs,
),
}

// window['___removeDupeSpaces'] = () =>
// removeDupeSpaces({ storageManager: deps.storageManager })
}

get defaultUninstallURL() {
Expand Down Expand Up @@ -323,7 +331,7 @@ class BackgroundScript {
}

setupRemoteFunctions() {
makeRemotelyCallable(this.remoteFunctions)
registerRemoteFunctions(this.remoteFunctions)
}

setupWebExtAPIHandlers() {
Expand Down Expand Up @@ -370,16 +378,23 @@ class BackgroundScript {
runtimeAPI.reload()
}

broadcastSpaceChangeToAllTabs: RemoteBGScriptInterface['broadcastListChangeToAllTabs'] = async (
broadcastSpaceChangeToAllTabs: RemoteBGScriptInterface<
'provider'
>['broadcastListChangeToAllTabs']['function'] = async (
{ tab: originatingTab },
params,
) => {
let { bgModules } = this.deps
try {
await bgModules.tabManagement.mapTabChunks(
async (tab) => {
if (!bgModules.tabManagement.canTabRunContentScripts(tab)) {
if (
tab.id === originatingTab.id ||
!bgModules.tabManagement.canTabRunContentScripts(tab)
) {
return
}

if (params.type === 'create') {
await runInTab<InPageUIContentScriptRemoteInterface>(
tab.id,
Expand Down Expand Up @@ -408,9 +423,9 @@ class BackgroundScript {
? this.deps.tabsAPI.update
: this.deps.tabsAPI.create

private openDashboardPage: RemoteBGScriptInterface['openOverviewTab'] = async (
params,
) => {
private openDashboardPage: RemoteBGScriptInterface<
'provider'
>['openOverviewTab']['function'] = async (params) => {
let addedQuery
if (params?.selectedSpace) {
addedQuery = `selectedSpace=${params.selectedSpace}`
Expand Down Expand Up @@ -445,20 +460,21 @@ class BackgroundScript {
}
}

private openOptionsPage: RemoteBGScriptInterface['openOptionsTab'] = async (
query,
params,
) => {
private openOptionsPage: RemoteBGScriptInterface<
'provider'
>['openOptionsTab']['function'] = async ({ query, params }) => {
await this.chooseTabOpenFn(params)({
url: `${OPTIONS_URL}#${query}`,
})
}

private createCheckoutLink: RemoteBGScriptInterface['createCheckoutLink'] = async (
billingPeriod: 'monthly' | 'yearly',
selectedPremiumPlans: PremiumPlans[],
doNotOpen: boolean,
) => {
private createCheckoutLink: RemoteBGScriptInterface<
'provider'
>['createCheckoutLink']['function'] = async ({
billingPeriod,
selectedPremiumPlans,
doNotOpen,
}) => {
const currentUser = await this.deps.bgModules.auth.authService.getCurrentUser()
const creationTime = currentUser?.creationTime
const currentTime = Date.now()
Expand Down
58 changes: 35 additions & 23 deletions src/background-script/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import type { RemoteCollectionsInterface } from 'src/custom-lists/background/typ
import type { ImageSupportInterface } from 'src/image-support/background/types'
import type { RemotePageActivityIndicatorInterface } from 'src/page-activity-indicator/background/types'
import type { RemoteSyncSettingsInterface } from 'src/sync-settings/background/types'
import type { RemoteFunctionRole } from 'src/util/webextensionRPC'
import type {
RemoteFunctionRole,
RemoteFunctionWithExtraArgs,
RemoteFunctionWithoutExtraArgs,
} from 'src/util/webextensionRPC'

export type GenerateServerID = (collectionName: string) => number | string
export interface LocalExtensionSettings {
Expand All @@ -17,30 +21,38 @@ export interface OpenTabParams {
openInSameTab?: boolean
}

export interface RemoteBGScriptInterface {
openOptionsTab: (query: string, params?: OpenTabParams) => Promise<void>
openOverviewTab: (
params?: OpenTabParams & {
export interface RemoteBGScriptInterface<Role extends RemoteFunctionRole> {
openOptionsTab: RemoteFunctionWithoutExtraArgs<
Role,
{ query: string; params?: OpenTabParams }
>
openOverviewTab: RemoteFunctionWithoutExtraArgs<
Role,
OpenTabParams & {
missingPdf?: boolean
selectedSpace?: number
}
>
createCheckoutLink: RemoteFunctionWithoutExtraArgs<
Role,
{
billingPeriod: 'monthly' | 'yearly'
selectedPremiumPlans: PremiumPlans[]
doNotOpen: boolean
},
) => Promise<void>
createCheckoutLink: (
billingPeriod: 'monthly' | 'yearly',
selectedPremiumPlans: PremiumPlans[],
doNotOpen: boolean,
) => Promise<'error' | 'success'>
broadcastListChangeToAllTabs: (
params:
| {
type: 'create'
list: UnifiedList<'user-list' | 'page-link'>
}
| {
type: 'delete'
localListId: number
},
) => Promise<void>
'error' | 'success'
>
broadcastListChangeToAllTabs: RemoteFunctionWithExtraArgs<
Role,
| {
type: 'create'
list: UnifiedList<'user-list' | 'page-link'>
}
| {
type: 'delete'
localListId: number
}
>
}

// TODO: Fill in this type with remaining BG modules
Expand All @@ -53,5 +65,5 @@ export interface BackgroundModuleRemoteInterfaces<
pageActivityIndicator: RemotePageActivityIndicatorInterface
imageSupport: ImageSupportInterface<Role>
syncSettings: RemoteSyncSettingsInterface
bgScript: RemoteBGScriptInterface
bgScript: RemoteBGScriptInterface<Role>
}
8 changes: 4 additions & 4 deletions src/content-scripts/content_script/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export async function main(
// 2. Initialise dependencies required by content scripts
const analyticsBG = runInBackground<AnalyticsCoreInterface>()
const authBG = runInBackground<AuthRemoteFunctionsInterface>()
const bgScriptBG = runInBackground<RemoteBGScriptInterface>()
const bgScriptBG = runInBackground<RemoteBGScriptInterface<'caller'>>()
const pkmSyncBG = runInBackground<PKMSyncBackgroundModule>()
const summarizeBG = runInBackground<SummarizationInterface<'caller'>>()
const annotationsBG = runInBackground<AnnotationInterface<'caller'>>()
Expand Down Expand Up @@ -1797,7 +1797,7 @@ class PageInfo {
export function setupWebUIActions(args: {
contentScriptsBG: ContentScriptsInterface<'caller'>
pageActivityIndicatorBG: RemotePageActivityIndicatorInterface
bgScriptBG: RemoteBGScriptInterface
bgScriptBG: RemoteBGScriptInterface<'caller'>
}) {
const confirmRequest = (requestId: number) => {
const detail: MemexRequestHandledDetail = { requestId }
Expand Down Expand Up @@ -1873,8 +1873,8 @@ export function setupWebUIActions(args: {
export async function injectCustomUIperPage(
annotationsFunctions,
pkmSyncBG,
collectionsBG,
bgScriptBG,
collectionsBG: RemoteCollectionsInterface,
bgScriptBG: RemoteBGScriptInterface<'caller'>,
pageInfo,
inPageUI,
) {
Expand Down
4 changes: 3 additions & 1 deletion src/content-scripts/content_script/in-page-ui-injections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export const main: InPageUIInjectionsMain = async ({
syncSettings,
() =>
searchDisplayProps.bgScriptBG.openOptionsTab(
'settings',
{
query: 'settings',
},
),
)
} catch (err) {
Expand Down
7 changes: 4 additions & 3 deletions src/content-scripts/content_script/injectionUtils/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { RemoteBGScriptInterface } from 'src/background-script/types'
import type { RemoteBGScriptInterface } from 'src/background-script/types'
import type { RemoteCollectionsInterface } from 'src/custom-lists/background/types'
import { findClassElementSWithRetries, renderSpacesBar } from './utils'

export async function injectTelegramCustomUI(
collectionsBG,
bgScriptBG: RemoteBGScriptInterface,
collectionsBG: RemoteCollectionsInterface,
bgScriptBG: RemoteBGScriptInterface<'caller'>,
url: string,
) {
try {
Expand Down
10 changes: 5 additions & 5 deletions src/content-scripts/content_script/injectionUtils/twitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RemoteBGScriptInterface } from 'src/background-script/types'
import type { RemoteBGScriptInterface } from 'src/background-script/types'
import type { RemoteCollectionsInterface } from 'src/custom-lists/background/types'
import { renderSpacesBar } from './utils'
import { RemoteCollectionsInterface } from 'src/custom-lists/background/types'

export async function getActiveTwitterUserName(
maxRetries,
Expand All @@ -17,7 +17,7 @@ export async function getActiveTwitterUserName(

export async function trackTwitterMessageList(
collectionsBG: RemoteCollectionsInterface,
bgScriptBG: RemoteBGScriptInterface,
bgScriptBG: RemoteBGScriptInterface<'caller'>,
) {
const maxRetries = 40
const delayInMilliseconds = 500
Expand Down Expand Up @@ -93,8 +93,8 @@ export async function trackTwitterMessageList(
}

export async function injectTwitterProfileUI(
collectionsBG,
bgScriptBG: RemoteBGScriptInterface,
collectionsBG: RemoteCollectionsInterface,
bgScriptBG: RemoteBGScriptInterface<'caller'>,
url: string,
) {
const maxRetries = 40
Expand Down
6 changes: 3 additions & 3 deletions src/content-scripts/content_script/injectionUtils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnifiedList } from 'src/annotations/cache/types'
import { RemoteBGScriptInterface } from 'src/background-script/types'
import type { UnifiedList } from 'src/annotations/cache/types'
import type { RemoteBGScriptInterface } from 'src/background-script/types'

export async function findClassElementSWithRetries(className, retries, delay) {
return new Promise((resolve, reject) => {
Expand All @@ -23,7 +23,7 @@ export async function findClassElementSWithRetries(className, retries, delay) {

export function renderSpacesBar(
lists: UnifiedList[],
bgScriptBG: RemoteBGScriptInterface,
bgScriptBG: RemoteBGScriptInterface<'caller'>,
url?: string,
) {
let spacesBar: HTMLElement
Expand Down
Loading

0 comments on commit c4dc584

Please sign in to comment.