Exemplo n.º 1
0
        public TsChatUpdate GetChatUpdate(int lastChatMessageID, int lastChatRequestID)
        {
            TsChatUpdate update = new TsChatUpdate();

            try
            {
                User user = Users.GetUser(LoginUser.Anonymous, TSAuthentication.UserID);
                user.UpdatePing();

                LoginUser loginUser = new LoginUser(user.UserID, user.OrganizationID, null);

                List <GrowlMessage> chatMessageGrowl = new List <GrowlMessage>();
                List <GrowlMessage> chatRequestGrowl = new List <GrowlMessage>();

                update.LastChatRequestID = lastChatRequestID;
                update.LastChatMessageID = lastChatMessageID;
                update.ChatMessageCount  = 0;
                update.ChatRequestCount  = 0;

                if (user.IsChatUser && ChatUserSettings.GetSetting(loginUser, user.UserID).IsAvailable)
                {
                    ChatMessages chatMessages = new ChatMessages(loginUser);
                    update.ChatMessageCount = chatMessages.GetUnreadMessageCount(loginUser.UserID, ChatParticipantType.User);
                    update.ChatRequestCount = user.IsChatUser ? ChatRequests.GetWaitingRequestCount(loginUser, loginUser.UserID, loginUser.OrganizationID) : 0;

                    if (lastChatMessageID < 0)
                    {
                        update.LastChatMessageID = ChatMessages.GetLastMessageID(loginUser);
                    }
                    chatMessages.LoadUnpreviewedMessages(loginUser.UserID, update.LastChatMessageID);

                    foreach (ChatMessage chatMessage in chatMessages)
                    {
                        chatMessageGrowl.Add(new GrowlMessage(chatMessage.Message, chatMessage.PosterName, "ui-state-active"));
                        update.LastChatMessageID = chatMessage.ChatMessageID;
                    }

                    ChatRequests requests = new ChatRequests(loginUser);
                    requests.LoadNewWaitingRequests(loginUser.UserID, loginUser.OrganizationID, lastChatRequestID);

                    foreach (ChatRequest chatRequest in requests)
                    {
                        chatRequestGrowl.Add(new GrowlMessage(string.Format("{0} {1} is requesting a chat!", chatRequest.Row["FirstName"].ToString(), chatRequest.Row["LastName"].ToString()), "Chat Request", "ui-state-error"));
                        update.LastChatRequestID = chatRequest.ChatRequestID;
                    }
                }

                update.NewChatMessages = chatMessageGrowl.ToArray();
                update.NewChatRequests = chatRequestGrowl.ToArray();
            }
            catch (Exception ex)
            {
                ExceptionLogs.LogException(LoginUser.Anonymous, ex, "Main Chat Upate");
            }
            return(update);
        }
Exemplo n.º 2
0
    public static string[] GetChatHtml(UserInfoProxy userInfo, int chatID, int lastMessageID)
    {
        LoginUser       loginUser = new LoginUser(userInfo.UserID, userInfo.OrganizationID, null);
        ChatUserSetting setting   = ChatUserSettings.GetSetting(loginUser, loginUser.UserID);

        setting.CurrentChatID = chatID;
        setting.Collection.Save();

        Chat chat = Chats.GetChat(loginUser, chatID);

        if (chat == null || chat.OrganizationID != loginUser.OrganizationID)
        {
            return(null);
        }

        ChatMessages messages = new ChatMessages(loginUser);
        int          i        = messages.GetLastMessageID(chatID);
        string       chatHtml = "";

        if (i > -1 && i != lastMessageID)
        {
            ChatParticipants.UpdateLastMessageID(loginUser, loginUser.UserID, ChatParticipantType.User, chatID, i);
            chatHtml = chat.GetHtml(true, loginUser.CultureInfo);
        }


        // load typers
        ChatParticipants participants = new ChatParticipants(loginUser);

        participants.LoadTypers(loginUser.UserID, ChatParticipantType.User, chatID, 2);
        StringBuilder typers = new StringBuilder();

        foreach (ChatParticipant item in participants)
        {
            if (typers.Length > 0)
            {
                typers.Append(", ");
            }
            typers.Append(item.FirstName);
        }

        if (typers.Length > 0)
        {
            typers.Append(" is typing a message.");
        }

        return(new string[] { chatID.ToString(), i.ToString(), chatHtml, typers.ToString() });
    }
