Skip to content

Commit 04d89c1

Browse files
committed
Address comments
1 parent 2616ccc commit 04d89c1

17 files changed

+102
-89
lines changed

config/webpack.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
options: { esModules: true }
5151
},
5252
enforce: 'post',
53-
exclude: [/\.test\.ts$/, /test(ing)?\//]
53+
exclude: [/\.test\.ts$/, /\btest(ing)?\//]
5454
},
5555
{
5656
test: /\.js$/,

packages/messaging-types/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import {
2424
CompleteFn
2525
} from '@firebase/util';
2626

27-
export class FirebaseMessaging {
28-
private constructor(app: FirebaseApp);
27+
export interface FirebaseMessaging {
2928
readonly app: FirebaseApp;
3029
// TODO: remove the token parameter and just delete the token that matches
3130
// this app if it exists.

packages/messaging/src/controllers/sw-controller.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import {
3434
import {
3535
FCM_MSG,
3636
DEFAULT_VAPID_KEY,
37-
FN_CAMPAIGN_ID,
38-
FN_CAMPAIGN_NAME,
39-
FN_CAMPAIGN_TIME,
40-
FN_CAMPAIGN_ANALYTICS_ENABLED
37+
CONSOLE_CAMPAIGN_ID,
38+
CONSOLE_CAMPAIGN_NAME,
39+
CONSOLE_CAMPAIGN_TIME,
40+
CONSOLE_CAMPAIGN_ANALYTICS_ENABLED
4141
} from '../util/constants';
4242
import { dbSet } from '../helpers/idb-manager';
4343
import { getFakeTokenDetails } from '../testing/fakes/token-details';
@@ -440,10 +440,10 @@ describe('SwController', () => {
440440
delete NOTIFICATION_CLICK_PAYLOAD.notification!.data![FCM_MSG].fcmOptions;
441441
// Add FN data.
442442
NOTIFICATION_CLICK_PAYLOAD.notification!.data![FCM_MSG].data = {
443-
[FN_CAMPAIGN_ID]: '123456',
444-
[FN_CAMPAIGN_NAME]: 'Campaign Name',
445-
[FN_CAMPAIGN_TIME]: '1234567890',
446-
[FN_CAMPAIGN_ANALYTICS_ENABLED]: '1'
443+
[CONSOLE_CAMPAIGN_ID]: '123456',
444+
[CONSOLE_CAMPAIGN_NAME]: 'Campaign Name',
445+
[CONSOLE_CAMPAIGN_TIME]: '1234567890',
446+
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]: '1'
447447
};
448448

449449
const matchAllSpy = spy(self.clients, 'matchAll');

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,12 +23,13 @@ import {
2323
MessagePayload,
2424
NotificationDetails
2525
} from '../interfaces/message-payload';
26-
import { FCM_MSG, FN_CAMPAIGN_ID, DEFAULT_VAPID_KEY } from '../util/constants';
26+
import { FCM_MSG, DEFAULT_VAPID_KEY } from '../util/constants';
2727
import { MessageType, InternalMessage } from '../interfaces/internal-message';
2828
import { dbGet } from '../helpers/idb-manager';
2929
import { Unsubscribe } from '@firebase/util';
3030
import { sleep } from '../helpers/sleep';
3131
import { FirebaseApp } from '@firebase/app-types';
32+
import { isConsoleMessage } from '../helpers/is-console-message';
3233

3334
// Let TS know that this is a service worker
3435
declare const self: ServiceWorkerGlobalScope;
@@ -39,10 +40,6 @@ export class SwController implements FirebaseMessaging {
3940
private vapidKey: string | null = null;
4041
private bgMessageHandler: BgMessageHandler | null = null;
4142

42-
get app(): FirebaseApp {
43-
return this.firebaseDependencies.app;
44-
}
45-
4643
constructor(
4744
private readonly firebaseDependencies: FirebaseInternalDependencies
4845
) {
@@ -57,6 +54,10 @@ export class SwController implements FirebaseMessaging {
5754
});
5855
}
5956

57+
get app(): FirebaseApp {
58+
return this.firebaseDependencies.app;
59+
}
60+
6061
/**
6162
* Calling setBackgroundMessageHandler will opt in to some specific
6263
* behaviours.
@@ -355,7 +356,7 @@ function getLink(payload: MessagePayload): string | null {
355356
return link;
356357
}
357358

358-
if (payload.data?.[FN_CAMPAIGN_ID]) {
359+
if (isConsoleMessage(payload.data)) {
359360
// Notification created in the Firebase Console. Redirect to origin.
360361
return self.location.origin;
361362
} else {

packages/messaging/src/controllers/window-controller.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import {
2727
DEFAULT_VAPID_KEY,
2828
DEFAULT_SW_SCOPE,
2929
DEFAULT_SW_PATH,
30-
FN_CAMPAIGN_ANALYTICS_ENABLED,
31-
FN_CAMPAIGN_ID,
32-
FN_CAMPAIGN_NAME,
33-
FN_CAMPAIGN_TIME
30+
CONSOLE_CAMPAIGN_ANALYTICS_ENABLED,
31+
CONSOLE_CAMPAIGN_ID,
32+
CONSOLE_CAMPAIGN_NAME,
33+
CONSOLE_CAMPAIGN_TIME
3434
} from '../util/constants';
3535
import { Stub, Spy } from '../testing/sinon-types';
3636
import '../testing/setup';
@@ -413,10 +413,10 @@ describe('WindowController', () => {
413413

414414
it('calls analytics.logEvent if the message has analytics enabled for PUSH_RECEIVED', async () => {
415415
const data = {
416-
[FN_CAMPAIGN_ID]: '123456',
417-
[FN_CAMPAIGN_NAME]: 'Campaign Name',
418-
[FN_CAMPAIGN_TIME]: '1234567890',
419-
[FN_CAMPAIGN_ANALYTICS_ENABLED]: '1'
416+
[CONSOLE_CAMPAIGN_ID]: '123456',
417+
[CONSOLE_CAMPAIGN_NAME]: 'Campaign Name',
418+
[CONSOLE_CAMPAIGN_TIME]: '1234567890',
419+
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]: '1'
420420
};
421421
const message: InternalMessage = {
422422
firebaseMessaging: {
@@ -439,9 +439,9 @@ describe('WindowController', () => {
439439
'notification_foreground',
440440
{
441441
/* eslint-disable camelcase */
442-
message_id: data[FN_CAMPAIGN_ID],
443-
message_name: data[FN_CAMPAIGN_NAME],
444-
message_time: data[FN_CAMPAIGN_TIME],
442+
message_id: data[CONSOLE_CAMPAIGN_ID],
443+
message_name: data[CONSOLE_CAMPAIGN_NAME],
444+
message_time: data[CONSOLE_CAMPAIGN_TIME],
445445
message_device_time: clock.now
446446
/* eslint-enable camelcase */
447447
}
@@ -450,10 +450,10 @@ describe('WindowController', () => {
450450

451451
it('calls analytics.logEvent if the message has analytics enabled for NOTIFICATION_CLICKED', async () => {
452452
const data = {
453-
[FN_CAMPAIGN_ID]: '123456',
454-
[FN_CAMPAIGN_NAME]: 'Campaign Name',
455-
[FN_CAMPAIGN_TIME]: '1234567890',
456-
[FN_CAMPAIGN_ANALYTICS_ENABLED]: '1'
453+
[CONSOLE_CAMPAIGN_ID]: '123456',
454+
[CONSOLE_CAMPAIGN_NAME]: 'Campaign Name',
455+
[CONSOLE_CAMPAIGN_TIME]: '1234567890',
456+
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]: '1'
457457
};
458458
const message: InternalMessage = {
459459
firebaseMessaging: {
@@ -472,9 +472,9 @@ describe('WindowController', () => {
472472
expect(onMessageSpy).not.to.have.been.called;
473473
expect(logEventSpy).to.have.been.calledOnceWith('notification_open', {
474474
/* eslint-disable camelcase */
475-
message_id: data[FN_CAMPAIGN_ID],
476-
message_name: data[FN_CAMPAIGN_NAME],
477-
message_time: data[FN_CAMPAIGN_TIME],
475+
message_id: data[CONSOLE_CAMPAIGN_ID],
476+
message_name: data[CONSOLE_CAMPAIGN_NAME],
477+
message_time: data[CONSOLE_CAMPAIGN_TIME],
478478
message_device_time: clock.now
479479
/* eslint-enable camelcase */
480480
});

packages/messaging/src/controllers/window-controller.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -22,15 +22,17 @@ import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2222
import { NextFn, Observer, Unsubscribe } from '@firebase/util';
2323
import { InternalMessage, MessageType } from '../interfaces/internal-message';
2424
import {
25-
FN_CAMPAIGN_ID,
26-
FN_CAMPAIGN_ANALYTICS_ENABLED,
27-
FN_CAMPAIGN_NAME,
28-
FN_CAMPAIGN_TIME,
25+
CONSOLE_CAMPAIGN_ID,
26+
CONSOLE_CAMPAIGN_ANALYTICS_ENABLED,
27+
CONSOLE_CAMPAIGN_NAME,
28+
CONSOLE_CAMPAIGN_TIME,
2929
DEFAULT_SW_PATH,
3030
DEFAULT_SW_SCOPE,
3131
DEFAULT_VAPID_KEY
3232
} from '../util/constants';
3333
import { FirebaseApp } from '@firebase/app-types';
34+
import { ConsoleMessageData } from '../interfaces/message-payload';
35+
import { isConsoleMessage } from '../helpers/is-console-message';
3436

3537
export class WindowController implements FirebaseMessaging {
3638
private vapidKey: string | null = null;
@@ -188,11 +190,9 @@ export class WindowController implements FirebaseMessaging {
188190

189191
const { data } = payload;
190192
if (
191-
data &&
192-
FN_CAMPAIGN_ID in data &&
193-
data[FN_CAMPAIGN_ANALYTICS_ENABLED] === '1'
193+
isConsoleMessage(data) &&
194+
data[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED] === '1'
194195
) {
195-
// This message has a campaign id, meaning it was sent using the FN Console.
196196
// Analytics is enabled on this message, so we should log it.
197197
await this.logEvent(type, data);
198198
}
@@ -206,9 +206,9 @@ export class WindowController implements FirebaseMessaging {
206206
const analytics = await this.firebaseDependencies.analyticsProvider.get();
207207
analytics.logEvent(eventType, {
208208
/* eslint-disable camelcase */
209-
message_id: data[FN_CAMPAIGN_ID],
210-
message_name: data[FN_CAMPAIGN_NAME],
211-
message_time: data[FN_CAMPAIGN_TIME],
209+
message_id: data[CONSOLE_CAMPAIGN_ID],
210+
message_name: data[CONSOLE_CAMPAIGN_NAME],
211+
message_time: data[CONSOLE_CAMPAIGN_TIME],
212212
message_device_time: Math.floor(Date.now() / 1000)
213213
/* eslint-enable camelcase */
214214
});
@@ -225,11 +225,3 @@ function getEventType(messageType: MessageType): string {
225225
throw new Error();
226226
}
227227
}
228-
229-
/** Additional data of a message sent from the FN Console. */
230-
interface ConsoleMessageData {
231-
[FN_CAMPAIGN_ID]: string;
232-
[FN_CAMPAIGN_TIME]: string;
233-
[FN_CAMPAIGN_NAME]?: string;
234-
[FN_CAMPAIGN_ANALYTICS_ENABLED]?: '1';
235-
}

packages/messaging/src/core/api.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2019 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
1717

1818
import { ErrorCode, ERROR_FACTORY } from '../util/errors';
1919
import { DEFAULT_VAPID_KEY, ENDPOINT } from '../util/constants';
20-
import { TokenDetails } from '../interfaces/token-details';
20+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
2121
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
22-
import { SubscriptionOptions } from '../interfaces/subscription-options';
2322
import { AppConfig } from '../interfaces/app-config';
2423

2524
export interface ApiResponse {

packages/messaging/src/core/token-management.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { dbGet, dbSet } from '../helpers/idb-manager';
2828
import * as apiModule from './api';
2929
import { Stub } from '../testing/sinon-types';
3030
import { getFakeTokenDetails } from '../testing/fakes/token-details';
31-
import { TokenDetails } from '../interfaces/token-details';
31+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
3232
import { arrayToBase64 } from '../helpers/array-to-base64';
33-
import { SubscriptionOptions } from '../interfaces/subscription-options';
3433

3534
describe('Token Management', () => {
3635
let tokenDetails: TokenDetails;

packages/messaging/src/core/token-management.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import { dbGet, dbSet, dbRemove } from '../helpers/idb-manager';
1919
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
20-
import { SubscriptionOptions } from '../interfaces/subscription-options';
21-
import { TokenDetails } from '../interfaces/token-details';
20+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
2221
import { requestUpdateToken, requestGetToken, requestDeleteToken } from './api';
2322
import { arrayToBase64 } from '../helpers/array-to-base64';
2423
import { ERROR_FACTORY, ErrorCode } from '../util/errors';

packages/messaging/src/helpers/array-to-base64.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

packages/messaging/src/helpers/idb-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const OBJECT_STORE_NAME = 'firebase-messaging-store';
2828
let dbPromise: Promise<DB> | null = null;
2929
function getDbPromise(): Promise<DB> {
3030
if (!dbPromise) {
31-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
31+
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => {
3232
// We don't use 'break' in this switch statement, the fall-through
3333
// behavior is what we want, because if there are multiple versions between
3434
// the old version and the current version, we want ALL the migrations
3535
// that correspond to those versions to run, not only the last one.
3636
// eslint-disable-next-line default-case
37-
switch (upgradeDB.oldVersion) {
37+
switch (upgradeDb.oldVersion) {
3838
case 0:
39-
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
39+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
4040
}
4141
});
4242
}

packages/messaging/src/interfaces/subscription-options.ts renamed to packages/messaging/src/helpers/is-console-message.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
/**
19-
* Additional options and values required by a Push API subscription.
20-
*/
21-
export interface SubscriptionOptions {
22-
vapidKey: string;
23-
swScope: string;
24-
endpoint: string;
25-
auth: string;
26-
p256dh: string;
18+
import { ConsoleMessageData } from '../interfaces/message-payload';
19+
import { CONSOLE_CAMPAIGN_ID } from '../util/constants';
20+
21+
export function isConsoleMessage(data: unknown): data is ConsoleMessageData {
22+
// This message has a campaign ID, meaning it was sent using the
23+
// Firebase Console.
24+
return typeof data === 'object' && !!data && CONSOLE_CAMPAIGN_ID in data;
2725
}

packages/messaging/src/interfaces/message-payload.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
import {
19+
CONSOLE_CAMPAIGN_ID,
20+
CONSOLE_CAMPAIGN_TIME,
21+
CONSOLE_CAMPAIGN_NAME,
22+
CONSOLE_CAMPAIGN_ANALYTICS_ENABLED
23+
} from '../util/constants';
24+
1825
export interface NotificationDetails extends NotificationOptions {
1926
title: string;
2027
click_action?: string; // eslint-disable-line camelcase
@@ -27,5 +34,13 @@ export interface FcmOptions {
2734
export interface MessagePayload {
2835
fcmOptions?: FcmOptions;
2936
notification?: NotificationDetails;
30-
data?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
37+
data?: unknown;
38+
}
39+
40+
/** Additional data of a message sent from the FN Console. */
41+
export interface ConsoleMessageData {
42+
[CONSOLE_CAMPAIGN_ID]: string;
43+
[CONSOLE_CAMPAIGN_TIME]: string;
44+
[CONSOLE_CAMPAIGN_NAME]?: string;
45+
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]?: '1';
3146
}

packages/messaging/src/interfaces/token-details.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { SubscriptionOptions } from './subscription-options';
19-
2018
export interface TokenDetails {
2119
token: string;
2220
createTime: number;
2321
/** Does not exist in Safari since it's not using Push API. */
2422
subscriptionOptions?: SubscriptionOptions;
2523
}
24+
25+
/**
26+
* Additional options and values required by a Push API subscription.
27+
*/
28+
export interface SubscriptionOptions {
29+
vapidKey: string;
30+
swScope: string;
31+
endpoint: string;
32+
auth: string;
33+
p256dh: string;
34+
}

0 commit comments

Comments
 (0)