Пример #1
0
        public string GetHtml(bool includeTimeStamps, CultureInfo culture)
        {
            ChatParticipant initiator = ChatParticipants.GetChatParticipant(Collection.LoginUser, InitiatorID, InitiatorType, ChatID);
            ChatMessages    messages  = new ChatMessages(Collection.LoginUser);
            StringBuilder   builder   = new StringBuilder("<div class=\"chat-messages\">");

            builder.Append(string.Format("<p class=\"chat-notification\">Initiated On: {0}<p>", DateCreated.ToString("g", culture)));
            if (initiator != null)
            {
                builder.Append(string.Format("<p class=\"chat-notification\">Initiated By: {0} {1}, {2} (<a href=\"mailto:{3}\">{3}</a>)<p>", initiator.FirstName, initiator.LastName, initiator.CompanyName, initiator.Email));
            }

            messages.LoadByChatID(ChatID);
            List <string> eventsList = GetHTMLGlobalEventAttributes();

            foreach (ChatMessage message in messages)
            {
                string time        = message.DateCreated.ToString("h:mm");
                string timeFormat  = includeTimeStamps ? time + ": " : "";
                string messageText = message.Message.Replace("<", "&lt;").Replace(">", "&gt;");

                bool containsProhibitedText = false;
                int  i = 0;

                while (i < eventsList.Count && !containsProhibitedText)
                {
                    containsProhibitedText = message.Message.Trim().IndexOf("<script ") >= 0 || message.Message.Trim().Contains(string.Format(" {0}=", eventsList[i]));
                    i++;
                }

                if ((message.Message.Trim().IndexOf("<img ") == 0 && !containsProhibitedText) ||
                    (message.Message.Trim().IndexOf("/chatattachments/") > 0 && !containsProhibitedText) ||
                    (message.Message.Trim().IndexOf("<a target=\"_blank\" href=") >= 0 && !containsProhibitedText))
                {
                    messageText = message.Message;
                }

                if (message.IsNotification)
                {
                    builder.Append(string.Format("<p class=\"chat-notification\">{0}{1}<p>", includeTimeStamps ? time + ": " : "", messageText));
                }
                else if (message.PosterType == ChatParticipantType.User)
                {
                    if (message.PosterID == Collection.LoginUser.UserID)
                    {
                        builder.Append(string.Format("<p class=\"chat-message-self\"><span class=\"chat-name\">{1}{0}: </span>{2}</p>", message.Row["PosterName"].ToString(), timeFormat, messageText));
                    }
                    else
                    {
                        builder.Append(string.Format("<p class=\"chat-message-user\"><span class=\"chat-name\">{1}{0}: </span>{2}</p>", message.Row["PosterName"].ToString(), timeFormat, messageText));
                    }
                }
                else
                {
                    builder.Append(string.Format("<p class=\"chat-message-client\"><span class=\"chat-name\">{1}{0}: </span>{2}</p>", message.Row["PosterName"].ToString(), timeFormat, messageText));
                }
            }
            builder.Append("</div>");
            return(builder.ToString());
        }
Пример #2
0
        public static void KickOutDisconnectedClients(LoginUser loginUser, int organizationID)
        {
            ChatParticipants participants = new ChatParticipants(loginUser);

            participants.LoadExternalDisconnected(organizationID);
            foreach (ChatParticipant participant in participants)
            {
                participant.DateLeft = DateTime.UtcNow;
                participant.Collection.Save();
                AddNotification(loginUser, participant.ChatID, participant.ParticipantID, participant.ParticipantType, string.Format("{0} {1} has left the chat.", participant.FirstName, participant.LastName));
            }
        }
Пример #3
0
        public static ChatParticipant GetChatParticipant(LoginUser loginUser, int chatParticipantID)
        {
            ChatParticipants chatParticipants = new ChatParticipants(loginUser);

            chatParticipants.LoadByChatParticipantID(chatParticipantID);
            if (chatParticipants.IsEmpty)
            {
                return(null);
            }
            else
            {
                return(chatParticipants[0]);
            }
        }
Пример #4
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);
        }
Пример #5
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);
        }
        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");
            }
        }
        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]);
        }
Пример #8
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));
        }
Пример #9
0
 public ChatParticipant(DataRow row, ChatParticipants chatParticipants) : base(row, chatParticipants)
 {
     _chatParticipants = chatParticipants;
 }