<commit_before>﻿using Fan.Blogs.Enums;
using Fan.Blogs.Helpers;
using Fan.Blogs.Models;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace Fan.Blogs.ViewModels
{
    /// <summary>
    /// View model for a blog post.
    /// </summary>
    /// <remarks>
    /// This is used on Index.cshtml, Post.cshtml, _PostInfo.cshtml and _Comments.cshtml views,
    /// as well as <see cref="BlogController.Feed"/>.
    /// </remarks>
    public class BlogPostViewModel
    {
        public BlogPostViewModel(BlogPost blogPost, BlogSettings blogSettings, HttpRequest request)
        {
            Title = blogPost.Title;
            Body = blogPost.Body;
            Excerpt = blogPost.Excerpt;
            Author = blogPost.User.DisplayName;
            CreatedOn = blogPost.CreatedOn;
            CreatedOnFriendly = blogPost.CreatedOnFriendly;
            Tags = blogPost.Tags;
            Category = blogPost.Category;

            RelativeLink = string.Format("/" + BlogRoutes.POST_RELATIVE_URL_TEMPLATE, CreatedOn.Year, CreatedOn.Month, CreatedOn.Day, blogPost.Slug);
            var permalinkPart = string.Format(BlogRoutes.POST_PERMA_URL_TEMPLATE, blogPost.Id);
            Permalink = $"{request.Scheme}://{request.Host}/{permalinkPart}";
            CanonicalUrl = $"{request.Scheme}://{request.Host}{RelativeLink}";
            EditLink = string.Format("/" + BlogRoutes.POST_EDIT_URL_TEMPLATE, blogPost.Id);

            DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}";
            ShowDisqus = blogSettings.AllowCommentsOnBlogPost && blogSettings.CommentProvider == ECommentProvider.Disqus;
            DisqusShortname = blogSettings.DisqusShortname;

            var hash = "";
            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i<blogPost.Tags.Count-1) sb.Append(",");
                }
                hash = sb.ToString();
            }
            TwitterShareLink = hash.IsNullOrEmpty() ?
                $"https://twitter.com/intent/tweet?text={Title}&url={Permalink}" :
                $"https://twitter.com/intent/tweet?text={Title}&url={Permalink}&hashtags={hash}";
            FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={Permalink}";
            GoogleShareLink = $"https://plus.google.com/share?url={Permalink}";
            LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={Permalink}&title={Title}";
        }

        // -------------------------------------------------------------------- BlogPost

        /// <summary>
        /// The title of the blog post.
        /// </summary>
        public string Title { get; }
        public string Body { get; }
        public string Excerpt { get; }
        /// <summary>
        /// Display name of the user who authored the post.
        /// </summary>
        public string Author { get; }
        /// <summary><commit_msg>#89 Shorten social sharing URL<commit_after>﻿using Fan.Blogs.Enums;
using Fan.Blogs.Helpers;
using Fan.Blogs.Models;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace Fan.Blogs.ViewModels
{
    /// <summary>
    /// View model for a blog post.
    /// </summary>
    /// <remarks>
    /// This is used on Index.cshtml, Post.cshtml, _PostInfo.cshtml and _Comments.cshtml views,
    /// as well as <see cref="BlogController.Feed"/>.
    /// </remarks>
    public class BlogPostViewModel
    {
        public BlogPostViewModel(BlogPost blogPost, BlogSettings blogSettings, HttpRequest request)
        {
            Title = blogPost.Title;
            Body = blogPost.Body;
            Excerpt = blogPost.Excerpt;
            Author = blogPost.User.DisplayName;
            CreatedOn = blogPost.CreatedOn;
            CreatedOnFriendly = blogPost.CreatedOnFriendly;
            Tags = blogPost.Tags;
            Category = blogPost.Category;

            RelativeLink = string.Format("/" + BlogRoutes.POST_RELATIVE_URL_TEMPLATE, CreatedOn.Year, CreatedOn.Month, CreatedOn.Day, blogPost.Slug);
            var permalinkPart = string.Format(BlogRoutes.POST_PERMA_URL_TEMPLATE, blogPost.Id);
            Permalink = $"{request.Scheme}://{request.Host}/{permalinkPart}";
            CanonicalUrl = $"{request.Scheme}://{request.Host}{RelativeLink}";
            EditLink = string.Format("/" + BlogRoutes.POST_EDIT_URL_TEMPLATE, blogPost.Id);

            DisqusPageIdentifier = $"{ECommentTargetType.BlogPost}_{blogPost.Id}";
            ShowDisqus = blogSettings.AllowCommentsOnBlogPost && blogSettings.CommentProvider == ECommentProvider.Disqus;
            DisqusShortname = blogSettings.DisqusShortname;

            var hash = "";
            if (blogPost.Tags.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < blogPost.Tags.Count; i++)
                {
                    var tag = blogPost.Tags[i];
                    sb.Append(tag.Slug.Replace("-", ""));
                    if (i<blogPost.Tags.Count-1) sb.Append(",");
                }
                hash = sb.ToString();
            }

            var requestHostShort = request.Host.ToString().StartsWith("www.") ? 
                request.Host.ToString().Remove(0, 4) : request.Host.ToString();
            var permalinkShort = $"{requestHostShort}/{permalinkPart}";

            TwitterShareLink = hash.IsNullOrEmpty() ?
                $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}" :
                $"https://twitter.com/intent/tweet?text={Title}&url={permalinkShort}&hashtags={hash}";
            FacebookShareLink = $"https://www.facebook.com/sharer/sharer.php?u={permalinkShort}";
            GoogleShareLink = $"https://plus.google.com/share?url={permalinkShort}";
            LinkedInShareLink = $"http://www.linkedin.com/shareArticle?mini=true&url={permalinkShort}&title={Title}";
        }

        // -------------------------------------------------------------------- BlogPost

        /// <summary>
        /// The title of the blog post.
        /// </summary>
        public string Title { get; }
        public string Body { get; }
        public string Excerpt { get; }
        /// <summary>
        /// Display name of the user who authored the post.
        /// </summary>
        public string Author { get; }
        /// <summary>
{'fraction_non_alphanumeric': 0.1494044926880748, 'fraction_numerical': 0.0015076134479119555, 'mean_word_length': 1.9923319801533603, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 8, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1451212', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1853, 'n_words': 344}