Fixing Live Writer draft support

work item: 16607

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-11-17 18:19:34 -08:00
parent 29c38d5a0b
commit f729293337
3 changed files with 74 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
namespace Orchard.Core.XmlRpc {
public interface IXmlRpcDriver {
void Process(int id);
void Process(object item);
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml.Linq;
@@ -90,7 +89,8 @@ namespace Orchard.Blogs.Services {
urlHelper,
Convert.ToInt32(context.Request.Params[0].Value),
Convert.ToString(context.Request.Params[1].Value),
Convert.ToString(context.Request.Params[2].Value));
Convert.ToString(context.Request.Params[2].Value),
context._drivers);
context.Response = new XRpcMethodResponse().Add(result);
}
@@ -151,7 +151,7 @@ namespace Orchard.Blogs.Services {
throw new ArgumentException();
var array = new XRpcArray();
foreach (var blogPost in _blogPostService.Get(blog).Take(numberOfPosts)) {
foreach (var blogPost in _blogPostService.Get(blog, 0, numberOfPosts, VersionOptions.Latest)) {
array.Add(CreateBlogStruct(blogPost, urlHelper));
}
return array;
@@ -177,7 +177,7 @@ namespace Orchard.Blogs.Services {
var slug = content.Optional<string>("wp_slug");
var blogPost = _contentManager.New<BlogPostPart>("BlogPost");
// BodyPart
if (blogPost.Is<BodyPart>()) {
blogPost.As<BodyPart>().Text = description;
@@ -212,19 +212,25 @@ namespace Orchard.Blogs.Services {
UrlHelper urlHelper,
int postId,
string userName,
string password) {
string password,
IEnumerable<IXmlRpcDriver> drivers) {
var user = _membershipService.ValidateUser(userName, password);
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
var blogPost = _blogPostService.Get(postId);
var blogPost = _blogPostService.Get(postId, VersionOptions.Latest);
if (blogPost == null)
throw new ArgumentException();
return CreateBlogStruct(blogPost, urlHelper);
var postStruct = CreateBlogStruct(blogPost, urlHelper);
foreach (var driver in drivers)
driver.Process(postStruct);
return postStruct;
}
private bool MetaWeblogEditPost(int postId, string userName, string password, XRpcStruct content, bool publish, ICollection<IXmlRpcDriver> drivers) {
private bool MetaWeblogEditPost(int postId, string userName, string password, XRpcStruct content, bool publish, IEnumerable<IXmlRpcDriver> drivers) {
var user = _membershipService.ValidateUser(userName, password);
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
@@ -251,7 +257,7 @@ namespace Orchard.Blogs.Services {
return true;
}
private bool MetaWeblogDeletePost(string appkey, string postId, string userName, string password, bool publish, ICollection<IXmlRpcDriver> drivers) {
private bool MetaWeblogDeletePost(string appkey, string postId, string userName, string password, bool publish, IEnumerable<IXmlRpcDriver> drivers) {
var user = _membershipService.ValidateUser(userName, password);
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);

View File

@@ -7,6 +7,7 @@ using Orchard.Core.XmlRpc;
using Orchard.Core.XmlRpc.Models;
using Orchard.Security;
using Orchard.Tags.Helpers;
using Orchard.Tags.Models;
namespace Orchard.Tags.Services {
public class XmlRpcHandler : IXmlRpcHandler {
@@ -15,7 +16,6 @@ namespace Orchard.Tags.Services {
private readonly IContentManager _contentManager;
private readonly ITagService _tagService;
private readonly IOrchardServices _orchardServices;
private IEnumerable<string> _tags;
public XmlRpcHandler(
IMembershipService membershipService,
@@ -39,6 +39,10 @@ namespace Orchard.Tags.Services {
public void Process(XmlRpcContext context) {
switch (context.Request.MethodName) {
case "metaWeblog.getCategories": // hack... because live writer still asks for it...
if (context.Response == null)
context.Response = new XRpcMethodResponse();
break;
case "wp.getTags":
var tags = MetaWeblogGetTags(
Convert.ToString(context.Request.Params[0].Value),
@@ -46,6 +50,14 @@ namespace Orchard.Tags.Services {
Convert.ToString(context.Request.Params[2].Value));
context.Response = new XRpcMethodResponse().Add(tags);
break;
case "metaWeblog.getPost":
MetaWeblogAttachTagsToPost(
GetPost(context.Response),
Convert.ToInt32(context.Request.Params[0].Value),
Convert.ToString(context.Request.Params[1].Value),
Convert.ToString(context.Request.Params[2].Value),
context._drivers);
break;
case "metaWeblog.newPost":
MetaWeblogUpdateTags(
GetId(context.Response),
@@ -69,6 +81,38 @@ namespace Orchard.Tags.Services {
}
}
private void MetaWeblogAttachTagsToPost(XRpcStruct postStruct, int postId, string userName, string password, ICollection<IXmlRpcDriver> drivers) {
if (postId < 1)
return;
var user = _membershipService.ValidateUser(userName, password);
_authorizationService.CheckAccess(StandardPermissions.AccessAdminPanel, user, null);
var driver = new XmlRpcDriver(item => {
var post = item as XRpcStruct;
if (post == null)
return;
var contentItem = _contentManager.Get(postId, VersionOptions.Latest);
if (contentItem == null)
return;
var tags = contentItem.As<TagsPart>().CurrentTags.Select(tag => tag.TagName);
post.Set("mt_keywords", string.Join(", ", tags));
});
if (postStruct != null)
driver.Process(postStruct);
else
drivers.Add(driver);
}
private static XRpcStruct GetPost(XRpcMethodResponse response) {
return response != null && response.Params.Count == 1 && response.Params[0].Value is XRpcStruct
? response.Params[0].Value as XRpcStruct
: null;
}
private static int GetId(XRpcMethodResponse response) {
return response != null && response.Params.Count == 1 && response.Params[0].Value is int
? Convert.ToInt32(response.Params[0].Value)
@@ -77,7 +121,7 @@ namespace Orchard.Tags.Services {
private XRpcArray MetaWeblogGetTags(string appKey, string userName, string password) {
var user = _membershipService.ValidateUser(userName, password);
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
_authorizationService.CheckAccess(StandardPermissions.AccessAdminPanel, user, null);
var array = new XRpcArray();
foreach (var tag in _tagService.GetTags()) {
@@ -103,16 +147,20 @@ namespace Orchard.Tags.Services {
if (string.IsNullOrWhiteSpace(rawTags))
return;
var driver = new XmlRpcDriver(id => {
var contentItem = _contentManager.Get(id);
var tags = TagHelpers.ParseCommaSeparatedTagNames(rawTags);
var driver = new XmlRpcDriver(item => {
if (!(item is int))
return;
var id = (int)item;
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
if (contentItem == null)
return;
_orchardServices.WorkContext.CurrentUser = user;
_tagService.UpdateTagsForContentItem(id, _tags);
_tagService.UpdateTagsForContentItem(id, tags);
});
_tags = TagHelpers.ParseCommaSeparatedTagNames(rawTags);
if (contentItemId > 0)
driver.Process(contentItemId);
else
@@ -120,14 +168,14 @@ namespace Orchard.Tags.Services {
}
public class XmlRpcDriver : IXmlRpcDriver {
private readonly Action<int> _process;
private readonly Action<object> _process;
public XmlRpcDriver(Action<int> process) {
public XmlRpcDriver(Action<object > process) {
_process = process;
}
public void Process(int id) {
_process(id);
public void Process(object item) {
_process(item);
}
}
}