/// <summary>
        /// Inserts a chat user.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the user.</param>
        /// <param name="chatUser">The chat user to add.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task AddUserAsync(ChatRoomInfo chatRoom, ChatUserInfo chatUser, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (chatUser == null)
            {
                throw new ArgumentNullException("chatUser");
            }

            chatUser.RoomId = chatRoom.Id;

            var users = s_users.Get(chatRoom.Id) as ConcurrentDictionary <string, ChatUserInfo>;

            if (users == null)
            {
                users = new ConcurrentDictionary <string, ChatUserInfo>();
            }
            if (users.TryAdd(chatUser.UserName, chatUser))
            {
                s_users.Set(chatRoom.Id, users, DateTimeOffset.MaxValue);
            }
            return(Task.FromResult(0));
        }
        /// <summary>
        /// Gets all the users for the given chat room.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the chat users.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// The chat users.
        /// </returns>
        public virtual async Task <ContactItem> GetContactByUserNameAsync(ChatRoomInfo chatRoom, string userName, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (ProjectManagerFactory == null)
            {
                throw new InvalidOperationException("ProjectManagerFactory is not supported.");
            }
            var projectManager = ProjectManagerFactory();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (chatRoom.ProjectId == null)
            {
                return(null);
            }
            var project = await projectManager.FindByIdAsync((int)chatRoom.ProjectId, cancellationToken);

            if (project == null)
            {
                throw new InvalidOperationException("The project was not found.");
            }
            return(await projectManager.GetContactByMailAsync(project, userName, ContactField.None, cancellationToken));
        }
        /// <summary>
        /// Inserts a chat message.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the chat message.</param>
        /// <param name="chatMessage">The chat message to add.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        protected virtual async Task LogMessageAsync(ChatRoomInfo chatRoom, ChatMessageInfo chatMessage, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            var logManager = GetLogManager();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (chatMessage == null)
            {
                throw new ArgumentNullException("chatMessage");
            }
            if (chatMessage.From == null)
            {
                throw new InvalidOperationException("The user 'from' is unspecified.");
            }
            if (chatMessage.To == null)
            {
                throw new InvalidOperationException("The user 'to' is unspecified.");
            }
            if (chatMessage.From.Admin)
            {
                await logManager.LogAsync(new EventItem
                {
                    AnonymId   = GuidUtility.NewSequentialGuid(),
                    ObjectType = ObjectType.Message,
                    ObjectId   = chatRoom.PageId,
                    Contact    = chatMessage.From.ContactId != null ? new AccountItem {
                        Id = (int)chatMessage.From.ContactId
                    } : null,
                    Project = chatRoom.ProjectId != null ? new UniqueItem {
                        Id = (int)chatRoom.ProjectId
                    } : null,
                    ClientId  = chatMessage.To.IPAddress,
                    CustomUri = chatMessage.To.NickName + " >>",
                    Message   = chatMessage.Message
                }, chatRoom.Users.Values, cancellationToken);
            }
            if (chatMessage.To.Admin)
            {
                await logManager.LogAsync(new EventItem
                {
                    AnonymId   = GuidUtility.NewSequentialGuid(),
                    ObjectType = ObjectType.Message,
                    ObjectId   = chatRoom.PageId,
                    Contact    = chatMessage.From.ContactId != null ? new AccountItem {
                        Id = (int)chatMessage.From.ContactId
                    } : null,
                    Project = chatRoom.ProjectId != null ? new UniqueItem {
                        Id = (int)chatRoom.ProjectId
                    } : null,
                    ClientId  = chatMessage.From.IPAddress,
                    CustomUri = "<< " + chatMessage.From.NickName,
                    Message   = chatMessage.Message
                }, chatRoom.Users.Values, cancellationToken);
            }
        }
 /// <summary>
 /// Returns true if the given user is an administrator.
 /// </summary>
 /// <param name="chatRoom">The chat room.</param>
 /// <returns>
 /// A value indicating whether the user is an administator.
 /// </returns>
 public virtual bool IsAdmin(ChatRoomInfo chatRoom, string userName)
 {
     ThrowIfDisposed();
     if (chatRoom == null)
     {
         throw new ArgumentNullException("chatRoom");
     }
     return(chatRoom.Users.ContainsKey(userName));
 }
 /// <summary>
 /// Removes a chat room.
 /// </summary>
 /// <param name="chatRoom">The chat room to remove.</param>
 /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
 /// <returns>
 /// A task that represents the asynchronous operation.
 /// </returns>
 public virtual Task DeleteAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     if (chatRoom == null)
     {
         throw new ArgumentNullException("chatRoom");
     }
     return(Store.DeleteAsync(chatRoom, cancellationToken));
 }
 /// <summary>
 /// Removes a chat room.
 /// </summary>
 /// <param name="chatRoom">The chat room to remove.</param>
 /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
 /// <returns>
 /// A task that represents the asynchronous operation.
 /// </returns>
 public virtual Task DeleteAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     if (chatRoom == null)
     {
         throw new ArgumentNullException("chatRoom");
     }
     s_rooms.Remove(chatRoom.Id);
     return(Task.FromResult(0));
 }
 /// <summary>
 /// Updates a chat room.
 /// </summary>
 /// <param name="chatRoom">The chat room to update.</param>
 /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
 /// <returns>
 /// A task that represents the asynchronous operation.
 /// </returns>
 public virtual Task UpdateAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     if (chatRoom == null)
     {
         throw new ArgumentNullException("chatRoom");
     }
     //Rooms.Set(chatRoom.Id, chatRoom, DateTimeOffset.MaxValue);
     return(Task.FromResult(0));
 }
        /// <summary>
        /// Gets all the Connection IDs for the given chat room.
        /// </summary>
        /// <param name="chatRoom">The chat room.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <IList <string> > GetConnectionsAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            var userConnectionStore = GetUserConnectionStore();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            return(userConnectionStore.GetConnectionsAsync(chatRoom, cancellationToken));
        }
 /// <summary>
 /// Removes a chat message.
 /// </summary>
 /// <param name="chatRoom">The chat room which contains the chat message.</param>
 /// <param name="chatMessage">The chat message to remove.</param>
 /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
 /// <returns>
 /// A task that represents the asynchronous operation.
 /// </returns>
 public virtual Task RemoveMessageAsync(ChatRoomInfo chatRoom, ChatMessageInfo chatMessage, CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     if (chatRoom == null)
     {
         throw new ArgumentNullException("chatRoom");
     }
     if (chatMessage == null)
     {
         throw new ArgumentNullException("chatMessage");
     }
     return(Task.FromResult(0));
 }
