<commit_before>﻿/*
 * Copyright (c) 2017-2018 <NAME>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 * 
 * https://github.com/piranhacms/piranha.core
 * 
 */

using System;

namespace Piranha.Web
{
    public class PostRouter
    {
        /// <summary>
        /// Invokes the router.
        /// </summary>
        /// <param name="api">The current api</param>
        /// <param name="url">The requested url</param>
        /// <param name="siteId">The requested site id</param>
        /// <returns>The piranha response, null if no matching post was found</returns>
        public static IRouteResponse Invoke(IApi api, string url, Guid siteId)
        {
            if (!String.IsNullOrWhiteSpace(url) && url.Length > 1)
            {
                var segments = url.Substring(1).Split(new char[] { '/' });

                if (segments.Length >= 2)
                {
                    var post = api.Posts.GetBySlug<Models.PostInfo>(segments[0], segments[1], siteId);

                    if (post != null)
                    {
                        var page = api.Pages.GetById<Models.PageInfo>(post.BlogId);
                        var site = api.Sites.GetById(page.SiteId);
                        var route = post.Route ?? "/post";
                        var lastModified = !site.ContentLastModified.HasValue || post.LastModified > site.ContentLastModified 
                            ? post.LastModified : site.ContentLastModified.Value;

                        if (segments.Length > 2)
                        {
                            route += "/" + string.Join("/", segments.Subset(2));
                        }

                        return new RouteResponse
                        {
                            PageId = post.BlogId,
                            Route = route,
                            QueryString = $"id={post.Id}&piranha_handled=true",
                            IsPublished = post.Published.HasValue && post.Published.Value <= DateTime.Now,
                            CacheInfo = new HttpCacheInfo
                            {
                                EntityTag = Utils.GenerateETag(post.Id.ToString(), lastModified),
                                LastModified = lastModified
                            }
                        };
                    }
                }
            }
            return null;
        }
    }
}<commit_msg>Added so posts for unpublished archives are considered unpublished. Fixes #323<commit_after>﻿/*
 * Copyright (c) 2017-2018 <NAME>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 * 
 * https://github.com/piranhacms/piranha.core
 * 
 */

using System;

namespace Piranha.Web
{
    public class PostRouter
    {
        /// <summary>
        /// Invokes the router.
        /// </summary>
        /// <param name="api">The current api</param>
        /// <param name="url">The requested url</param>
        /// <param name="siteId">The requested site id</param>
        /// <returns>The piranha response, null if no matching post was found</returns>
        public static IRouteResponse Invoke(IApi api, string url, Guid siteId)
        {
            if (!String.IsNullOrWhiteSpace(url) && url.Length > 1)
            {
                var segments = url.Substring(1).Split(new char[] { '/' });

                if (segments.Length >= 2)
                {
                    var post = api.Posts.GetBySlug<Models.PostInfo>(segments[0], segments[1], siteId);

                    if (post != null)
                    {
                        var page = api.Pages.GetById<Models.PageInfo>(post.BlogId);
                        var site = api.Sites.GetById(page.SiteId);
                        var route = post.Route ?? "/post";
                        var lastModified = !site.ContentLastModified.HasValue || post.LastModified > site.ContentLastModified 
                            ? post.LastModified : site.ContentLastModified.Value;

                        if (segments.Length > 2)
                        {
                            route += "/" + string.Join("/", segments.Subset(2));
                        }

                        return new RouteResponse
                        {
                            PageId = post.BlogId,
                            Route = route,
                            QueryString = $"id={post.Id}&piranha_handled=true",
                            IsPublished = post.Published.HasValue && page.Published.HasValue && post.Published.Value <= DateTime.Now && page.Published.Value <= DateTime.Now,
                            CacheInfo = new HttpCacheInfo
                            {
                                EntityTag = Utils.GenerateETag(post.Id.ToString(), lastModified),
                                LastModified = lastModified
                            }
                        };
                    }
                }
            }
            return null;
        }
    }
}
{'fraction_non_alphanumeric': 0.10473766640563821, 'fraction_numerical': 0.006460454189506656, 'mean_word_length': 1.2280854775403403, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2621277', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1213, 'n_words': 283}