Example #1
0
        public void CreateChatGroup(string creatorUsername, string groupName, string message, List <int> memberIds)
        {
            if (LMSChatHelper.IsGroupUnique(chatGroups, groupName))
            {
                ChatUser groupCreator = connectedChatUsers.Where(user => user.Username.Equals(creatorUsername)).FirstOrDefault();
                if (groupCreator != null)
                {
                    ChatGroup      chatGroup      = new ChatGroup(groupName, groupCreator);
                    MessageDetails messageDetails = LMSChatHelper.GenerateMessageDetails(message, groupCreator);
                    chatGroup.GroupMessages.Add(messageDetails);

                    List <ChatUser> members = LMSChatHelper.GenerateGroupMembers(connectedChatUsers, memberIds);
                    if (!members.Contains(groupCreator))
                    {
                        members.Add(groupCreator);
                    }

                    chatGroup.GroupUsers = members;

                    chatGroups.Add(chatGroup);

                    List <string> connectionIds = LMSChatHelper.GenerateConnectionIds(members);
                    connectionIds.ForEach(x => Groups.Add(x, groupName));

                    Clients.Clients(connectionIds).UpdateGroup(new { success = true, group = chatGroup, status = "created" });
                }
            }
            else
            {
                Clients.Caller.ShowMessage("Group name is not unique. Please try with another group name!");
            }
        }
Example #2
0
        public void AddNewMembers(string groupName, List <int> newMembersIds)
        {
            ChatGroup     chatGroup      = chatGroups.Where(x => x.GroupName.Equals(groupName)).FirstOrDefault();
            List <string> newConnections = new List <string>();
            string        newConnection  = "";

            if (chatGroup != null)
            {
                List <ChatUser> members = LMSChatHelper.GenerateGroupMembers(connectedChatUsers, newMembersIds);
                foreach (ChatUser member in members)
                {
                    bool exists = chatGroup.GroupUsers.Any(x => x.UserId.Equals(member.UserId));
                    if (!exists)
                    {
                        chatGroup.GroupUsers.Add(member);
                        newConnection = member.ConnectionId;
                        newConnections.Add(newConnection);
                        Groups.Add(newConnection, groupName);
                        Clients.Client(newConnection).UpdateGroup(new { success = true, group = chatGroup, status = "created" });
                    }
                }
                List <string> connectionIds = LMSChatHelper.GenerateConnectionIds(chatGroup.GroupUsers);
                newConnections.ForEach(x => connectionIds.Remove(x));
                Clients.Clients(connectionIds).UpdateGroup(new { success = true, group = chatGroup, status = "updated" });
            }
            else
            {
                Clients.Caller.ShowMessage("Error while adding new members to group: " + groupName + ".");
            }
        }