From 7bf3673a800495e2a8038627d46db4989fc3bd27 Mon Sep 17 00:00:00 2001 From: Daniel <nieltansah@gmail.com> Date: Fri, 24 Nov 2017 03:18:20 +0700 Subject: [PATCH] app: Add ability to store chats --- app/api/chat.js | 22 ++++++++++-- app/service/db.js | 83 +++++++++++++++---------------------------- app/service/relay.js | 4 +++ data/env.conf.example | 2 ++ 4 files changed, 54 insertions(+), 57 deletions(-) diff --git a/app/api/chat.js b/app/api/chat.js index 7c90983..ee178c4 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 0930038..6ce5ce6 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 11e739e..df5f17c 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 d2eae4f..0d333d3 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 -- GitLab