diff --git a/app/models/ConvModl.js b/app/models/ConvModl.js index 65a2351..1346be9 100644 --- a/app/models/ConvModl.js +++ b/app/models/ConvModl.js @@ -4,9 +4,7 @@ module.exports = mongoose.model('Conv', { titre: { type: String, default: "Conversation" - }, - started: { - type: Date, - default: Date.now } + // TODO Visibilité (brouillon) + // TODO Répertoire }); diff --git a/app/models/MembreModl.js b/app/models/MembreModl.js index 2f4fb07..7a28b42 100644 --- a/app/models/MembreModl.js +++ b/app/models/MembreModl.js @@ -5,7 +5,7 @@ module.exports = mongoose.model('Membre', { type: String, default: 'login' }, - section: { + section: { // TODO From /etc/groups type: String, default: 'IMA' }, diff --git a/app/models/MessModl.js b/app/models/MessModl.js new file mode 100644 index 0000000..d43573c --- /dev/null +++ b/app/models/MessModl.js @@ -0,0 +1,19 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Mess', { + content: { + type: String, + default: "Contenu du message" + }, + login: { + type: String, + default: 'login' + }, + conv: { + type: String, + }, + date: { + type: Date, + default: Date.now + } +}); diff --git a/app/routes/ApiRtes.js b/app/routes/ApiRtes.js index 127330c..d82012f 100644 --- a/app/routes/ApiRtes.js +++ b/app/routes/ApiRtes.js @@ -2,12 +2,13 @@ var MembresServ = require('../services/MembresServ'); var SessionsServ = require('../services/SessionsServ'); var DecryptServ = require('../services/DecryptServ'); var ConvsServ = require('../services/ConvsServ'); +var MessServ = require('../services/MessServ'); var express = require('express'); var api = express(); // Authentication -requireAuth = function () { +reqAuth = function () { return function (req, res, next) { SessionsServ.use(req.cookies.session, function (err) { if (err) { @@ -24,18 +25,49 @@ requireAuth = function () { }; }; -requirePerm = function (perm) { +reqVerified = function (verify) { return function (req, res, next) { - requireAuth()(req, res, function () { - if (req.session[perm]) { - next(); - } else { - res.status(403).end(); - } + reqAuth()(req, res, function () { + verify(req, res, function (err, verified) { + if (err) { + res.status(500).send(err); + } else { + if (verified) { + next(); + } else { + res.status(403); + } + } + }); + }); + }; +}; + +reqPerm = function (perm) { + return reqVerified(function (req, res, cb) { + cb(null, req.session[perm]); + }); +}; + +assert = function (test) { + return function (req, res, next) { + reqAuth()(req, res, function () { + test(req, res, function (err, verified) { + if (err) { + res.status(500).send(err); + } else { + if (verified) { + next(); + } else { + res.status(400); + } + } + }); }); }; }; + // Sessions api.get('/session', function (req, res) { // Informations sur la session if (req.cookies && req.cookies.session) { @@ -87,7 +119,7 @@ api.get('/membres', function (req, res) { // Liste des membres }); }); -api.post('/membres', requirePerm('canAddMembre'), function (req, res) { // Ajout d'un membre +api.post('/membres', reqPerm('canAddMembre'), function (req, res) { // Ajout d'un membre MembresServ.add(req.body, function (err, membre) { if (err) res.send(err); @@ -96,7 +128,7 @@ api.post('/membres', requirePerm('canAddMembre'), function (req, res) { // Ajout }); }); -api.delete('/membres/:membre_id', requirePerm('canDelMembre'), function (req, res) { // Supression d'un membre +api.delete('/membres/:membre_id', reqPerm('canDelMembre'), function (req, res) { // Supression d'un membre MembresServ.remove(req.params.membre_id, function (err, membre) { if (err) res.send(err); @@ -125,7 +157,7 @@ api.get('/convs/:conv_id', function (req, res) { // Une conv }); }); -api.post('/convs', requirePerm('canAddConv'), function (req, res) { // Ajout d'un conv +api.post('/convs', reqPerm('canAddConv'), function (req, res) { // Ajout d'un conv ConvsServ.add(req.body, function (err, conv) { if (err) res.send(err); @@ -134,7 +166,7 @@ api.post('/convs', requirePerm('canAddConv'), function (req, res) { // Ajout d'u }); }); -api.delete('/convs/:conv_id', requirePerm('canDelConv'), function (req, res) { // Supression d'un conv +api.delete('/convs/:conv_id', reqPerm('canDelConv'), function (req, res) { // Supression d'un conv ConvsServ.remove(req.params.conv_id, function (err, conv) { if (err) res.send(err); @@ -144,8 +176,8 @@ api.delete('/convs/:conv_id', requirePerm('canDelConv'), function (req, res) { / }); // Messages -api.get('/messs', function (req, res) { // Liste des messs - MessServ.list(function (err, messs) { +api.get('/messs/:conv_id', function (req, res) { // Liste des messs + MessServ.list(req.params.conv_id, function (err, messs) { if (err) res.send(err); else @@ -162,8 +194,10 @@ api.get('/messs/:mess_id', function (req, res) { // Une mess }); }); -api.post('/messs', requireAuth(), function (req, res) { // Ajout d'un mess - MessServ.add(req.body, function (err, mess) { +api.post('/messs', reqAuth(), function (req, res) { // Ajout d'un mess + data = req.body; + data.login = req.session.login; + MessServ.add(data, function (err, mess) { if (err) res.send(err); else @@ -171,7 +205,7 @@ api.post('/messs', requireAuth(), function (req, res) { // Ajout d'un mess }); }); -api.delete('/messs/:mess_id', requireAuth(), function (req, res) { // Supression d'un mess +api.delete('/messs/:mess_id', reqAuth(), function (req, res) { // Supression d'un mess MessServ.remove(req.params.mess_id, function (err, mess) { if (err) res.send(err); @@ -180,4 +214,7 @@ api.delete('/messs/:mess_id', requireAuth(), function (req, res) { // Supression }); }); +// TODO 404 +// TODO 418 + module.exports = api; diff --git a/app/services/ConvsServ.js b/app/services/ConvsServ.js index 570116d..a421137 100644 --- a/app/services/ConvsServ.js +++ b/app/services/ConvsServ.js @@ -18,7 +18,16 @@ ConvsServ.addData = function (conv, cb) { cb(null, conv); }; -ConvsServ.get = function(id, cb) { +ConvsServ.exists = function (id, cb) { + ConvModl.findById(id).exec(function (err, conv) { + if (err) + cb(err); + else + cb(null, true); + }); +}; + +ConvsServ.get = function (id, cb) { ConvModl.findById(id).lean().exec(function (err, conv) { if (err) cb(err); @@ -27,7 +36,7 @@ ConvsServ.get = function(id, cb) { }); }; -ConvsServ.list = function (cb) { +ConvsServ.list = function (cb) { // TODO Visibilité ConvModl.find({}).lean().exec(function (err, Convs) { async.mapSeries(Convs, ConvsServ.addData, cb); }); @@ -36,11 +45,15 @@ ConvsServ.list = function (cb) { ConvsServ.add = function (data, cb) { ConvModl.create({ titre: data.titre - }, function(err, Conv) { + }, function (err, Conv) { ConvsServ.get(Conv._id, cb); }); }; +ConvsServ.canWriteIn = function (id, login, cb) { + ConvsServ.exists(id, cb); +}; + ConvsServ.remove = function (id, cb) { // TODO Trash ConvModl.remove({ diff --git a/app/services/MessServ.js b/app/services/MessServ.js new file mode 100644 index 0000000..10673c8 --- /dev/null +++ b/app/services/MessServ.js @@ -0,0 +1,64 @@ +var MessModl = require('../models/MessModl'); +// var NomsServ = require('../services/NomsServ'); +var ConvsServ = require('../services/ConvsServ'); +var async = require('async'); + +var MesssServ = {}; + +MesssServ.addData = function (mess, cb) { + // NomsServ.get(Mess.login, function (nom) { + // if (nom) { + // Mess.nom = nom; + // } else { + // Mess.nom = Mess.login; + // } + // cb(null, Mess); + // }); + cb(null, mess); +}; + +MesssServ.get = function (id, cb) { + MessModl.findById(id).lean().exec(function (err, mess) { + if (err) + cb(err); + else + MesssServ.addData(mess, cb); + }); +}; + +MesssServ.list = function (conv, cb) { + MessModl.find({ + conv: conv + }).lean().exec(function (err, Messs) { + async.mapSeries(Messs, MesssServ.addData, cb); + }); +}; + +MesssServ.add = function (data, cb) { + ConvsServ.canWriteIn(data.conv, data.login, function (err, canWriteIn) { + if (err) + cb(err); + else { + if (canWriteIn) { + MessModl.create({ + content: data.content, + login: data.login, + conv: data.conv + }, function (err, Mess) { + MesssServ.get(Mess._id, cb); + }); + } else { + cb('unauthorized'); + } + } + }); +}; + +MesssServ.remove = function (id, cb) { + // TODO Trash + MessModl.remove({ + _id: id + }, cb); +}; + +module.exports = MesssServ; diff --git a/app/services/SessionsServ.js b/app/services/SessionsServ.js index 61ac047..77a0cb1 100644 --- a/app/services/SessionsServ.js +++ b/app/services/SessionsServ.js @@ -15,8 +15,10 @@ sessions.addData = function (session, cb) { } session.canAddMembre = session.login == 'gbontoux'; session.canDelMembre = session.login == 'gbontoux'; - session.canAddConv = session.login == 'gbontoux'; + session.canAddConv = true; session.canDelConv = session.login == 'gbontoux'; + session.canAddMess = true; + session.canDelMess = session.login == 'gbontoux'; cb(session); }); }; @@ -70,7 +72,7 @@ sessions.use = function (id, cb) { if (err) { cb(err); } else { - _this.cur = session; + _this.cur = session; // TODO Get rid of _this.cur cb(null); } }); diff --git a/public/js/controllers/ForumConvCtrl.js b/public/js/controllers/ForumConvCtrl.js index fa53efb..fab63f1 100644 --- a/public/js/controllers/ForumConvCtrl.js +++ b/public/js/controllers/ForumConvCtrl.js @@ -1,31 +1,39 @@ angular.module('ForumConvCtrl', ['SessionsServ', 'ForumServ', 'NotifyServ']).controller('ForumConvCtrl', ['$scope', '$routeParams', 'SessionServ', 'ForumServ', 'NotifyServ', function ($scope, $routeParams, SessionServ, ForumServ, NotifyServ) { + $scope.messs = []; $scope.conv = {}; - // $scope.formData = {}; + $scope.formData = {}; $scope.session = SessionServ.cur; SessionServ.onChange(function () { $scope.session = SessionServ.cur; }); - ForumServ.getConv($routeParams.conv_id, function(err, conv) { + ForumServ.getConv($routeParams.conv_id, function (err, conv) { if (!err) $scope.conv = conv; + ForumServ.getMesss(conv._id, function (err, messs) { + if (!err) + $scope.messs = messs; + }); }); - // - // $scope.createConv = function () { - // ForumServ.createConv($scope.formData, function(err, conv) { - // if (!err) - // $scope.formData = {}; - // $scope.convs.push(conv); - // }); - // }; - // - // $scope.deleteConv = function (index) { - // ForumServ.deleteConv($scope.convs[index]._id, function(err) { - // if (!err) - // $scope.convs.splice(index, 1); - // }); - // }; + + $scope.createMess = function () { + data = $scope.formData; + data.conv = $scope.conv._id; + ForumServ.createMess(data, function (err, mess) { + console.log(mess); + if (!err) + $scope.formData = {}; + $scope.messs.push(mess); + }); + }; + + $scope.deleteMess = function (index) { + ForumServ.deleteMess($scope.messs[index]._id, function (err) { + if (!err) + $scope.messs.splice(index, 1); + }); + }; } ]); diff --git a/public/js/services/ForumServ.js b/public/js/services/ForumServ.js index 4fd5fe4..f165d42 100644 --- a/public/js/services/ForumServ.js +++ b/public/js/services/ForumServ.js @@ -44,6 +44,42 @@ angular.module('ForumServ', ['NotifyServ']).service('ForumServ', ['$http', 'Noti .error(function (data) { not.error("Impossible de supprimer le conv", data); }); + }, + + // Message + getMesss: function (conv, cb) { + // TODO Dirs + $http.get('/api/messs/' + conv) + .success(function (data) { + cb(null, data); + }) + .error(function (data) { // TODO CBs + NotifyServ.error("Impossible d'obtenir la liste des messs", data); + }); + }, + + createMess: function (data, cb) { + var not = NotifyServ.promise("Ajout du mess..."); + $http.post('/api/messs', data) + .success(function (mess) { + not.success("Mess ajouté"); + cb(null, mess); + }) + .error(function (data) { + not.error("Impossible d'ajouter le mess"); + }); + }, + + deleteMess: function (id, cb) { + var not = NotifyServ.promise("Suppression du mess..."); + $http.delete('/api/messs/' + id) + .success(function (mess) { + not.success("Mess supprimé"); + cb(null); + }) + .error(function (data) { + not.error("Impossible de supprimer le mess", data); + }); } }; return a; diff --git a/public/views/forumConv.html b/public/views/forumConv.html index 0d5f301..0443768 100644 --- a/public/views/forumConv.html +++ b/public/views/forumConv.html @@ -1,10 +1,36 @@