Blame view

app/services/MessServ.js 1.52 KB
f22cd7f3   Geoffrey PREUD'HOMME   Système de messag...
1
2
3
4
5
6
7
8
9
10
11
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
  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;