Skip to content

Commit

Permalink
Cleaned up extra create and on_session fields
Browse files Browse the repository at this point in the history
* Cleaned up delivery_platform, browser_name, browser_version, operating_system, operating_system_version, device_platform
* These parameters are not valid on the api/v1/players endpoint so are unnecessary
* Added NotificationIcons type
  • Loading branch information
jkasten2 committed Sep 24, 2019
1 parent 9da8f9e commit df8ed4d
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 226 deletions.
20 changes: 3 additions & 17 deletions src/bell/Dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import bowser from 'bowser';

import Event from '../Event';
import SdkEnvironment from '../managers/SdkEnvironment';
import { addDomElement, clearDomElementChildren, isChromeLikeBrowser } from '../utils';
import {addDomElement, clearDomElementChildren, getPlatformNotificationIcon} from '../utils';
import AnimatedElement from './AnimatedElement';
import Bell from './Bell';



export default class Dialog extends AnimatedElement {

public bell: Bell;
public subscribeButtonId: string;
public unsubscribeButtonId: string;
public notificationIcons: any;
public notificationIcons: NotificationIcons | null;

constructor(bell: Bell) {
super('.onesignal-bell-launcher-dialog', 'onesignal-bell-launcher-dialog-opened', undefined, 'hidden',
Expand All @@ -25,18 +23,6 @@ export default class Dialog extends AnimatedElement {
this.notificationIcons = null;
}

getPlatformNotificationIcon() {
if (this.notificationIcons) {
if (isChromeLikeBrowser() || bowser.firefox || bowser.msedge) {
return this.notificationIcons.chrome || this.notificationIcons.safari;
}
else if (bowser.safari) {
return this.notificationIcons.safari || this.notificationIcons.chrome;
}
}
else return null;
}

show() {
return this.updateBellLauncherDialogBody()
.then(() => super.show());
Expand Down Expand Up @@ -74,7 +60,7 @@ export default class Dialog extends AnimatedElement {
this.bell.state === Bell.STATES.UNSUBSCRIBED && currentSetSubscription === false) {

let notificationIconHtml = '';
let imageUrl = this.getPlatformNotificationIcon();
let imageUrl = getPlatformNotificationIcon(this.notificationIcons);
if (imageUrl) {
notificationIconHtml = `<div class="push-notification-icon"><img src="${imageUrl}"></div>`
} else {
Expand Down
5 changes: 0 additions & 5 deletions src/models/DevicePlatformKind.ts

This file was deleted.

99 changes: 3 additions & 96 deletions src/models/DeviceRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@ import bowser from 'bowser';
import Environment from '../Environment';
import NotImplementedError from '../errors/NotImplementedError';
import { DeliveryPlatformKind } from './DeliveryPlatformKind';
import { DevicePlatformKind } from './DevicePlatformKind';
import { Serializable } from './Serializable';
import { SubscriptionStateKind } from './SubscriptionStateKind';
import { OneSignalUtils } from "../utils/OneSignalUtils";

export interface FlattenedDeviceRecord {
/* Old Parameters */
device_type: DeliveryPlatformKind;
language: string;
timezone: number;
device_os: number;
sdk: string;
notification_types: SubscriptionStateKind | undefined;

/* New Parameters */
delivery_platform: DeliveryPlatformKind;
browser_name: string;
browser_version: number;
operating_system: string;
operating_system_version: string;
device_platform: DevicePlatformKind;
device_model: string;
// TODO: Make it a required parameter
app_id?: string;
Expand All @@ -38,11 +28,7 @@ export abstract class DeviceRecord implements Serializable {
public deliveryPlatform: DeliveryPlatformKind;
public language: string;
public timezone: number;
public browserName: string;
public browserVersion: number;
public operatingSystem: string;
public operatingSystemVersion: string;
public devicePlatform: DevicePlatformKind;
public deviceModel: string;
public sdkVersion: string;
public appId: string | undefined;
Expand All @@ -53,89 +39,18 @@ export abstract class DeviceRecord implements Serializable {
// this.appId = OneSignal.context.appConfig.appId;
this.language = Environment.getLanguage();
this.timezone = new Date().getTimezoneOffset() * -60;
this.browserName = bowser.name;
this.browserVersion = parseInt(String(bowser.version)) !== NaN ? parseInt(String(bowser.version)) : -1;
this.operatingSystem = this.getBrowserOperatingSystem();
this.operatingSystemVersion = String(bowser.osversion);
this.devicePlatform = this.getDevicePlatform();
const browserVersion = parseInt(String(bowser.version), 10);
this.browserVersion = isNaN(browserVersion) ? -1 : browserVersion;
this.deviceModel = navigator.platform;
this.sdkVersion = Environment.version().toString();
this.deliveryPlatform = this.getDeliveryPlatform();
// Unimplemented properties are appId, subscriptionState, and subscription
}

getDevicePlatform(): DevicePlatformKind {
const isMobile = bowser.mobile;
const isTablet = bowser.tablet;

if (isMobile) {
return DevicePlatformKind.Mobile;
} else if (isTablet) {
return DevicePlatformKind.Tablet;
} else {
return DevicePlatformKind.Desktop;
}
}

isSafari(): boolean {
return bowser.safari && window.safari !== undefined && window.safari.pushNotification !== undefined;
}

getBrowserOperatingSystem(): string {
/*
mac
windows - other than Windows Phone
windowsphone
linux - other than android, chromeos, webos, tizen, and sailfish
chromeos
android
ios - also sets one of iphone/ipad/ipod
blackberry
firefoxos
webos - may also set touchpad
bada
tizen
sailfish
*/
if (bowser.mac) {
return "Mac OS X";
}
if (bowser.windows) {
return "Microsoft Windows";
}
if (bowser.windowsphone) {
return "Microsoft Windows Phone";
}
if (bowser.linux) {
return "Linux";
}
if (bowser.chromeos) {
return "Google Chrome OS";
}
if (bowser.android) {
return "Google Android";
}
if (bowser.ios) {
return "Apple iOS";
}
if (bowser.blackberry) {
return "Blackberry";
}
if (bowser.firefoxos) {
return "Mozilla Firefox OS";
}
if (bowser.webos) {
return "WebOS";
}
if (bowser.tizen) {
return "Tizen";
}
if (bowser.sailfish) {
return "Sailfish OS";
}
return "Unknown";
}

getDeliveryPlatform(): DeliveryPlatformKind {
// For testing purposes, allows changing the browser user agent
const browser = OneSignalUtils.redetectBrowserUserAgent();
Expand All @@ -153,21 +68,13 @@ export abstract class DeviceRecord implements Serializable {

serialize(): FlattenedDeviceRecord {
const serializedBundle: FlattenedDeviceRecord = {
/* Old Parameters */
device_type: this.deliveryPlatform,
language: this.language,
timezone: this.timezone,
device_os: this.browserVersion,
device_model: this.deviceModel,
sdk: this.sdkVersion,
notification_types: this.subscriptionState,
/* New Parameters */
delivery_platform: this.deliveryPlatform,
browser_name: this.browserName,
browser_version: this.browserVersion,
operating_system: this.operatingSystem,
operating_system_version: this.operatingSystemVersion,
device_platform: this.devicePlatform,
device_model: this.deviceModel,
};

if (this.appId) {
Expand Down
5 changes: 5 additions & 0 deletions src/models/NotificationIcons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface NotificationIcons {
chrome?: string;
firefox?: string;
safari?: string;
}
17 changes: 3 additions & 14 deletions src/popover/Popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import bowser from 'bowser';

import Event from '../Event';
import MainHelper from '../helpers/MainHelper';
import { addCssClass, addDomElement, once, removeDomElement, isChromeLikeBrowser } from '../utils';
import {addCssClass, addDomElement, getPlatformNotificationIcon, once, removeDomElement} from '../utils';
import { SlidedownPermissionMessageOptions } from '../models/AppConfig';

export default class Popover {
public options: SlidedownPermissionMessageOptions;
public notificationIcons: any;
public notificationIcons: NotificationIcons | null;

static get EVENTS() {
return {
Expand Down Expand Up @@ -83,18 +83,7 @@ export default class Popover {
}

getPlatformNotificationIcon(): string {
if (!this.notificationIcons)
return 'default-icon';

if (bowser.safari && this.notificationIcons.safari)
return this.notificationIcons.safari;
else if (bowser.firefox && this.notificationIcons.firefox)
return this.notificationIcons.firefox;

return this.notificationIcons.chrome ||
this.notificationIcons.firefox ||
this.notificationIcons.safari ||
'default-icon';
return getPlatformNotificationIcon(this.notificationIcons);
}

get container() {
Expand Down
29 changes: 10 additions & 19 deletions src/service-worker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,27 +499,18 @@ export class ServiceWorker {
* to 14 with requireInteraction and 28 without, we force Mac OS X Chrome
* notifications to be transient.
*/
if (typeof options !== "object") {
return options;
} else {
const clone = {...options};

if (bowser.name === '' && bowser.version === '') {
var browser = (bowser as any)._detect(navigator.userAgent);
} else {
var browser: any = bowser;
}
const clone = { ...options };
const browser = OneSignalUtils.redetectBrowserUserAgent();

if (browser.chrome &&
browser.mac &&
clone) {
clone.requireInteraction = false;
}
if (forcePersistNotifications) {
clone.requireInteraction = true;
}
return clone;
if (browser.chrome &&
browser.mac &&
clone) {
clone.requireInteraction = false;
}
if (forcePersistNotifications) {
clone.requireInteraction = true;
}
return clone;
}

/**
Expand Down
35 changes: 13 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import bowser from 'bowser';

import TimeoutError from './errors/TimeoutError';
import SdkEnvironment from './managers/SdkEnvironment';
import { WindowEnvironmentKind } from './models/WindowEnvironmentKind';
Expand All @@ -9,7 +7,7 @@ import { OneSignalUtils } from './utils/OneSignalUtils';
import { PermissionUtils } from './utils/PermissionUtils';
import { BrowserUtils } from './utils/BrowserUtils';
import { Utils } from "./utils/Utils";

import bowser from 'bowser';

export function isArray(variable: any) {
return Object.prototype.toString.call(variable) === '[object Array]';
Expand All @@ -19,13 +17,6 @@ export function decodeHtmlEntities(text: string) {
return BrowserUtils.decodeHtmlEntities(text);
}

export function isChromeLikeBrowser() {
return bowser.chrome ||
(bowser as any).chromium ||
(bowser as any).opera ||
(bowser as any).yandexbrowser;
}

export function removeDomElement(selector: string) {
var els = document.querySelectorAll(selector);
if (els.length > 0) {
Expand All @@ -38,10 +29,6 @@ export function removeDomElement(selector: string) {
}
}

export function isLocalhostAllowedAsSecureOrigin() {
return OneSignalUtils.isLocalhostAllowedAsSecureOrigin();
}

/**
* Helper method for public APIs that waits until OneSignal is initialized, rejects if push notifications are
* not supported, and wraps these tasks in a Promise.
Expand Down Expand Up @@ -409,13 +396,17 @@ export function incrementSdkLoadCount() {
(<any>window).__oneSignalSdkLoadCount = getSdkLoadCount() + 1;
}

/**
* Returns the email with all whitespace removed and converted to lower case.
*/
export function prepareEmailForHashing(email: string): string {
return email.replace(/\s/g, '').toLowerCase();
}
export function getPlatformNotificationIcon(notificationIcons: NotificationIcons | null): string {
if (!notificationIcons)
return 'default-icon';

if (bowser.safari && notificationIcons.safari)
return notificationIcons.safari;
else if (bowser.firefox && notificationIcons.firefox)
return notificationIcons.firefox;

export function encodeHashAsUriComponent(hash: any): string {
return Utils.encodeHashAsUriComponent(hash);
return notificationIcons.chrome ||
notificationIcons.firefox ||
notificationIcons.safari ||
'default-icon';
}
2 changes: 1 addition & 1 deletion src/utils/OneSignalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class OneSignalUtils {

public static redetectBrowserUserAgent(): IBowser {
// During testing, the browser object may be initialized before the userAgent is injected
if (bowser.name === '' && bowser.version === '') {
if (bowser.name === "" && bowser.version === "") {
return bowser._detect(navigator.userAgent);
}
return bowser;
Expand Down
9 changes: 9 additions & 0 deletions test/support/tester/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ export class AssertInitSDK {
this.firedSDKInitializedPublic = false;
}
}

// PushSubscriptionOptions is a class present in browsers that support the Push API
export function setupBrowserWithPushAPIWithVAPIDEnv(sandbox: SinonSandbox): void {
const classDef = function() {};
classDef.prototype.applicationServerKey = null;
classDef.prototype.userVisibleOnly = null;

sandbox.stub((<any>global), "PushSubscriptionOptions").value(classDef);
}
Loading

0 comments on commit df8ed4d

Please sign in to comment.