コード例 #1
0
        /// <summary>
        /// Obtains the chat history for a particular conversation pair.
        /// </summary>
        /// <param name="echo">Information about the echo</param>
        public GetChatHistoryResponse getChatHistory(GetChatHistoryRequest request)
        {
            bool               result  = false;
            string             message = "";
            string             user1   = request.getCommand.history.user1;
            string             user2   = request.getCommand.history.user2;
            List <ChatMessage> msgs    = new List <ChatMessage>();

            if (openConnection() == true)
            {
                GetChatHistory ChatHistory = new GetChatHistory();

                string query = @"SELECT * FROM " + dbname + @".CHAT WHERE (SENDER = '" + user1 +
                               @"' AND RECEIVER = '" + user2 + @"') OR (SENDER = '" + user2 + @"' AND RECEIVER = '" + user1 + @"') ORDER BY timestamp ASC;";
                try
                {
                    MySqlCommand    command = new MySqlCommand(query, connection);
                    MySqlDataReader reader  = command.ExecuteReader();
                    while (reader.Read())
                    {
                        ChatMessage msg = new ChatMessage();
                        msg.sender          = reader.GetString("sender");
                        msg.receiver        = reader.GetString("receiver");
                        msg.unix_timestamp  = reader.GetInt32("timestamp");
                        msg.messageContents = reader.GetString("message");

                        msgs.Add(msg);
                    }
                    reader.Close();

                    request.getCommand.history.messages = msgs;
                    result  = true;
                    message = "successfuly found history for users";
                }
                catch (MySqlException e)
                {
                    message = e.Message;
                }
                catch (Exception e)
                {
                    Messages.Debug.consoleMsg("Unable to complete select from chat contacts database." +
                                              " Error :" + e.Message);
                    Messages.Debug.consoleMsg("The query was:" + query);

                    message = e.Message;
                }
                finally
                {
                    closeConnection();
                }
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                message = "Unable to connect to database";
            }
            return(new GetChatHistoryResponse(result, message, request.getCommand));
        }
コード例 #2
0
        public GetChatHistoryResponse retrieveChatHistory(GetChatHistory getRequest)
        {
            bool   result   = false;
            string message  = "";
            string sender   = getRequest.history.user1;
            string receiver = getRequest.history.user2;

            string query = @"SELECT * FROM " + dbname + ".chatmessages "
                           + "WHERE (sender='" + sender + "' AND receiver='" + receiver + "') OR (sender='" + receiver + "' AND receiver='" + sender + "') ORDER BY id ASC;";

            GetChatHistory chathistory = new GetChatHistory()
            {
                history = new ChatHistory
                {
                    user1    = sender,
                    user2    = receiver,
                    messages = new List <ChatMessage>()
                }
            };

            if (openConnection() == true)
            {
                result = true;
                MySqlCommand    command    = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = command.ExecuteReader();
                // if tuples are returned
                if (dataReader.HasRows)
                {
                    while (dataReader.Read())
                    {
                        ChatMessage temp = new ChatMessage();
                        temp.sender          = dataReader.GetString(1);
                        temp.receiver        = dataReader.GetString(2);
                        temp.unix_timestamp  = dataReader.GetInt32(3);
                        temp.messageContents = dataReader.GetString(4);
                        chathistory.history.messages.Add(temp);
                    }

                    dataReader.Close();
                    closeConnection();
                    return(new GetChatHistoryResponse(result, message, chathistory));
                }
                // no tuples returned
                else
                {
                    dataReader.Close();
                    closeConnection();
                    return(new GetChatHistoryResponse(result, message, chathistory));
                }
            }
            else
            {
                result  = false;
                message = "Could not connect to database.";
            }
            return(new GetChatHistoryResponse(result, message, chathistory));
        }
