示例#1
0
 public void SendMisspush(string userid, string appid)
 {
     try
     {
         Recipients _rep = bus_Recipients.Get_ByIdAppID(userid, appid);
         if (_rep != null)
         {
             LogGenerationHelper.WriteToFile("Pushing leftover notif");
             HashSet <string> receiver;
             var context = GlobalHost.ConnectionManager.GetHubContext <NotificationHub>();
             if (Users.TryGetValue(new Tuple <string, string>(userid, appid), out receiver))
             {
                 foreach (var cid in receiver)
                 {
                     foreach (string notif in _rep.NotifIDs)
                     {
                         NotificationActivity _not = bus_NotificationActivity.Get_ById(notif);
                         context.Clients.Client(cid).receiveNotification(_not.Content);
                     }
                 }
             }
             bus_Recipients.Update_ReceiveTime(_rep);
         }
         else
         {
             LogGenerationHelper.WriteToFile("No leftover notif");
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
     }
 }
示例#2
0
 public HttpResponseMessage Remove([FromBody] AppInfo model)
 {
     if (model.Id == null)
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         AppInfo _appinfo = bus_AppInfo.Get_ById(model.Id);
         if (_appinfo == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound));
         }
         else
         {
             bus_AppInfo.Remove(model.Id);
             return(Request.CreateResponse(HttpStatusCode.OK));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#3
0
        public override Task OnDisconnected(bool stopCalled)
        {
            LogGenerationHelper.WriteToFile("Disconnected");
            string userID       = Context.QueryString["userid"] == null ? string.Empty : Context.QueryString["userid"];
            string appID        = Context.QueryString["appid"] == null ? string.Empty : Context.QueryString["appid"];
            string connectionId = Context.ConnectionId;

            HashSet <string> user;

            Users.TryGetValue(new Tuple <string, string>(userID, appID), out user);

            if (user != null)
            {
                lock (user)
                {
                    user.RemoveWhere(cid => cid.Equals(connectionId));
                    if (!user.Any())
                    {
                        HashSet <string> removedUser;
                        Users.TryRemove(new Tuple <string, string>(userID, appID), out removedUser);
                        //Clients.Others.userDisconnected(userName);
                    }
                }
            }

            return(base.OnDisconnected(stopCalled));
        }
示例#4
0
 public HttpResponseMessage GetAll()
 {
     try
     {
         return(Request.CreateResponse(HttpStatusCode.OK, bus_AppInfo.GetAll()));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#5
0
 public HttpResponseMessage SendToList([FromBody] NotificationModel value)
 {
     try
     {
         //NotificationActivity _request = JsonConvert.DeserializeObject<NotificationActivity>(value);
         if (value.RecipientIDs == null || string.IsNullOrEmpty(value.Content))
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
         IEnumerable <string> _sessionGuid;
         User _usr = new User();
         if (Request.Headers.TryGetValues("SessionGuid", out _sessionGuid))
         {
             string _sessID = _sessionGuid.First();
             if (MemoryCacher.GetValue(_sessID) == null)
             {
                 return(Request.CreateResponse(HttpStatusCode.Forbidden));
             }
             string _userID = MemoryCacher.GetValue(_sessID).ToString();
             _usr = bus_user.Get_ById(_userID);
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
         List <string> _webappid = value.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).ToList();
         if (_webappid != null && _webappid.Count > 0)
         {
             NotificationActivity _notif = bus_NotificationActivity.Create(new NotificationActivity
             {
                 Content      = value.Content,
                 UserID       = _usr.Id,
                 RecipientIDs = value.RecipientIDs,
                 AppIDs       = _webappid,
                 SendTime     = DateTime.Now
             });
             foreach (string _id in _webappid)
             {
                 notificationHub.SendSegment(_notif.Id, value.RecipientIDs, _id, value.Content);
             }
             return(Request.CreateResponse(HttpStatusCode.OK, new { NotifID = _notif.Id }));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#6
0
 public HttpResponseMessage GetAllUnpushNotif()
 {
     try
     {
         List <NotificationActivity> notifications = bus_NotificationActivity.Get_Unpush();
         return(Request.CreateResponse(HttpStatusCode.OK, notifications));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
示例#7
0
 public HttpResponseMessage SendToList_Routine([FromBody] NotificationModel value)
 {
     try
     {
         if (value.RecipientIDs == null || string.IsNullOrEmpty(value.Content) || value.Routine == null || value.Routine.Time == null)
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
         IEnumerable <string> _sessionGuid;
         User _usr = new User();
         if (Request.Headers.TryGetValues("SessionGuid", out _sessionGuid))
         {
             string _sessID = _sessionGuid.First();
             if (MemoryCacher.GetValue(_sessID) == null)
             {
                 return(Request.CreateResponse(HttpStatusCode.Forbidden));
             }
             string _userID = MemoryCacher.GetValue(_sessID).ToString();
             _usr = bus_user.Get_ById(_userID);
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
         string _webappid = value.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).FirstOrDefault();
         if (_webappid != null)
         {
             NotificationActivity _notif = bus_NotificationActivity.Create(new NotificationActivity
             {
                 Content      = value.Content,
                 UserID       = _usr.Id,
                 RecipientIDs = value.RecipientIDs,
                 AppIDs       = new List <string> {
                     _webappid
                 },
                 IsRoutine = true,
                 Routine   = value.Routine
             });
             return(Request.CreateResponse(HttpStatusCode.OK, new { NotifID = _notif.Id }));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#8
0
 public HttpResponseMessage GetAll()
 {
     try
     {
         List <User> lstUser = bus_User.GetAll();
         lstUser.ForEach(_ => _.Password = "******");
         return(Request.CreateResponse(HttpStatusCode.OK, lstUser));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#9
0
 public HttpResponseMessage Add([FromBody] AppInfo model)
 {
     if (string.IsNullOrEmpty(model.Name) || string.IsNullOrEmpty(model.Type))
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         bus_AppInfo.Create(model);
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#10
0
 public HttpResponseMessage Add([FromBody] User model)
 {
     if (string.IsNullOrEmpty(model.UserName) || string.IsNullOrEmpty(model.Password))
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         User _usr = bus_User.Create(model);
         return(Request.CreateResponse(HttpStatusCode.OK, new { UserID = _usr.Id }));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#11
0
 public HttpResponseMessage GetNotif_ByUserID([FromBody] NotifHistoryModel model)
 {
     if (model.UserID == null)
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         List <NotificationActivity> lstNotif = bus_NotificationActivity.Get_ByUserID(model.UserID);
         return(Request.CreateResponse(HttpStatusCode.OK, lstNotif));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#12
0
 public HttpResponseMessage Update([FromBody] Templates model)
 {
     if (string.IsNullOrEmpty(model.Name) || string.IsNullOrEmpty(model.Type))
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         bool _rs = bus_Templates.Update(model);
         return(Request.CreateResponse(HttpStatusCode.OK, _rs));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#13
0
        //public void SendAll(string appid, string message)
        //{
        //    try
        //    {
        //        List<HashSet<string>> lstSel = Users.Where(x => x.Key.Item2 == appid).Select(x => x.Value).ToList();
        //        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        //        foreach (var supItem in lstSel)
        //        {
        //            foreach (var cid in supItem)
        //            {
        //                context.Clients.Client(cid).receiveNotification(message);
        //            }
        //        }
        //        context.Clients.All.receiveNotification(message);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public void SendSpecific(string userID, string message)
        //{
        //    try
        //    {
        //        //Send To
        //        UserHubModel receiver;
        //        var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
        //        if (Users.TryGetValue(userID, out receiver))
        //        {
        //            var cid = receiver.ConnectionIds.FirstOrDefault();
        //            context.Clients.Client(cid).receiveNotification(message);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public void SendSegment(string notifid, List <string> userIDs, string appid, string message)
        {
            try
            {
                //Send To
                //UserHubModel receiver;
                HashSet <string> receiver;
                var context = GlobalHost.ConnectionManager.GetHubContext <NotificationHub>();
                foreach (string id in userIDs)
                {
                    if (Users.TryGetValue(new Tuple <string, string>(id, appid), out receiver))
                    {
                        foreach (var cid in receiver)
                        {
                            LogGenerationHelper.WriteToFile("Sending notif to: " + id + " - cid: " + cid);
                            context.Clients.Client(cid).receiveNotification(message);
                        }
                    }
                    else
                    {
                        //cap nhat vao bang Recipients
                        LogGenerationHelper.WriteToFile("Not found: " + id + " - Updating to Recipients");
                        Recipients _rep = bus_Recipients.Get_ByIdAppID(id, appid);
                        if (_rep == null)
                        {
                            bus_Recipients.Create(new Recipients {
                                RecipientID = id,
                                AppID       = appid,
                                NotifIDs    = new List <string> {
                                    notifid
                                }
                            });
                        }
                        else
                        {
                            _rep.NotifIDs.Add(notifid);
                            bus_Recipients.Replace(_rep);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#14
0
 public HttpResponseMessage GetNotif_ByTime([FromBody] NotifHistoryModel model)
 {
     //NotificationActivity _request = JsonConvert.DeserializeObject<NotificationActivity>(value);
     if (model.FromTime == DateTime.MinValue || model.ToTime == DateTime.MinValue)
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         List <NotificationActivity> lstNotif = bus_NotificationActivity.Get_ByTime(model.FromTime, model.ToTime);
         return(Request.CreateResponse(HttpStatusCode.OK, lstNotif));
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#15
0
 public HttpResponseMessage Logout()
 {
     try
     {
         IEnumerable <string> _sessionGuid;
         if (Request.Headers.TryGetValues("SessionGuid", out _sessionGuid))
         {
             MemoryCacher.Delete(_sessionGuid.First());
             return(Request.CreateResponse(HttpStatusCode.OK, new { SessionGuid = _sessionGuid.First() }));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#16
0
        public override Task OnConnected()
        {
            //string userID = Context.User.Identity.Name;
            LogGenerationHelper.WriteToFile("Connected");
            string userID       = Context.QueryString["userid"] == null ? string.Empty : Context.QueryString["userid"];
            string appID        = Context.QueryString["appid"] == null ? string.Empty : Context.QueryString["appid"];
            string connectionId = Context.ConnectionId;

            var user = Users.GetOrAdd(new Tuple <string, string>(userID, appID), _ => new HashSet <string>());

            lock (user)
            {
                user.Add(connectionId);
                //if (user.ConnectionIds.Count == 1)
                //{
                //    Clients.Others.userConnected(userID);
                //}
            }
            this.SendMisspush(userID, appID);
            return(base.OnConnected());
        }
示例#17
0
 public HttpResponseMessage Update([FromBody] User model)
 {
     if (model.Id == null)
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest));
     }
     try
     {
         User _usr = bus_User.Get_ById(model.Id);
         if (_usr == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.OK, bus_User.Update(model)));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }
示例#18
0
 public HttpResponseMessage Start()
 {
     try
     {
         Thread _thd = (Thread)MemoryCacher.GetValue(SessionConst.SESSION_THREAD);
         if (_thd == null)
         {
             Thread t1 = new Thread(() =>
             {
                 while (true)
                 {
                     LogGenerationHelper.WriteToFile("Scheduled Sender running");
                     List <NotificationActivity> _lstSel = bus_NotificationActivity.Get_Unpush();
                     foreach (NotificationActivity notif in _lstSel)
                     {
                         try
                         {
                             LogGenerationHelper.WriteToFile("Scheduled Sender sending");
                             string _webappid = notif.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).FirstOrDefault();
                             notificationHub.SendSegment(notif.Id, notif.RecipientIDs, _webappid, notif.Content);
                             bus_NotificationActivity.Update_SendTime(notif);
                         }
                         catch (Exception ex)
                         {
                             LogGenerationHelper.WriteToFile($"{notif.Id} -> {ex.Message}");
                         }
                     }
                     List <NotificationActivity> _lstRoutine = bus_NotificationActivity.Get_UncancelRoutine();
                     foreach (NotificationActivity routine in _lstRoutine)
                     {
                         try
                         {
                             LogGenerationHelper.WriteToFile("Routine Sender sending");
                             if (routine.Routine.Type == RoutineType.DAILY)
                             {
                                 if (((DateTime)routine.Routine.Time).TimeOfDay >= DateTime.Now.TimeOfDay)
                                 {
                                     if (routine.SendTime != null && ((DateTime)routine.SendTime).Date != DateTime.Today)
                                     {
                                         string _webappid = routine.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).FirstOrDefault();
                                         notificationHub.SendSegment(routine.Id, routine.RecipientIDs, _webappid, routine.Content);
                                         bus_NotificationActivity.Update_SendTime(routine);
                                     }
                                 }
                             }
                             else if (routine.Routine.Type == RoutineType.WEEKLY)
                             {
                                 if (routine.Routine.DayOfWeek == DateTime.Now.DayOfWeek && ((DateTime)routine.Routine.Time).TimeOfDay >= DateTime.Now.TimeOfDay)
                                 {
                                     if (routine.SendTime != null && ((DateTime)routine.SendTime).Date != DateTime.Today)
                                     {
                                         string _webappid = routine.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).FirstOrDefault();
                                         notificationHub.SendSegment(routine.Id, routine.RecipientIDs, _webappid, routine.Content);
                                         bus_NotificationActivity.Update_SendTime(routine);
                                     }
                                 }
                             }
                             else if (routine.Routine.Type == RoutineType.MONTHLY)
                             {
                                 if (((DateTime)routine.Routine.Time).Day == DateTime.Now.Day && ((DateTime)routine.Routine.Time).TimeOfDay >= DateTime.Now.TimeOfDay)
                                 {
                                     if (routine.SendTime != null && ((DateTime)routine.SendTime).Date != DateTime.Today)
                                     {
                                         string _webappid = routine.AppIDs.Where(x => bus_AppInfo.Get_ById(x).Type == AppTypeConst.WEBAPP).FirstOrDefault();
                                         notificationHub.SendSegment(routine.Id, routine.RecipientIDs, _webappid, routine.Content);
                                         bus_NotificationActivity.Update_SendTime(routine);
                                     }
                                 }
                             }
                         }
                         catch (Exception ex)
                         {
                             LogGenerationHelper.WriteToFile($"{routine.Id} -> {ex.Message}");
                         }
                     }
                     Thread.Sleep(60_000);
                 }
             });
             MemoryCacher.Add(SessionConst.SESSION_THREAD, t1, DateTimeOffset.Now.AddYears(1));
             t1.Start();
             return(Request.CreateResponse(HttpStatusCode.OK));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest, new { Error = "Thread already started." }));
         }
     }
     catch (Exception ex)
     {
         LogGenerationHelper.WriteToFile(ex.Message);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError));
     }
 }