Devices can now subscribe to specific topics.

- Device can now become (in)active
- Error alert makes sound
- Alerts now execute function on click
This commit is contained in:
2020-11-14 21:15:05 +01:00
parent ab1b90d020
commit 8b54431449
20 changed files with 171 additions and 42 deletions

View File

@@ -1,9 +1,11 @@
import { Response } from "express";
import { logger } from "../app";
import { logger, rabbitmq } from "../app";
import { LivebeatRequest } from "../lib/request";
import { Beat } from "../models/beat/beat.model.";
import { Phone } from "../models/phone/phone.model";
export async function GetPhone(req: LivebeatRequest, res: Response) {
const phoneId: String = req.params['id'];
@@ -16,7 +18,7 @@ export async function GetPhone(req: LivebeatRequest, res: Response) {
// Check database for phone
const phone = await Phone.findOne({ androidId: phoneId, user: req.user?._id });
if (phone === undefined) {
if (phone === null) {
res.status(404).send();
return;
}
@@ -53,7 +55,7 @@ export async function PostPhone(req: LivebeatRequest, res: Response) {
}
// Create phone
await Phone.create({
const newPhone = await Phone.create({
androidId,
displayName,
modelName,
@@ -63,7 +65,8 @@ export async function PostPhone(req: LivebeatRequest, res: Response) {
active: false
});
logger.info(`New device (${displayName}) registered for ${req.user?.name}.`)
logger.info(`New device (${displayName}) registered for ${req.user?.name}.`);
rabbitmq.publish(req.user?.id, newPhone.toJSON(), 'phone_register')
res.status(200).send();
}

View File

@@ -232,7 +232,7 @@ export async function Resource(req: Request, res: Response) {
return;
}
// TODO: This has to change if we want to allow users to see the realtime movement of others.
// TODO: This has to change if we want to allow users to see the realtime movement of others.
if (resource.toString().startsWith('tracker-') && resource != 'tracker-' + username) {
res.status(200).send('deny');
return;
@@ -242,6 +242,34 @@ export async function Resource(req: Request, res: Response) {
}
export async function Topic(req: Request, res: Response) {
res.status(200);
const username = req.query.username;
const routingKey = req.query.routing_key;
if (routingKey === undefined || username === undefined) {
res.send('deny');
return;
}
// Check if it's us
if (username.toString() == 'backend') {
res.status(200).send('allow');
return;
}
// Check if user exists
const user = await User.findOne({ name: username.toString() });
if (user === null) {
res.send('deny');
return;
}
if (routingKey !== user.id) {
res.send('deny');
return;
}
res.status(200).send('allow');
}