コード例 #3
0
ファイル: ChatController.cs プロジェクト: ArtinRezaee/SENG401
        public ActionResult Conversation(string otherUser = "")
        {
            if (Globals.isLoggedIn() == false)
            {
                return(RedirectToAction("Index", "Authentication"));
            }
            if ("".Equals(otherUser))
            {
                throw new System.Exception("Did not supply all required arguments.");
            }

            ServiceBusConnection connection = ConnectionManager.getConnectionObject(Globals.getUser());

            if (connection == null)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            GetChatHistory getCommand = new GetChatHistory()
            {
                history = new ChatHistory()
                {
                    user1 = Globals.getUser(),
                    user2 = otherUser
                }
            };

            GetChatHistoryRequest request = new GetChatHistoryRequest(getCommand);

            GetChatHistoryResponse response = connection.getChatHistory(request);

            string newConvoHtml = "";

            foreach (ChatMessage msg in response.responseData.history.messages)
            {
                if (msg.sender.Equals(Globals.getUser()))
                {
                    newConvoHtml +=
                        "<p class=\"message\">" +
                        "<span class=\"username\">You: </span>" +
                        msg.messageContents +
                        "</p>";
                }
                else
                {
                    newConvoHtml +=
                        "<p class=\"message\">" +
                        "<span class=\"username\" style=\"color:aqua;\">" + msg.sender + ": </span>" +
                        msg.messageContents +
                        "</p>";
                }
            }

            return(Content(newConvoHtml));
        }
コード例 #4
0
        public GetChatHistory getChatHistory(string userName, string compName)
        {
            if (openConnection() == true)
            {
                string query = "SELECT * FROM " + dbname + ".chathistory WHERE ((sender = @Username AND receiver= @Company) OR (sender = @Company AND receiver = @Username)) ORDER BY timestamp;";

                MySqlCommand command = new MySqlCommand(query, connection);
                command.Parameters.AddWithValue("@Username", userName);
                command.Parameters.AddWithValue("@Company", compName);
                MySqlDataReader reader = command.ExecuteReader();

                List <ChatMessage> messageList = new List <ChatMessage>();

                if (reader.Read())
                {
                    do
                    {
                        ChatMessage temp = new ChatMessage()
                        {
                            sender          = reader.GetString("sender"),
                            receiver        = reader.GetString("receiver"),
                            unix_timestamp  = reader.GetInt32("timestamp"),
                            messageContents = reader.GetString("message")
                        };

                        messageList.Add(temp);
                    } while (reader.Read());
                }
                else
                {
                    Debug.consoleMsg("Error: No conversation between user: '******' and company: '" + compName + "' in database");
                }

                ChatHistory hist = new ChatHistory()
                {
                    messages = messageList,
                    user1    = userName,
                    user2    = compName
                };

                GetChatHistory ret = new GetChatHistory()
                {
                    history = hist
                };

                closeConnection();
                return(ret);
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                closeConnection();
                return(null);
            }
        }
コード例 #5
0
        public object Any(GetChatHistory request)
        {
            var msgs = request.Channels.Map(x =>
                                            ChatHistory.GetRecentChatHistory(x, request.AfterId, request.Take))
                       .SelectMany(x => x)
                       .OrderBy(x => x.Id)
                       .ToList();

            return(new GetChatHistoryResponse
            {
                Results = msgs
            });
        }
コード例 #6
0
ファイル: ChatController.cs プロジェクト: ArtinRezaee/SENG401
        public ActionResult Index()
        {
            if (Globals.isLoggedIn() == false)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            ServiceBusConnection connection = ConnectionManager.getConnectionObject(Globals.getUser());

            if (connection == null)
            {
                return(RedirectToAction("Index", "Authentication"));
            }

            GetChatContacts getContactsCommand = new GetChatContacts
            {
                usersname    = Globals.getUser(),
                contactNames = null
            };

            GetChatContactsRequest  contactsRequest  = new GetChatContactsRequest(getContactsCommand);
            GetChatContactsResponse contactsResponse = connection.getAllChatContacts(contactsRequest);

            ChatHistory firstDisplayedChatHistory = null;

            if (contactsResponse.responseData.contactNames.Count != 0)
            {
                GetChatHistory getHistoryCommand = new GetChatHistory()
                {
                    history = new ChatHistory
                    {
                        user1 = Globals.getUser(),
                        user2 = contactsResponse.responseData.contactNames[0]
                    }
                };

                GetChatHistoryRequest historyRequest = new GetChatHistoryRequest(getHistoryCommand);
                firstDisplayedChatHistory = connection.getChatHistory(historyRequest).responseData.history;
            }
            else
            {
                firstDisplayedChatHistory = new ChatHistory();
            }

            ViewBag.ChatInstances        = contactsResponse.responseData.contactNames;
            ViewBag.DisplayedChatHistory = firstDisplayedChatHistory;

            return(View());
        }
