mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-07-15 18:55:14 +08:00
Adding display links to feed. Deduplicating some worker code into an inspector class.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045457
This commit is contained in:
parent
59ca753956
commit
341d2fa817
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using System.Xml.Linq;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Modules;
|
||||
@ -165,6 +166,7 @@ namespace Orchard.Core.Tests.Feeds.Controllers {
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterModule(new ImplicitCollectionSupportModule());
|
||||
builder.Register<FeedController>();
|
||||
builder.Register(new RouteCollection());
|
||||
builder.Register(mockContentManager.Object).As<IContentManager>();
|
||||
builder.Register<RssFeedFormatProvider>().As<IFeedFormatterProvider>();
|
||||
builder.Register<CorePartsFeedItemBuilder>().As<IFeedItemBuilder>();
|
||||
|
@ -64,6 +64,9 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Routing">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
|
@ -51,6 +51,12 @@ namespace Orchard.Core.Feeds.Controllers {
|
||||
return context.FeedFormatter.Process(context, () => {
|
||||
bestQueryMatch.FeedQuery.Execute(context);
|
||||
_feedItemBuilders.Invoke(x => x.Populate(context), Logger);
|
||||
foreach (var contextualizer in context.Response.Contextualizers) {
|
||||
if (ControllerContext != null &&
|
||||
ControllerContext.RequestContext != null) {
|
||||
contextualizer(ControllerContext.RequestContext);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Orchard.Core.Feeds.Models {
|
||||
public class FeedResponse {
|
||||
public FeedResponse() {
|
||||
Items = new List<FeedItem>();
|
||||
Contextualizers = new List<Action<RequestContext>>();
|
||||
}
|
||||
|
||||
public IList<FeedItem> Items { get; set; }
|
||||
public XElement Element { get; set; }
|
||||
public ActionResult Result { get; set; }
|
||||
public IList<Action<RequestContext>> Contextualizers { get; set; }
|
||||
|
||||
public void Contextualize(Action<RequestContext> contextualizer) {
|
||||
Contextualizers.Add(contextualizer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,61 +1,60 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Feeds.Models;
|
||||
|
||||
namespace Orchard.Core.Feeds.Services {
|
||||
public class CorePartsFeedItemBuilder : IFeedItemBuilder {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly RouteCollection _routes;
|
||||
|
||||
public CorePartsFeedItemBuilder(IContentManager contentManager) {
|
||||
public CorePartsFeedItemBuilder(IContentManager contentManager, RouteCollection routes) {
|
||||
_contentManager = contentManager;
|
||||
_routes = routes;
|
||||
}
|
||||
|
||||
public void Populate(FeedContext context) {
|
||||
foreach (var feedItem in context.Response.Items) {
|
||||
// locate parts
|
||||
var contentItem = feedItem.ContentItem;
|
||||
var metadata = _contentManager.GetItemMetadata(contentItem);
|
||||
var common = contentItem.Get<ICommonAspect>();
|
||||
var routable = contentItem.Get<RoutableAspect>();
|
||||
var body = contentItem.Get<BodyAspect>();
|
||||
var inspector = new ItemInspector(
|
||||
feedItem.ContentItem,
|
||||
_contentManager.GetItemMetadata(feedItem.ContentItem));
|
||||
|
||||
// standard fields
|
||||
var link = "/todo";// metadata.DisplayRouteValues();
|
||||
|
||||
var title = metadata.DisplayText;
|
||||
if (string.IsNullOrEmpty(title) && routable != null)
|
||||
title = routable.Title;
|
||||
|
||||
var contentText = title;
|
||||
if (body != null && !string.IsNullOrEmpty(body.Text))
|
||||
contentText = body.Text;
|
||||
|
||||
DateTime? publishedDate = null;
|
||||
if (common != null)
|
||||
publishedDate = common.PublishedUtc ?? common.ModifiedUtc;
|
||||
|
||||
// TODO: author
|
||||
|
||||
|
||||
// add to known formats
|
||||
if (context.Format == "rss") {
|
||||
feedItem.Element.SetElementValue("title", title);
|
||||
feedItem.Element.SetElementValue("link", link);
|
||||
feedItem.Element.SetElementValue("description", contentText);
|
||||
if (publishedDate != null)
|
||||
feedItem.Element.SetElementValue("pubDate", publishedDate);//TODO: format
|
||||
//feedItem.Data.SetElementValue("description", contentText);
|
||||
feedItem.Element.Add(new XElement("guid", new XAttribute("isPermaLink", "true"), new XText(link)));
|
||||
var link = new XElement("link");
|
||||
var guid = new XElement("guid", new XAttribute("isPermaLink", "true"));
|
||||
|
||||
feedItem.Element.SetElementValue("title", inspector.Title);
|
||||
feedItem.Element.Add(link);
|
||||
feedItem.Element.SetElementValue("description", inspector.Description);
|
||||
if (inspector.PublishedUtc != null)
|
||||
feedItem.Element.SetElementValue("pubDate", inspector.PublishedUtc);//TODO: format
|
||||
feedItem.Element.Add(guid);
|
||||
|
||||
context.Response.Contextualize(requestContext => {
|
||||
var urlHelper = new UrlHelper(requestContext, _routes);
|
||||
link.Add(urlHelper.RouteUrl(inspector.Link));
|
||||
guid.Add(urlHelper.RouteUrl(inspector.Link));
|
||||
});
|
||||
}
|
||||
else {
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "link", link);
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "title", title);
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "description", contentText);
|
||||
if (publishedDate != null)
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "published-date", Convert.ToString(publishedDate)); // format? cvt to generic T?
|
||||
var feedItem1 = feedItem;
|
||||
context.Response.Contextualize(requestContext => {
|
||||
var urlHelper = new UrlHelper(requestContext, _routes);
|
||||
context.FeedFormatter.AddProperty(context, feedItem1, "published-date", urlHelper.RouteUrl(inspector.Link));
|
||||
});
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "title", inspector.Title);
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "description", inspector.Description);
|
||||
|
||||
if (inspector.PublishedUtc != null)
|
||||
context.FeedFormatter.AddProperty(context, feedItem, "published-date", Convert.ToString(inspector.PublishedUtc)); // format? cvt to generic T?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
59
src/Orchard.Web/Core/Feeds/StandardBuilders/ItemInspector.cs
Normal file
59
src/Orchard.Web/Core/Feeds/StandardBuilders/ItemInspector.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Web.Routing;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Common.Models;
|
||||
|
||||
namespace Orchard.Core.Feeds.Services {
|
||||
public class ItemInspector {
|
||||
private readonly IContent _item;
|
||||
private readonly ContentItemMetadata _metadata;
|
||||
private readonly ICommonAspect _common;
|
||||
private readonly RoutableAspect _routable;
|
||||
private readonly BodyAspect _body;
|
||||
|
||||
public ItemInspector(IContent item, ContentItemMetadata metadata) {
|
||||
_item = item;
|
||||
_metadata = metadata;
|
||||
_common = item.Get<ICommonAspect>();
|
||||
_routable = item.Get<RoutableAspect>();
|
||||
_body = item.Get<BodyAspect>();
|
||||
}
|
||||
|
||||
public string Title {
|
||||
get {
|
||||
if (_metadata != null && !string.IsNullOrEmpty(_metadata.DisplayText))
|
||||
return _metadata.DisplayText;
|
||||
if (_routable != null && !string.IsNullOrEmpty(_routable.Title))
|
||||
return _routable.Title;
|
||||
return _item.ContentItem.ContentType + " #" + _item.ContentItem.Id;
|
||||
}
|
||||
}
|
||||
|
||||
public RouteValueDictionary Link {
|
||||
get {
|
||||
if (_metadata != null) {
|
||||
return _metadata.DisplayRouteValues;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string Description {
|
||||
get {
|
||||
if (_body != null && !string.IsNullOrEmpty(_body.Text)) {
|
||||
return _body.Text;
|
||||
}
|
||||
return Title;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? PublishedUtc {
|
||||
get {
|
||||
if (_common != null && _common.PublishedUtc != null)
|
||||
return _common.PublishedUtc;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Web.Mvc;
|
||||
using System.Xml.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Common.Records;
|
||||
using Orchard.Core.Feeds.Models;
|
||||
using Orchard.Core.Feeds.Services;
|
||||
|
||||
namespace Orchard.Core.Feeds.StandardQueries {
|
||||
[UsedImplicitly]
|
||||
@ -34,17 +37,25 @@ namespace Orchard.Core.Feeds.StandardQueries {
|
||||
var containerId = (int)containerIdValue.ConvertTo(typeof(int));
|
||||
var container = _contentManager.Get(containerId);
|
||||
|
||||
var containerRoutable = container.As<RoutableAspect>();
|
||||
var containerBody = container.As<BodyAspect>();
|
||||
if (containerRoutable != null) {
|
||||
context.FeedFormatter.AddProperty(context, null, "title", containerRoutable.Title);
|
||||
context.FeedFormatter.AddProperty(context, null, "link", "/" + containerRoutable.Slug);
|
||||
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container));
|
||||
if (context.Format == "rss") {
|
||||
var link = new XElement("link");
|
||||
context.Response.Element.SetElementValue("title", inspector.Title);
|
||||
context.Response.Element.Add(link);
|
||||
context.Response.Element.SetElementValue("description", inspector.Description);
|
||||
|
||||
context.Response.Contextualize(requestContext => {
|
||||
var urlHelper = new UrlHelper(requestContext);
|
||||
link.Add(urlHelper.RouteUrl(inspector.Link));
|
||||
});
|
||||
}
|
||||
if (containerBody != null) {
|
||||
context.FeedFormatter.AddProperty(context, null, "description", containerBody.Text);
|
||||
}
|
||||
else if (containerRoutable != null) {
|
||||
context.FeedFormatter.AddProperty(context, null, "description", containerRoutable.Title);
|
||||
else {
|
||||
context.FeedFormatter.AddProperty(context, null, "title", inspector.Title);
|
||||
context.FeedFormatter.AddProperty(context, null, "description", inspector.Description);
|
||||
context.Response.Contextualize(requestContext => {
|
||||
var urlHelper = new UrlHelper(requestContext);
|
||||
context.FeedFormatter.AddProperty(context, null, "link", urlHelper.RouteUrl(inspector.Link));
|
||||
});
|
||||
}
|
||||
|
||||
var items = _contentManager.Query()
|
||||
|
@ -83,6 +83,7 @@
|
||||
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
|
||||
<Compile Include="Feeds\Controllers\FeedController.cs" />
|
||||
<Compile Include="Feeds\Rss\Routes.cs" />
|
||||
<Compile Include="Feeds\StandardBuilders\ItemInspector.cs" />
|
||||
<Compile Include="Feeds\StandardQueries\ContainerFeedQuery.cs" />
|
||||
<Compile Include="Feeds\StandardBuilders\CorePartsFeedItemBuilder.cs" />
|
||||
<Compile Include="Feeds\IFeedFormatter.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user