public void LoadTypers(int id, ChatParticipantType participantType, int chatID, int seconds)
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = @"  
SELECT cp.*, cc.FirstName, cc.LastName, cc.Email, cc.CompanyName, 
CASE WHEN DATEDIFF(second, cc.LastPing, GETUTCDATE()) < 15 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS IsOnline
FROM ChatParticipants cp
LEFT JOIN ChatClients cc ON cc.ChatClientID = cp.ParticipantID
WHERE GETUTCDATE() < DATEADD(second, @Seconds, cp.LastTyped)
AND cp.ChatID = @ChatID
AND NOT (cp.ParticipantType = @ParticipantType AND cp.ParticipantID = @ParticipantID)
AND cp.ParticipantTYpe = 1

UNION

SELECT cp.*, u.FirstName, u.LastName, u.Email, o.Name, 
CASE WHEN DATEDIFF(second, u.LastPing, GETUTCDATE()) < 15 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS IsOnline
FROM ChatParticipants cp
LEFT JOIN Users u ON u.UserID = cp.ParticipantID
LEFT JOIN Organizations o ON o.OrganizationID = u.OrganizationID
WHERE GETUTCDATE() < DATEADD(second, @Seconds, cp.LastTyped)
AND cp.ChatID = @ChatID
AND NOT (cp.ParticipantType = @ParticipantType AND cp.ParticipantID = @ParticipantID)
AND cp.ParticipantTYpe = 0
";
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@ChatID", chatID);
                command.Parameters.AddWithValue("@ParticipantType", (int)participantType);
                command.Parameters.AddWithValue("@Seconds", seconds);
                command.Parameters.AddWithValue("@ParticipantID", id);
                Fill(command);
            }
        }
Example #2
0
 public ParticipationCandidate(Guid userId, ChatParticipantType chatParticipantType, string style = null, string metadata = null)
 {
     UserId = userId;
     ChatParticipantType = chatParticipantType;
     Style    = style;
     Metadata = metadata;
 }
        public async Task <ChatParticipant> Set(Guid chatId, ChatUser chatUser, ChatParticipantType participantType, ChatParticipantStatus participantStatus,
                                                Guid setterId)
        {
            await Task.Yield();

            return(SetUser(chatId, chatUser, participantType, participantStatus));
        }
Example #4
0
 public ApplyToChatCommand(Guid initiatorUserId, Guid chatId, ChatParticipantType chatParticipantType, string style = null, string metadata = null) : base(initiatorUserId)
 {
     ChatId = chatId;
     ChatParticipantType = chatParticipantType;
     Style    = style;
     Metadata = metadata;
 }
        protected virtual bool IsHigherParticipantType(ChatParticipantType participantType, ChatParticipantType compareToParticipantType)
        {
            var typesPriority  = ParticipantsTypesPriority;
            var targetIndex    = typesPriority.IndexOf(participantType);
            var compareToIndex = typesPriority.IndexOf(compareToParticipantType);

            return(targetIndex > compareToIndex);
        }
Example #6
0
        public virtual async Task Apply(Guid currentUserId, Guid chatId, ChatParticipantType chatParticipantType, string style = null, string metadata = null)
        {
            await ChatParticipantsPermissionValidator.ValidateApply(currentUserId, chatId, chatParticipantType, style, metadata, ServiceName)
            .ConfigureAwait(false);

            var command = ChatParticipantsCommandBuilder.BuildApplyToChatCommand(currentUserId, chatId, chatParticipantType, style, metadata);
            await ChatCommandSender.Send(command).ConfigureAwait(false);
        }
