public ActionResult View(Guid from)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var userFrom = MembershipService.GetUser(from);
                var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser.Id);
                if (userFrom.Id != LoggedOnReadOnlyUser.Id)
                {
                    // Mark all messages read sent to this user from the userFrom
                    var unreadMessages = loggedOnUser.PrivateMessagesReceived.Where(x => x.UserFrom.Id == from && !x.IsRead);

                    foreach (var message in unreadMessages)
                    {
                        // Update message as read
                        message.IsRead = true;

                        // Get the sent version and update that too
                        var sentMessage = _privateMessageService.GetMatchingSentPrivateMessage(message.DateSent, message.UserFrom.Id, message.UserTo.Id);
                        if (sentMessage != null)
                        {
                            sentMessage.IsRead = true;
                        }
                    }

                    // Commit all changes
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }

                    // Get all the received messages from userFrom
                    // and then get all the sent messages to userFrom

                    var allMessages = loggedOnUser.PrivateMessagesReceived.Where(x => x.UserFrom.Id == from && x.IsSentMessage == false).ToList();
                    allMessages.AddRange(loggedOnUser.PrivateMessagesSent.Where(x => x.UserTo.Id == from && x.IsSentMessage == true).ToList());

                    // Now order them into an order of messages
                    var date = DateTime.UtcNow.AddMinutes(-AppConstants.TimeSpanInMinutesToShowMembers);

                    var viewModel = new ViewPrivateMessageViewModel
                    {
                        From = userFrom,
                        PrivateMessages = allMessages.OrderByDescending(x => x.DateSent).ToList(),
                        FromUserIsOnline = userFrom.LastActivityDate > date,
                        IsAjaxRequest = Request.IsAjaxRequest()
                    };

                    return View(viewModel);
                }
                var noPermissionText = LocalizationService.GetResourceString("Errors.NoPermission");
                if (Request.IsAjaxRequest())
                {
                    return Content(noPermissionText);
                }
                return ErrorToHomePage(noPermissionText);
            }
        }
Exemplo n.º 2
0
        public ActionResult View(Guid from)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var userFrom = MembershipService.GetUser(from);

                if (userFrom != LoggedOnUser)
                {
                    // Mark all messages read sent to this user from the userFrom
                    var unreadMessages = LoggedOnUser.PrivateMessagesReceived.Where(x => x.UserFrom.Id == from && !x.IsRead);

                    foreach (var message in unreadMessages)
                    {
                        // Update message as read
                        message.IsRead = true;

                        // Get the sent version and update that too
                        var sentMessage = _privateMessageService.GetMatchingSentPrivateMessage(message.DateSent, message.UserFrom.Id, message.UserTo.Id);
                        if (sentMessage != null)
                        {
                            sentMessage.IsRead = true;
                        }
                    }

                    // Commit all changes
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }

                    // Get all the received messages from userFrom
                    // and then get all the sent messages to userFrom

                    var allMessages = LoggedOnUser.PrivateMessagesReceived.Where(x => x.UserFrom.Id == from && x.IsSentMessage == false).ToList();
                    allMessages.AddRange(LoggedOnUser.PrivateMessagesSent.Where(x => x.UserTo.Id == from && x.IsSentMessage == true).ToList());

                    // Now order them into an order of messages

                    var viewModel = new ViewPrivateMessageViewModel
                    {
                        From = userFrom,
                        PrivateMessages = allMessages.OrderByDescending(x => x.DateSent).ToList()
                    };

                    return View(viewModel);
                }

                return ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"));
            }
        }
        public ActionResult AjaxMore(GetMoreViewModel viewModel)
        {
            using (UnitOfWorkManager.NewUnitOfWork())
            {
                if (Request.IsAjaxRequest())
                {
                    var userFrom = MembershipService.GetUser(viewModel.UserId);
                    var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser.Id);

                    var settings = SettingsService.GetSettings();
                    if (!settings.EnablePrivateMessages || LoggedOnReadOnlyUser.DisablePrivateMessages == true)
                    {
                        return Content(LocalizationService.GetResourceString("Errors.GenericMessage"));
                    }

                    var allMessages = _privateMessageService.GetUsersPrivateMessages(viewModel.PageIndex, SiteConstants.Instance.PagingGroupSize, loggedOnUser, userFrom);

                    var partialViewModel = new ViewPrivateMessageViewModel
                    {
                        From = userFrom,
                        PrivateMessages = allMessages,
                        IsAjaxRequest = Request.IsAjaxRequest(),
                    };

                    return PartialView(partialViewModel);
                }
                return Content(string.Empty);
            }
        }