示例#1
0
        public async Task <ActionResult> Chat(string username)
        {
            XFrameOptionsDeny();
            var userAppId = User.Identity.GetUserId();

            if (userAppId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            using (var db = new ZapContext())
            {
                var vm = new ChatMessagesViewModel();

                var otherUserInfo = await db.Users
                                    .Where(u => u.Name == username)
                                    .Select(u => new
                {
                    u.Id,
                    User       = u,
                    IsBlocking = u.BlockingUsers.Select(bu => bu.AppId).Contains(userAppId)
                })
                                    .AsNoTracking()
                                    .FirstOrDefaultAsync().ConfigureAwait(true);

                if (otherUserInfo.IsBlocking)
                {
                    return(RedirectToAction("Blocked"));
                }

                var userId = await db.Users
                             .Where(u => u.AppId == userAppId)
                             .Select(u => u.Id)
                             .FirstOrDefaultAsync().ConfigureAwait(true);

                vm.Messages = otherUserInfo == null ? new List <ChatMessageViewModel> ()
                    : await GetChats(db, userId, otherUserInfo.Id, 10, 0).ConfigureAwait(true);

                if (otherUserInfo == null)
                {
                    vm.OtherUser = new User()
                    {
                        Name  = username,
                        Id    = 0,
                        AppId = ""
                    };
                }
                else
                {
                    vm.OtherUser = otherUserInfo.User;
                    if (otherUserInfo.Id == userId)   // disallow chatting with yourself
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }

                return(View(vm));
            }
        }
示例#2
0
        public IActionResult CreateMessage(int sneakerId, string MsgChat)
        {
            var viewModel = new ChatMessagesViewModel
            {
                SneakerId = sneakerId,
                MsgChat   = MsgChat
            };

            return(View(viewModel));
        }
        public async Task <ActionResult> Chat(string username)
        {
            var userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            using (var db = new ZapContext())
            {
                var vm = new ChatMessagesViewModel();

                var user = db.Users
                           .Include("Messages")
                           .Include("Messages.PostLink")
                           .Include("Messages.From")
                           .Where(u => u.AppId == userId).FirstOrDefault();

                var otheruser = db.Users
                                .Include("Messages")
                                .Include("Messages.PostLink")
                                .Include("Messages.From")
                                .Where(u => u.Name == username).FirstOrDefault();

                if (otheruser == null)
                {
                    return(RedirectToAction("Index", "Messages"));
                }

                int thisUserId  = user.Id;
                int otherUserId = otheruser.Id;

                // Better to just search from & to?

                var receivedMessages = user.Messages.Where(m => m.From != null && m.From.Id == otherUserId).Where(m => !m.IsDeleted).Where(m => m.Title.StartsWith("Private")).ToList();
                var sentMessages     = otheruser.Messages.Where(m => m.From != null && m.From.Id == thisUserId).Where(m => !m.IsDeleted).Where(m => m.Title.StartsWith("Private")).ToList();

                var messages = new List <ChatMessageViewModel>();

                foreach (var m in receivedMessages)
                {
                    messages.Add(new ChatMessageViewModel()
                    {
                        Message    = m,
                        From       = otheruser,
                        To         = user,
                        IsReceived = true,
                    });
                }

                foreach (var m in sentMessages)
                {
                    messages.Add(new ChatMessageViewModel()
                    {
                        Message    = m,
                        From       = user,
                        To         = otheruser,
                        IsReceived = false,
                    });
                }

                vm.Messages = messages.OrderBy(mv => mv.Message.TimeStamp).ToList();

                return(View(vm));
            }
        }
示例#4
0
        public async Task <IActionResult> CreateMessage(ChatMessagesViewModel viewModel)
        {
            // find the sender from the sneaker id that was passed in
            Sneaker sneaker = await _context.Sneaker
                              .Include(u => u.User)
                              .FirstOrDefaultAsync(u => u.SneakerId == viewModel.SneakerId);

            if (viewModel.ChatMessages == null) // some reason the list of chat messages is never there
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        //cmd.CommandText = @"SELECT MessageId, Msg, Date,
                        //                       SneakerId, SenderId, ReceiverId
                        //                FROM Message
                        //                WHERE SneakerId = @sneakerId
                        //                AND (SenderId = @userId
                        //                        OR ReceiverId = @userId)
                        //                ORDER BY Date;";
                        cmd.CommandText = @"SELECT MessageId, Msg, Date,
                                               SneakerId, SenderId, ReceiverId
                                        FROM Message
                                        WHERE SneakerId = @sneakerId
                                        AND (SenderId = @userId
                                                AND ReceiverId = @otherUserId)
                                        ORDER BY Date;";
                        var currUser = await GetCurrentUserAsync();

                        cmd.Parameters.Add(new SqlParameter("@userId", currUser.Id));
                        cmd.Parameters.Add(new SqlParameter("@otherUserId", viewModel.OtherUserId));
                        cmd.Parameters.Add(new SqlParameter("@sneakerId", viewModel.SneakerId));

                        SqlDataReader reader = await cmd.ExecuteReaderAsync();

                        List <Message> theMsgs = new List <Message>();

                        while (reader.Read())
                        {
                            Message message = new Message
                            {
                                MessageId  = reader.GetInt32(reader.GetOrdinal("MessageId")),
                                Msg        = reader.GetString(reader.GetOrdinal("Msg")),
                                Date       = reader.GetDateTime(reader.GetOrdinal("Date")),
                                SneakerId  = reader.GetInt32(reader.GetOrdinal("SneakerId")),
                                SenderId   = reader.GetString(reader.GetOrdinal("SenderId")),
                                ReceiverId = reader.GetString(reader.GetOrdinal("ReceiverId"))
                            };
                            theMsgs.Add(message);
                        }
                        reader.Close();
                        ViewBag.User = currUser.FirstName;

                        cmd.CommandText = @"SELECT MessageId, Msg, Date,
                                               SneakerId, SenderId, ReceiverId
                                        FROM Message
                                        WHERE SneakerId = @theSneakerId
                                        AND (SenderId = @theOtherUserId
                                                AND ReceiverId = @theUserId)
                                        ORDER BY Date;";

                        cmd.Parameters.Add(new SqlParameter("@theUserId", currUser.Id));
                        cmd.Parameters.Add(new SqlParameter("theOtherUserId", viewModel.OtherUserId));
                        cmd.Parameters.Add(new SqlParameter("@theSneakerId", viewModel.SneakerId));
                        SqlDataReader reader1 = await cmd.ExecuteReaderAsync();

                        while (reader1.Read())
                        {
                            Message message = new Message
                            {
                                MessageId  = reader1.GetInt32(reader1.GetOrdinal("MessageId")),
                                Msg        = reader1.GetString(reader1.GetOrdinal("Msg")),
                                Date       = reader1.GetDateTime(reader1.GetOrdinal("Date")),
                                SneakerId  = reader1.GetInt32(reader1.GetOrdinal("SneakerId")),
                                SenderId   = reader1.GetString(reader1.GetOrdinal("SenderId")),
                                ReceiverId = reader1.GetString(reader1.GetOrdinal("ReceiverId"))
                            };
                            theMsgs.Add(message);
                        }
                        viewModel.ChatMessages = theMsgs.OrderBy(m => m.Date).ToList();
                    }
                }
            }// end of the IF statement
            var sneakerOwner = sneaker.User;
            var currentUser  = await GetCurrentUserAsync();

            var messages       = viewModel.ChatMessages; // get all the messages
            int messagesLength = 0;                      // set the length to 0 so it's not null


            // if the messesages length is not null, then get the count of the messages
            // and save it to the variable
            if (messages != null)
            {
                int count = 0;
                foreach (var msg in messages)
                {
                    count++;
                }
                messagesLength = count;
            }

            // if the messageLength is not 0, it holds msgs
            // then grab the last message, no reason, could get first
            // this if statement is to get the receiver's id
            if (messagesLength != 0)
            {
                Message lastMessage = messages.Last();
                if (lastMessage.SenderId != currentUser.Id)
                { // if the current user wasn't the sender of the last message
                  // that was sent, that means the other user of the viewmodel
                  //  is the sender so grab that id
                    viewModel.OtherUserId = lastMessage.SenderId;
                }
                else
                {
                    // else the other user was the receiver
                    viewModel.OtherUserId = lastMessage.ReceiverId;
                }


                // now create the message with the correct sender & receiver
                Message message = new Message()
                {
                    Date       = DateTime.Now,
                    Msg        = viewModel.Message.Msg,
                    SenderId   = currentUser.Id,
                    ReceiverId = viewModel.OtherUserId,
                    Sneaker    = sneaker,
                    SneakerId  = sneaker.SneakerId
                };

                // add that message to the db
                _context.Add(message);
                await _context.SaveChangesAsync();
            }
            else
            {
                // else this is the beginning of a new chat message
                Message message = new Message()
                {
                    Date       = DateTime.Now,
                    Msg        = viewModel.Message.Msg,
                    SenderId   = currentUser.Id,
                    ReceiverId = sneaker.UserId,
                    Sneaker    = sneaker,
                    SneakerId  = sneaker.SneakerId
                };
                _context.Add(message);
                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Chat), new { sneakerId = sneaker.SneakerId, otherUserId = viewModel.OtherUserId }));
        }