Example #10
0
        /// <summary>
        /// Gets the information from the cache for the chat user associated with the specified unique identifier.
        /// </summary>
        /// <param name="chatRoom">The chat site which owns the user.</param>
        /// <param name="userName">The user name for the item to be found.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <ChatUserInfo> GetUserByUserNameAsync(ChatRoomInfo chatRoom, string userName, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            var userStore = GetUserStore();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("userName");
            }
            return(userStore.GetUserByUserNameAsync(chatRoom, userName, cancellationToken));
        }
        //
        // IChatRoomUserStore
        //

        /// <summary>
        /// Gets all the users for the given chat room.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the chat users.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <IList <ChatUserInfo> > GetUsersAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }

            var users = s_users.Get(chatRoom.Id) as ConcurrentDictionary <string, ChatUserInfo>;

            if (users != null)
            {
                return(Task.FromResult <IList <ChatUserInfo> >(users.Values.ToList()));
            }
            return(Task.FromResult <IList <ChatUserInfo> >(new List <ChatUserInfo>()));
        }
        //
        // IChatRoomConnectionStore
        //

        /// <summary>
        /// Gets all the connections for the given chat room.
        /// </summary>
        /// <param name="chatRoom">The chat room.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <IList <string> > GetConnectionsAsync(ChatRoomInfo chatRoom, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }

            var connections = s_roomConnections.Get(chatRoom.Id) as ImmutableHashSet <string>;

            if (connections != null)
            {
                return(Task.FromResult <IList <string> >(connections.ToList()));
            }
            return(Task.FromResult <IList <string> >(new List <string>()));
        }
Example #13
0
        /// <summary>
        /// Inserts a chat user.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the user.</param>
        /// <param name="chatUser">The chat user to add.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual async Task AddUserAsync(ChatRoomInfo chatRoom, ChatUserInfo chatUser, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            var userStore = GetUserStore();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (chatUser == null)
            {
                throw new ArgumentNullException("chatUser");
            }
            await userStore.AddUserAsync(chatRoom, chatUser, cancellationToken);

            await UpdateAsync(chatRoom, cancellationToken);
        }
