Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"passport": "^0.3.2",
"passport-auth0": "^0.6.0",
"path": "^0.12.7",
"sendbird-nodejs": "^1.0.0",
"serve-static": "^1.11.1",
"winston": "^2.1.1"
},
Expand Down
6 changes: 6 additions & 0 deletions src/config/param-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ export default {
params: {
flightname: flightName
}
},

chat: {
params: {
flightname: flightName
}
}
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const path = require('path');
Promise.promisifyAll(mongoose);

// connect to mongo db
winston.log('info', 'starting api service.');
winston.log('info', 'gomake-api service started');

mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } });
mongoose.connection.on('error', () => {
winston.log('error', `Timestamp: ${new Date()} | ` +
Expand Down
80 changes: 80 additions & 0 deletions src/server/controllers/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

import Flight from '../models/flight';
import contentResponse from '../helpers/APIResponse';
import SendBird from 'sendbird-nodejs';

const API_TOKEN = process.env.GM_SENDBIRD_API_TOKEN;
const CREATE_ERROR = 405;
const CREATE_USERS_NOT_FOUND = 404;
const CREATE_SUCCESS = 200;

function createFlightChannel(req, res) {
const flightName = req.params.flightname.toUpperCase();
const getFlight = getFlightFromFlightName(flightName);
const sb = getSendBirdInstance();

getFlight
.then(getUsersForFlight)
.then((userIds) => {
const options = {
name: flightName,
custom_type: flightName,
user_ids: userIds,
is_distinct: true
};
return sb.groupChannels.create(options);
})
.then((response) => {
if (response) {
const channelUrl = response.channel_url;
res.send(CREATE_SUCCESS, contentResponse({ channel_url: channelUrl }));
} else {
res.sendStatus(CREATE_ERROR);
}
}, (err) => {
console.log(err);
res.sendStatus(err);
});
}

function getFlightFromFlightName(flightName) {
return Flight.getFlightFromFlightName(flightName);
}

function getSendBirdInstance() {
return new SendBird(API_TOKEN);
}

function getUsersForFlight(foundFlight) {
if (foundFlight) {
const userIds = foundFlight.userIds || [];
if (!!!userIds.length) {
return Promise.reject(CREATE_USERS_NOT_FOUND);
}
return Promise.resolve(userIds);
}
return Promise.reject(CREATE_ERROR);
}

function getFlightChannel(req, res) {
const flightName = req.params.flightname.toUpperCase();
getChannelForFlight(flightName)
.then((response) => {
const channels = response.channels;
res.json(contentResponse(channels));
});
}

function getChannelForFlight(flightName) {
const sb = getSendBirdInstance();
const options = {
custom_type: flightName
};
return sb.groupChannels.list(options);
}

export default {
createFlightChannel,
getFlightChannel
};
3 changes: 2 additions & 1 deletion src/server/models/flight.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const FlightSchema = new mongoose.Schema({
launchLocation: mongoose.Schema.Types.GeoJSON,
launchAltitude: Number,
registeredTrackers: Array,
deviceIds: Array
deviceIds: Array,
userIds: Array
}, {
timestamps: true
});
Expand Down
18 changes: 17 additions & 1 deletion src/server/routes/flight.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import validate from 'express-validation';
import telemetryCtrl from '../controllers/telemetry';
import flightInfoCtrl from '../controllers/flight';
import historyCtrl from '../controllers/history';
import chat from '../controllers/chat';
import paramValidation from '../../config/param-validation';
import authentication from '../middleware/authentication';

Expand All @@ -16,7 +17,6 @@ router.route('/:flightname')
router.route('/:flightname')
.post(validate(paramValidation.flightInfo), flightInfoCtrl.postFlightInfo);


/** GET flight telemetry */
router.route('/:flightname/telemetry')
.get(validate(paramValidation.telemetry), telemetryCtrl.getTelemetry);
Expand All @@ -29,4 +29,20 @@ router.route('/:flightname/telemetry')
router.route('/:flightname/history')
.get(validate(paramValidation.history), historyCtrl.getFlightHistory);

/** POST flight chat: create group_channel for flight if does not exist */
router.route('/:flightname/chat')
.post(validate(paramValidation.chat), chat.createFlightChannel);

/** GET flight chat: get group_channel for flight */
router.route('/:flightname/chat')
.get(validate(paramValidation.chat), chat.getFlightChannel);

/** POST flight user ids: create group_channel for flight if does not exist */
router.route('/:flightname/users/:userId')
.post(validate(paramValidation.chat), chat.createFlightChannel);

/** GET flight user ids: get group_channel for flight */
router.route('/:flightname/users/:userId')
.get(validate(paramValidation.chat), chat.getFlightChannel);

export default router;
Empty file added src/server/routes/history.js
Empty file.