Commit f22cd7f33a7d884a38dc27af7a3d070768a3b9c6
1 parent
79edca45
Système de messages basique
Messs...
Showing
11 changed files
with
256 additions
and
54 deletions
Show diff stats
app/models/ConvModl.js
app/models/MembreModl.js
... | ... | @@ -0,0 +1,19 @@ |
1 | +var mongoose = require('mongoose'); | |
2 | + | |
3 | +module.exports = mongoose.model('Mess', { | |
4 | + content: { | |
5 | + type: String, | |
6 | + default: "Contenu du message" | |
7 | + }, | |
8 | + login: { | |
9 | + type: String, | |
10 | + default: 'login' | |
11 | + }, | |
12 | + conv: { | |
13 | + type: String, | |
14 | + }, | |
15 | + date: { | |
16 | + type: Date, | |
17 | + default: Date.now | |
18 | + } | |
19 | +}); | ... | ... |
app/routes/ApiRtes.js
... | ... | @@ -2,12 +2,13 @@ var MembresServ = require('../services/MembresServ'); |
2 | 2 | var SessionsServ = require('../services/SessionsServ'); |
3 | 3 | var DecryptServ = require('../services/DecryptServ'); |
4 | 4 | var ConvsServ = require('../services/ConvsServ'); |
5 | +var MessServ = require('../services/MessServ'); | |
5 | 6 | var express = require('express'); |
6 | 7 | |
7 | 8 | var api = express(); |
8 | 9 | |
9 | 10 | // Authentication |
10 | -requireAuth = function () { | |
11 | +reqAuth = function () { | |
11 | 12 | return function (req, res, next) { |
12 | 13 | SessionsServ.use(req.cookies.session, function (err) { |
13 | 14 | if (err) { |
... | ... | @@ -24,18 +25,49 @@ requireAuth = function () { |
24 | 25 | }; |
25 | 26 | }; |
26 | 27 | |
27 | -requirePerm = function (perm) { | |
28 | +reqVerified = function (verify) { | |
28 | 29 | return function (req, res, next) { |
29 | - requireAuth()(req, res, function () { | |
30 | - if (req.session[perm]) { | |
31 | - next(); | |
32 | - } else { | |
33 | - res.status(403).end(); | |
34 | - } | |
30 | + reqAuth()(req, res, function () { | |
31 | + verify(req, res, function (err, verified) { | |
32 | + if (err) { | |
33 | + res.status(500).send(err); | |
34 | + } else { | |
35 | + if (verified) { | |
36 | + next(); | |
37 | + } else { | |
38 | + res.status(403); | |
39 | + } | |
40 | + } | |
41 | + }); | |
42 | + }); | |
43 | + }; | |
44 | +}; | |
45 | + | |
46 | +reqPerm = function (perm) { | |
47 | + return reqVerified(function (req, res, cb) { | |
48 | + cb(null, req.session[perm]); | |
49 | + }); | |
50 | +}; | |
51 | + | |
52 | +assert = function (test) { | |
53 | + return function (req, res, next) { | |
54 | + reqAuth()(req, res, function () { | |
55 | + test(req, res, function (err, verified) { | |
56 | + if (err) { | |
57 | + res.status(500).send(err); | |
58 | + } else { | |
59 | + if (verified) { | |
60 | + next(); | |
61 | + } else { | |
62 | + res.status(400); | |
63 | + } | |
64 | + } | |
65 | + }); | |
35 | 66 | }); |
36 | 67 | }; |
37 | 68 | }; |
38 | 69 | |
70 | + | |
39 | 71 | // Sessions |
40 | 72 | api.get('/session', function (req, res) { // Informations sur la session |
41 | 73 | if (req.cookies && req.cookies.session) { |
... | ... | @@ -87,7 +119,7 @@ api.get('/membres', function (req, res) { // Liste des membres |
87 | 119 | }); |
88 | 120 | }); |
89 | 121 | |
90 | -api.post('/membres', requirePerm('canAddMembre'), function (req, res) { // Ajout d'un membre | |
122 | +api.post('/membres', reqPerm('canAddMembre'), function (req, res) { // Ajout d'un membre | |
91 | 123 | MembresServ.add(req.body, function (err, membre) { |
92 | 124 | if (err) |
93 | 125 | res.send(err); |
... | ... | @@ -96,7 +128,7 @@ api.post('/membres', requirePerm('canAddMembre'), function (req, res) { // Ajout |
96 | 128 | }); |
97 | 129 | }); |
98 | 130 | |
99 | -api.delete('/membres/:membre_id', requirePerm('canDelMembre'), function (req, res) { // Supression d'un membre | |
131 | +api.delete('/membres/:membre_id', reqPerm('canDelMembre'), function (req, res) { // Supression d'un membre | |
100 | 132 | MembresServ.remove(req.params.membre_id, function (err, membre) { |
101 | 133 | if (err) |
102 | 134 | res.send(err); |
... | ... | @@ -125,7 +157,7 @@ api.get('/convs/:conv_id', function (req, res) { // Une conv |
125 | 157 | }); |
126 | 158 | }); |
127 | 159 | |
128 | -api.post('/convs', requirePerm('canAddConv'), function (req, res) { // Ajout d'un conv | |
160 | +api.post('/convs', reqPerm('canAddConv'), function (req, res) { // Ajout d'un conv | |
129 | 161 | ConvsServ.add(req.body, function (err, conv) { |
130 | 162 | if (err) |
131 | 163 | res.send(err); |
... | ... | @@ -134,7 +166,7 @@ api.post('/convs', requirePerm('canAddConv'), function (req, res) { // Ajout d'u |
134 | 166 | }); |
135 | 167 | }); |
136 | 168 | |
137 | -api.delete('/convs/:conv_id', requirePerm('canDelConv'), function (req, res) { // Supression d'un conv | |
169 | +api.delete('/convs/:conv_id', reqPerm('canDelConv'), function (req, res) { // Supression d'un conv | |
138 | 170 | ConvsServ.remove(req.params.conv_id, function (err, conv) { |
139 | 171 | if (err) |
140 | 172 | res.send(err); |
... | ... | @@ -144,8 +176,8 @@ api.delete('/convs/:conv_id', requirePerm('canDelConv'), function (req, res) { / |
144 | 176 | }); |
145 | 177 | |
146 | 178 | // Messages |
147 | -api.get('/messs', function (req, res) { // Liste des messs | |
148 | - MessServ.list(function (err, messs) { | |
179 | +api.get('/messs/:conv_id', function (req, res) { // Liste des messs | |
180 | + MessServ.list(req.params.conv_id, function (err, messs) { | |
149 | 181 | if (err) |
150 | 182 | res.send(err); |
151 | 183 | else |
... | ... | @@ -162,8 +194,10 @@ api.get('/messs/:mess_id', function (req, res) { // Une mess |
162 | 194 | }); |
163 | 195 | }); |
164 | 196 | |
165 | -api.post('/messs', requireAuth(), function (req, res) { // Ajout d'un mess | |
166 | - MessServ.add(req.body, function (err, mess) { | |
197 | +api.post('/messs', reqAuth(), function (req, res) { // Ajout d'un mess | |
198 | + data = req.body; | |
199 | + data.login = req.session.login; | |
200 | + MessServ.add(data, function (err, mess) { | |
167 | 201 | if (err) |
168 | 202 | res.send(err); |
169 | 203 | else |
... | ... | @@ -171,7 +205,7 @@ api.post('/messs', requireAuth(), function (req, res) { // Ajout d'un mess |
171 | 205 | }); |
172 | 206 | }); |
173 | 207 | |
174 | -api.delete('/messs/:mess_id', requireAuth(), function (req, res) { // Supression d'un mess | |
208 | +api.delete('/messs/:mess_id', reqAuth(), function (req, res) { // Supression d'un mess | |
175 | 209 | MessServ.remove(req.params.mess_id, function (err, mess) { |
176 | 210 | if (err) |
177 | 211 | res.send(err); |
... | ... | @@ -180,4 +214,7 @@ api.delete('/messs/:mess_id', requireAuth(), function (req, res) { // Supression |
180 | 214 | }); |
181 | 215 | }); |
182 | 216 | |
217 | +// TODO 404 | |
218 | +// TODO 418 | |
219 | + | |
183 | 220 | module.exports = api; | ... | ... |
app/services/ConvsServ.js
... | ... | @@ -18,7 +18,16 @@ ConvsServ.addData = function (conv, cb) { |
18 | 18 | cb(null, conv); |
19 | 19 | }; |
20 | 20 | |
21 | -ConvsServ.get = function(id, cb) { | |
21 | +ConvsServ.exists = function (id, cb) { | |
22 | + ConvModl.findById(id).exec(function (err, conv) { | |
23 | + if (err) | |
24 | + cb(err); | |
25 | + else | |
26 | + cb(null, true); | |
27 | + }); | |
28 | +}; | |
29 | + | |
30 | +ConvsServ.get = function (id, cb) { | |
22 | 31 | ConvModl.findById(id).lean().exec(function (err, conv) { |
23 | 32 | if (err) |
24 | 33 | cb(err); |
... | ... | @@ -27,7 +36,7 @@ ConvsServ.get = function(id, cb) { |
27 | 36 | }); |
28 | 37 | }; |
29 | 38 | |
30 | -ConvsServ.list = function (cb) { | |
39 | +ConvsServ.list = function (cb) { // TODO Visibilité | |
31 | 40 | ConvModl.find({}).lean().exec(function (err, Convs) { |
32 | 41 | async.mapSeries(Convs, ConvsServ.addData, cb); |
33 | 42 | }); |
... | ... | @@ -36,11 +45,15 @@ ConvsServ.list = function (cb) { |
36 | 45 | ConvsServ.add = function (data, cb) { |
37 | 46 | ConvModl.create({ |
38 | 47 | titre: data.titre |
39 | - }, function(err, Conv) { | |
48 | + }, function (err, Conv) { | |
40 | 49 | ConvsServ.get(Conv._id, cb); |
41 | 50 | }); |
42 | 51 | }; |
43 | 52 | |
53 | +ConvsServ.canWriteIn = function (id, login, cb) { | |
54 | + ConvsServ.exists(id, cb); | |
55 | +}; | |
56 | + | |
44 | 57 | ConvsServ.remove = function (id, cb) { |
45 | 58 | // TODO Trash |
46 | 59 | ConvModl.remove({ | ... | ... |
... | ... | @@ -0,0 +1,64 @@ |
1 | +var MessModl = require('../models/MessModl'); | |
2 | +// var NomsServ = require('../services/NomsServ'); | |
3 | +var ConvsServ = require('../services/ConvsServ'); | |
4 | +var async = require('async'); | |
5 | + | |
6 | +var MesssServ = {}; | |
7 | + | |
8 | +MesssServ.addData = function (mess, cb) { | |
9 | + // NomsServ.get(Mess.login, function (nom) { | |
10 | + // if (nom) { | |
11 | + // Mess.nom = nom; | |
12 | + // } else { | |
13 | + // Mess.nom = Mess.login; | |
14 | + // } | |
15 | + // cb(null, Mess); | |
16 | + // }); | |
17 | + cb(null, mess); | |
18 | +}; | |
19 | + | |
20 | +MesssServ.get = function (id, cb) { | |
21 | + MessModl.findById(id).lean().exec(function (err, mess) { | |
22 | + if (err) | |
23 | + cb(err); | |
24 | + else | |
25 | + MesssServ.addData(mess, cb); | |
26 | + }); | |
27 | +}; | |
28 | + | |
29 | +MesssServ.list = function (conv, cb) { | |
30 | + MessModl.find({ | |
31 | + conv: conv | |
32 | + }).lean().exec(function (err, Messs) { | |
33 | + async.mapSeries(Messs, MesssServ.addData, cb); | |
34 | + }); | |
35 | +}; | |
36 | + | |
37 | +MesssServ.add = function (data, cb) { | |
38 | + ConvsServ.canWriteIn(data.conv, data.login, function (err, canWriteIn) { | |
39 | + if (err) | |
40 | + cb(err); | |
41 | + else { | |
42 | + if (canWriteIn) { | |
43 | + MessModl.create({ | |
44 | + content: data.content, | |
45 | + login: data.login, | |
46 | + conv: data.conv | |
47 | + }, function (err, Mess) { | |
48 | + MesssServ.get(Mess._id, cb); | |
49 | + }); | |
50 | + } else { | |
51 | + cb('unauthorized'); | |
52 | + } | |
53 | + } | |
54 | + }); | |
55 | +}; | |
56 | + | |
57 | +MesssServ.remove = function (id, cb) { | |
58 | + // TODO Trash | |
59 | + MessModl.remove({ | |
60 | + _id: id | |
61 | + }, cb); | |
62 | +}; | |
63 | + | |
64 | +module.exports = MesssServ; | ... | ... |
app/services/SessionsServ.js
... | ... | @@ -15,8 +15,10 @@ sessions.addData = function (session, cb) { |
15 | 15 | } |
16 | 16 | session.canAddMembre = session.login == 'gbontoux'; |
17 | 17 | session.canDelMembre = session.login == 'gbontoux'; |
18 | - session.canAddConv = session.login == 'gbontoux'; | |
18 | + session.canAddConv = true; | |
19 | 19 | session.canDelConv = session.login == 'gbontoux'; |
20 | + session.canAddMess = true; | |
21 | + session.canDelMess = session.login == 'gbontoux'; | |
20 | 22 | cb(session); |
21 | 23 | }); |
22 | 24 | }; |
... | ... | @@ -70,7 +72,7 @@ sessions.use = function (id, cb) { |
70 | 72 | if (err) { |
71 | 73 | cb(err); |
72 | 74 | } else { |
73 | - _this.cur = session; | |
75 | + _this.cur = session; // TODO Get rid of _this.cur | |
74 | 76 | cb(null); |
75 | 77 | } |
76 | 78 | }); | ... | ... |
public/js/controllers/ForumConvCtrl.js
1 | 1 | angular.module('ForumConvCtrl', ['SessionsServ', 'ForumServ', 'NotifyServ']).controller('ForumConvCtrl', ['$scope', '$routeParams', 'SessionServ', 'ForumServ', 'NotifyServ', |
2 | 2 | function ($scope, $routeParams, SessionServ, ForumServ, NotifyServ) { |
3 | + $scope.messs = []; | |
3 | 4 | $scope.conv = {}; |
4 | - // $scope.formData = {}; | |
5 | + $scope.formData = {}; | |
5 | 6 | |
6 | 7 | $scope.session = SessionServ.cur; |
7 | 8 | SessionServ.onChange(function () { |
8 | 9 | $scope.session = SessionServ.cur; |
9 | 10 | }); |
10 | - ForumServ.getConv($routeParams.conv_id, function(err, conv) { | |
11 | + ForumServ.getConv($routeParams.conv_id, function (err, conv) { | |
11 | 12 | if (!err) |
12 | 13 | $scope.conv = conv; |
14 | + ForumServ.getMesss(conv._id, function (err, messs) { | |
15 | + if (!err) | |
16 | + $scope.messs = messs; | |
17 | + }); | |
13 | 18 | }); |
14 | - // | |
15 | - // $scope.createConv = function () { | |
16 | - // ForumServ.createConv($scope.formData, function(err, conv) { | |
17 | - // if (!err) | |
18 | - // $scope.formData = {}; | |
19 | - // $scope.convs.push(conv); | |
20 | - // }); | |
21 | - // }; | |
22 | - // | |
23 | - // $scope.deleteConv = function (index) { | |
24 | - // ForumServ.deleteConv($scope.convs[index]._id, function(err) { | |
25 | - // if (!err) | |
26 | - // $scope.convs.splice(index, 1); | |
27 | - // }); | |
28 | - // }; | |
19 | + | |
20 | + $scope.createMess = function () { | |
21 | + data = $scope.formData; | |
22 | + data.conv = $scope.conv._id; | |
23 | + ForumServ.createMess(data, function (err, mess) { | |
24 | + console.log(mess); | |
25 | + if (!err) | |
26 | + $scope.formData = {}; | |
27 | + $scope.messs.push(mess); | |
28 | + }); | |
29 | + }; | |
30 | + | |
31 | + $scope.deleteMess = function (index) { | |
32 | + ForumServ.deleteMess($scope.messs[index]._id, function (err) { | |
33 | + if (!err) | |
34 | + $scope.messs.splice(index, 1); | |
35 | + }); | |
36 | + }; | |
29 | 37 | |
30 | 38 | } |
31 | 39 | ]); | ... | ... |
public/js/services/ForumServ.js
... | ... | @@ -44,6 +44,42 @@ angular.module('ForumServ', ['NotifyServ']).service('ForumServ', ['$http', 'Noti |
44 | 44 | .error(function (data) { |
45 | 45 | not.error("Impossible de supprimer le conv", data); |
46 | 46 | }); |
47 | + }, | |
48 | + | |
49 | + // Message | |
50 | + getMesss: function (conv, cb) { | |
51 | + // TODO Dirs | |
52 | + $http.get('/api/messs/' + conv) | |
53 | + .success(function (data) { | |
54 | + cb(null, data); | |
55 | + }) | |
56 | + .error(function (data) { // TODO CBs | |
57 | + NotifyServ.error("Impossible d'obtenir la liste des messs", data); | |
58 | + }); | |
59 | + }, | |
60 | + | |
61 | + createMess: function (data, cb) { | |
62 | + var not = NotifyServ.promise("Ajout du mess..."); | |
63 | + $http.post('/api/messs', data) | |
64 | + .success(function (mess) { | |
65 | + not.success("Mess ajouté"); | |
66 | + cb(null, mess); | |
67 | + }) | |
68 | + .error(function (data) { | |
69 | + not.error("Impossible d'ajouter le mess"); | |
70 | + }); | |
71 | + }, | |
72 | + | |
73 | + deleteMess: function (id, cb) { | |
74 | + var not = NotifyServ.promise("Suppression du mess..."); | |
75 | + $http.delete('/api/messs/' + id) | |
76 | + .success(function (mess) { | |
77 | + not.success("Mess supprimé"); | |
78 | + cb(null); | |
79 | + }) | |
80 | + .error(function (data) { | |
81 | + not.error("Impossible de supprimer le mess", data); | |
82 | + }); | |
47 | 83 | } |
48 | 84 | }; |
49 | 85 | return a; | ... | ... |
public/views/forumConv.html
1 | 1 | <div class="container"> |
2 | 2 | <ol class="breadcrumb"> |
3 | - <li><a href="#">Home</a></li> | |
4 | - <li><a href="#">Library</a></li> | |
5 | - <li class="active">Conv</li> | |
3 | + <li><a href="#">Forum</a></li> | |
4 | + <li><a href="#">Conversations</a></li> | |
5 | + <li class="active">{{ conv.titre }}</li> | |
6 | 6 | </ol> |
7 | 7 | <h1>{{ conv.titre }}</h1> |
8 | + <div class="panel panel-default" ng-repeat="mess in messs"> | |
9 | + <div class="panel-heading"> | |
10 | + {{ mess.login }} | |
11 | + <button ng-if="session.canDelMess" type="button" class="btn btn-danger" ng-click="deleteMess($index)"> | |
12 | + <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Supprimer | |
13 | + </button> | |
14 | + </div> | |
15 | + <div class="panel-body"> | |
16 | + {{ mess.content }} | |
17 | + </div> | |
18 | + </div> | |
19 | + <div class="form-group" ng-if="session.canAddMess"> | |
20 | + <div class="panel panel-default"> | |
21 | + <div class="panel-heading"> | |
22 | + <h4 class="panel-title"> | |
23 | + Nouveau message | |
24 | + </h4> | |
25 | + </div> | |
26 | + <div class="panel-body"> | |
27 | + <textarea class="form-control" rows="3" ng-model="formData.content"></textarea> | |
28 | + <button type="submit" class="btn btn-primary" aria-label="Ajouter" ng-click="createMess()"> | |
29 | + <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter | |
30 | + </button> | |
31 | + </div> | |
32 | + </div> | |
33 | + </div> | |
8 | 34 | <nav> |
9 | 35 | <ul class="pagination"> |
10 | 36 | <li> |
... | ... | @@ -24,4 +50,4 @@ |
24 | 50 | </li> |
25 | 51 | </ul> |
26 | 52 | </nav> |
27 | 53 | -</div> |
54 | +</div> | |
28 | 55 | \ No newline at end of file | ... | ... |
public/views/forumDir.html
1 | 1 | <div class="container"> |
2 | 2 | <ol class="breadcrumb"> |
3 | - <li><a href="#">Home</a></li> | |
4 | - <li><a href="#">Library</a></li> | |
5 | - <li class="active">Data</li> | |
3 | + <li><a href="#">Forum</a></li> | |
4 | + <li><a href="#">Dossiers</a></li> | |
5 | + <li class="active">{{ dossier.name }}</li> | |
6 | 6 | </ol> |
7 | 7 | <div class="panel panel-default" ng-repeat="conv in convs"> |
8 | 8 | <div class="panel-heading"> |
... | ... | @@ -11,8 +11,7 @@ |
11 | 11 | </a> |
12 | 12 | </div> |
13 | 13 | <div class="panel-body"> |
14 | - <strong>Date : </strong>{{ conv.date }} | |
15 | - <br/> | |
14 | + <!-- <strong>Date : </strong>{{ conv.date }}<br/> --> | |
16 | 15 | <button ng-if="session.canDelConv" type="button" class="btn btn-danger" ng-click="deleteConv($index)"> |
17 | 16 | <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Supprimer |
18 | 17 | </button> |
... | ... | @@ -52,4 +51,4 @@ |
52 | 51 | </li> |
53 | 52 | </ul> |
54 | 53 | </nav> |
55 | 54 | -</div> |
55 | +</div> | |
56 | 56 | \ No newline at end of file | ... | ... |