Example #7
0
 public ChatParticipant(ChatParticipantType chatParticipantType, ChatParticipantStatus chatParticipantStatus, DateTime readTimestamp, long readIndex, long version, Guid userId, string name, string details, Guid?avatarId = null, bool inviteOnly = false, bool canCreateChat = true, string style = null, string metadata = null)
     : base(userId, name, details, avatarId, inviteOnly, canCreateChat, style, metadata)
 {
     ChatParticipantType   = chatParticipantType;
     ChatParticipantStatus = chatParticipantStatus;
     ReadTimestamp         = readTimestamp;
     ReadIndex             = readIndex;
     Version = version;
 }
        public async Task <ChatParticipant> ChangeType(Guid chatId, Guid userId, ChatParticipantType participantType, Guid setterId)
        {
            await Task.Yield();

            var p = Store.Chats[chatId].Participants.FirstOrDefault(r => r.UserId == userId);

            p.ChatParticipantType = participantType;
            p.Version            += 1;
            return(p);
        }
        public virtual async Task ValidateApply(Guid currentUserId, Guid chatId, ChatParticipantType chatParticipantType, string serviceName, string style, string metadata,
                                                string methodName = null)
        {
            var chat = await ReadChatStore.Retrieve(chatId);

            if (chat == null)
            {
                throw new DotChatNotFoundChatException(ErrorModule.Security, ErrorOperation.Get, chatId, currentUserId);
            }
            if (chat.PrivacyMode.NotIn(ChatPrivacyMode.Public, ChatPrivacyMode.Protected))
            {
                throw new DotChatAccessDeniedException(new ErrorCode(ErrorType.AccessDeniedDueToAdvancedProtection, ErrorModule.Security, ErrorOperation.Add, ErrorEntity.Participant),
                                                       serviceName, methodName, chatId, currentUserId);
            }
        }
Example #10
0
        public static ChatMessageProxy LeaveChat(LoginUser loginUser, int id, ChatParticipantType type, int chatID)
        {
            Chat            chat = Chats.GetChat(loginUser, chatID);
            ChatParticipant self = ChatParticipants.GetChatParticipant(loginUser, id, type, chatID);

            if (self == null || self.DateLeft != null)
            {
                return(null);
            }

            self.DateLeft = DateTime.UtcNow;
            self.Collection.Save();

            ChatMessageProxy message = AddNotification(loginUser, chatID, id, type, string.Format("{0} {1} has left the chat.", self.FirstName, self.LastName));

            if (self.ParticipantType != ChatParticipantType.User)
            {
                return(message);
            }

            ChatParticipants participants = new ChatParticipants(loginUser);

            participants.LoadByChatID(chatID);

            bool allUsersGone = true;

            foreach (ChatParticipant item in participants)
            {
                if (item.DateLeft == null && item.ParticipantType == ChatParticipantType.User)
                {
                    allUsersGone = false;
                    break;
                }
            }

            if (allUsersGone)
            {
                foreach (ChatParticipant item in participants)
                {
                    if (item.DateLeft == null)
                    {
                        LeaveChat(loginUser, item.ParticipantID, item.ParticipantType, chatID);
                    }
                }
            }

            return(message);
        }
        public static void UpdateTyping(LoginUser loginUser, int id, ChatParticipantType participantType, int chatID)
        {
            ChatParticipants chatParticipants = new ChatParticipants(loginUser);

            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = @"  
UPDATE ChatParticipants SET LastTyped = GETUTCDATE()
WHERE ChatID = @ChatID AND ParticipantType = @ParticipantType AND ParticipantID = @ParticipantID
";
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@ChatID", chatID);
                command.Parameters.AddWithValue("@ParticipantType", (int)participantType);
                command.Parameters.AddWithValue("@ParticipantID", id);
                chatParticipants.ExecuteNonQuery(command, "ChatParticipants");
            }
        }
        private ChatParticipant SetUser(Guid chatId, ChatUser chatUser, ChatParticipantType participantType, ChatParticipantStatus participantStatus)
        {
            var participants = Store.Chats[chatId].Participants;

            lock (participants)
            {
                var exist = participants.FirstOrDefault(r => r.UserId == chatUser.UserId);
                if (exist == null)
                {
                    exist = new ChatParticipant(participantType, participantStatus, DateTime.MinValue, -1, 0, chatUser);
                    participants.Add(exist);
                }
                else
                {
                    exist.Version              += 1;
                    exist.ChatParticipantType   = participantType;
                    exist.ChatParticipantStatus = participantStatus;
                }
                return(exist);
            }
        }
Example #13
0
        public static void JoinChat(LoginUser loginUser, int id, ChatParticipantType type, int chatID, string ipAddress)
        {
            ChatMessages    messages = new ChatMessages(loginUser);
            ChatParticipant part     = (new ChatParticipants(loginUser)).AddNewChatParticipant();

            part.ChatID          = chatID;
            part.DateJoined      = DateTime.UtcNow;
            part.ParticipantID   = id;
            part.ParticipantType = type;
            part.IPAddress       = ipAddress;
            part.LastTyped       = DateTime.UtcNow.AddYears(-10);
            part.LastMessageID   = messages.GetLastMessageID(chatID);
            part.Collection.Save();

            part = ChatParticipants.GetChatParticipant(loginUser, id, type, chatID);
            if (part == null)
            {
                return;
            }

            AddNotification(loginUser, chatID, id, type, string.Format("{0} {1} has joined the chat.", part.FirstName, part.LastName));
        }
