madskristensen/MiniBlog

How pubDate will remain same after opening/editing in WLW

Opened this issue · 2 comments

I have a requirements, that editing post in WLW, the pubDate must stay same as it was initially.
For example I need to edit some post from year 2010 and I need that the published date is staying unchanged. At the moment it is possible only in web-page. In WLW I need to enter each time published date, because it is not loaded into WLW.

I came up with solution to change the code. Can you suggest, is there any better way to do that.

I changed 3 things:

  1. Post.cs:
    public Post()
    {
        ID = Guid.NewGuid().ToString();
        Title = "My new post";
        Author = HttpContext.Current.User.Identity.Name;
        Content = "the content";
        //default datetime only if new posting from web
        if (Blog.IsNewPost)
        {
            PubDate = DateTime.UtcNow;
        }
        LastModified = DateTime.UtcNow;
        Categories = new string[0];
        Comments = new List<Comment>();
        IsPublished = true;
        Latitude = "";
        Longtitude = "";
        Place = "";
    }
  1. MetaWebLogHandler.cs
    string IMetaWeblog.AddPost(string blogid, string username, string password, Post post, bool publish)
    {
        ValidateUser(username, password);
        if (!string.IsNullOrWhiteSpace(post.Slug))
        {
            post.Slug = PostHandler.CreateSlug(post.Slug);
        }
        else
        {
            post.Slug = PostHandler.CreateSlug(post.Title);    
        }
        //in case new post from WLW and pubdate is empty
        if (post.PubDate == DateTime.MinValue)
        {
            post.PubDate = DateTime.UtcNow;
        }
        post.IsPublished = publish;
        Storage.Save(post);
        return post.ID;
    }
  1. MetaWebLogHandler.cs
 bool IMetaWeblog.UpdatePost(string postid, string username, string password, Post post, bool publish)
    {
        ValidateUser(username, password);

        Post match = Storage.GetAllPosts().FirstOrDefault(p => p.ID == postid);

        if (match != null)
        {
            match.Title = post.Title;
            match.Excerpt = post.Excerpt;
            match.Content = post.Content;
            //if posting date is not empty (published date is marked in WLW)  
            if (post.PubDate != DateTime.MinValue)
            {
                match.PubDate = post.PubDate;
            }
            if (!string.Equals(match.Slug, post.Slug, StringComparison.OrdinalIgnoreCase))
                match.Slug = PostHandler.CreateSlug(post.Slug);

            match.Categories = post.Categories;
            match.IsPublished = publish;
            Storage.Save(match);
        }
        return match != null;
    }

Not sure your issue and code align...

The changes you've made to IMetaWeblog.UpdatePost allow you to change the publication date, the code as originally stated did not change match,PubDate so it didn't matter what WLW said it would remain the same

Interesting. Based on my testing, when you open old post in WLW, the PublishedDate checkbox is not set and the date is empty.
After clicking publish, the post will take current date.
I will make some additional testing.