-
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
32b732a
commit 615dddd
Showing
5 changed files
with
125 additions
and
44 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
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,53 @@ | ||
import { ServerScope } from 'nano' | ||
|
||
import { getDeviceById, getDevicesByLoginId } from '../db/couchDevices' | ||
import { PushEventRow } from '../db/couchPushEvents' | ||
import { PushSender } from './pushSender' | ||
|
||
/** | ||
* Handles all the effects once a row has been triggered. | ||
*/ | ||
export async function triggerEvent( | ||
connection: ServerScope, | ||
sender: PushSender, | ||
eventRow: PushEventRow, | ||
date: Date | ||
): Promise<void> { | ||
const { event } = eventRow | ||
const { broadcastTxs = [], pushMessage } = event | ||
|
||
if (pushMessage != null) { | ||
const deviceRows = | ||
event.deviceId != null | ||
? [await getDeviceById(connection, event.deviceId, date)] | ||
: event.loginId != null | ||
? await getDevicesByLoginId(connection, event.loginId) | ||
: [] | ||
|
||
// Sort the devices by app: | ||
const apiKeys = new Map<string, string[]>() | ||
for (const row of deviceRows) { | ||
const { apiKey, deviceToken } = row.device | ||
if (apiKey == null || deviceToken == null) continue | ||
const tokens = apiKeys.get(apiKey) ?? [] | ||
tokens.push(deviceToken) | ||
apiKeys.set(apiKey, tokens) | ||
} | ||
|
||
for (const [apiKey, tokens] of apiKeys) { | ||
await sender.send(apiKey, tokens, pushMessage) | ||
} | ||
|
||
// TODO: Take note of any errors. | ||
event.pushMessageError = undefined | ||
} | ||
|
||
for (const tx of broadcastTxs) { | ||
console.log(tx) // TODO | ||
event.broadcastTxErrors = [] | ||
} | ||
|
||
event.state = event.recurring ? 'waiting' : 'triggered' | ||
event.triggered = date | ||
await eventRow.save() | ||
} |