Example #14
0
        public int GetUnreadMessageCount(int participantID, ChatParticipantType participantType)
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = @"
SELECT COUNT(*) FROM ChatMessages cm
LEFT JOIN ChatParticipants cp ON cp.ChatID = cm.ChatID 
WHERE cm.ChatMessageID > cp.LastMessageID
AND cp.ParticipantType = @ParticipantType 
AND cp.ParticipantID = @ParticipantID 
AND cp.DateLeft IS NULL
AND NOT (cm.PosterID = @ParticipantID AND cm.PosterType = @ParticipantType)

";
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@ParticipantType", (int)participantType);
                command.Parameters.AddWithValue("@ParticipantID", participantID);

                object o = ExecuteScalar(command);
                return(o == DBNull.Value ? 0 : (int)o);
            }
        }
Example #15
0
 public virtual IApplyToChatCommand BuildApplyToChatCommand(Guid currentUserId, Guid chatId, ChatParticipantType chatParticipantType, string style, string metadata)
 {
     return(new ApplyToChatCommand(currentUserId, chatId, chatParticipantType, style, metadata));
 }
        public static ParticipantInfo GetParticipantInfo(LoginUser loginUser, int id, ChatParticipantType type)
        {
            ParticipantInfo result = null;

            switch (type)
            {
            case ChatParticipantType.User:
                UsersViewItem user = UsersView.GetUsersViewItem(loginUser, id);
                if (user != null)
                {
                    result = new ParticipantInfo(user.FirstName, user.LastName, user.Email, user.Organization, user.IsOnline);
                }
                break;

            case ChatParticipantType.External:
                ChatClientsViewItem client = ChatClientsView.GetChatClientsViewItem(loginUser, id);
                if (client != null)
                {
                    result = new ParticipantInfo(client.FirstName, client.LastName, client.Email, client.CompanyName, client.IsOnline);
                }
                break;

            default:
                break;
            }
            return(result);
        }
        public async Task <IReadOnlyCollection <ChatParticipant> > Set(Guid chatId, IEnumerable <ChatUser> chatUsers, ChatParticipantType participantType,
                                                                       ChatParticipantStatus participantStatus, Guid setterId)
        {
            await Task.Yield();

            return(chatUsers.Select(chatUser => SetUser(chatId, chatUser, participantType, participantStatus)).ToList());
        }
        public static ChatParticipant GetChatParticipant(LoginUser loginUser, int id, ChatParticipantType type, int chatID)
        {
            ChatParticipants chatParticipants = new ChatParticipants(loginUser);

            using (SqlCommand command = new SqlCommand())
            {
//SELECT * FROM ChatParticipants WHERE ChatID = @ChatID AND ParticipantType = @ParticipantType AND ParticipantID = @ParticipantID
                if (type == ChatParticipantType.External)
                {
                    command.CommandText = @"
SELECT cp.*, cc.FirstName, cc.LastName, cc.Email, cc.CompanyName,
CASE WHEN DATEDIFF(second, cc.LastPing, GETUTCDATE()) < 15 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS IsOnline
FROM ChatParticipants cp
LEFT JOIN ChatClients cc ON cc.ChatClientID = cp.ParticipantID
WHERE cp.ChatID = @ChatID
AND cp.ParticipantType = 1
AND cp.ParticipantID = @ParticipantID
";
                }
                else
                {
                    command.CommandText = @"
SELECT cp.*, u.FirstName, u.LastName, u.Email, o.Name, 
CASE WHEN DATEDIFF(second, u.LastPing, GETUTCDATE()) < 15 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS IsOnline
FROM ChatParticipants cp
LEFT JOIN Users u ON u.UserID = cp.ParticipantID
LEFT JOIN Organizations o ON o.OrganizationID = u.OrganizationID
WHERE cp.ChatID = @ChatID
AND cp.ParticipantType = 0
AND cp.ParticipantID = @ParticipantID
";
                }
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@ChatID", chatID);
                command.Parameters.AddWithValue("@ParticipantID", id);
                chatParticipants.Fill(command);
            }
            if (chatParticipants.IsEmpty)
            {
                return(null);
            }
            return(chatParticipants[0]);
        }
        public virtual async Task ValidateInvite(Guid currentUserId, Guid chatId, Guid userId, ChatParticipantType chatParticipantType, string style, string metadata,
                                                 string serviceName, string methodName = null)
        {
            var participant = await ReadChatParticipantStore.Retrieve(chatId, currentUserId);

            if (CanNotAddParticipant(participant))
            {
                throw new DotChatAccessDeniedException(new ErrorCode(ErrorType.AccessDenied, ErrorModule.Security, ErrorOperation.Add, ErrorEntity.Participant),
                                                       serviceName, methodName, chatId, currentUserId);
            }

            if (IsHigherParticipantType(chatParticipantType, participant.ChatParticipantType))
            {
                throw new DotChatAccessDeniedException(new ErrorCode(ErrorType.AccessDeniedDueToSelfElevation, ErrorModule.Security, ErrorOperation.Add, ErrorEntity.ParticipantType),
                                                       serviceName, methodName, chatId, currentUserId);
            }
        }
