/// <summary> /// 创建 <see cref="ActivityCommentList"/> /// </summary> /// <param name="activityId">动态 ID</param> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="before">起始位置</param> /// <param name="take">获取数量</param> /// <param name="isOperator">当前登录用户是否为运维职员</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns><see cref="ActivityCommentList"/></returns> public static async Task <ActivityCommentList> CreateAsync(string activityId, string currentUserId, int before, int take, bool isOperator, KeylolDbContext dbContext, CachedDataProvider cachedData) { var activity = await dbContext.Activities.FindAsync(activityId); if (activity == null || (activity.Archived != ArchivedState.None && currentUserId != activity.AuthorId && !isOperator)) { return(new ActivityCommentList(0)); } var queryResult = await(from comment in dbContext.ActivityComments where comment.ActivityId == activity.Id && comment.Sid < before orderby comment.Sid descending select new { AuthorIdCode = comment.Commentator.IdCode, AuthorAvatarImage = comment.Commentator.AvatarImage, AuthorUserName = comment.Commentator.UserName, AuthorId = comment.CommentatorId, comment.Id, comment.PublishTime, comment.SidForActivity, comment.Content, comment.Archived, comment.Warned }).Take(take).ToListAsync(); var result = new ActivityCommentList(queryResult.Count); foreach (var c in queryResult) { var activityComment = new ActivityComment { Id = c.Id, SidForActivity = c.SidForActivity, Archived = c.Archived != ArchivedState.None }; // ReSharper disable once PossibleInvalidOperationException if (!activityComment.Archived.Value || currentUserId == c.AuthorId || isOperator) { activityComment.AuthorIdCode = c.AuthorIdCode; activityComment.AuthorAvatarImage = c.AuthorAvatarImage; activityComment.AuthorUserName = c.AuthorUserName; activityComment.LikeCount = await cachedData.Likes.GetTargetLikeCountAsync(c.Id, LikeTargetType.ActivityComment); activityComment.Liked = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Likes.IsLikedAsync(currentUserId, c.Id, LikeTargetType.ActivityComment); activityComment.PublishTime = c.PublishTime; activityComment.Content = c.Content; activityComment.Warned = c.Warned; } result.Add(activityComment); } result.Reverse(); return(result); }
/// <summary> /// 创建 <see cref="ActivityPage"/> /// </summary> /// <param name="authorIdCode">作者识别码</param> /// <param name="sidForAuthor">动态在作者名下的序号</param> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="isOperator">当前登录用户是否为运维职员</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <param name="userManager"><see cref="KeylolUserManager"/></param> /// <returns><see cref="ActivityPage"/></returns> public static async Task <ActivityPage> CreateAsync(string authorIdCode, int sidForAuthor, string currentUserId, bool isOperator, KeylolDbContext dbContext, CachedDataProvider cachedData, KeylolUserManager userManager) { var activityPage = new ActivityPage(); var activity = await dbContext.Activities .Include(a => a.Author) .Include(a => a.TargetPoint) .Where(a => a.Author.IdCode == authorIdCode && a.SidForAuthor == sidForAuthor) .SingleOrDefaultAsync(); if (activity == null) { return(activityPage); } activityPage.Archived = activity.Archived != ArchivedState.None; if (activityPage.Archived.Value && currentUserId != activity.AuthorId && !isOperator) { return(activityPage); } activityPage.PointBasicInfo = await PointBasicInfo.CreateAsync(currentUserId, activity.TargetPoint, dbContext, cachedData); activityPage.AuthorBasicInfo = new UserBasicInfo { Id = activity.Author.Id, IdCode = activity.Author.IdCode, AvatarImage = activity.Author.AvatarImage, UserName = activity.Author.UserName, FriendCount = await cachedData.Subscriptions.GetFriendCountAsync(activity.AuthorId), SubscribedUserCount = await cachedData.Subscriptions .GetSubscribedUserCountAsync(activity.AuthorId), SubscriberCount = await cachedData.Subscriptions .GetSubscriberCountAsync(activity.AuthorId, SubscriptionTargetType.User), IsFriend = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Users.IsFriendAsync(currentUserId, activity.AuthorId), Subscribed = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Subscriptions.IsSubscribedAsync(currentUserId, activity.AuthorId, SubscriptionTargetType.User), SteamId = await userManager.GetSteamIdAsync(activity.AuthorId) }; if (!string.IsNullOrWhiteSpace(activityPage.AuthorBasicInfo.SteamId)) { activityPage.AuthorBasicInfo.SteamProfileName = activity.Author.SteamProfileName; } activityPage.AuthorPlayedTime = activity.TargetPoint.SteamAppId == null ? null : (await dbContext.UserSteamGameRecords .Where(r => r.UserId == activity.AuthorId && r.SteamAppId == activity.TargetPoint.SteamAppId) .SingleOrDefaultAsync())?.TotalPlayedTime; activityPage.Id = activity.Id; activityPage.Rejected = activity.Rejected; activityPage.Warned = activity.Warned; activityPage.PublishTime = activity.PublishTime; activityPage.Rating = activity.Rating; activityPage.Content = activity.Content; activityPage.CoverImage = activity.CoverImage; var attachedPointIds = Helpers.SafeDeserialize <List <string> >(activity.AttachedPoints) ?? new List <string>(); activityPage.AttachedPoints = (from id in attachedPointIds join point in await(from point in dbContext.Points where attachedPointIds.Contains(point.Id) select new { point.Type, point.Id, point.IdCode, point.AvatarImage, point.ChineseName, point.EnglishName }).ToListAsync() on id equals point.Id select new PointBasicInfo { Type = point.Type, Id = point.Id, IdCode = point.IdCode, AvatarImage = point.AvatarImage, ChineseName = point.ChineseName, EnglishName = point.EnglishName }).ToList(); var comments = await ActivityCommentList.CreateAsync(activity, 1, currentUserId, isOperator, true, dbContext, cachedData); activityPage.CommentCount = comments.Item2; activityPage.CommentPageCount = comments.Item3; activityPage.Comments = comments.Item1; activityPage.LikeCount = await cachedData.Likes.GetTargetLikeCountAsync(activity.Id, LikeTargetType.Activity); activityPage.Liked = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Likes.IsLikedAsync(currentUserId, activity.Id, LikeTargetType.Activity); return(activityPage); }
/// <summary> /// 创建 <see cref="ActivityCommentList"/> /// </summary> /// <param name="activity">动态对象</param> /// <param name="page">分页页码</param> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="isOperator">当前登录用户是否为运维职员</param> /// <param name="returnMeta">是否返回元数据(总页数、总评论数)</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns>Item1 表示 <see cref="ActivityCommentList"/>, Item2 表示总评论数,Item3 表示总页数</returns> public static async Task <Tuple <ActivityCommentList, int, int> > CreateAsync(Models.Activity activity, int page, string currentUserId, bool isOperator, bool returnMeta, KeylolDbContext dbContext, CachedDataProvider cachedData) { if (activity.Archived != ArchivedState.None && currentUserId != activity.AuthorId && !isOperator) { return(new Tuple <ActivityCommentList, int, int>(new ActivityCommentList(0), 0, 0)); } var queryResult = await(from comment in dbContext.ActivityComments where comment.ActivityId == activity.Id orderby comment.Sid select new { AuthorIdCode = comment.Commentator.IdCode, AuthorAvatarImage = comment.Commentator.AvatarImage, AuthorUserName = comment.Commentator.UserName, AuthorId = comment.CommentatorId, comment.Id, comment.PublishTime, comment.SidForActivity, comment.Content, comment.Archived, comment.Warned }).TakePage(page, RecordsPerPage).ToListAsync(); var result = new ActivityCommentList(queryResult.Count); foreach (var c in queryResult) { var activityComment = new ActivityComment { Id = c.Id, SidForActivity = c.SidForActivity, Archived = c.Archived != ArchivedState.None }; // ReSharper disable once PossibleInvalidOperationException if (!activityComment.Archived.Value || currentUserId == c.AuthorId || isOperator) { activityComment.AuthorIdCode = c.AuthorIdCode; activityComment.AuthorAvatarImage = c.AuthorAvatarImage; activityComment.AuthorUserName = c.AuthorUserName; activityComment.AuthorPlayedTime = activity.TargetPoint.SteamAppId == null ? null : (await dbContext.UserSteamGameRecords .Where(r => r.UserId == c.AuthorId && r.SteamAppId == activity.TargetPoint.SteamAppId) .SingleOrDefaultAsync())?.TotalPlayedTime; activityComment.LikeCount = await cachedData.Likes.GetTargetLikeCountAsync(c.Id, LikeTargetType.ActivityComment); activityComment.Liked = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Likes.IsLikedAsync(currentUserId, c.Id, LikeTargetType.ActivityComment); activityComment.PublishTime = c.PublishTime; activityComment.Content = c.Content; activityComment.Warned = c.Warned; } result.Add(activityComment); } var count = await cachedData.ActivityComments.GetActivityCommentCountAsync(activity.Id); return(new Tuple <ActivityCommentList, int, int>(result, count, count > 0 ? (int)Math.Ceiling(count / (double)RecordsPerPage) : 1)); }