コード例 #7
0
        public GetChatHistoryResponse getChatHistory(ChatHistory chatHist)
        {
            string         responseMessage = "";
            GetChatHistory userChatHistory = new GetChatHistory();
            bool           result          = false;

            if (openConnection() == true)
            {
                String query = @"SELECT * FROM chatdata WHERE (receiver = '" + chatHist.user1 + @"' AND 
                sender = '" + chatHist.user2 + @"') OR " + @"(receiver = '" + chatHist.user2 + @"' AND 
                sender = '" + chatHist.user1 + @"')";

                MySqlCommand    doQuery      = new MySqlCommand(query, connection);
                MySqlDataReader incomingData = doQuery.ExecuteReader();

                List <ChatMessage> userMessages = new List <ChatMessage>();

                while (incomingData.Read())
                {
                    result = true;
                    ChatMessage msg = new ChatMessage();
                    msg.receiver        = incomingData.GetString("receiver");
                    msg.sender          = incomingData.GetString("sender");
                    msg.unix_timestamp  = incomingData.GetInt32("timestamp");
                    msg.messageContents = incomingData.GetString("message");
                    userMessages.Add(msg);
                }
                chatHist.messages       = userMessages;
                userChatHistory.history = chatHist;

                if (result == false)
                {
                    responseMessage = "No chat history was found between users.";
                }
                incomingData.Close();
                closeConnection();
            }
            else
            {
                responseMessage = "Error connecting to the database.";
                Debug.consoleMsg("Error connecting to the database.");
            }
            return(new GetChatHistoryResponse(result, responseMessage, userChatHistory));
        }
コード例 #8
0
        public void ReceiveGetChatHistory(GetChatHistory getChatHistory)
        {
            Console.WriteLine("GetChatHistory");

            ClientChannel channel;

            if (Channels.TryGetValue(getChatHistory.channelID, out channel))
            {
                foreach (Packets.Types.Data.MessageData message in getChatHistory.messages)
                {
                    ClientMessage newMessage = channel.AddMessage(message);
                    OnMessageAdded?.Invoke(newMessage);
                }
            }
            else
            {
                Console.WriteLine("Warning: Server sent chat history for a channel that doesn't exist");
            }
        }
コード例 #9
0
        public GetChatHistory getHistory(GetChatHistory historyCmd)
        {
            GetChatHistory history = new GetChatHistory
            {
                history = new ChatHistory
                {
                    user1    = historyCmd.history.user1,
                    user2    = historyCmd.history.user2,
                    messages = new List <ChatMessage>()
                }
            };


            if (openConnection() == true)
            {
                string query = $@"SELECT * FROM chat WHERE (sender = '{history.history.user1}' AND receiver = '{history.history.user2}')
                    OR (sender = '{history.history.user2}' AND receiver = '{history.history.user1}');";

                MySqlCommand    cmd    = new MySqlCommand(query, connection);
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    ChatMessage msg = new ChatMessage
                    {
                        sender          = reader.GetString("sender"),
                        receiver        = reader.GetString("receiver"),
                        messageContents = reader.GetString("content"),
                        unix_timestamp  = reader.GetInt32("timestamp")
                    };
                    history.history.messages.Add(msg);
                }

                closeConnection();
                return(history);
            }
            else
            {
                Debug.consoleMsg("Unable to connect to database");
                return(null);
            }
        }
コード例 #10
0
        public Task Handle(GetChatHistoryRequest message, IMessageHandlerContext context)
        {
            try
            {
                GetChatHistory history = ChatDatabase.getInstance().getHistory(message.getCommand);

                if (history != null)
                {
                    return(context.Reply(new GetChatHistoryResponse(true, "Successful", history)));
                }
                else
                {
                    return(context.Reply(new GetChatHistoryResponse(false, "Unable to connect to database", message.getCommand)));
                }
            }
            catch (Exception err)
            {
                return(context.Reply(new GetChatHistoryResponse(false, err.Message, message.getCommand)));
            }
        }
