Exemplo n.º 1
0
        public static void new_chat(Guid applicationId, Guid currentUserId, Dictionary <string, string> data)
        {
            Guid userId = Guid.Empty;

            if (!Guid.TryParse(data["UserID"], out userId))
            {
                return;
            }

            if (!UserGroups.ContainsKey(currentUserId))
            {
                UserGroups[currentUserId] = new List <UserGroup>();
            }

            UserGroup group = UserGroups[currentUserId].Where(
                u => u.Private == true && ChatGroups.ContainsKey(u.GroupID) &&
                ChatGroups[u.GroupID].Any(v => v == userId) && (
                    (userId == currentUserId && ChatGroups[u.GroupID].Count == 1) ||
                    (userId != currentUserId && ChatGroups[u.GroupID].Count == 2)
                    )).FirstOrDefault();

            string groupId = string.Empty;

            if (group != null)
            {
                groupId = group.GroupID;
            }
            else
            {
                groupId = Guid.NewGuid().ToString();
                if (!UserGroups.ContainsKey(userId))
                {
                    UserGroups[userId] = new List <UserGroup>();
                }

                UserGroups[currentUserId].Add(new UserGroup()
                {
                    Private = true, GroupID = groupId
                });
                ChatGroups[groupId] = new List <Guid>()
                {
                    currentUserId
                };
                if (userId != currentUserId)
                {
                    UserGroups[userId].Add(new UserGroup()
                    {
                        Private = true, GroupID = groupId
                    });
                    ChatGroups[groupId].Add(userId);
                }
            }

            prepare_user(applicationId, userId);

            RaaiVanHub.SendData(applicationId, currentUserId, RealTimeAction.ChatWindow,
                                "{\"GroupID\":\"" + groupId + "\",\"User\":" + _get_user_json(applicationId, userId) + "}");
        }
Exemplo n.º 2
0
        public static void new_chat_member(Guid applicationId, Guid currentUserId, Dictionary <string, string> data)
        {
            Guid userId = Guid.Empty;

            if (!data.ContainsKey("GroupID") || !Guid.TryParse(data["UserID"], out userId))
            {
                return;
            }

            string groupId = data["GroupID"];

            if (!ChatGroups.ContainsKey(groupId))
            {
                return;
            }

            List <Guid> audience = new List <Guid>()
            {
                currentUserId
            };

            var result = "";

            if (ChatGroups[groupId].Contains(userId))
            {
                result = "{\"ErrorText\":\"" + "Sorry, " + userId + " is already in group chat." + "\"}";
            }
            else
            {
                ChatGroups[groupId].Add(userId);
                audience = ChatGroups[groupId].Where(u => u != userId && u != currentUserId).Select(v => v).ToList();

                foreach (Guid uId in audience)
                {
                    UserGroup ug = !UserGroups.ContainsKey(uId) ? null :
                                   UserGroups[uId].Where(u => u.GroupID == groupId).FirstOrDefault();
                    if (ug != null)
                    {
                        ug.Private = false;
                    }
                }

                UserGroups[userId].Add(new UserGroup()
                {
                    Private = false, GroupID = groupId
                });

                prepare_user(applicationId, userId);
                result = _get_user_json(applicationId, userId);
            }

            RaaiVanHub.SendData(applicationId, audience, RealTimeAction.NewChatMember,
                                "{\"GroupID\":\"" + groupId + "\",\"Data\":" + result + "}");
        }
Exemplo n.º 3
0
        public override Task OnDisconnected(bool stopCalled)
        {
            Guid userId = ConnectedUsers.ContainsKey(Context.ConnectionId) ?
                          ConnectedUsers[Context.ConnectionId].UserID : Guid.Empty;

            if (userId == Guid.Empty)
            {
                return(base.OnDisconnected(stopCalled));
            }

            Guid tenantId = Guid.Empty;

            try
            {
                tenantId = ConnectedUsers.ContainsKey(Context.ConnectionId) ?
                           ConnectedUsers[Context.ConnectionId].ApplicationID : Guid.Empty;
            }
            catch (Exception ex) { }

            ConnectedUsers.Remove(Context.ConnectionId);

            if (tenantId == Guid.Empty)
            {
                return(base.OnDisconnected(stopCalled));
            }

            if (UserConnectionsDic.ContainsKey(userId) && UserConnectionsDic[userId].Any(u => u == Context.ConnectionId))
            {
                UserConnectionsDic[userId].Remove(Context.ConnectionId);

                //if the user is now offline, tell his/her friends
                if (UserConnectionsDic[userId].Count == 0)
                {
                    UserConnectionsDic.Remove(userId);

                    if (UsersDic.ContainsKey(userId))
                    {
                        UsersDic.Remove(userId);
                    }
                    if (FriendsDic.ContainsKey(userId))
                    {
                        FriendsDic.Remove(userId);
                    }

                    RaaiVanHub.SendData(tenantId, get_friends(tenantId, userId), RealTimeAction.WentOffline,
                                        "{\"UserID\":\"" + userId.ToString() + "\"}");
                }
            }

            return(base.OnDisconnected(stopCalled));
        }
Exemplo n.º 4
0
        public override Task OnConnected()
        {
            Guid tenantId = Guid.Empty;

            try
            {
                tenantId = PublicMethods.get_current_tenant(Context.Request.GetHttpContext().GetOwinContext().Request,
                                                            RaaiVanSettings.Tenants).Id;
            }
            catch (Exception ex) { return(base.OnConnected()); }

            if (!RaaiVanSettings.RealTime(tenantId))
            {
                return(base.OnConnected());
            }

            Guid currentUserId = get_current_user_id();

            if (currentUserId == Guid.Empty)
            {
                return(base.OnConnected());
            }

            if (!UserConnectionsDic.ContainsKey(currentUserId))
            {
                UserConnectionsDic[currentUserId] = new List <string>();
            }

            bool added = false;

            if (!UserConnectionsDic[currentUserId].Any(u => u == Context.ConnectionId))
            {
                UserConnectionsDic[currentUserId].Add(Context.ConnectionId);
                added = true;
            }

            ConnectedUsers.Add(Context.ConnectionId,
                               new Registration(Context.ConnectionId, tenantId, currentUserId, new List <string>()));

            //if the user is now online, tell his/her friends
            if (UserConnectionsDic[currentUserId].Count == 1 && added)
            {
                prepare_user(tenantId, currentUserId);
                string userJson = _get_user_json(tenantId, currentUserId);

                RaaiVanHub.SendData(tenantId, get_friends(tenantId, currentUserId), RealTimeAction.IsOnline, userJson);
            }

            return(base.OnConnected());
        }
Exemplo n.º 5
0
        private static void hide_is_typing(Guid applicationId, Guid currentUserId, Dictionary <string, string> data)
        {
            if (!data.ContainsKey("GroupID") || currentUserId == Guid.Empty)
            {
                return;
            }

            string groupId = data["GroupID"];

            if (!ChatGroups.ContainsKey(groupId))
            {
                return;
            }

            IHubContext context = GlobalHost.ConnectionManager.GetHubContext <RaaiVanHub>();

            prepare_user(applicationId, currentUserId);

            RaaiVanHub.SendData(applicationId, ChatGroups[groupId].Where(u => u != currentUserId).ToList(), RealTimeAction.IsNotTyping,
                                "{\"GroupID\":\"" + groupId + "\",\"User\":" + _get_user_json(applicationId, currentUserId) + "}");
        }