Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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));
        }