Esempio n. 1
0
        public static int AcceptRequest(LoginUser loginUser, int userID, int chatRequestID, string ipAddress)
        {
            ChatRequest request = ChatRequests.GetChatRequest(loginUser, chatRequestID);

            if (request == null || request.IsAccepted)
            {
                return(-1);
            }
            request.IsAccepted   = true;
            request.TargetUserID = userID;
            request.Collection.Save();

            Chats.JoinChat(loginUser, userID, ChatParticipantType.User, request.ChatID, ipAddress);

            if (request.RequestType == ChatRequestType.External && request.Message.Trim() != "")
            {
                ChatMessage chatMessage = (new ChatMessages(loginUser)).AddNewChatMessage();
                chatMessage.ChatID     = request.ChatID;
                chatMessage.Message    = request.Message.Trim();
                chatMessage.PosterID   = request.RequestorID;
                chatMessage.PosterType = request.RequestorType;
                chatMessage.Collection.Save();
            }

            return(request.ChatID);
        }
        public void LoadByUserID(int userID, int organizationID)
        {
            Chats chats = new Chats(LoginUser);

            chats.LoadByUserID(userID, organizationID);
            if (chats.IsEmpty)
            {
                return;
            }

            StringBuilder builder = new StringBuilder();

            foreach (Chat chat in chats)
            {
                if (builder.Length > 0)
                {
                    builder.Append(",");
                }
                builder.Append(chat.ChatID.ToString());
            }

            using (SqlCommand command = new SqlCommand())
            {
                string text = @"
(
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,
ISNULL((SELECT TOP 1 cm.ChatMessageID FROM ChatMessages cm WHERE cm.ChatID= cp.ChatID ORDER BY cm.ChatMessageID DESC), 0) AS 'LastPostedMessageID',
ISNULL((SELECT TOP 1 cp2.LastMessageID FROM ChatParticipants cp2 WHERE cp2.ChatID= cp.ChatID AND cp2.ParticipantType=0 AND cp2.ParticipantID=@UserID), 0) AS 'MyLastMessageID'
FROM ChatParticipants cp
LEFT JOIN ChatClients cc ON cc.ChatClientID = cp.ParticipantID
WHERE cp.ChatID IN ({0})
--AND cp.DateLeft IS NULL
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,
ISNULL((SELECT TOP 1 cm.ChatMessageID FROM ChatMessages cm WHERE cm.ChatID= cp.ChatID ORDER BY cm.ChatMessageID DESC), 0) AS 'LastPostedMessageID',
ISNULL((SELECT TOP 1 cp2.LastMessageID FROM ChatParticipants cp2 WHERE cp2.ChatID= cp.ChatID AND cp2.ParticipantType=0 AND cp2.ParticipantID=@UserID), 0) AS 'MyLastMessageID'
FROM ChatParticipants cp
LEFT JOIN Users u ON u.UserID = cp.ParticipantID
LEFT JOIN Organizations o ON o.OrganizationID = u.OrganizationID
WHERE NOT (cp.ParticipantID = @UserID AND cp.ParticipantType = 0) 
AND cp.ChatID IN ({0})
--AND cp.DateLeft IS NULL
AND cp.ParticipantType = 0
)
ORDER BY ChatID

";

                command.CommandText = string.Format(text, builder.ToString());
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@UserID", userID);
                Fill(command);
            }
        }
        public static Chat GetChat(LoginUser loginUser, int chatID)
        {
            Chats chats = new Chats(loginUser);

            chats.LoadByChatID(chatID);
            if (chats.IsEmpty)
            {
                return(null);
            }
            else
            {
                return(chats[0]);
            }
        }
Esempio n. 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);
        }
Esempio n. 5
0
        public static void UpdateAction(LoginUser loginUser, int chatID)
        {
            Chat chat = Chats.GetChat(loginUser, chatID);

            if (chat == null || chat.ActionID == null)
            {
                return;
            }

            Action action = Actions.GetAction(loginUser, (int)chat.ActionID);

            if (action == null)
            {
                return;
            }

            action.Description = chat.GetHtml(true, loginUser.OrganizationCulture);
            action.Collection.Save();
        }
 public Chat(DataRow row, Chats chats) : base(row, chats)
 {
     _chats = chats;
 }
Esempio n. 7
0
 partial void AfterRowInsert(ChatMessage message)
 {
     Chats.UpdateAction(LoginUser, message.ChatID);
 }
Esempio n. 8
0
        public static ChatRequest RequestChat(LoginUser loginUser, int organizationID, string firstName, string lastName, string email, string message, string ipAddress, int groupID = 0)
        {
            ChatClients clients = new ChatClients(loginUser);
            ChatClient  client  = clients.IsEmpty ? (new ChatClients(loginUser)).AddNewChatClient() : clients[0];

            client.OrganizationID = organizationID;
            client.FirstName      = firstName;
            client.LastName       = lastName;
            client.Email          = email;
            Users users = new Users(loginUser);

            if (!string.IsNullOrEmpty(email))
            {
                users.LoadByEmail(organizationID, email);

                try
                {
                    if (!users.IsEmpty)
                    {
                        client.LinkedUserID = users[0].UserID;
                        client.CompanyName  = Organizations.GetOrganization(loginUser, users[0].OrganizationID).Name;
                    }
                    else
                    {
                        string       emailDomain = email.Substring(email.LastIndexOf('@') + 1).Trim();
                        Organization org         = Organization.GetCompanyByDomain(organizationID, emailDomain, loginUser);
                        client.CompanyName = (org != null) ? org.Name : "";
                    }
                }
                catch (Exception)
                {
                    client.CompanyName = "";
                }
            }
            else
            {
                Organization org = Organization.GetCompanyByDomain(organizationID, string.Empty, loginUser, forceUnknown: true);
                client.CompanyName = (org != null) ? org.Name : "";
            }

            client.Collection.Save();


            Chat chat = (new Chats(loginUser)).AddNewChat();

            chat.InitiatorID    = client.ChatClientID;
            chat.InitiatorType  = ChatParticipantType.External;
            chat.OrganizationID = organizationID;
            chat.Collection.Save();

            Chats.JoinChat(loginUser, client.ChatClientID, ChatParticipantType.External, chat.ChatID, ipAddress);

            ChatRequest request = (new ChatRequests(loginUser)).AddNewChatRequest();

            request.RequestorID    = client.ChatClientID;
            request.RequestorType  = ChatParticipantType.External;
            request.OrganizationID = organizationID;
            request.ChatID         = chat.ChatID;
            request.Message        = message;
            request.IsAccepted     = false;
            request.RequestType    = ChatRequestType.External;
            request.GroupID        = groupID;
            request.Collection.Save();

            return(request);
        }