コード例 #1
0
        #pragma warning restore S4144

        private async Task PublishContentEvent(
            IOrchestrationContext context,
            ContentEventType eventType)
        {
            if (!_eventGridConfiguration.CurrentValue.PublishEvents)
            {
                _logger.LogInformation("Event grid publishing is disabled. No events will be published.");
                return;
            }

            try
            {
                IContentItemVersion contentItemVersion = eventType switch
                {
                    ContentEventType.Published => _publishedContentItemVersion,
                    ContentEventType.Draft => _previewContentItemVersion,
                    _ => _neutralEventContentItemVersion
                };

                string userId = _syncNameProvider.GetEventIdPropertyValue(
                    context.ContentItem.Content.GraphSyncPart,
                    contentItemVersion);

                ContentEvent contentEvent = new ContentEvent(context.ContentItem, userId, eventType);
                await _eventGridContentClient.Publish(contentEvent);
            }
            catch (Exception publishException)
            {
                _logger.LogError(publishException, "The event grid event could not be published.");
                await context.Notifier.Add("Warning: the event grid event could not be published. Composite apps might not show your changes.",
                                           "Exception", publishException, type : NotifyType.Warning);
            }
        }
        // use 2 part segmented eventType?
        public ContentEvent(
            ContentItem contentItem,
            string userId,
            ContentEventType contentEventType)
        {
            Id = Guid.NewGuid().ToString();

            //todo: use SyncNameProvider?
            // do we assume id ends with a guid, or do we need a setting to extract the eventgrid id from the full id?
            // string userId = contentItem.Content.GraphSyncPart.Text;

            string itemId = userId.Substring(userId.Length - 36);

            Subject = $"/content/{contentItem.ContentType.ToLower()}/{itemId}";

            //todo: the new activity should be Stop()ed
            Data            = new ContentEventData(userId, itemId, contentItem.ContentItemVersionId, contentItem.DisplayText, contentItem.Author, contentItem.ContentType, Activity.Current ?? new Activity(nameof(ContentEvent)).Start());
            EventType       = GetEventType(contentEventType);
            EventTime       = DateTime.UtcNow;
            MetadataVersion = null;
            DataVersion     = "1.0";
        }
        private string GetEventType(ContentEventType contentEventType)
        {
            string eventType = contentEventType.ToString().ToLowerInvariant();

            return(eventType == "draftdiscarded" ? "draft-discarded" : eventType);
        }