Exemplo n.º 3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.AddHeader("Expires", "-1");
            context.Response.AddHeader("Pragma", "no-cache");

            if (TSAuthentication.Ticket == null)
            {
                context.Response.ContentType = "text/plain";
                context.Response.StatusCode  = (int)HttpStatusCode.Forbidden;
                context.Response.ClearContent();
                context.Response.End();
                return;
            }

            TsChatUpdate update = new TsChatUpdate();

            using (Stream receiveStream = context.Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                {
                    string  requestContent = readStream.ReadToEnd();
                    JObject args           = JObject.Parse(requestContent);

                    int lastChatRequestID = int.Parse(args["lastChatRequestID"].ToString());;
                    int lastChatMessageID = int.Parse(args["lastChatMessageID"].ToString());;

                    try
                    {
                        User user = Users.GetUser(LoginUser.Anonymous, TSAuthentication.UserID);

                        if (user == null)
                        {
                            context.Response.ContentType = "text/plain";
                            context.Response.StatusCode  = (int)HttpStatusCode.Forbidden;
                            context.Response.ClearContent();
                            context.Response.End();
                            return;
                        }

                        user.UpdatePing();

                        LoginUser loginUser = new LoginUser(user.UserID, user.OrganizationID, null);

                        List <GrowlMessage> chatMessageGrowl = new List <GrowlMessage>();
                        List <GrowlMessage> chatRequestGrowl = new List <GrowlMessage>();

                        update.LastChatRequestID = lastChatRequestID;
                        update.LastChatMessageID = lastChatMessageID;
                        update.ChatMessageCount  = 0;
                        update.ChatRequestCount  = 0;

                        if (user.IsChatUser && ChatUserSettings.GetSetting(loginUser, user.UserID).IsAvailable)
                        {
                            ChatMessages chatMessages = new ChatMessages(loginUser);
                            update.ChatMessageCount = chatMessages.GetUnreadMessageCount(loginUser.UserID, ChatParticipantType.User);
                            update.ChatRequestCount = user.IsChatUser ? ChatRequests.GetWaitingRequestCount(loginUser, loginUser.UserID, loginUser.OrganizationID) : 0;

                            if (lastChatMessageID < 0)
                            {
                                update.LastChatMessageID = ChatMessages.GetLastMessageID(loginUser);
                            }
                            chatMessages.LoadUnpreviewedMessages(loginUser.UserID, update.LastChatMessageID);

                            foreach (ChatMessage chatMessage in chatMessages)
                            {
                                chatMessageGrowl.Add(new GrowlMessage(chatMessage.Message, chatMessage.PosterName, "ui-state-active"));
                                update.LastChatMessageID = chatMessage.ChatMessageID;
                            }

                            ChatRequests requests = new ChatRequests(loginUser);
                            requests.LoadNewWaitingRequests(loginUser.UserID, loginUser.OrganizationID, lastChatRequestID);

                            foreach (ChatRequest chatRequest in requests)
                            {
                                chatRequestGrowl.Add(new GrowlMessage(string.Format("{0} {1} is requesting a chat!", chatRequest.Row["FirstName"].ToString(), chatRequest.Row["LastName"].ToString()), "Chat Request", "ui-state-error"));
                                update.LastChatRequestID = chatRequest.ChatRequestID;
                            }
                        }
                        update.NewChatMessages = chatMessageGrowl.ToArray();
                        update.NewChatRequests = chatRequestGrowl.ToArray();
                    }
                    catch (Exception ex)
                    {
                        ExceptionLogs.LogException(LoginUser.Anonymous, ex, "Main Chat Upate");
                    }
                }
            }
            context.Response.ContentType = "application/json; charset=utf-8";
            context.Response.Write(JsonConvert.SerializeObject(update));
        }