mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
Merge pull request #32 from brendan-ch/chore/split-notification-service
[INT-45] chore/split-notification-service
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { NotificationService } from "./services/NotificationService";
|
||||
import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler";
|
||||
import { GetterSetterRepository } from "./repositories/GetterSetterRepository";
|
||||
|
||||
export interface ServerContext {
|
||||
repository: GetterSetterRepository;
|
||||
notificationService: NotificationService;
|
||||
notificationService: ETANotificationScheduler;
|
||||
}
|
||||
|
||||
11
src/index.ts
11
src/index.ts
@@ -5,9 +5,10 @@ import { MergedResolvers } from "./MergedResolvers";
|
||||
import { ServerContext } from "./ServerContext";
|
||||
import { UnoptimizedInMemoryRepository } from "./repositories/UnoptimizedInMemoryRepository";
|
||||
import { TimedApiBasedRepositoryLoader } from "./loaders/TimedApiBasedRepositoryLoader";
|
||||
import { NotificationService } from "./services/NotificationService";
|
||||
import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler";
|
||||
import { configDotenv } from "dotenv";
|
||||
import { loadTestData } from "./loaders/loadTestData";
|
||||
import { AppleNotificationSender } from "./notifications/senders/AppleNotificationSender";
|
||||
|
||||
configDotenv();
|
||||
|
||||
@@ -21,19 +22,19 @@ async function main() {
|
||||
});
|
||||
|
||||
const repository = new UnoptimizedInMemoryRepository();
|
||||
let notificationService: NotificationService;
|
||||
let notificationService: ETANotificationScheduler;
|
||||
if (process.argv.length > 2 && process.argv[2] == "integration-testing") {
|
||||
console.log("Using integration testing setup")
|
||||
await loadTestData(repository);
|
||||
notificationService = new NotificationService(repository, false);
|
||||
const appleNotificationSender = new AppleNotificationSender(false);
|
||||
notificationService = new ETANotificationScheduler(repository, appleNotificationSender);
|
||||
} else {
|
||||
const repositoryDataUpdater = new TimedApiBasedRepositoryLoader(
|
||||
repository
|
||||
);
|
||||
await repositoryDataUpdater.start();
|
||||
notificationService = new NotificationService(repository);
|
||||
notificationService = new ETANotificationScheduler(repository);
|
||||
}
|
||||
notificationService.reloadAPNsTokenIfTimePassed();
|
||||
|
||||
const { url } = await startStandaloneServer(server, {
|
||||
listen: {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { GetterRepository } from "../repositories/GetterRepository";
|
||||
import jwt from "jsonwebtoken";
|
||||
import fs from "fs";
|
||||
import { TupleKey } from "../types/TupleKey";
|
||||
import { IEta } from "../entities/entities";
|
||||
import http2 from "http2";
|
||||
import { GetterRepository } from "../../repositories/GetterRepository";
|
||||
import { TupleKey } from "../../types/TupleKey";
|
||||
import { IEta } from "../../entities/entities";
|
||||
import { AppleNotificationSender, NotificationAlertArguments } from "../senders/AppleNotificationSender";
|
||||
|
||||
export interface ScheduledNotificationData {
|
||||
deviceId: string;
|
||||
@@ -11,26 +9,13 @@ export interface ScheduledNotificationData {
|
||||
stopId: string;
|
||||
}
|
||||
|
||||
interface APNsUrl {
|
||||
fullUrl: string;
|
||||
path: string;
|
||||
host: string;
|
||||
}
|
||||
|
||||
export class NotificationService {
|
||||
export class ETANotificationScheduler {
|
||||
public readonly secondsThresholdForNotificationToFire = 180;
|
||||
|
||||
private apnsToken: string | undefined = undefined;
|
||||
|
||||
private _lastRefreshedTimeMs: number | undefined = undefined;
|
||||
get lastRefreshedTimeMs() {
|
||||
return this._lastRefreshedTimeMs;
|
||||
}
|
||||
|
||||
constructor(private repository: GetterRepository, private shouldActuallySendNotifications = true) {
|
||||
constructor(private repository: GetterRepository,
|
||||
private appleNotificationSender = new AppleNotificationSender()
|
||||
) {
|
||||
this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this);
|
||||
this.reloadAPNsTokenIfTimePassed = this.reloadAPNsTokenIfTimePassed.bind(this);
|
||||
this.lastReloadedTimeForAPNsIsTooRecent = this.lastReloadedTimeForAPNsIsTooRecent.bind(this);
|
||||
this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this);
|
||||
this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this);
|
||||
this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold = this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold.bind(this);
|
||||
@@ -45,48 +30,8 @@ export class NotificationService {
|
||||
*/
|
||||
private deviceIdsToDeliverTo: { [key: string]: Set<string> } = {}
|
||||
|
||||
public reloadAPNsTokenIfTimePassed() {
|
||||
if (this.lastReloadedTimeForAPNsIsTooRecent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keyId = process.env.APNS_KEY_ID;
|
||||
const teamId = process.env.APNS_TEAM_ID;
|
||||
|
||||
const privateKeyBase64 = process.env.APNS_PRIVATE_KEY;
|
||||
if (!privateKeyBase64) return;
|
||||
const privateKey = Buffer.from(privateKeyBase64, 'base64').toString('utf-8');
|
||||
|
||||
const tokenHeader = {
|
||||
alg: "ES256",
|
||||
"kid": keyId,
|
||||
};
|
||||
|
||||
const nowMs = Date.now();
|
||||
const claimsPayload = {
|
||||
"iss": teamId,
|
||||
"iat": Math.ceil(nowMs / 1000), // APNs requires number of seconds since Epoch
|
||||
};
|
||||
|
||||
this.apnsToken = jwt.sign(claimsPayload, privateKey, {
|
||||
algorithm: "ES256",
|
||||
header: tokenHeader
|
||||
});
|
||||
this._lastRefreshedTimeMs = nowMs;
|
||||
}
|
||||
|
||||
private lastReloadedTimeForAPNsIsTooRecent() {
|
||||
const thirtyMinutesMs = 1800000;
|
||||
return this._lastRefreshedTimeMs && Date.now() - this._lastRefreshedTimeMs < thirtyMinutesMs;
|
||||
}
|
||||
|
||||
private async sendEtaNotificationImmediately(notificationData: ScheduledNotificationData): Promise<boolean> {
|
||||
if (!this.shouldActuallySendNotifications) {
|
||||
return true; // pretend that the notification sent
|
||||
}
|
||||
|
||||
const { deviceId, shuttleId, stopId } = notificationData;
|
||||
this.reloadAPNsTokenIfTimePassed();
|
||||
|
||||
const shuttle = await this.repository.getShuttleById(shuttleId);
|
||||
const stop = await this.repository.getStopById(stopId);
|
||||
@@ -107,72 +52,11 @@ export class NotificationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Send the fetch request
|
||||
const bundleId = process.env.APNS_BUNDLE_ID;
|
||||
if (typeof bundleId !== "string") {
|
||||
throw new Error("APNS_BUNDLE_ID environment variable is not set correctly");
|
||||
const notificationAlertArguments: NotificationAlertArguments = {
|
||||
title: "Shuttle is arriving",
|
||||
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`,
|
||||
}
|
||||
|
||||
const { path, host } = NotificationService.getAPNsFullUrlToUse(deviceId);
|
||||
|
||||
const headers = {
|
||||
':method': 'POST',
|
||||
':path': path,
|
||||
'authorization': `bearer ${this.apnsToken}`,
|
||||
"apns-push-type": "alert",
|
||||
"apns-expiration": "0",
|
||||
"apns-priority": "10",
|
||||
"apns-topic": bundleId,
|
||||
};
|
||||
try {
|
||||
const client = http2.connect(host);
|
||||
const req = client.request(headers);
|
||||
req.setEncoding('utf8');
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
req.on('response', (headers, flags) => {
|
||||
if (headers[":status"] !== 200) {
|
||||
reject(`APNs request failed with status ${headers[":status"]}`);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
|
||||
req.write(JSON.stringify({
|
||||
aps: {
|
||||
alert: {
|
||||
title: "Shuttle is arriving",
|
||||
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
|
||||
}
|
||||
}
|
||||
}));
|
||||
req.end();
|
||||
});
|
||||
return true;
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static getAPNsFullUrlToUse(deviceId: string): APNsUrl {
|
||||
// Construct the fetch request
|
||||
const devBaseUrl = "https://api.development.push.apple.com"
|
||||
const prodBaseUrl = "https://api.push.apple.com"
|
||||
|
||||
let hostToUse = devBaseUrl;
|
||||
if (process.env.APNS_IS_PRODUCTION === "1") {
|
||||
hostToUse = prodBaseUrl;
|
||||
}
|
||||
|
||||
const path = "/3/device/" + deviceId;
|
||||
const fullUrl = hostToUse + path;
|
||||
|
||||
const constructedObject = {
|
||||
fullUrl,
|
||||
host: hostToUse,
|
||||
path,
|
||||
}
|
||||
return constructedObject;
|
||||
return this.appleNotificationSender.sendNotificationImmediately(deviceId, notificationAlertArguments);
|
||||
}
|
||||
|
||||
private async etaSubscriberCallback(eta: IEta) {
|
||||
141
src/notifications/senders/AppleNotificationSender.ts
Normal file
141
src/notifications/senders/AppleNotificationSender.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
import http2 from "http2";
|
||||
|
||||
interface APNsUrl {
|
||||
fullUrl: string;
|
||||
path: string;
|
||||
host: string;
|
||||
}
|
||||
|
||||
export interface NotificationAlertArguments {
|
||||
title: string;
|
||||
body: string;
|
||||
}
|
||||
|
||||
export class AppleNotificationSender {
|
||||
private apnsToken: string | undefined = undefined;
|
||||
private _lastRefreshedTimeMs: number | undefined = undefined;
|
||||
|
||||
constructor(private shouldActuallySendNotifications = true) {
|
||||
}
|
||||
|
||||
get lastRefreshedTimeMs(): number | undefined {
|
||||
return this._lastRefreshedTimeMs;
|
||||
}
|
||||
|
||||
private lastReloadedTimeForAPNsIsTooRecent() {
|
||||
const thirtyMinutesMs = 1800000;
|
||||
return this._lastRefreshedTimeMs && Date.now() - this._lastRefreshedTimeMs < thirtyMinutesMs;
|
||||
}
|
||||
|
||||
public reloadAPNsTokenIfTimePassed() {
|
||||
if (this.lastReloadedTimeForAPNsIsTooRecent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keyId = process.env.APNS_KEY_ID;
|
||||
const teamId = process.env.APNS_TEAM_ID;
|
||||
|
||||
const privateKeyBase64 = process.env.APNS_PRIVATE_KEY;
|
||||
if (!privateKeyBase64) return;
|
||||
const privateKey = Buffer.from(privateKeyBase64, 'base64').toString('utf-8');
|
||||
|
||||
const tokenHeader = {
|
||||
alg: "ES256",
|
||||
"kid": keyId,
|
||||
};
|
||||
|
||||
const nowMs = Date.now();
|
||||
const claimsPayload = {
|
||||
"iss": teamId,
|
||||
"iat": Math.ceil(nowMs / 1000), // APNs requires number of seconds since Epoch
|
||||
};
|
||||
|
||||
this.apnsToken = jwt.sign(claimsPayload, privateKey, {
|
||||
algorithm: "ES256",
|
||||
header: tokenHeader
|
||||
});
|
||||
this._lastRefreshedTimeMs = nowMs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a notification immediately.
|
||||
* @param deviceId
|
||||
* @param notificationAlertArguments
|
||||
*
|
||||
* @return Boolean promise indicating whether the
|
||||
* notification was sent successfully.
|
||||
*/
|
||||
public async sendNotificationImmediately(deviceId: string, notificationAlertArguments: NotificationAlertArguments) {
|
||||
if (!this.shouldActuallySendNotifications) {
|
||||
// pretend that the notification sent
|
||||
return true;
|
||||
}
|
||||
|
||||
this.reloadAPNsTokenIfTimePassed();
|
||||
|
||||
const bundleId = process.env.APNS_BUNDLE_ID;
|
||||
if (typeof bundleId !== "string") {
|
||||
throw new Error("APNS_BUNDLE_ID environment variable is not set correctly");
|
||||
}
|
||||
|
||||
const { path, host } = AppleNotificationSender.getAPNsFullUrlToUse(deviceId);
|
||||
|
||||
const headers = {
|
||||
':method': 'POST',
|
||||
':path': path,
|
||||
'authorization': `bearer ${this.apnsToken}`,
|
||||
"apns-push-type": "alert",
|
||||
"apns-expiration": "0",
|
||||
"apns-priority": "10",
|
||||
"apns-topic": bundleId,
|
||||
};
|
||||
try {
|
||||
const client = http2.connect(host);
|
||||
const req = client.request(headers);
|
||||
req.setEncoding('utf8');
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
req.on('response', (headers, flags) => {
|
||||
if (headers[":status"] !== 200) {
|
||||
reject(`APNs request failed with status ${headers[":status"]}`);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
|
||||
req.write(JSON.stringify({
|
||||
aps: {
|
||||
alert: notificationAlertArguments,
|
||||
}
|
||||
}));
|
||||
req.end();
|
||||
});
|
||||
return true;
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static getAPNsFullUrlToUse(deviceId: string): APNsUrl {
|
||||
// Construct the fetch request
|
||||
const devBaseUrl = "https://api.development.push.apple.com"
|
||||
const prodBaseUrl = "https://api.push.apple.com"
|
||||
|
||||
let hostToUse = devBaseUrl;
|
||||
if (process.env.APNS_IS_PRODUCTION === "1") {
|
||||
hostToUse = prodBaseUrl;
|
||||
}
|
||||
|
||||
const path = "/3/device/" + deviceId;
|
||||
const fullUrl = hostToUse + path;
|
||||
|
||||
const constructedObject = {
|
||||
fullUrl,
|
||||
host: hostToUse,
|
||||
path,
|
||||
}
|
||||
return constructedObject;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user