Parent: [65557d] (diff)

Download this file

controller.js    134 lines (108 with data), 3.4 kB

  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
var mongoose = require('mongoose');
var logger, core, user, saveNotif, notificationChannel;
var socket = require('../../../ws/mobile.js');
var context = require('../../../../../../context');
function sayHello(req, res) {
logger.info('Welcome to c2net!');
return res.json(200, {message: core.getMessage()});
}
function createUserNotification(notification){
for(var i=0; i<notification.userEmail.length; i++){
console.log(notification.userEmail[i]);
user.findByEmail(notification.userEmail[i],function(err, u){
if(!u || err)
{
logger.info('User not found ...');
logger.debug(err);
return;
}
u.objectType = 'user';
logger.info("ID found by Email :" + u._id)
var data = {
title: notification.title,
author: u.id,
action: notification.body.substring(0,25)+'...',
object: notification.body,
link: notification.link,
level: "notification.level",
timestamps: new Date(),
target: [u],
data: {},
context: "context",
icon : notification.icon
};
saveNotif.save(data,function(err,notifData){
if(err)
{
console.log(err);
return;
}
});
var notif = {
title : notification.title,
body : notification.body,
component : notification.component,
link : notification.link,
userEmail : notification.userEmail[i],
icon : notification.icon,
type : notification.type
};
try{socket.emit(u._id, notif);
console.log("notification Handled by controller and emmited to socket.")
}
catch(err){
console.log("----emiter error:-----")
console.log(err)
}
});
}
}
function sendNotification(req, res){
createUserNotification(req.body);
//console.log(User.findById(req.user._id));
//console.log(saveNotif);
/*
var notif = {
title:req.body.title,
msg: req.body.msg,
status: 'unread',
type: req.body.type
};
user.newNotification(req.user._id, notif);
socket.emit(req.user._id, notif);
return res.json(204, {message:"User is notified!"});
*/
return res.json(204, {message:"User is notified!"});
}
module.exports = function(dependencies) {
var PubSub = dependencies('pubsub').local;
var PubSubG = dependencies('pubsub').global;
saveNotif = dependencies('notification').notification;
user = dependencies('user');
logger = context.logger;
core = require('./core')(dependencies);
notificationChannel = context.requires.send_notifications;
console.log(notificationChannel);
notificationChannel.subscribe('C2NET_Notification');
notificationChannel.on('message', function(data){
createUserNotification(JSON.parse(data.toString()));
});
// User login ------------------------------------------------------------------------------
PubSub.topic('login:success').subscribe(function(data) {
logger.info("Login successfully!");
var notif = {
title:'Login successfully',
msg: 'Welcome to C2NET!',
status: 'unread',
type: 'login'
};
// console.log(data)
// user.newNotification(data._id, notif);
// socket.emit(data._id, notif);
});
return {
sayHello: sayHello,
sendNotification:sendNotification
}
};