示例#5
0
        // GET: All this does is get the chatMessages for one conversation
        public async Task <IActionResult> Chat(int sneakerId, string otherUserId)
        {
            // userId is the buyer, sneakerId is for the sneaker for sale
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"SELECT MessageId, Msg, Date,
                                               SneakerId, SenderId, ReceiverId
                                        FROM Message
                                        WHERE SneakerId = @sneakerId
                                        AND (SenderId = @userId
                                                AND ReceiverId = @otherUserId)
                                        ORDER BY Date;";

                    var currentUser = await GetCurrentUserAsync();

                    //var curruserId = currentUser.Id;
                    var sneakerInQuestion = _context.Sneaker
                                            .Include(u => u.User)
                                            .FirstOrDefault(s => s.SneakerId == sneakerId);

                    ViewBag.SneakerOwnerId = sneakerInQuestion.UserId;
                    ViewBag.SneakerOwner   = sneakerInQuestion.User.FirstName;
                    ViewBag.currUserName   = currentUser.FirstName;

                    cmd.Parameters.Add(new SqlParameter("@userId", currentUser.Id));
                    cmd.Parameters.Add(new SqlParameter("@otherUserId", otherUserId));
                    cmd.Parameters.Add(new SqlParameter("@sneakerId", sneakerId));

                    SqlDataReader reader = await cmd.ExecuteReaderAsync();

                    List <Message> messages = new List <Message>();

                    while (reader.Read())
                    {
                        Message message = new Message
                        {
                            MessageId  = reader.GetInt32(reader.GetOrdinal("MessageId")),
                            Msg        = reader.GetString(reader.GetOrdinal("Msg")),
                            Date       = reader.GetDateTime(reader.GetOrdinal("Date")),
                            SneakerId  = reader.GetInt32(reader.GetOrdinal("SneakerId")),
                            SenderId   = reader.GetString(reader.GetOrdinal("SenderId")),
                            ReceiverId = reader.GetString(reader.GetOrdinal("ReceiverId"))
                        };
                        messages.Add(message);
                    }
                    reader.Close();

                    cmd.CommandText = @"SELECT MessageId, Msg, Date,
                                               SneakerId, SenderId, ReceiverId
                                        FROM Message
                                        WHERE SneakerId = @theSneakerId
                                        AND (SenderId = @theOtherUserId
                                                AND ReceiverId = @currUserId)
                                        ORDER BY Date;";


                    cmd.Parameters.Add(new SqlParameter("@currUserId", currentUser.Id));
                    cmd.Parameters.Add(new SqlParameter("@theOtherUserId", otherUserId));
                    cmd.Parameters.Add(new SqlParameter("@theSneakerId", sneakerId));

                    SqlDataReader reader1 = await cmd.ExecuteReaderAsync();

                    while (reader1.Read())
                    {
                        Message message = new Message
                        {
                            MessageId  = reader1.GetInt32(reader1.GetOrdinal("MessageId")),
                            Msg        = reader1.GetString(reader1.GetOrdinal("Msg")),
                            Date       = reader1.GetDateTime(reader1.GetOrdinal("Date")),
                            SneakerId  = reader1.GetInt32(reader1.GetOrdinal("SneakerId")),
                            SenderId   = reader1.GetString(reader1.GetOrdinal("SenderId")),
                            ReceiverId = reader1.GetString(reader1.GetOrdinal("ReceiverId"))
                        };
                        messages.Add(message);
                    }
                    reader1.Close();

                    var sneaker = _context.Sneaker.Find(sneakerId);
                    ViewBag.User = currentUser.FirstName;

                    var viewModel = new ChatMessagesViewModel
                    {
                        ChatMessages = messages.OrderBy(m => m.Date).ToList(),
                        SneakerId    = sneaker.SneakerId
                    };

                    return(View(viewModel));
                }
            }
        }
示例#6
0
 public ChatMessagesPage()
 {
     InitializeComponent();
     BindingContext = viewModel = new ChatMessagesViewModel();
 }