Can we subscribe to an event, when a scheduled block/page unpublish?

Can we subscribe to an event, when a scheduled block/page unpublish? I want to catch if a block changes/get automatically unpublished/Starts to show

Thanks

Litium version: 7.3

When you working with the draft version of the block or a page it is the DraftBlockUpdated or DraftPageUpdated event that will be fired. When the live version will be updated the BlockUpdated or PageUpdated event is fired. The event will be fired also on other situations where the block or page will be updated without explicit the publish/unpublish operation is performed.

To add into this and also need to think of a block or page can be published but not active on the channel. The system will not raise any event when block or page enter or exit the active on channel period.

To create a potential active/Inactive event, are there some “lastchange” field on the blocks or is it calkylated dynamic? So to add events for this we need to add a customer table/field to keep track? And do some crons? Or do you see a better way?

The active date is calculated in runtime so keeping track of them is little harder.

This is untested code but it may be possible to do something like this

using System;
using System.Linq;
using System.Threading.Tasks;
using Litium.Blocks;
using Litium.Blocks.Events;
using Litium.Events;
using Litium.Runtime;
using Litium.Scheduler;

namespace Litium.Accelerator
{
    [Autostart]
    internal class SpecialBlockEventEngine
    {
        public SpecialBlockEventEngine(
            EventBroker eventBroker,
            SchedulerService schedulerService
            )
        {
            eventBroker.Subscribe<BlockUpdated>(x =>
            {
                var systemId = x.SystemId;
                foreach (var channelLink in x.Item.ChannelLinks)
                {
                    var channelSystemId = channelLink.ChannelSystemId;
                    var startDate = channelLink.StartDateTimeUtc;
                    if (startDate != null && startDate > DateTimeOffset.Now)
                    {
                        schedulerService.ScheduleJob<Processor>(z => z.LinkActivation(systemId, channelSystemId, startDate.Value), new ScheduleJobArgs { ExecuteAt = startDate.Value });
                    }

                    var endDate = channelLink.EndDateTimeUtc;
                    if (endDate != null && endDate > DateTimeOffset.Now)
                    {
                        schedulerService.ScheduleJob<Processor>(z => z.LinkDeactivation(systemId, channelSystemId, endDate.Value), new ScheduleJobArgs { ExecuteAt = endDate.Value });
                    }
                }
            });
        }

        private class Processor
        {
            private readonly EventBroker _eventBroker;
            private readonly BlockService _blockService;

            public Processor(
                EventBroker eventBroker,
                BlockService blockService
                )
            {
                _eventBroker = eventBroker;
                _blockService = blockService;
            }

            public Task LinkActivation(Guid systemId, Guid channelSystemId, DateTimeOffset startDate)
            {
                var block = _blockService.Get(systemId);
                if (block is object)
                {
                    if (block.ChannelLinks.FirstOrDefault(x => x.ChannelSystemId == channelSystemId && x.StartDateTimeUtc == startDate) is object)
                    {
                        _eventBroker.Publish(new BlockActivatedOnChannel
                        {
                            BlockSystemId = systemId,
                            ChannelSystemId = channelSystemId
                        });
                    }
                }

                return Task.CompletedTask;
            }

            public Task LinkDeactivation(Guid systemId, Guid channelSystemId, DateTimeOffset endDate)
            {
                var block = _blockService.Get(systemId);
                if (block is object)
                {
                    if (block.ChannelLinks.FirstOrDefault(x => x.ChannelSystemId == channelSystemId && x.EndDateTimeUtc == endDate) is object)
                    {
                        _eventBroker.Publish(new BlockDeactivatedOnChannel
                        {
                            BlockSystemId = systemId,
                            ChannelSystemId = channelSystemId
                        });
                    }
                }

                return Task.CompletedTask;
            }
        }
    }

    public class BlockActivatedOnChannel : IMessage
    {
        public Guid ChannelSystemId { get; set; }
        public Guid BlockSystemId { get; set; }
    }

    public class BlockDeactivatedOnChannel : IMessage
    {
        public Guid ChannelSystemId { get; set; }
        public Guid BlockSystemId { get; set; }
    }
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.