diff --git a/app/api/chat.js b/app/api/chat.js index 7c909839213b0c772362262529abc48526f20bc6..ee178c44987e4a9b764080aa58d725e78c6538b6 100644 --- a/app/api/chat.js +++ b/app/api/chat.js @@ -6,6 +6,7 @@ const express = require('express') +const db = require('../service/db') const cloud = require('../service/cloud') const relay = require('../service/relay') @@ -13,13 +14,20 @@ let app = express.Router() // Administration. -app.post('/init', function (req, res) { +app.post('/init', async function (req, res, next) { let id = req.projek.driver.id let token = req.body.token - cloud.register(id, token) + try { + cloud.register(id, token) - res.json({}) + let model = await db.ChatHistory.prepareAsync([id, relay.get(id)]) + res.json({ + chats: model.chats + }) + } catch (e) { + next(e) + } }) app.delete('/', function (req, res) { @@ -38,7 +46,15 @@ app.post('/', async function (req, res, next) { let message = req.body.message try { + let model = await db.ChatHistory.prepareAsync([id, relay.get(id)]) + + model.chats.push({ + owner_id: id, + content: message + }) + await relay.sendAsync(id, message) + await model.save() res.json({}) } catch (e) { diff --git a/app/service/db.js b/app/service/db.js index 0930038c7ca2ffcd38c9e9d551217e7fd4b7496f..6ce5ce691014996a40297115895b0f389925df25 100644 --- a/app/service/db.js +++ b/app/service/db.js @@ -1,66 +1,41 @@ -class ChatData { - constructor () { - this.mongoose = require('mongoose') - this.mongoose.connect('mongodb://localhost/wbd3_chat') +/* + * Tugas Besar 3 WBD - AMEN + */ - this.chatSchema = new this.mongoose.Schema({ - participant_ids: [Number], - chats: [{ - owner_id: Number, - content: String - }] - }) - this.ChatModel = this.mongoose.model('Chat', this.chatSchema) - } - - addChat (senderId, receiverId, content) { - let participantIds = [ senderId, receiverId ] - participantIds.sort() - - let chat = { - owner_id: senderId, - content: content - } +'use strict' - this.ChatModel.findOneAndUpdate( - { participant_ids: participantIds }, - { $push: { chats: chat } }, (err, done) => { - if (err) { - console.error(err) - } +const mongoose = require('mongoose') - if (!done) { - let chat = new this.ChatModel({ - participant_ids: participantIds, - chats: [{ - owner_id: senderId, - content: content - }] - }) +// Connect database. +mongoose.connect(process.env.MONGODB_URI) - chat.save() - } - }) - } +let ChatHistorySchema = mongoose.Schema({ + participant_ids: [Number], + chats: [{ + owner_id: Number, + content: String + }] +}) - findChat (firstParticipantId, secondParticipantId) { - let participantIds = [ firstParticipantId, secondParticipantId ] - participantIds.sort() +let ChatHistory - this.ChatModel.findOne({ participant_ids: participantIds }, function (err, result) { - if (err) { - return console.error(err) - } +ChatHistorySchema.statics.prepareAsync = async function (participantIds) { + participantIds.sort() - if (result) { - let chats = result.chats + let result = await this.findOne({ participant_ids: participantIds }) - chats.forEach(function (chat) { - console.log(chat) - }) - } + if (!result) { + result = new ChatHistory({ + participant_ids: participantIds, + chats: [] }) } + + return result } -module.exports = ChatData +ChatHistory = mongoose.model('ChatHistory', ChatHistorySchema) + +module.exports = { + ChatHistory: ChatHistory +} diff --git a/app/service/relay.js b/app/service/relay.js index 11e739eb5fe41e915226a7173e81a336e60d3735..df5f17c3bf25dd538e84e4ad3baaaa8f0d4759ed 100644 --- a/app/service/relay.js +++ b/app/service/relay.js @@ -21,6 +21,10 @@ module.exports = { delete db[pairId] }, + get (id) { + return db[id] + }, + async sendAsync (id, message) { await cloud.sendAsync(db[id], message) } diff --git a/data/env.conf.example b/data/env.conf.example index d2eae4f87ff80d79f7ef0567b37b23266ab314ab..0d333d3ed748044d6dcb67ba13c8d55f98570e07 100644 --- a/data/env.conf.example +++ b/data/env.conf.example @@ -1,2 +1,4 @@ CHAT_PORT=8081 FIREBASE_CERT=data/firebase.json + +MONGODB_URI=mongodb://localhost/wbd3_chat