Exemplo n.º 1
0
    public async Task RunAsync(
        [EventGridTrigger()] EventGridEvent eventGridEvent,
        [Queue(Constants.Queues.TwitterTweetsToSend)] ICollector <string> outboundMessages)
    {
        // Get the Source Data identifier for the event
        var tableEvent = JsonSerializer.Deserialize <TableEvent>(eventGridEvent.Data.ToString());

        if (tableEvent == null)
        {
            _logger.LogError("Failed to parse the TableEvent data for event '{eventGridEvent.Id}'", eventGridEvent.Id);
            return;
        }

        // Create the scheduled tweets for it
        _logger.LogDebug("Looking for source with fields '{tableEvent.PartitionKey}' and '{tableEvent.RowKey}'", tableEvent.PartitionKey, tableEvent.RowKey);
        var sourceData = await _sourceDataRepository.GetAsync(tableEvent.PartitionKey, tableEvent.RowKey);

        if (sourceData == null)
        {
            _logger.LogWarning("Record for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}' was NOT found", tableEvent.PartitionKey, tableEvent.RowKey);
            return;
        }

        _logger.LogDebug("Composing tweet for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}'.", tableEvent.PartitionKey, tableEvent.RowKey);

        var tweet = ComposeTweet(sourceData);

        if (!string.IsNullOrEmpty(tweet))
        {
            outboundMessages.Add(tweet);
        }

        // Done
        _logger.LogDebug("Done composing tweet for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}'.", tableEvent.PartitionKey, tableEvent.RowKey);
    }
Exemplo n.º 2
0
    private async Task <FacebookPostStatus> GetFacebookPostStatusForSourceData(TableEvent tableEvent)
    {
        if (tableEvent is null)
        {
            return(null);
        }

        var statusText = "ICYMI: ";
        var sourceData = await _sourceDataRepository.GetAsync(tableEvent.PartitionKey, tableEvent.RowKey);

        if (sourceData is null)
        {
            _logger.LogWarning($"Record for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}' was not found.");
            return(null);
        }

        statusText = sourceData.SourceSystem switch
        {
            SourceSystems.SyndicationFeed => "Blog Post: ",
            SourceSystems.YouTube => "Video: ",
            _ => statusText
        };

        var url         = sourceData.ShortenedUrl ?? sourceData.Url;
        var postTitle   = sourceData.Title;
        var hashTagList = HashTagList(sourceData.Tags);

        if (statusText.Length + url.Length + postTitle.Length + 3 + hashTagList.Length >= MaxFacebookStatusText)
        {
            var newLength = MaxFacebookStatusText - statusText.Length - url.Length - hashTagList.Length - 1;
            postTitle = postTitle.Substring(0, newLength - 4) + "...";
        }

        var facebookPostStatus = new FacebookPostStatus
        {
            StatusText = $"{statusText} {postTitle} {hashTagList}",
            LinkUri    = url
        };

        _logger.LogDebug(
            "Composed Facebook Status: StatusText='{facebookPostStatus.StatusText}', LinkUrl='{facebookPostStatus.LinkUri}'",
            facebookPostStatus.StatusText, facebookPostStatus.LinkUri);
        return(facebookPostStatus);
    }
Exemplo n.º 3
0
    public async Task RunAsync(
        [EventGridTrigger()] EventGridEvent eventGridEvent,
        [Queue(Constants.Queues.FacebookPostStatusToPage)]
        ICollector <FacebookPostStatus> outboundMessages)
    {
        // Get the Source Data identifier for the event
        if (eventGridEvent.Data is null)
        {
            _logger.LogError("The event data was null for event '{eventGridEvent.Id}'", eventGridEvent.Id);
            return;
        }
        var tableEvent = JsonSerializer.Deserialize <TableEvent>(eventGridEvent.Data.ToString());

        if (tableEvent == null)
        {
            _logger.LogError("Failed to parse the TableEvent data for event '{eventGridEvent.Id}'", eventGridEvent.Id);
            return;
        }

        // Create the Facebook posts for it
        _logger.LogDebug("Looking for source with fields '{tableEvent.PartitionKey}' and '{tableEvent.RowKey}'", tableEvent.PartitionKey, tableEvent.RowKey);
        var sourceData = await _sourceDataRepository.GetAsync(tableEvent.PartitionKey, tableEvent.RowKey);

        if (sourceData == null)
        {
            _logger.LogWarning("Record for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}' was NOT found", tableEvent.PartitionKey, tableEvent.RowKey);
            return;
        }

        _logger.LogDebug("Composing Facebook status for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}'.", tableEvent.PartitionKey, tableEvent.RowKey);

        var status = ComposeStatus(sourceData);

        if (status != null)
        {
            outboundMessages.Add(status);
        }

        // Done
        _logger.LogDebug("Done composing Facebook status for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}'.", tableEvent.PartitionKey, tableEvent.RowKey);
    }
Exemplo n.º 4
0
    private async Task <string> GetTweetForSourceData(TableEvent tableEvent)
    {
        if (tableEvent is null)
        {
            return(null);
        }

        var statusText = "ICYMI: ";
        var sourceData = await _sourceDataRepository.GetAsync(tableEvent.PartitionKey, tableEvent.RowKey);

        if (sourceData is null)
        {
            _logger.LogWarning($"Record for '{tableEvent.PartitionKey}', '{tableEvent.RowKey}' was not found.");
            return(null);
        }

        statusText = sourceData.SourceSystem switch
        {
            SourceSystems.SyndicationFeed => "Blog Post: ",
            SourceSystems.YouTube => "Video: ",
            _ => statusText
        };

        var url         = sourceData.ShortenedUrl ?? sourceData.Url;
        var postTitle   = sourceData.Title;
        var hashTagList = HashTagList(sourceData.Tags);

        if (statusText.Length + url.Length + postTitle.Length + 3 + hashTagList.Length >= MaxTweetLength)
        {
            var newLength = MaxTweetLength - statusText.Length - url.Length - hashTagList.Length - 1;
            postTitle = string.Concat(postTitle.AsSpan(0, newLength - 4), "...");
        }

        var tweet = $"{statusText} {postTitle} {url} {hashTagList}";

        _logger.LogDebug("Composed tweet '{tweet}'", tweet);
        return(statusText);
    }