Example #14
0
        /// <summary>
        /// Removes a chat message.
        /// </summary>
        /// <param name="chatRoom">The chat room which contains the chat message.</param>
        /// <param name="chatMessage">The chat message to remove.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual async Task RemoveMessageAsync(ChatRoomInfo chatRoom, ChatMessageInfo chatMessage, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            var userMessage = GetUserMessageStore();

            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (chatMessage == null)
            {
                throw new ArgumentNullException("chatMessage");
            }
            await userMessage.RemoveMessageAsync(chatRoom, chatMessage, cancellationToken);

            await UpdateAsync(chatRoom, cancellationToken);
        }
Example #15
0
        /// <summary>
        /// Creates a room for the given page.
        /// </summary>
        /// <param name="manager">The portal manager.</param>
        /// <param name="portal">The site.</param>
        /// <param name="page">The page.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// The created chat room.
        /// </returns>
        private async Task <ChatRoomInfo> CreateAsync(PortalManager manager, PortalItem portal, PageItem page, CancellationToken cancellationToken)
        {
            var chatRoom = new ChatRoomInfo
            {
                Id        = portal.Id.ToString(),
                PageId    = page.Id,
                PortalId  = portal.Id,
                ProjectId = portal.Project?.Id
            };

            foreach (var account in portal.Owners)
            {
                chatRoom.Users.Add(account.Email.Address, account);
            }
            await Store.CreateAsync(chatRoom, cancellationToken);

            return(chatRoom);
        }
Example #16
0
        /// <summary>
        /// Gets the chat user for the caller.
        /// </summary>
        /// <param name="chatRoom">The chat room.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// The chat user.
        /// </returns>
        private async Task <ChatUserInfo> GetCallerUserAsync(ChatRoomInfo chatRoom = null,
                                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            if (chatRoom == null)
            {
                chatRoom = await GetCallerRoomAsync(cancellationToken);
            }
            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            string user = Clients.Caller.user;

            if (user != null)
            {
                return(await ChatManager.GetUserByUserNameAsync(chatRoom, user, cancellationToken));
            }
            return(null);
        }
        /// <summary>
        /// Gets the information from the cache for the chat user associated with the specified unique identifier.
        /// </summary>
        /// <param name="chatRoom">The chat room which owns the user.</param>
        /// <param name="userName">The user name for the item to be found.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <ChatUserInfo> GetUserByUserNameAsync(ChatRoomInfo chatRoom, string userName, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (chatRoom == null)
            {
                throw new ArgumentNullException("chatRoom");
            }
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }

            var user  = default(ChatUserInfo);
            var users = s_users.Get(chatRoom.Id) as ConcurrentDictionary <string, ChatUserInfo>;

            if (users != null)
            {
                users.TryGetValue(userName, out user);
            }
            return(Task.FromResult(user));
        }
Example #18
0
        /// <summary>
        /// Gets or adds a new user to the given room.
        /// </summary>
        /// <param name="room">The room.</param>
        /// <param name="userName">The user name.</param>
        /// <param name="nickName">The nick name.</param>
        /// <param name="ipAddress">The IP address.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// The chat user.
        /// </returns>
        private async Task <ChatUserInfo> GetOrAddRoomUserAsync(ChatRoomInfo room, string userName, string ipAddress,
                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            int?   contactId = null;
            string nickName  = null;

            if (!string.IsNullOrEmpty(userName))
            {
                var contact = await ChatManager.GetContactByUserNameAsync(room, userName, cancellationToken);

                if (contact != null)
                {
                    contactId = contact.Id;
                    userName  = contact.Email.Address;
                    nickName  = contact.NickName ?? contact.Email.Name ?? contact.FirstName ?? contact.LastName;
                }
            }
            if (string.IsNullOrEmpty(userName))
            {
                userName = ChatManager.GenerateClientId(ipAddress);
                nickName = nickName ?? userName;
            }
            var user = await ChatManager.GetUserByUserNameAsync(room, userName, cancellationToken);

            if (user == null)
            {
                user = new ChatUserInfo
                {
                    ContactId = contactId,
                    ClientId  = ChatManager.GenerateClientId(ipAddress),
                    IPAddress = ipAddress,
                    UserName  = userName,
                    NickName  = nickName
                };
                await ChatManager.AddUserAsync(room, user, cancellationToken);
            }
            return(user);
        }