/// <summary> /// creates a new forum topic and post. /// </summary> /// <param name="forumId"></param> /// <param name="forumType"></param> /// <param name="subject"></param> /// <param name="message"></param> /// <param name="memberId"></param> /// <returns></returns> public static long CreateNewForumTopicAndPost(Guid forumId, ForumOwnerTypeEnum forumType, string subject, string message, Guid memberId, long groupId, bool emailGroupAboutPost, bool pinMessage, bool lockMessage, long chosenCategory) { try { var dc = new ManagementContext(); var forum = dc.Forums.Where(x => x.ForumId == forumId).FirstOrDefault(); if (forum != null) { var member = dc.Members.Where(x => x.MemberId == memberId).FirstOrDefault(); member.TotalForumPosts = member.TotalForumPosts + 1; DataModels.Forum.ForumMessage mess = new DataModels.Forum.ForumMessage(); mess.Member = member; if (!String.IsNullOrEmpty(message)) { message = message.Replace(Environment.NewLine, "<br/>"); message = message.Replace("\r", "<br/>"); } mess.MessageHTML = message; if (!String.IsNullOrEmpty(message)) mess.MessagePlain = _htmlRegex.Replace(message, " "); else mess.MessagePlain = message; mess.LastModified = DateTime.UtcNow; RDN.Library.DataModels.Forum.ForumTopic topic = new DataModels.Forum.ForumTopic(); topic.CreatedByMember = member; topic.LastPostDateTime = DateTime.UtcNow; topic.LastPostByMember = member; if (groupId > 0) topic.GroupId = groupId; topic.Forum = forum; topic.Messages.Add(mess); topic.TopicTitle = subject; topic.LastModified = DateTime.UtcNow; topic.IsSticky = pinMessage; topic.IsLocked = lockMessage; if (chosenCategory > 0) topic.Category = forum.Categories.Where(x => x.CategoryId == chosenCategory).FirstOrDefault(); forum.Topics.Add(topic); int c = dc.SaveChanges(); UpdateForumInbox(dc, topic, memberId); string groupName = "Forum"; if (topic.Forum.LeagueOwner != null) groupName = topic.Forum.LeagueOwner.Name; if (topic.GroupId > 0) { try { var group = SiteCache.GetAllGroups().Where(x => x != null && x.Id == topic.GroupId).FirstOrDefault(); if (group != null) groupName = group.GroupName; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: forumId.ToString() + ":" + forumType + ":" + subject + ":" + message + ":" + memberId.ToString() + ":" + groupId + ":" + emailGroupAboutPost + ":" + pinMessage + ":" + lockMessage + ":" + chosenCategory); } } if (topic.Forum.LeagueOwner != null) { var notify = new ForumNotificationFactory(forumId, topic.Forum.LeagueOwner.LeagueId, true, emailGroupAboutPost, topic.GroupId, topic.TopicId, groupName, topic.TopicTitle, mess.MessageHTML, member.MemberId, member.DerbyName) .LeagueEmailAboutForumPost() .EmailMembersOnWatchList(); //notify.MembersSent.Add(new MemberDisplayBasic() { MemberId = member.MemberId, UserId = member.AspNetUserId }); var fact = new MobileNotificationFactory() .Initialize("Forum Post:", topic.TopicTitle, Mobile.Enums.NotificationTypeEnum.Forum) .AddId(topic.TopicId) .AddMembers(notify.membersAlreadyEmailed) .SendNotifications(); } return topic.TopicId; } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: forumId.ToString() + ":" + forumType + ":" + subject + ":" + message + ":" + memberId.ToString() + ":" + groupId + ":" + emailGroupAboutPost + ":" + pinMessage + ":" + lockMessage + ":" + chosenCategory); } return 0; }
/// <summary> /// /// </summary> /// <param name="page"></param> /// <param name="forumId"></param> /// <param name="groupId"></param> /// <param name="categoryId">id of the category selected. /// Category ID could be -1 which in that case the user selected just UNREAD messages. /// </param> /// <returns></returns> public static List<ForumTopicJson> GetForumTopicsJson(int page, int count, Guid forumId, long groupId, long categoryId, bool isArchived, ForumOwnerTypeEnum forumType) { List<ForumTopicJson> topics = new List<ForumTopicJson>(); try { Guid memId = RDN.Library.Classes.Account.User.GetMemberId(); var dc = new ManagementContext(); int pNum = page * count; //because if the page is greater than 1, then the league already went through the default 100 topics. if (page > 0) pNum = ((page - 1) * count) + Forum.DEFAULT_PAGE_SIZE; var groups = MemberCache.GetGroupsApartOf(memId).ToList(); var groupIds = groups.Select(x => x.Id).ToList(); groupIds.Add(0); List<DataModels.Forum.ForumTopic> dbSticky = new List<DataModels.Forum.ForumTopic>(); List<DataModels.Forum.ForumTopic> db = new List<DataModels.Forum.ForumTopic>(); if (groupIds.Where(x => x == groupId).FirstOrDefault() != null) { if (categoryId == 0 || categoryId == -1) { //don't call stickies if we are on second page. if (page == 0) { dbSticky = (from xx in dc.ForumTopics where xx.Forum.ForumId == forumId where xx.GroupId == groupId where xx.IsRemoved == false where xx.IsSticky == true where xx.IsArchived == isArchived select xx).OrderByDescending(x => x.LastPostDateTime).AsParallel().ToList(); } db = (from xx in dc.ForumTopics where xx.Forum.ForumId == forumId where xx.GroupId == groupId where xx.IsRemoved == false where xx.IsArchived == isArchived select xx).OrderByDescending(x => x.LastPostDateTime).Skip(pNum).Take(count).AsParallel().ToList(); } else { //don't call stickies if we are on second page. if (page == 0) { dbSticky = (from xx in dc.ForumTopics where xx.Forum.ForumId == forumId where xx.Category.CategoryId == categoryId where xx.GroupId == groupId where xx.IsRemoved == false where xx.IsArchived == isArchived where xx.IsSticky == true select xx).OrderByDescending(x => x.LastPostDateTime).AsParallel().ToList(); } db = (from xx in dc.ForumTopics where xx.Forum.ForumId == forumId where xx.Category.CategoryId == categoryId where xx.GroupId == groupId where xx.IsRemoved == false where xx.IsArchived == isArchived select xx).OrderByDescending(x => x.LastPostDateTime).Skip(pNum).Take(count).AsParallel().ToList(); } bool isManager = false; bool isModerator = false; if (forumType == ForumOwnerTypeEnum.league) { isManager = RDN.Library.Cache.MemberCache.IsManagerOrBetterOfLeague(memId); isModerator = MemberCache.IsModeratorOrBetterOfLeagueGroup(memId, groupId); } else if (forumType == ForumOwnerTypeEnum.main) { isManager = MemberCache.IsAdministrator(memId); isModerator = MemberCache.IsAdministrator(memId); } if (categoryId != -1) { foreach (var message in dbSticky) { if (topics.Where(x => x.TopicId == message.TopicId).FirstOrDefault() == null) { ForumTopicJson top = new ForumTopicJson(); if (message.Category != null) { top.Category = message.Category.NameOfCategory; top.CategoryId = message.Category.CategoryId; } top.ForumOwnerTypeEnum = forumType.ToString(); top.ForumId = forumId; top.IsArchived = isArchived; top.LastModified = message.LastModified.GetValueOrDefault(); top.Created = message.Created; top.CreatedHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.Created); top.CreatedByMember = new MemberDisplayBasic(); top.CreatedByMember.DerbyName = message.CreatedByMember.DerbyName; top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.CreatedByMember.DerbyName); top.CreatedByMember.MemberId = message.CreatedByMember.MemberId; top.LastPostByMember = new MemberDisplayBasic(); top.LastPostByMember.DerbyName = message.LastPostByMember.DerbyName; top.LastPostByMember.MemberId = message.LastPostByMember.MemberId; top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.LastPostByMember.DerbyName); if (message.LastPostDateTime != null) top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastPostDateTime.GetValueOrDefault()); else top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastModified.GetValueOrDefault()); top.TopicId = message.TopicId; top.GroupId = message.GroupId; top.TopicTitle = message.TopicTitle; top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.TopicTitle); top.ViewCount = message.ViewCount; top.Replies = message.Messages.Where(x => x.IsRemoved == false).Count() - 1; top.IsLocked = message.IsLocked; top.IsPinned = message.IsSticky; //is league manager top.IsManagerOfTopic = isManager; //if nt league manager and group id > 0 if (message.GroupId > 0 && !isManager) top.IsManagerOfTopic = isModerator; if (message.TopicsInbox.Where(x => x.ToUser.MemberId == memId).FirstOrDefault() == null) top.IsRead = true; if (!top.IsPinned) topics.Add(top); else topics.Insert(0, top); } } } foreach (var message in db) { if (topics.Where(x => x.TopicId == message.TopicId).FirstOrDefault() == null) { ForumTopicJson top = new ForumTopicJson(); if (message.Category != null) { top.Category = message.Category.NameOfCategory; top.CategoryId = message.Category.CategoryId; } top.ForumOwnerTypeEnum = forumType.ToString(); top.ForumId = forumId; top.IsArchived = isArchived; top.LastModified = message.LastModified.GetValueOrDefault(); top.Created = message.Created; top.CreatedHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.Created); top.CreatedByMember = new MemberDisplayBasic(); top.CreatedByMember.DerbyName = message.CreatedByMember.DerbyName; top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.CreatedByMember.DerbyName); top.CreatedByMember.MemberId = message.CreatedByMember.MemberId; top.LastPostByMember = new MemberDisplayBasic(); top.LastPostByMember.DerbyName = message.LastPostByMember.DerbyName; top.LastPostByMember.MemberId = message.LastPostByMember.MemberId; top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.LastPostByMember.DerbyName); if (message.LastPostDateTime != null) top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastPostDateTime.GetValueOrDefault()); else top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastModified.GetValueOrDefault()); top.TopicId = message.TopicId; top.GroupId = message.GroupId; top.TopicTitle = message.TopicTitle; top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.TopicTitle); top.ViewCount = message.ViewCount; top.Replies = message.Messages.Where(x => x.IsRemoved == false).Count() - 1; top.IsLocked = message.IsLocked; top.IsPinned = message.IsSticky; //is league manager top.IsManagerOfTopic = isManager; //if nt league manager and group id > 0 if (message.GroupId > 0 && !isManager) top.IsManagerOfTopic = isModerator; if (message.TopicsInbox.Where(x => x.ToUser.MemberId == memId).FirstOrDefault() == null) top.IsRead = true; if (categoryId != -1) { //users selects something other than unread topics. if (!top.IsPinned) topics.Add(top); else topics.Insert(0, top); } else if (top.IsRead == false) { //case happens when the user selects to only see unread topics. //which means the category will be -1. topics.Add(top); } } } } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return topics; }
/// <summary> /// gets all the forum topics from the DB for the forum id. /// </summary> /// <param name="forumId"></param> public static Forum GetForumTopics(Guid memberId, Guid forumId, ForumOwnerTypeEnum ownerType, long groupId, int count, int page, bool isArchived) { Forum forum = new Forum(); try { var dc = new ManagementContext(); var groups = MemberCache.GetGroupsApartOf(memberId).ToList(); var db = (from xx in dc.Forums.Include("Topics").Include("Topics.Messages").Include("Topics.TopicsInbox") where xx.ForumId == forumId select new { Created = xx.Created, ForumId = xx.ForumId, ForumName = xx.ForumName, LeagueId = xx.LeagueOwner, FederationId = xx.FederationOwner, Categories = xx.Categories.OrderBy(x => x.NameOfCategory) }).FirstOrDefault(); forum.Created = db.Created; forum.ForumId = db.ForumId; forum.ForumName = db.ForumName; bool isManager = RDN.Library.Cache.MemberCache.IsManagerOrBetterOfLeague(memberId); //need to add the default forum. groups.Insert(0, new LeagueGroup { Id = 0, GroupName = db.ForumName }); if (groups.Where(x => x.Id == groupId).FirstOrDefault() == null) return forum; if (ownerType == ForumOwnerTypeEnum.federation) groups = groups.Where(x => x.Id == 0).ToList(); foreach (var g in groups) { ForumGroup gff = new ForumGroup(); gff.GroupId = g.Id; gff.GroupName = g.GroupName; //if nt league manager and group id > 0 if (gff.GroupId > 0 && !isManager) isManager = MemberCache.IsModeratorOrBetterOfLeagueGroup(memberId, gff.GroupId); //this is making sure its the actual starting group. if (g.Id == groupId) { var topics = dc.ForumTopics.Where(x => gff.GroupId == x.GroupId && x.IsRemoved == false && x.IsArchived == isArchived && x.Forum.ForumId == forumId).OrderByDescending(x => x.LastPostDateTime).Skip(page * count).Take(count).AsParallel(); foreach (var topic in topics) { if (gff.Topics.Where(x => x.TopicId == topic.TopicId).FirstOrDefault() == null) { ForumTopic top = DisplayForumTopic(memberId, isManager, topic); if (!top.IsPinned) gff.Topics.Add(top); else gff.Topics.Insert(0, top); } } var topicsSticky = dc.ForumTopics.Where(x => gff.GroupId == x.GroupId && x.IsRemoved == false && x.IsArchived == isArchived && x.IsSticky == true && x.Forum.ForumId == forumId).OrderByDescending(x => x.LastPostDateTime).Skip(page * count).Take(count).AsParallel(); foreach (var topic in topicsSticky) { if (gff.Topics.Where(x => x.TopicId == topic.TopicId).FirstOrDefault() == null) { ForumTopic top = DisplayForumTopic(memberId, isManager, topic); if (!top.IsPinned) gff.Topics.Add(top); else gff.Topics.Insert(0, top); } } var topicsCount = dc.ForumTopics.Where(x => gff.GroupId == x.GroupId && x.IsRemoved == false && x.IsArchived == isArchived && x.Forum.ForumId == forumId).AsParallel().Count(); gff.PageSize = count; gff.CurrentPage = page; gff.NumberOfRecords = topicsCount; gff.NumberOfPages = (int)Math.Ceiling((double)topicsCount / count); forum.CurrentGroup = gff; } gff.UnreadTopics = dc.ForumInbox.Where(x => x.Topic.GroupId == gff.GroupId && x.Topic.IsRemoved == false && x.Topic.IsArchived == isArchived && x.Topic.Forum.ForumId == forumId && x.ToUser.MemberId == memberId).Count(); forum.GroupTopics.Add(gff); } //add the categories. //adds the unread category if there are unread messages ForumCategory unreadCat = new ForumCategory(); unreadCat.CategoryId = -1; unreadCat.CategoryName = "Unread"; unreadCat.GroupId = 0; var gro = forum.GroupTopics.Where(x => x.GroupId == groupId).FirstOrDefault(); if (gro != null) unreadCat.UnreadTopics = gro.UnreadTopics; if (unreadCat.UnreadTopics > 0) forum.Categories.Add(unreadCat); ForumCategory latestCat = new ForumCategory(); latestCat.CategoryId = 0; latestCat.CategoryName = "Latest"; latestCat.GroupId = groupId; if (gro != null) latestCat.UnreadTopics = gro.UnreadTopics; var cats = db.Categories.Where(x => x.IsRemoved == false && x.GroupId == latestCat.GroupId).OrderBy(x => x.NameOfCategory); if (cats.Count() > 0 || unreadCat.UnreadTopics > 0) forum.Categories.Add(latestCat); foreach (var cat in cats) { ForumCategory c = new ForumCategory(); c.CategoryId = cat.CategoryId; c.CategoryName = cat.NameOfCategory; c.GroupId = cat.GroupId; c.UnreadTopics = dc.ForumInbox.Where(x => x.Topic.GroupId == cat.GroupId && x.Topic.Forum.ForumId == forumId && x.ToUser.MemberId == memberId && x.Topic.Category.CategoryId == cat.CategoryId).Count(); forum.Categories.Add(c); } if (ownerType == ForumOwnerTypeEnum.federation) { forum.FederationId = db.FederationId.FederationId; forum.Type = ForumOwnerTypeEnum.federation; } else if (ownerType == ForumOwnerTypeEnum.league) { forum.LeagueId = db.LeagueId.LeagueId; forum.Type = ForumOwnerTypeEnum.league; } } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return forum; }
/// <summary> /// creates a new forum. /// </summary> /// <param name="ownerId">the league or federation id</param> /// <param name="ownerType">type of owner so we can choose who owns it.</param> /// <param name="forumName">name of the forum.</param> public static Guid CreateNewForum(Guid ownerId, ForumOwnerTypeEnum ownerType, string forumName) { try { var dc = new ManagementContext(); RDN.Library.DataModels.Forum.Forum forum = new DataModels.Forum.Forum(); if (ownerType == ForumOwnerTypeEnum.federation) { forum.FederationOwner = dc.Federations.Where(x => x.FederationId == ownerId).FirstOrDefault(); } else if (ownerType == ForumOwnerTypeEnum.league) { forum.LeagueOwner = dc.Leagues.Where(x => x.LeagueId == ownerId).FirstOrDefault(); } forum.ForumName = forumName; dc.Forums.Add(forum); dc.SaveChanges(); return forum.ForumId; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return new Guid(); }