Built motion from commit 06df96e on branch develop.
[motion.git] / server / api / fax_message / fax_message.socket.js
1 /**
2  * Broadcast updates to client when the model changes
3  */
4
5 'use strict';
6
7 var FaxMessage = require('../../models').FaxMessage;
8 var Contact = require('../../models').Contact;
9 var FaxRoom = require('../../models').FaxRoom;
10 var FaxAccount = require('../../models').FaxAccount;
11
12 exports.register = function(socket) {
13   FaxMessage.afterCreate(function(doc) {
14     FaxMessage
15       .findOne({
16         where: {
17           id: doc.id
18         },
19         include: [{
20           model: Contact,
21           as: 'From'
22         }, {
23           model: Contact,
24           as: 'To'
25         }, {
26           model: FaxRoom,
27           include: [{
28             model: FaxAccount
29           }]
30         }]
31       })
32       .then(function(faxMessage) {
33         onSave(socket, faxMessage);
34       })
35       .catch(function(err) {
36         console.error(err);
37       });
38   });
39
40   FaxMessage.afterUpdate(function(doc) {
41     FaxMessage
42       .findOne({
43         where: {
44           id: doc.id
45         },
46         include: [{
47           model: Contact,
48           as: 'From'
49         }, {
50           model: Contact,
51           as: 'To'
52         }, {
53           model: FaxRoom,
54           include: [{
55             model: FaxAccount
56           }]
57         }]
58       })
59       .then(function(faxMessage) {
60         onSave(socket, faxMessage);
61         onUpdate(socket, faxMessage);
62       })
63       .catch(function(err) {
64         console.error(err);
65       });
66   });
67
68   FaxMessage.afterDestroy(function(doc) {
69     onRemove(socket, doc);
70   });
71 }
72
73 function onSave(socket, doc, cb) {
74   socket.emit('fax_message:save', doc);
75   // socket.emit('fax_message:' + doc.directory + ':save', doc);
76 }
77
78 function onUpdate(socket, doc, cb) {
79   socket.emit('fax_message:update', doc);
80 }
81
82 function onRemove(socket, doc, cb) {
83   socket.emit('fax_message:remove', doc);
84   // socket.emit('fax_message:' + doc.directory + ':remove', doc);
85 }