/// <summary> /// Returns a Post by Id /// </summary> /// <param name="postId"></param> /// <param name="weblogInfo"></param> /// <returns></returns> public Post GetPost(string postId, WeblogInfo weblogInfo) { if (weblogInfo.Type == WeblogTypes.MetaWeblogApi || weblogInfo.Type == WeblogTypes.Wordpress) { MetaWebLogWordpressApiClient client; client = new MetaWebLogWordpressApiClient(weblogInfo); return(client.GetPost(postId)); } // Medium doesn't support post retrieval so return null return(null); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogInfo weblogInfo, bool sendAsDraft = false) { var editor = Model.ActiveEditor; if (editor == null) { return(false); } var doc = editor.MarkdownDocument; WeblogModel.ActivePost = new Post() { DateCreated = DateTime.Now }; // start by retrieving the current Markdown from the editor string markdown = editor.GetMarkdown(); // Retrieve Meta data from post and clean up the raw markdown // so we render without the config data var meta = WeblogPostMetadata.GetPostConfigFromMarkdown(markdown, WeblogModel.ActivePost, weblogInfo); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); WeblogModel.ActivePost.Body = html; WeblogModel.ActivePost.PostId = meta.PostId; WeblogModel.ActivePost.PostStatus = meta.PostStatus; // Custom Field Processing: // Add custom fields from existing post // then add or update our custom fields var customFields = new Dictionary <string, CustomField>(); // load existing custom fields from post online if possible if (!string.IsNullOrEmpty(meta.PostId)) { var existingPost = GetPost(meta.PostId, weblogInfo); if (existingPost != null && meta.CustomFields != null && existingPost.CustomFields != null) { customFields = existingPost.CustomFields .ToDictionary(cf => cf.Key, cf => cf); } } // add custom fields from Weblog configuration if (weblogInfo.CustomFields != null) { foreach (var kvp in weblogInfo.CustomFields) { if (!customFields.ContainsKey(kvp.Key)) { AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value); } } } // add custom fields from Meta data if (meta.CustomFields != null) { foreach (var kvp in meta.CustomFields) { AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value.Value); } } if (!string.IsNullOrEmpty(markdown)) { AddOrUpdateCustomField(customFields, "mt_markdown", markdown); } WeblogModel.ActivePost.CustomFields = customFields.Values.ToArray(); var config = WeblogAddinConfiguration.Current; var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName); if (kv.Equals(default(KeyValuePair <string, WeblogInfo>))) { MessageBox.Show("Invalid Weblog configuration selected.", "Weblog Posting Failed", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } weblogInfo = kv.Value; var type = weblogInfo.Type; if (type == WeblogTypes.Unknown) { type = weblogInfo.Type; } string basePath = Path.GetDirectoryName(doc.Filename); string postUrl = null; if (type == WeblogTypes.MetaWeblogApi || type == WeblogTypes.Wordpress) { MetaWebLogWordpressApiClient client; client = new MetaWebLogWordpressApiClient(weblogInfo); // if values are already configured don't overwrite them again client.DontInferFeaturedImage = meta.DontInferFeaturedImage; client.FeaturedImageUrl = meta.FeaturedImageUrl; client.FeatureImageId = meta.FeaturedImageId; if (!client.PublishCompletePost(WeblogModel.ActivePost, basePath, sendAsDraft, markdown)) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show("Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } var post = client.GetPost(WeblogModel.ActivePost.PostId); if (post != null) { postUrl = post.Url; } } if (type == WeblogTypes.Medium) { var client = new MediumApiClient(weblogInfo); var result = client.PublishCompletePost(WeblogModel.ActivePost, basePath, sendAsDraft); if (result == null) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show($"Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } // this is null postUrl = client.PostUrl; } meta.PostId = WeblogModel.ActivePost.PostId.ToString(); // retrieve the raw editor markdown markdown = editor.GetMarkdown(); meta.RawMarkdownBody = markdown; // add the meta configuration to it markdown = meta.SetPostYaml(); // write it back out to editor editor.SetMarkdown(markdown, updateDirtyFlag: true); try { // preview post if (!string.IsNullOrEmpty(weblogInfo.PreviewUrl)) { var url = weblogInfo.PreviewUrl.Replace("{0}", WeblogModel.ActivePost.PostId.ToString()); ShellUtils.GoUrl(url); } else { if (!string.IsNullOrEmpty(postUrl)) { ShellUtils.GoUrl(postUrl); } else { ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority)); } } } catch { mmApp.Log("Failed to display Weblog Url after posting: " + weblogInfo.PreviewUrl ?? postUrl ?? weblogInfo.ApiUrl); } return(true); }