Example #20
0
 public ChatParticipant(ChatParticipantType chatParticipantType, ChatParticipantStatus chatParticipantStatus, DateTime readTimestamp, long readIndex, long version, IChatUser user)
     : this(chatParticipantType, chatParticipantStatus, readTimestamp, readIndex, version, user.UserId, user.Name, user.Details, user.AvatarId, user.InviteOnly, user.CanCreateChat, user.Style, user.Metadata)
 {
 }
Example #21
0
 public virtual async Task Apply(Guid chatId, ChatParticipantType chatParticipantType, string style, string metadata)
 {
     await ChatParticipantsService.Apply(CurrentUserId, chatId, chatParticipantType, style, metadata);
 }
Example #22
0
        protected virtual async Task <TChatParticipant> SetParticipationCandidate(Guid chatId, Guid userId, ChatParticipantType participantType, ChatParticipantStatus participantStatus, string style, string metadata, Guid setterId)
        {
            var user = await ReadUserStore.Retrieve(userId).ConfigureAwait(false);

            user = ReadUserStore.Customize(user, style, metadata);
            return(await ChatParticipantStore.Set(chatId, user, participantType,
                                                  participantStatus, setterId).ConfigureAwait(false));
        }
Example #23
0
        public static ChatMessageProxy AddNotification(LoginUser loginUser, int chatID, int id, ChatParticipantType type, string message)
        {
            ChatMessage chatMessage = (new ChatMessages(loginUser)).AddNewChatMessage();

            chatMessage.ChatID         = chatID;
            chatMessage.Message        = message;
            chatMessage.IsNotification = true;
            chatMessage.PosterID       = id;
            chatMessage.PosterType     = type;
            chatMessage.Collection.Save();
            return(chatMessage.GetProxy());
        }
Example #24
0
        public static ChatMessageProxy AbandonedChatRequest(LoginUser loginUser, Chat chat, int id, ChatParticipantType type, int chatID)
        {
            ChatParticipant self = ChatParticipants.GetChatParticipant(loginUser, id, type, chatID);

            if (self == null || self.DateLeft != null)
            {
                return(null);
            }

            self.DateLeft = DateTime.UtcNow;
            self.Collection.Save();

            ChatMessageProxy message = AddNotification(loginUser, chatID, id, type, string.Format("{0} {1} has abandoned the chat request.", self.FirstName, self.LastName));

            return(message);
        }
Example #25
0
 public virtual async Task Apply(Guid chatId, ChatParticipantType chatParticipantType)
 {
     await ChatParticipantsService.Apply(CurrentUserId, chatId, chatParticipantType, null, null);
 }
Example #26
0
 public virtual IChangeChatParticipantTypeCommand BuildChangeChatParticipantTypeCommand(Guid currentUserId, Guid chatId, Guid userId,
                                                                                        ChatParticipantType chatParticipantType, string style, string metadata)
 {
     return(new ChangeChatParticipantTypeCommand(currentUserId, chatId, userId, chatParticipantType, style, metadata));
 }
Example #27
0
 public virtual async Task ChangeType(Guid chatId, Guid userId, ChatParticipantType chatParticipantType, string style, string metadata)
 {
     await ChatParticipantsService.ChangeType(CurrentUserId, chatId, userId, chatParticipantType, style, metadata);
 }
Example #28
0
 public virtual async Task ChangeType(Guid chatId, Guid userId, ChatParticipantType chatParticipantType)
 {
     await ChatParticipantsService.ChangeType(CurrentUserId, chatId, userId, chatParticipantType, null, null);
 }