Beispiel #1
0
        public void Vote(Post post, Identity voter, VoteType vote)
        {
            using (var repo = new Repository(_directory.FullName))
            {
                var postCommit = repo.Branches[post.Id].Tip;

                // Retrieve existing tree
                var commitRoot = postCommit.Tree;
                var votesDir = (Tree)commitRoot[VOTES_DIR].Target;
                var repliesDir = (Tree)commitRoot[REPLIES_DIR].Target;

                // Copy existing content to new votes treedef
                var newVotesDir = new TreeDefinition();
                foreach (TreeEntry obj in votesDir)
                {
                    newVotesDir.Add(obj.Name, obj);
                }
                // Add new vote to new votes treedef
                Vote(repo, newVotesDir, vote);

                // Assemble new root treedef
                var newPostRoot = new TreeDefinition();
                newPostRoot.Add(VOTES_DIR, repo.ObjectDatabase.CreateTree(newVotesDir));
                newPostRoot.Add(REPLIES_DIR, repliesDir);

                // Commit new root treedef to post branch
                var message = string.Format("{0} by {1}", vote, voter.Name);
                var sig = new Signature(voter.Name, voter.Identifier, DateTimeOffset.UtcNow);
                CommitToBranch(repo, post.Id, message, sig, repo.ObjectDatabase.CreateTree(newPostRoot));
            }
        }
Beispiel #2
0
        public void NewPost(Post post)
        {
            using (var repo = new Repository(_directory.FullName))
            {
                var json = JsonConvert.SerializeObject(post);
                var sig = new Signature(post.Author.Name, post.Author.Identifier, post.Timestamp);

                // Create post structure
                var votesDir = repo.ObjectDatabase.CreateTree(new TreeDefinition());
                var repliesDir = repo.ObjectDatabase.CreateTree(new TreeDefinition());
                var postRoot = new TreeDefinition();
                postRoot.Add(VOTES_DIR, votesDir);
                postRoot.Add(REPLIES_DIR, repliesDir);

                var commit = CommitToBranch(repo, CONTENT_BRANCH, json, sig, repo.ObjectDatabase.CreateTree(postRoot));
                // Create a named branch for all future content on this post
                repo.CreateBranch(commit.Sha, commit);
            }
        }