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!"); } }
public void RemoveMember(string groupName, string memberUsername) { ChatGroup chatGroup = chatGroups.Where(x => x.GroupName.Equals(groupName)).FirstOrDefault(); if (chatGroup != null) { ChatUser userToRemove = chatGroup.GroupUsers.Where(x => x.Username.Equals(memberUsername)).FirstOrDefault(); if (userToRemove != null) { string removedConnection = userToRemove.ConnectionId; Groups.Remove(removedConnection, groupName); chatGroup.GroupUsers.Remove(userToRemove); List <string> connectionIds = LMSChatHelper.GenerateConnectionIds(chatGroup.GroupUsers); Clients.Clients(connectionIds).UpdateGroup(new { success = true, group = chatGroup, status = "updated" }); Clients.Client(removedConnection).RemovedFromGroup(new { success = true, group = chatGroup }); } else { Clients.Caller.ShowMessage("User: "******" is not in group: " + groupName + "."); } } else { Clients.Caller.ShowMessage("Error while removing member from group: " + groupName + "."); } }
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 + "."); } }
public void SendGroupMessage(string messageText, string groupName, string senderUsername) { ChatGroup chatGroup = chatGroups.Where(x => x.GroupName.Equals(groupName)).FirstOrDefault(); if (chatGroup != null) { ChatUser messageSender = connectedChatUsers.Where(x => x.Username.Equals(senderUsername)).FirstOrDefault(); MessageDetails message = LMSChatHelper.GenerateMessageDetails(messageText, messageSender); chatGroup.GroupMessages.Add(message); List <string> connectionIds = LMSChatHelper.GenerateConnectionIds(chatGroup.GroupUsers); Clients.Clients(connectionIds).UpdateChat(messageSender, messageText, chatGroup.GroupName); } else { Clients.Caller.ShowMessage("Error while sending message."); } }