public List <ItemModels.MessagePreview> GetPreviews(int boardId, int page, int unread) { var participation = new List <DataModels.Participant>(); var viewLogs = new List <DataModels.ViewLog>(); var historyTimeLimit = SettingsRepository.HistoryTimeLimit(); if (UserContext.IsAuthenticated) { participation = DbContext.Participants.Where(r => r.UserId == UserContext.ApplicationUser.Id).ToList(); viewLogs = DbContext.ViewLogs.Where(r => r.LogTime >= historyTimeLimit && r.UserId == UserContext.ApplicationUser.Id).ToList(); } var sortedMessageIds = GetIndexIds(boardId, page, unread, historyTimeLimit, participation, viewLogs); var messageQuery = from message in DbContext.Messages where sortedMessageIds.Contains(message.Id) select new { message.Id, message.ShortPreview, message.ViewCount, message.ReplyCount, message.TimePosted, message.LastReplyId, message.LastReplyById, message.LastReplyPosted }; var messages = messageQuery.ToList(); var take = SettingsRepository.MessagesPerPage(); var popularityLimit = SettingsRepository.PopularityLimit(); var messagePreviews = new List <ItemModels.MessagePreview>(); foreach (var messageId in sortedMessageIds) { var message = messages.First(item => item.Id == messageId); var messagePreview = new ItemModels.MessagePreview { Id = message.Id, ShortPreview = string.IsNullOrEmpty(message.ShortPreview.Trim()) ? "No subject" : message.ShortPreview, Views = message.ViewCount, Replies = message.ReplyCount, Pages = Convert.ToInt32(Math.Ceiling(1.0 * message.ReplyCount / take)), LastReplyId = message.Id, Popular = message.ReplyCount > popularityLimit, Pinned = PinRepository.Any(item => item.MessageId == message.Id) }; messagePreviews.Add(messagePreview); var lastMessageTime = message.TimePosted; if (message.LastReplyId != 0) { var lastReplyDetails = (from item in DbContext.Messages join postedBy in DbContext.Users on item.PostedById equals postedBy.Id where item.Id == message.LastReplyId select new { postedBy.DisplayName, item.ShortPreview }).FirstOrDefault(); if (lastReplyDetails != null) { messagePreview.LastReplyId = message.LastReplyId; messagePreview.LastReplyPreview = lastReplyDetails.ShortPreview; messagePreview.LastReplyByName = lastReplyDetails.DisplayName; messagePreview.LastReplyById = message.LastReplyById; messagePreview.LastReplyPosted = message.LastReplyPosted.ToPassedTimeString(); messagePreview.LastReplyPostedDT = message.LastReplyPosted; lastMessageTime = message.LastReplyPosted; } } if (lastMessageTime > historyTimeLimit) { messagePreview.Unread = GetUnreadLevel(message.Id, lastMessageTime, participation, viewLogs); } } return(messagePreviews); }
public ServiceModels.ServiceResponse GetLatest(int messageId) { var serviceResponse = new ServiceModels.ServiceResponse(); var record = DbContext.Messages.Find(messageId); if (record is null) { throw new HttpNotFoundError(); } if (record.ParentId > 0) { record = DbContext.Messages.Find(record.ParentId); } if (!UserContext.IsAuthenticated) { serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Topics.Display), nameof(Controllers.Topics), new { id = record.LastReplyId }); return(serviceResponse); } var historyTimeLimit = SettingsRepository.HistoryTimeLimit(); var viewLogs = DbContext.ViewLogs.Where(r => r.UserId == UserContext.ApplicationUser.Id && r.LogTime >= historyTimeLimit).ToList(); var latestViewTime = historyTimeLimit; foreach (var viewLog in viewLogs) { switch (viewLog.TargetType) { case EViewLogTargetType.All: if (viewLog.LogTime >= latestViewTime) { latestViewTime = viewLog.LogTime; } break; case EViewLogTargetType.Message: if (viewLog.TargetId == record.Id && viewLog.LogTime >= latestViewTime) { latestViewTime = viewLog.LogTime; } break; } } var messageIdQuery = from message in DbContext.Messages where message.Id == record.Id || message.ParentId == record.Id where message.TimePosted > latestViewTime select message.Id; var latestMessageId = messageIdQuery.FirstOrDefault(); if (latestMessageId == 0) { latestMessageId = record.LastReplyId; } if (latestMessageId == 0) { latestMessageId = record.Id; } serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Topics.Display), nameof(Controllers.Topics), new { id = latestMessageId }); return(serviceResponse); }