Blame view

app/services/MessServ.js 1.59 KB
f22cd7f3   Geoffrey PREUD'HOMME   Système de messag...
1
  var MessModl = require('../models/MessModl');
43b2778d   Geoffrey PREUD'HOMME   Rassemblement de ...
2
  // var PolyUserServ = require('../services/PolyUserServ');
f22cd7f3   Geoffrey PREUD'HOMME   Système de messag...
3
4
5
6
7
8
  var ConvsServ = require('../services/ConvsServ');
  var async = require('async');
  
  var MesssServ = {};
  
  MesssServ.addData = function (mess, cb) {
33dce699   Geoffrey PREUD'HOMME   Redesign du forum
9
10
      mess.auteur = {};
      mess.auteur.nom = mess.login;
43b2778d   Geoffrey PREUD'HOMME   Rassemblement de ...
11
      // PolyUserServ.get(Mess.login, function (err, nom) {
f22cd7f3   Geoffrey PREUD'HOMME   Système de messag...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      //     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;