From bce3eabc4b0b167c130ff838686e926bc656bd4f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 11 Apr 2014 08:56:39 +0200 Subject: [PATCH] #21: Implemented auto-follow ticket when creating a reply. --- .../OrchardPros/Handlers/ReplyPartHandler.cs | 22 ++++++++++++++++++- .../Services/Content/ISubscriptionService.cs | 1 + .../Services/Content/SubscriptionService.cs | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Orchard.Web/Modules/OrchardPros/Handlers/ReplyPartHandler.cs b/src/Orchard.Web/Modules/OrchardPros/Handlers/ReplyPartHandler.cs index 0ad0fb7d5..c09b4fb8d 100644 --- a/src/Orchard.Web/Modules/OrchardPros/Handlers/ReplyPartHandler.cs +++ b/src/Orchard.Web/Modules/OrchardPros/Handlers/ReplyPartHandler.cs @@ -7,6 +7,7 @@ using Orchard.JobsQueue.Services; using Orchard.Localization; using OrchardPros.Models; +using OrchardPros.Services.Content; using OrchardPros.Services.User; namespace OrchardPros.Handlers { @@ -15,15 +16,24 @@ public class ReplyPartHandler : ContentHandler { private readonly IShapeFactory _shapeFactory; private readonly IShapeDisplay _shapeDisplay; private readonly IJobsQueueService _jobsQueueService; + private readonly ISubscriptionService _subscriptionService; + + public ReplyPartHandler( + IUserManager userManager, + IShapeFactory shapeFactory, + IShapeDisplay shapeDisplay, + IJobsQueueService jobsQueueService, + ISubscriptionService subscriptionService) { - public ReplyPartHandler(IUserManager userManager, IShapeFactory shapeFactory, IShapeDisplay shapeDisplay, IJobsQueueService jobsQueueService) { _userManager = userManager; _shapeFactory = shapeFactory; _shapeDisplay = shapeDisplay; _jobsQueueService = jobsQueueService; + _subscriptionService = subscriptionService; T = NullLocalizer.Instance; OnCreated(UpdateStats); + OnCreated(Subscribe); OnCreated(SendNotifications); } @@ -37,6 +47,16 @@ private void UpdateStats(CreateContentContext context, ReplyPart part) { _userManager.AddActivityPoints(user, 5); } + private void Subscribe(CreateContentContext context, ReplyPart part) + { + var user = part.User; + var ticket = part.ContainingContent.As(); + var subscriptionSource = ticket.As(); + + if(!_subscriptionService.HasSubscription(subscriptionSource, user)) + _subscriptionService.Subscribe(subscriptionSource, user); + } + private void SendNotifications(CreateContentContext context, ReplyPart part) { var ticket = part.ContainingContent.As(); var subscriptionSource = ticket.As(); diff --git a/src/Orchard.Web/Modules/OrchardPros/Services/Content/ISubscriptionService.cs b/src/Orchard.Web/Modules/OrchardPros/Services/Content/ISubscriptionService.cs index d9dd256cf..0e95a662b 100644 --- a/src/Orchard.Web/Modules/OrchardPros/Services/Content/ISubscriptionService.cs +++ b/src/Orchard.Web/Modules/OrchardPros/Services/Content/ISubscriptionService.cs @@ -9,5 +9,6 @@ public interface ISubscriptionService : IDependency { Subscription Subscribe(SubscriptionSourcePart subscriptionSource, IUser user); void Unsubscribe(SubscriptionSourcePart subscriptionSource, IUser user); IPagedList GetSubscriptionSourcesByUser(int userId, int? skip = null, int? take = null); + bool HasSubscription(SubscriptionSourcePart subscriptionSource, IUser user); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/OrchardPros/Services/Content/SubscriptionService.cs b/src/Orchard.Web/Modules/OrchardPros/Services/Content/SubscriptionService.cs index 986d15896..0c39ad272 100644 --- a/src/Orchard.Web/Modules/OrchardPros/Services/Content/SubscriptionService.cs +++ b/src/Orchard.Web/Modules/OrchardPros/Services/Content/SubscriptionService.cs @@ -60,5 +60,9 @@ public IPagedList GetSubscriptionSourcesByUser(int userI var ids = query.ToArray(); return _contentManager.GetMany(ids, VersionOptions.Published, QueryHints.Empty).ToPagedList(count); } + + public bool HasSubscription(SubscriptionSourcePart subscriptionSource, IUser user) { + return subscriptionSource.Subscribers.Any(x => x.Id == user.Id); + } } } \ No newline at end of file