-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cea6b1
commit d89f1fc
Showing
11 changed files
with
392 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { asMaybe } from 'cleaners' | ||
import { Serverlet } from 'serverlet' | ||
|
||
import { getApiKeyByKey } from '../db/couchApiKeys' | ||
import { getDeviceById } from '../db/couchDevices' | ||
import { asPushRequestBody } from '../types/pushApiTypes' | ||
import { DbRequest, DeviceRequest } from '../types/requestTypes' | ||
import { errorResponse } from '../types/responseTypes' | ||
|
||
/** | ||
* Parses the request payload and looks up the device. | ||
* Legacy routes do not use this one. | ||
*/ | ||
export const withDevice = | ||
(server: Serverlet<DeviceRequest>): Serverlet<DbRequest> => | ||
async request => { | ||
const { connection, date, log, req } = request | ||
|
||
// Parse the common request body: | ||
const body = asMaybe(asPushRequestBody)(req.body) | ||
if (body == null) { | ||
return errorResponse('Bad request body', { status: 400 }) | ||
} | ||
|
||
// Look up the key in the database: | ||
const apiKey = await log.debugTime( | ||
'getApiKeyByKey', | ||
getApiKeyByKey(connection, body.apiKey) | ||
) | ||
if (apiKey == null) { | ||
return errorResponse('Incorrect API key', { status: 401 }) | ||
} | ||
|
||
// Look up the device in the database: | ||
const deviceRow = await log.debugTime( | ||
'getDeviceById', | ||
getDeviceById(connection, body.deviceId, body.appId, date) | ||
) | ||
|
||
// Pass that along: | ||
return await server({ | ||
...request, | ||
apiKey, | ||
appId: body.appId, | ||
deviceRow, | ||
payload: body.data | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,87 @@ | ||
import { asMaybe, uncleaner } from 'cleaners' | ||
import { ServerScope } from 'nano' | ||
import { Serverlet } from 'serverlet' | ||
|
||
import { PushEvent } from '../types/pushTypes' | ||
import { ApiRequest } from '../types/requestTypes' | ||
import { jsonResponse } from '../types/responseTypes' | ||
import { getEventsByDeviceId } from '../db/couchPushEvents' | ||
import { | ||
asDeviceCheckinPayload, | ||
asDevicePayload, | ||
asDeviceUpdatePayload | ||
} from '../types/pushApiTypes' | ||
import { Device } from '../types/pushTypes' | ||
import { DeviceRequest } from '../types/requestTypes' | ||
import { errorResponse, jsonResponse } from '../types/responseTypes' | ||
|
||
const wasDevicePayload = uncleaner(asDevicePayload) | ||
type DevicePayload = ReturnType<typeof asDevicePayload> | ||
|
||
/** | ||
* POST /v2/device | ||
*/ | ||
export const deviceFetchRoute: Serverlet<ApiRequest> = async request => { | ||
return jsonResponse({}) | ||
} | ||
export interface DevicePayload { | ||
deviceId: string | ||
deviceToken: string | ||
events: PushEvent[] | ||
rootLoginIds: Uint8Array[] // asArray(asBase64) | ||
created: Date | ||
visited: Date | ||
export const deviceFetchRoute: Serverlet<DeviceRequest> = async request => { | ||
const { connection, deviceRow } = request | ||
|
||
return jsonResponse( | ||
wasDevicePayload(await makeDevicePayload(connection, deviceRow.device)) | ||
) | ||
} | ||
|
||
/** | ||
* POST /v2/device/checkin | ||
*/ | ||
export const deviceCheckinRoute: Serverlet<ApiRequest> = async request => { | ||
return jsonResponse({}) | ||
export const deviceCheckinRoute: Serverlet<DeviceRequest> = async request => { | ||
const { connection, date, deviceRow, payload } = request | ||
|
||
const clean = asMaybe(asDeviceCheckinPayload)(payload) | ||
if (clean == null) { | ||
return errorResponse('Incorrect device checkin payload', { status: 400 }) | ||
} | ||
|
||
await deviceRow.updateStatus({ | ||
deviceToken: clean.deviceToken, | ||
visited: date | ||
}) | ||
|
||
return jsonResponse( | ||
wasDevicePayload(await makeDevicePayload(connection, deviceRow.device)) | ||
) | ||
} | ||
|
||
/** | ||
* POST /v2/device/update | ||
*/ | ||
export const deviceUpdateRoute: Serverlet<ApiRequest> = async request => { | ||
return jsonResponse({}) | ||
export const deviceUpdateRoute: Serverlet<DeviceRequest> = async request => { | ||
const { connection, date, deviceRow, payload } = request | ||
|
||
const clean = asMaybe(asDeviceUpdatePayload)(payload) | ||
if (clean == null) { | ||
return errorResponse('Incorrect device update payload', { status: 400 }) | ||
} | ||
|
||
await deviceRow.updateStatus({ | ||
deviceToken: clean.deviceToken, | ||
rootLoginIds: clean.rootLoginIds, | ||
visited: date | ||
}) | ||
|
||
// TODO: | ||
// clean.createEvents | ||
// clean.removeEvents | ||
|
||
return jsonResponse( | ||
wasDevicePayload(await makeDevicePayload(connection, deviceRow.device)) | ||
) | ||
} | ||
export interface DeviceUpdatePayload { | ||
rootLoginIds: Uint8Array[] // asArray(asBase64) | ||
events: PushEvent[] | ||
deviceToken: string | ||
|
||
async function makeDevicePayload( | ||
connection: ServerScope, | ||
device: Device | ||
): Promise<DevicePayload> { | ||
const eventRows = await getEventsByDeviceId(connection, device.deviceId) | ||
|
||
return { | ||
deviceToken: device.deviceToken, | ||
rootLoginIds: device.rootLoginIds, | ||
events: eventRows.map(row => row.event) | ||
} | ||
} |
Oops, something went wrong.