public static async Task <MemberCallback[]> UserConnectionClosed(string userId, string languages)
        {
            var mbsvc = new UserAppMemberServiceProxy();
            var cntx  = Cntx;

            cntx.AcceptLanguages = languages;
            var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);

            if (memb != null)
            {
                memb.LastActivityDate = DateTime.UtcNow;
                List <MemberCallback> callbacks = new List <MemberCallback>();
                if (memb.ChangedMemberCallbacks != null)
                {
                    foreach (var c in memb.ChangedMemberCallbacks)
                    {
                        callbacks.Add(c.ShallowCopy());
                        c.StartAutoUpdating = true;
                        c.ConnectionID      = null;
                        c.IsDisconnected    = true;
                        c.LastActiveDate    = DateTime.UtcNow;
                    }
                }
                await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });

                return((from d in callbacks where d.ConnectionID != null && !d.IsDisconnected select d).ToArray());
            }
            return(null);
        }
        public static async Task<MemberCallback> OnUserConnected(string hubId, string userId, string connectId, string languages)
        {
            var mbsvc = new UserAppMemberServiceProxy();
            var cntx = Cntx;
            cntx.AcceptLanguages = languages;
            var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);
            if (memb != null)
            {
                memb.StartAutoUpdating = true;
                memb.LastActivityDate = DateTime.UtcNow;
                if (languages != null)
                    memb.AcceptLanguages = languages;
                List<MemberCallback> callbacks;
                if (memb.ChangedMemberCallbacks == null)
                    callbacks = new List<MemberCallback>();
                else
                    callbacks = new List<MemberCallback>(memb.ChangedMemberCallbacks);
                var cbk = (from d in callbacks where d.HubID == hubId && d.ChannelID == "System" select d).SingleOrDefault();
                if (cbk == null)
                {
                    cbk = new MemberCallback
                    {
                        ApplicationID = AppId,
                        UserID = userId,
                        HubID = hubId,
                        ChannelID = "System",
                        ConnectionID = connectId,
                        IsDisconnected = false,
                        LastActiveDate = DateTime.UtcNow
                    };
                }
                else
                {
                    // it is very important to turn this on, otherwise the property will not be marked as modified.
                    // and the service will not save the change!
                    cbk.StartAutoUpdating = true;
                    cbk.ConnectionID = connectId;
                    cbk.IsDisconnected = false;
                    cbk.LastActiveDate = DateTime.UtcNow;
                    // other more explicit way of doing so is given in the following:
                    //cbk.ConnectionID = connectId;
                    //cbk.IsConnectionIDModified = true;
                    //cbk.IsDisconnected = false;
                    //cbk.IsIsDisconnectedModified = true;
                    //cbk.LastActiveDate = DateTime.UtcNow;
                    //cbk.IsLastActiveDateModified = true;
                }
                memb.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                try
                {
                    await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });
                }
                catch (Exception ex)
                {

                }
                return cbk;
            }
            return null;
        }
        public static async Task <ConnectionStatus> UserConnected(string noticeHubId, string hubId, string peerId, string userId, string connectId, string languages)
        {
            var mbsvc = new UserAppMemberServiceProxy();
            var cntx  = Cntx;

            cntx.AcceptLanguages = languages;
            var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);

            if (memb != null)
            {
                memb.StartAutoUpdating = true;
                memb.LastActivityDate  = DateTime.UtcNow;
                memb.AcceptLanguages   = languages;
                List <MemberCallback> callbacks;
                if (memb.ChangedMemberCallbacks == null)
                {
                    callbacks = new List <MemberCallback>();
                }
                else
                {
                    callbacks = new List <MemberCallback>(memb.ChangedMemberCallbacks);
                }
                var cbk = (from d in callbacks where d.HubID == hubId && d.ChannelID == peerId select d).SingleOrDefault();
                if (cbk == null)
                {
                    cbk = new MemberCallback
                    {
                        ApplicationID  = AppId,
                        UserID         = userId,
                        HubID          = hubId,
                        ChannelID      = peerId,
                        ConnectionID   = connectId,
                        IsDisconnected = false,
                        LastActiveDate = DateTime.UtcNow
                    };
                }
                else
                {
                    // it is very important to turn this on, otherwise the property will not be marked as modified.
                    // and the service will not save the change!
                    cbk.StartAutoUpdating = true;
                    cbk.ConnectionID      = connectId;
                    cbk.IsDisconnected    = false;
                    cbk.LastActiveDate    = DateTime.UtcNow;
                }
                memb.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });

                UserServiceProxy usvc = new UserServiceProxy();
                var u = await usvc.LoadEntityByKeyAsync(cntx, userId);

                memb.UserRef = u;

                var peerMb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, peerId);

                ConnectionStatus status = new ConnectionStatus();
                status.me = cbk;
                MemberCallbackServiceProxy mbcsvc = new MemberCallbackServiceProxy();
                var peerCb = await mbcsvc.LoadEntityByKeyAsync(cntx, userId, hubId, AppId, peerId);

                if (peerCb == null || peerCb.ConnectionID == null || peerCb.IsDisconnected)
                {
                    MemberNotificationTypeServiceProxy ntsvc = new MemberNotificationTypeServiceProxy();
                    var ntype = await ntsvc.LoadEntityByKeyAsync(cntx, 3);

                    var notifier = await mbcsvc.LoadEntityByKeyAsync(cntx, "System", noticeHubId, AppId, peerId);

                    if (notifier != null && notifier.ConnectionID != null && !notifier.IsDisconnected)
                    {
                        status.peerNotifier = notifier;
                        status.status       = PeerStatus.Notifiable;
                    }
                    MemberNotification n = new MemberNotification
                    {
                        ID            = Guid.NewGuid().ToString(),
                        Title         = string.Format(ResourceUtils.GetString("20dc5913998d0e9ed01360475e46a0f9", "{0} invites you to chat, is waiting ...", peerMb.AcceptLanguages), u.Username),
                        CreatedDate   = DateTime.UtcNow,
                        PriorityLevel = 0,
                        ReadCount     = 0,
                        ApplicationID = AppId,
                        TypeID        = 3,
                        UserID        = peerId
                    };
                    n.NoticeMsg          = "{ \"peerId\": \"" + userId + "\", \"peer\": \"" + u.Username + "\", \"connectId\": \"" + connectId + "\", \"msg\": \"" + n.Title + "\", \"isCancel\": false }";
                    n.IsNoticeDataLoaded = true;
                    MemberNotificationServiceProxy nsvc = new MemberNotificationServiceProxy();
                    var r = await nsvc.AddOrUpdateEntitiesAsync(cntx, new MemberNotificationSet(), new MemberNotification[] { n });

                    status.noticeType  = ntype.TypeName;
                    status.noticeMsg   = "{ \"peerId\": \"" + userId + "\", \"peer\": \"" + u.Username + "\", \"connectId\": \"" + connectId + "\", \"msg\": \"" + n.Title + "\", \"isCancel\": false }";
                    status.noticeRecId = r.ChangedEntities[0].UpdatedItem.ID;
                }
                else
                {
                    UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
                    UserAssociation             utop  = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);

                    DateTime dt = DateTime.UtcNow;
                    if (utop == null)
                    {
                        utop = new UserAssociation
                        {
                            TypeID        = ApplicationContext.ChatAssocTypeId,
                            FromUserID    = userId,
                            ToUserID      = peerId,
                            CreateDate    = dt,
                            AssocCount    = 1,
                            LastAssoc     = dt,
                            InteractCount = 0,
                            Votes         = 0
                        };
                    }
                    else
                    {
                        utop.AssocCount++;
                    }
                    UserAssociation ptou = await uasvc.LoadEntityByKeyAsync(cntx, peerId, userId, ApplicationContext.ChatAssocTypeId);

                    if (ptou == null)
                    {
                        ptou = new UserAssociation
                        {
                            TypeID        = ApplicationContext.ChatAssocTypeId,
                            FromUserID    = peerId,
                            ToUserID      = userId,
                            CreateDate    = dt,
                            AssocCount    = 1,
                            LastAssoc     = dt,
                            InteractCount = 0,
                            Votes         = 0
                        };
                    }
                    else
                    {
                        ptou.AssocCount++;
                    }
                    await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), new UserAssociation[] { utop, ptou });

                    status.status = PeerStatus.Connected;
                }
                if (peerCb != null)
                {
                    peerCb.UserAppMemberRef = peerMb;
                }
                status.peer = peerCb;
                return(status);
            }
            return(null);
        }
 public static async Task<ConnectionStatus> UserConnected(string noticeHubId, string hubId, string peerId, string userId, string connectId, string languages)
 {
     var mbsvc = new UserAppMemberServiceProxy();
     var cntx = Cntx;
     cntx.AcceptLanguages = languages;
     var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);
     if (memb != null)
     {
         memb.StartAutoUpdating = true;
         memb.LastActivityDate = DateTime.UtcNow;
         memb.AcceptLanguages = languages;
         List<MemberCallback> callbacks;
         if (memb.ChangedMemberCallbacks == null)
             callbacks = new List<MemberCallback>();
         else
             callbacks = new List<MemberCallback>(memb.ChangedMemberCallbacks);
         var cbk = (from d in callbacks where d.HubID == hubId && d.ChannelID == peerId select d).SingleOrDefault();
         if (cbk == null)
         {
             cbk = new MemberCallback
             {
                 ApplicationID = AppId,
                 UserID = userId,
                 HubID = hubId,
                 ChannelID = peerId,
                 ConnectionID = connectId,
                 IsDisconnected = false,
                 LastActiveDate = DateTime.UtcNow
             };
         }
         else
         {
             // it is very important to turn this on, otherwise the property will not be marked as modified.
             // and the service will not save the change!
             cbk.StartAutoUpdating = true;
             cbk.ConnectionID = connectId;
             cbk.IsDisconnected = false;
             cbk.LastActiveDate = DateTime.UtcNow;
         }
         memb.ChangedMemberCallbacks = new MemberCallback[] { cbk };
         await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });
         UserServiceProxy usvc = new UserServiceProxy();
         var u = await usvc.LoadEntityByKeyAsync(cntx, userId);
         memb.UserRef = u;
         var peerMb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, peerId);
         ConnectionStatus status = new ConnectionStatus();
         status.me = cbk;
         MemberCallbackServiceProxy mbcsvc = new MemberCallbackServiceProxy();
         UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
         var peerCb = await mbcsvc.LoadEntityByKeyAsync(cntx, userId, hubId, AppId, peerId);
         if (peerCb == null || peerCb.ConnectionID == null || peerCb.IsDisconnected)
         {
             UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
             MemberNotificationTypeServiceProxy ntsvc = new MemberNotificationTypeServiceProxy();
             var ntype = await ntsvc.LoadEntityByKeyAsync(cntx, ApplicationContext.PrivateChatNoticeTypeId);
             var notifier = await mbcsvc.LoadEntityByKeyAsync(cntx, "System", noticeHubId, AppId, peerId);
             if (notifier != null && notifier.ConnectionID != null && !notifier.IsDisconnected)
             {
                 if (utop != null && utop.NoMessages != null && utop.NoMessages == true)
                 {
                     status.status = PeerStatus.InBlackList;
                 }
                 else if (utop != null && utop.DoNotNotify != null && utop.DoNotNotify == true)
                 {
                     status.status = PeerStatus.DoNotDisturb;
                 }
                 else if (utop != null && utop.NotifyButBlock != null && utop.NotifyButBlock == true)
                 {
                     status.peerNotifier = notifier;
                     status.status = PeerStatus.NotifyButBlock;
                 }
                 else
                 {
                     status.peerNotifier = notifier;
                     status.status = PeerStatus.Notifiable;
                 }
             }
             else
             {
                 if (utop == null || utop.NoMessages == null || utop.NoMessages == false)
                     status.status = PeerStatus.LeaveMessage;
             }
             MemberNotification n = new MemberNotification 
             {
                 ID = Guid.NewGuid().ToString(),
                 Title = string.Format(ResourceUtils.GetString("20dc5913998d0e9ed01360475e46a0f9", "{0} invites you to chat, is waiting ...", peerMb.AcceptLanguages), ""),
                 CreatedDate = DateTime.UtcNow,
                 PriorityLevel = 0,
                 ReadCount = 0,
                 ApplicationID = AppId,
                 TypeID = ApplicationContext.PrivateChatNoticeTypeId,
                 UserID = peerId
             };
             bool hasIcon = !string.IsNullOrEmpty(memb.IconMime);
             n.NoticeMsg = "{ \"peerId\": \"" + userId + "\", \"peer\": \"" + u.Username + "\", \"connectId\": \"" + connectId + "\", \"hasIcon\": " + (hasIcon ? "true" : "false") + ", \"msg\": \"" + n.Title + "\", \"isCancel\": false, ";
             if (utop != null && utop.NoMessages != null && utop.NoMessages == true)
                 n.NoticeMsg += "\"noMessages\": true, ";
             else
                 n.NoticeMsg += "\"noMessages\": false, ";
             if (utop != null && utop.DoNotNotify != null && utop.DoNotNotify == true)
                 n.NoticeMsg += "\"notDisturb\": true, ";
             else
                 n.NoticeMsg += "\"notDisturb\": false, ";
             if (utop != null && utop.NotifyButBlock != null && utop.NotifyButBlock == true)
                 n.NoticeMsg += "\"keepNotified\": true }";
             else
                 n.NoticeMsg += "\"keepNotified\": false }";
             n.IsNoticeDataLoaded = true;
             MemberNotificationServiceProxy nsvc = new MemberNotificationServiceProxy();
             var r = await nsvc.AddOrUpdateEntitiesAsync(cntx, new MemberNotificationSet(), new MemberNotification[] { n });
             status.noticeType = ntype.TypeName;
             status.noticeMsg = n.NoticeMsg;
             status.noticeRecId = r.ChangedEntities[0].UpdatedItem.ID;
         }
         else
         {
             DateTime dt = DateTime.UtcNow;
             UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
             if (utop == null)
             {
                 utop = new UserAssociation
                 {
                     TypeID = ApplicationContext.ChatAssocTypeId,
                     FromUserID = userId,
                     ToUserID = peerId,
                     CreateDate = dt,
                     AssocCount = 1,
                     LastAssoc = dt,
                     InteractCount = 0,
                     Votes = 0
                 };
             }
             else
                 utop.AssocCount++;
             UserAssociation ptou = await uasvc.LoadEntityByKeyAsync(cntx, peerId, userId, ApplicationContext.ChatAssocTypeId);
             if (ptou == null)
             {
                 ptou = new UserAssociation
                 {
                     TypeID = ApplicationContext.ChatAssocTypeId,
                     FromUserID = peerId,
                     ToUserID = userId,
                     CreateDate = dt,
                     AssocCount = 1,
                     LastAssoc = dt,
                     InteractCount = 0,
                     Votes = 0
                 };
             }
             else
                 ptou.AssocCount++;
             await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), new UserAssociation[] { utop, ptou });
             status.status = PeerStatus.Connected;
         }
         if (peerCb != null)
             peerCb.UserAppMemberRef = peerMb;
         status.peer = peerCb;
         return status;
     }
     return null;
 }
        public static async Task <MemberCallback> UserConnected(string hubId, string groupId, string userId, string connectId, string languages)
        {
            var mbsvc = new UserAppMemberServiceProxy();
            var cntx  = Cntx;

            cntx.AcceptLanguages = languages;
            var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);

            if (memb != null)
            {
                memb.StartAutoUpdating = true;
                memb.LastActivityDate  = DateTime.UtcNow;
                memb.AcceptLanguages   = languages;
                List <MemberCallback> callbacks;
                if (memb.ChangedMemberCallbacks == null)
                {
                    callbacks = new List <MemberCallback>();
                }
                else
                {
                    callbacks = new List <MemberCallback>(memb.ChangedMemberCallbacks);
                }
                var cbk = (from d in callbacks where d.HubID == hubId && d.ChannelID == groupId select d).SingleOrDefault();
                if (cbk == null)
                {
                    cbk = new MemberCallback
                    {
                        ApplicationID  = AppId,
                        UserID         = userId,
                        HubID          = hubId,
                        ChannelID      = groupId,
                        ConnectionID   = connectId,
                        IsDisconnected = false,
                        LastActiveDate = DateTime.UtcNow
                    };
                }
                else
                {
                    // it is very important to turn this on, otherwise the property will not be marked as modified.
                    // and the service will not save the change!
                    cbk.StartAutoUpdating = true;
                    cbk.ConnectionID      = connectId;
                    cbk.IsDisconnected    = false;
                    cbk.LastActiveDate    = DateTime.UtcNow;
                    // other more explicit way of doing so is given in the following:
                    //cbk.ConnectionID = connectId;
                    //cbk.IsConnectionIDModified = true;
                    //cbk.IsDisconnected = false;
                    //cbk.IsIsDisconnectedModified = true;
                    //cbk.LastActiveDate = DateTime.UtcNow;
                    //cbk.IsLastActiveDateModified = true;
                }
                memb.ChangedMemberCallbacks = new MemberCallback[] { cbk };
                await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });

                cbk.UserAppMemberRef = memb;
                if (memb.UserRef == null)
                {
                    memb.UserRef = await mbsvc.MaterializeUserRefAsync(cntx, memb);
                }
                return(cbk);
            }
            return(null);
        }
 public static async Task<MemberCallback[]> UserConnectionClosed(string userId, string languages)
 {
     var mbsvc = new UserAppMemberServiceProxy();
     var cntx = Cntx;
     cntx.AcceptLanguages = languages;
     var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);
     if (memb != null)
     {
         memb.LastActivityDate = DateTime.UtcNow;
         List<MemberCallback> callbacks = new List<MemberCallback>();
         if (memb.ChangedMemberCallbacks != null)
         {
             foreach (var c in memb.ChangedMemberCallbacks)
             {
                 callbacks.Add(c.ShallowCopy());
                 c.StartAutoUpdating = true;
                 c.ConnectionID = null;
                 c.IsDisconnected = true;
                 c.LastActiveDate = DateTime.UtcNow;
             }
         }
         await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });
         return (from d in callbacks where d.ConnectionID != null && !d.IsDisconnected select d).ToArray();
     }
     return null;
 }