コード例 #11
0
        /// <summary>
        /// Searches for company information
        /// </summary>
        /// <param name="message">Information about the company</param>
        /// <param name="context">Used to access information regarding the endpoints used for this handle</param>
        /// <returns>The response to be sent back to the calling process</returns>
        public Task Handle(GetChatHistoryRequest message, IMessageHandlerContext context)
        {
            // TODO: May need to fix
            ChatHistory h = new ChatHistory();

            h.user1    = message.getCommand.history.user1;
            h.user2    = message.getCommand.history.user2;
            h.messages = ChatDatabase.getInstance().getChats(message.getCommand.history);
            GetChatHistory hist = new GetChatHistory();

            hist.history = h;

            if (h.messages.Count > 0)
            {
                return(context.Reply(new GetChatHistoryResponse(true, "Successfully retrieved chats from database.", hist)));
            }
            else
            {
                return(context.Reply(new GetChatHistoryResponse(false, "Could not retrieve chats from database.", hist)));
            }
        }
 public GetChatHistoryRequest(GetChatHistory getCommand)
     : base(ChatRequest.getChatHistory)
 {
     this.getCommand = getCommand;
 }
 public GetChatHistoryResponse(bool result, string response, GetChatHistory responseData)
     : base(result, response)
 {
     this.responseData = responseData;
 }
コード例 #14
0
        /// <summary>
        /// This function will search the chat database for all of the chat messages passed between the 2 specified users.
        /// </summary>
        /// <param name="usersname">The name of the user</param>
        /// <param name="companyname">The name of the company</param>
        /// <returns>The chat history of the two users</returns>
        public GetChatHistoryResponse getChatHistory(GetChatHistoryRequest request)
        {
            GetChatHistory     requestData = request.getCommand;
            string             user1       = requestData.history.user1;
            string             user2       = requestData.history.user2;
            List <ChatMessage> messages    = new List <ChatMessage>();
            bool   result   = false;
            string response = "";

            if (openConnection() == true)
            {
                MySqlDataReader reader = null;

                try
                {
                    string query = "SELECT m.message, m.timestamp, m.sender " +
                                   "FROM " + databaseName + ".messages AS m LEFT JOIN " +
                                   databaseName + ".chats AS c ON m.id = c.id " +
                                   "WHERE (c.usersname='" + user1 + "' AND c.companyname='" + user2 + "') " +
                                   "OR (c.usersname='" + user2 + "' AND c.companyname='" + user1 + "');";

                    MySqlCommand command = new MySqlCommand(query, connection);
                    reader = command.ExecuteReader();

                    while (reader.Read() == true)
                    {
                        ChatMessage msg = new ChatMessage
                        {
                            messageContents = reader.GetString("message"),
                            unix_timestamp  = reader.GetInt32("timestamp")
                        };
                        string sender = reader.GetString("sender");
                        msg.sender = sender;
                        if (user1.Equals(sender))
                        {
                            msg.receiver = user2;
                        }
                        else
                        {
                            msg.receiver = user1;
                        }
                        messages.Add(msg);
                    }

                    requestData.history.messages = messages;
                    result = true;
                }
                catch (Exception e)
                {
                    response = e.Message;
                }
                finally
                {
                    if (reader != null && reader.IsClosed == false)
                    {
                        reader.Close();
                    }
                    closeConnection();
                }
                return(new GetChatHistoryResponse(result, response, requestData));
            }
            else
            {
                return(new GetChatHistoryResponse(false, "Could not connect to Database.", requestData));
            }
        }
コード例 #15
0
        public object Any(GetChatHistory request)
        {
            var msgs = request.Channels.Map(x =>
                ChatHistory.GetRecentChatHistory(x, request.AfterId, request.Take))
                .SelectMany(x => x)
                .OrderBy(x => x.Id)
                .ToList();

            return new GetChatHistoryResponse
            {
                Results = msgs
            };
        }