public void UnSubscribe(UnSubscribeEmailViewModel subscription) { if (Request.IsAjaxRequest()) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { // Add logic to add subscr var isCategory = subscription.SubscriptionType.Contains("category"); var id = subscription.Id; if (isCategory) { // get the category var cat = _categoryService.Get(id); if (cat != null) { // get the notifications by user var notifications = _categoryNotificationService.GetByUserAndCategory(LoggedOnUser, cat); if (notifications.Any()) { foreach (var categoryNotification in notifications) { // Delete _categoryNotificationService.Delete(categoryNotification); } } } } else { // get the topic var topic = _topicService.Get(id); if (topic != null) { // get the notifications by user var notifications = _topicNotificationService.GetByUserAndTopic(LoggedOnUser, topic); if (notifications.Any()) { foreach (var topicNotification in notifications) { // Delete _topicNotificationService.Delete(topicNotification); } } } } unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } } else { throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } }
public static TopicViewModel CreateTopicViewModel(Topic topic, PermissionSet permission, List <Post> posts, Post starterPost, int?pageIndex, int?totalCount, int?totalPages, MembershipUser loggedOnUser, Settings settings, ITopicNotificationService topicNotificationService, IPollAnswerService pollAnswerService, IVoteService voteService, IFavouriteService favouriteService, bool getExtendedData = false) { var userIsAuthenticated = loggedOnUser != null; // Check for online status var date = DateTime.UtcNow.AddMinutes(-AppConstants.TimeSpanInMinutesToShowMembers); var viewModel = new TopicViewModel { Permissions = permission, Topic = topic, Views = topic.Views, DisablePosting = loggedOnUser != null && loggedOnUser.DisablePosting == true, PageIndex = pageIndex, TotalCount = totalCount, TotalPages = totalPages, LastPostPermaLink = string.Concat(topic.NiceUrl, "?", AppConstants.PostOrderBy, "=", AppConstants.AllPosts, "#comment-", topic.LastPost.Id), MemberIsOnline = topic.User.LastActivityDate > date }; if (starterPost == null) { starterPost = posts.FirstOrDefault(x => x.IsTopicStarter); } // Get votes for all posts var postIds = posts.Select(x => x.Id).ToList(); postIds.Add(starterPost.Id); // Get all votes by post var votes = voteService.GetVotesByPosts(postIds); // Get all favourites for this user var allFavourites = favouriteService.GetByTopic(topic.Id); // Map the votes var startPostVotes = votes.Where(x => x.Post.Id == starterPost.Id).ToList(); var startPostFavs = allFavourites.Where(x => x.Post.Id == starterPost.Id).ToList(); // Create the starter post viewmodel viewModel.StarterPost = CreatePostViewModel(starterPost, startPostVotes, permission, topic, loggedOnUser, settings, startPostFavs); // Map data from the starter post viewmodel viewModel.VotesUp = startPostVotes.Count(x => x.Amount > 0); viewModel.VotesDown = startPostVotes.Count(x => x.Amount < 0); viewModel.Answers = totalCount != null ? (int)totalCount : posts.Count() - 1; // Create the ALL POSTS view models viewModel.Posts = CreatePostViewModels(posts, votes, permission, topic, loggedOnUser, settings, allFavourites); // ########### Full topic need everything if (getExtendedData) { // See if the user has subscribed to this topic or not var isSubscribed = userIsAuthenticated && topicNotificationService.GetByUserAndTopic(loggedOnUser, topic).Any(); viewModel.IsSubscribed = isSubscribed; // See if the topic has a poll, and if so see if this user viewing has already voted if (topic.Poll != null) { // There is a poll and a user // see if the user has voted or not viewModel.Poll = new PollViewModel { Poll = topic.Poll, UserAllowedToVote = permission[SiteConstants.Instance.PermissionVoteInPolls].IsTicked }; var answers = pollAnswerService.GetAllPollAnswersByPoll(topic.Poll); if (answers.Any()) { var pollvotes = answers.SelectMany(x => x.PollVotes).ToList(); if (userIsAuthenticated) { viewModel.Poll.UserHasAlreadyVoted = pollvotes.Count(x => x.User.Id == loggedOnUser.Id) > 0; } viewModel.Poll.TotalVotesInPoll = pollvotes.Count(); } } } return(viewModel); }
public ActionResult Show(string slug, int?p) { // Set the page index var pageIndex = p ?? 1; using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Get the topic var topic = _topicService.GetTopicBySlug(slug); if (topic != null) { // Note: Don't use topic.Posts as its not a very efficient SQL statement // Use the post service to get them as it includes other used entities in one // statement rather than loads of sql selects var sortQuerystring = Request.QueryString[AppConstants.PostOrderBy]; var orderBy = !string.IsNullOrEmpty(sortQuerystring) ? EnumUtils.ReturnEnumValueFromString <PostOrderBy>(sortQuerystring) : PostOrderBy.Standard; var posts = _postService.GetPagedPostsByTopic(pageIndex, SettingsService.GetSettings().PostsPerPage, int.MaxValue, topic.Id, orderBy); // Get the topic starter post var topicStarter = _postService.GetTopicStarterPost(topic.Id); // Get the permissions for the category that this topic is in var permissions = RoleService.GetPermissions(topic.Category, UsersRole); // If this user doesn't have access to this topic then // redirect with message if (permissions[AppConstants.PermissionDenyAccess].IsTicked) { return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"))); } // See if the user has subscribed to this topic or not var isSubscribed = UserIsAuthenticated && (_topicNotificationService.GetByUserAndTopic(LoggedOnUser, topic).Any()); // Populate the view model for this page var viewModel = new ShowTopicViewModel { Topic = topic, Posts = posts, PageIndex = posts.PageIndex, TotalCount = posts.TotalCount, Permissions = permissions, User = LoggedOnUser, IsSubscribed = isSubscribed, UserHasAlreadyVotedInPoll = false, TopicStarterPost = topicStarter }; // See if the topic has a poll, and if so see if this user viewing has already voted if (topic.Poll != null) { // There is a poll and a user // see if the user has voted or not var votes = topic.Poll.PollAnswers.SelectMany(x => x.PollVotes).ToList(); if (UserIsAuthenticated) { viewModel.UserHasAlreadyVotedInPoll = (votes.Count(x => x.User.Id == LoggedOnUser.Id) > 0); } viewModel.TotalVotesInPoll = votes.Count(); } // User has permission lets update the topic view count // but only if this topic doesn't belong to the user looking at it var addView = !(UserIsAuthenticated && LoggedOnUser.Id == topic.User.Id); if (!BotUtils.UserIsBot() && addView) { // Cool, user doesn't own this topic topic.Views = (topic.Views + 1); try { unitOfWork.Commit(); } catch (Exception ex) { LoggingService.Error(ex); } } return(View(viewModel)); } } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }
public void UnSubscribe(EmailSubscriptionViewModel subscription) { if (Request.IsAjaxRequest()) { try { // Add logic to add subscr var isCategory = subscription.SubscriptionType.Contains("category"); var isTag = subscription.SubscriptionType.Contains("tag"); var id = subscription.Id; var dbUser = MembershipService.GetUser(User.Identity.Name); if (isCategory) { // get the category var cat = _categoryService.Get(id); if (cat != null) { // get the notifications by user var notifications = _categoryNotificationService.GetByUserAndCategory(dbUser, cat, true); if (notifications.Any()) { foreach (var categoryNotification in notifications) { // Delete _categoryNotificationService.Delete(categoryNotification); } } } } else if (isTag) { // get the tag var tag = _topicTagService.Get(id); if (tag != null) { // get the notifications by user var notifications = _tagNotificationService.GetByUserAndTag(dbUser, tag, true); if (notifications.Any()) { foreach (var n in notifications) { // Delete _tagNotificationService.Delete(n); } } } } else { // get the topic var topic = _topicService.Get(id); if (topic != null) { // get the notifications by user var notifications = _topicNotificationService.GetByUserAndTopic(dbUser, topic, true); if (notifications.Any()) { foreach (var topicNotification in notifications) { // Delete _topicNotificationService.Delete(topicNotification); } } } } Context.SaveChanges(); } catch (Exception ex) { Context.RollBack(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } else { throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } }