Exemplo n.º 1
0
        public async Task ExecuteAsync(PostId postId, DateTime now, Func <Task> potentialRemovalOperation)
        {
            postId.AssertNotNull("postId");
            now.AssertUtc("now");
            potentialRemovalOperation.AssertNotNull("potentialRemovalOperation");

            var queuedCollectionId = await this.tryGetPostQueueId.ExecuteAsync(postId, now);

            if (queuedCollectionId == null)
            {
                await potentialRemovalOperation();
            }
            else
            {
                var weeklyReleaseSchedule = await this.getWeeklyReleaseSchedule.ExecuteAsync(queuedCollectionId);

                using (var transaction = TransactionScopeBuilder.CreateAsync())
                {
                    await potentialRemovalOperation();

                    await this.defragmentQueue.ExecuteAsync(queuedCollectionId, weeklyReleaseSchedule, now);

                    transaction.Complete();
                }
            }
        }
Exemplo n.º 2
0
        public Task <bool> IsWriteAllowedAsync(UserId requester, PostId postId)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            return(this.isPostOwner.ExecuteAsync(requester, postId));
        }
Exemplo n.º 3
0
        public async Task ExecuteAsync(PostId postId, QueueId queueId)
        {
            postId.AssertNotNull("postId");
            queueId.AssertNotNull("queueId");

            var nextLiveDate = await this.getLiveDateOfNewQueuedPost.ExecuteAsync(queueId);

            var post = new Post(postId.Value)
            {
                QueueId  = queueId.Value,
                LiveDate = nextLiveDate
            };

            var parameters = new SqlGenerationParameters <Post, Post.Fields>(post)
            {
                UpdateMask = Post.Fields.QueueId | Post.Fields.LiveDate,
                Conditions = new[]
                {
                    WherePostLiveDateUniqueToQueue, // Perform locks in 'descending supersets' to avoid deadlock.
                    WherePostNotInQueue
                }
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                var failedConditionIndex = await connection.UpdateAsync(parameters);

                var concurrencyFailure = failedConditionIndex == 0;

                if (concurrencyFailure)
                {
                    throw new OptimisticConcurrencyException(string.Format("Failed to optimistically queue post with queue {0}", queueId));
                }
            }
        }
Exemplo n.º 4
0
        public async Task <PostSecurityResult> IsReadAllowedAsync(UserId requester, PostId postId, DateTime timestamp)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            if (await this.isPostSubscriber.ExecuteAsync(requester, postId, timestamp))
            {
                return(PostSecurityResult.Subscriber);
            }

            if (await this.isPostOwner.ExecuteAsync(requester, postId))
            {
                return(PostSecurityResult.Owner);
            }

            if (await this.isPostFreeAccessUser.ExecuteAsync(requester, postId))
            {
                return(PostSecurityResult.GuestList);
            }

            if (await this.isFreePostDbStatement.ExecuteAsync(requester, postId))
            {
                return(PostSecurityResult.FreePost);
            }

            return(PostSecurityResult.Denied);
        }
Exemplo n.º 5
0
        public async Task <bool> IsCommentAllowedAsync(UserId requester, PostId postId, DateTime timestamp)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            return(await this.isPostSubscriber.ExecuteAsync(requester, postId, timestamp) ||
                   await this.isPostOwner.ExecuteAsync(requester, postId) ||
                   await this.isPostFreeAccessUser.ExecuteAsync(requester, postId));
        }
Exemplo n.º 6
0
        public async Task AssertReadAllowedAsync(UserId requester, PostId postId, DateTime timestamp)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            var isAllowed = await this.IsReadAllowedAsync(requester, postId, timestamp);

            if (isAllowed == PostSecurityResult.Denied)
            {
                throw new UnauthorizedException("Not allowed to read post. {0} {1}", requester, postId);
            }
        }
Exemplo n.º 7
0
        public async Task AssertWriteAllowedAsync(UserId requester, PostId postId)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            var isPostingAllowed = await this.IsWriteAllowedAsync(requester, postId);

            if (!isPostingAllowed)
            {
                throw new UnauthorizedException("Not allowed to write post. {0} {1}", requester, postId);
            }
        }
Exemplo n.º 8
0
        public async Task AssertCommentAllowedAsync(UserId requester, PostId postId, DateTime timestamp)
        {
            requester.AssertNotNull("requester");
            postId.AssertNotNull("postId");

            var isCommentOrLikeAllowed = await this.IsCommentAllowedAsync(requester, postId, timestamp);

            if (!isCommentOrLikeAllowed)
            {
                throw new UnauthorizedException("Not allowed to comment on post. {0} {1}", requester, postId);
            }
        }
Exemplo n.º 9
0
        public async Task ExecuteAsync(UserId userId, PostId postId, DateTime timestamp)
        {
            userId.AssertNotNull("userId");
            postId.AssertNotNull("postId");

            var like = new Like(postId.Value, null, userId.Value, null, timestamp);

            using (var connection = this.connectionFactory.CreateConnection())
            {
                await connection.UpsertAsync(
                    like,
                    Like.Fields.CreationDate);
            }
        }
Exemplo n.º 10
0
        public async Task ExecuteAsync(UserId userId, PostId postId)
        {
            userId.AssertNotNull("userId");
            postId.AssertNotNull("postId");

            using (var connection = this.connectionFactory.CreateConnection())
            {
                await connection.ExecuteAsync(
                    Sql,
                    new
                {
                    UserId = userId.Value,
                    PostId = postId.Value,
                });
            }
        }
Exemplo n.º 11
0
        public async Task <QueueId> ExecuteAsync(PostId postId, DateTime now)
        {
            postId.AssertNotNull("postId");
            now.AssertUtc("now");

            var parameters = new
            {
                PostId = postId.Value,
                Now    = now
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                return(await connection.ExecuteScalarAsync <QueueId>(Sql, parameters));
            }
        }
        public Task ExecuteAsync(
            PostId newPostId,
            ChannelId channelId,
            ValidComment content,
            DateTime?sheduledPostDate,
            QueueId queueId,
            ValidPreviewText previewText,
            FileId previewImageId,
            IReadOnlyList <FileId> fileIds,
            int previewWordCount,
            int wordCount,
            int imageCount,
            int fileCount,
            int videoCount,
            DateTime now)
        {
            newPostId.AssertNotNull("newPostId");
            content.AssertNotNull("content");
            channelId.AssertNotNull("channelId");

            var post = new Post(
                newPostId.Value,
                channelId.Value,
                null,
                queueId == null ? (Guid?)null : queueId.Value,
                null,
                previewImageId == null ? (Guid?)null : previewImageId.Value,
                null,
                previewText == null ? null : previewText.Value,
                content.Value,
                previewWordCount,
                wordCount,
                imageCount,
                fileCount,
                videoCount,
                default(DateTime), // Live date assigned by sub-statements.
                now);

            var postFiles = fileIds.EmptyIfNull().Select(v => new PostFile(newPostId.Value, v.Value)).ToList();

            if (queueId != null)
            {
                return(this.subStatements.QueuePostAsync(post, postFiles));
            }

            return(this.subStatements.SchedulePostAsync(post, postFiles, sheduledPostDate, now));
        }
        public async Task <bool> ExecuteAsync(UserId userId, PostId postId, DateTime now)
        {
            userId.AssertNotNull("userId");
            postId.AssertNotNull("postId");

            using (var connection = this.connectionFactory.CreateConnection())
            {
                return(await connection.ExecuteScalarAsync <bool>(
                           Sql,
                           new
                {
                    PostId = postId.Value,
                    RequestorId = userId.Value,
                    Now = now
                }));
            }
        }
        public async Task ExecuteAsync(UserId userId, PostId postId, CommentId commentId, Shared.Comment content, DateTime timestamp)
        {
            userId.AssertNotNull("userId");
            postId.AssertNotNull("postId");
            commentId.AssertNotNull("commentId");
            content.AssertNotNull("content");

            var comment = new Persistence.Comment(commentId.Value, postId.Value, null, userId.Value, null, content.Value, timestamp);

            using (var connection = this.connectionFactory.CreateConnection())
            {
                await connection.UpsertAsync(
                    comment,
                    Persistence.Comment.Fields.PostId
                    | Persistence.Comment.Fields.UserId
                    | Persistence.Comment.Fields.Content
                    | Persistence.Comment.Fields.CreationDate);
            }
        }
        public async Task <bool> ExecuteAsync(UserId requestorId, PostId postId, DateTime timestamp, int maximumPosts)
        {
            requestorId.AssertNotNull("requestorId");
            postId.AssertNotNull("postId");

            using (var connection = this.connectionFactory.CreateConnection())
            {
                var result = await connection.ExecuteScalarAsync <bool>(
                    Sql,
                    new
                {
                    PostId       = postId.Value,
                    UserId       = requestorId.Value,
                    Timestamp    = timestamp,
                    MaximumPosts = maximumPosts
                });

                return(result);
            }
        }
Exemplo n.º 16
0
        public async Task ExecuteAsync(PostId postId, DateTime newTime, DateTime now)
        {
            postId.AssertNotNull("postId");
            newTime.AssertUtc("newTime");
            now.AssertUtc("now");

            var post = new Post(postId.Value)
            {
                LiveDate = this.scheduledDateClipping.Apply(now, newTime),
                QueueId  = null
            };

            var parameters = new SqlGenerationParameters <Post, Post.Fields>(post)
            {
                UpdateMask = Post.Fields.LiveDate | Post.Fields.QueueId
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                await connection.UpdateAsync(parameters);
            }
        }
Exemplo n.º 17
0
        public async Task <GetPostDbResult> ExecuteAsync(UserId requestorId, PostId postId)
        {
            postId.AssertNotNull("postId");

            var parameters = new
            {
                PostId      = postId.Value,
                RequestorId = requestorId == null ? null : (Guid?)requestorId.Value,
            };

            var query = new StringBuilder();

            query.Append(GetNewsfeedDbStatement.GetSqlStart(requestorId, GetNewsfeedDbStatement.SqlQuerySource.FullPost));
            query.Append(SqlFilter);
            query.Append(GetFilesSql);

            using (var connection = this.connectionFactory.CreateConnection())
            {
                using (var multi = await connection.QueryMultipleAsync(query.ToString(), parameters))
                {
                    var post = (await multi.ReadAsync <PreviewNewsfeedPost.Builder>()).SingleOrDefault();

                    if (post == null)
                    {
                        return(null);
                    }

                    ProcessNewsfeedResults(post);

                    var files = (await multi.ReadAsync <GetPostDbResult.PostFileDbResult.Builder>()).ToList();

                    return(new GetPostDbResult(
                               post.Build(),
                               files.Select(v => v.Build()).ToList()));
                }
            }
        }
        public async Task <CommentsResult> ExecuteAsync(PostId postId)
        {
            postId.AssertNotNull("postId");

            using (var connection = this.connectionFactory.CreateConnection())
            {
                var result = await connection.QueryAsync <CommentWithUser>(
                    Sql,
                    new
                {
                    PostId = postId.Value
                });

                return(new CommentsResult(
                           new List <CommentsResult.Item>(
                               result.Select(v => new CommentsResult.Item(
                                                 new CommentId(v.Id),
                                                 new PostId(v.PostId),
                                                 new UserId(v.UserId),
                                                 new Username(v.UserName),
                                                 new Shared.Comment(v.Content),
                                                 DateTime.SpecifyKind(v.CreationDate, DateTimeKind.Utc))))));
            }
        }
Exemplo n.º 19
0
        public async Task ExecuteAsync(
            PostId postId,
            ValidComment content,
            ValidPreviewText previewText,
            FileId previewImageId,
            IReadOnlyList <FileId> fileIds,
            int previewWordCount,
            int wordCount,
            int imageCount,
            int fileCount,
            int videoCount)
        {
            postId.AssertNotNull("postId");
            fileIds.AssertNotNull("fileIds");

            var post = new Post(postId.Value)
            {
                // QueueId = command.QueueId.Value, - Removed as this would require a queue defragmentation if post is already queued. Unnecessary complexity for MVP.
                PreviewText      = previewText == null ? null : previewText.Value,
                PreviewImageId   = previewImageId == null ? (Guid?)null : previewImageId.Value,
                Content          = content == null ? null : content.Value,
                PreviewWordCount = previewWordCount,
                WordCount        = wordCount,
                ImageCount       = imageCount,
                FileCount        = fileCount,
                VideoCount       = videoCount
            };

            var postFiles = fileIds.Select(v => new PostFile(postId.Value, v.Value)).ToList();

            using (var transaction = TransactionScopeBuilder.CreateAsync())
            {
                using (var connection = this.connectionFactory.CreateConnection())
                {
                    // The order we access tables should match PostToChannelDbStatement.
                    var rowsUpdated = await connection.UpdateAsync(
                        post,
                        Post.Fields.PreviewText
                        | Post.Fields.PreviewImageId
                        | Post.Fields.Content
                        | Post.Fields.PreviewWordCount
                        | Post.Fields.WordCount
                        | Post.Fields.ImageCount
                        | Post.Fields.FileCount
                        | Post.Fields.VideoCount);

                    if (rowsUpdated > 0)
                    {
                        await connection.ExecuteAsync(
                            DeleteQuery,
                            new { PostId = postId.Value });

                        if (fileIds.Count > 0)
                        {
                            await connection.InsertAsync(postFiles);
                        }
                    }
                }

                transaction.Complete();
            }
        }