Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

features/AddTwitterHandle #721

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

saamerm
Copy link
Contributor

@saamerm saamerm commented Mar 23, 2021

  • Adds twitter handle for authors that have a matching "FirstName LastName" as their feed provides
  • Also, all authors currently have twitter handles stored in the solution, so we don't have to worry about authors that don't have twitter handles. In the chance that the handle is not active, it will still not break anything
  • Please ignore the whitespace changes to the LeomarisReyes.cs file

Testing

  • Tested in local host to make sure the RSS feed shows it correctly and doesn't cause an exception otherwise

Live in action:
Screen Shot 2021-03-23 at 7 51 44 PM

Note that this also causes the titles in the Preview to have the twitter handle as you can see:
image

* Removing Leomaris to fix the error on the preview page
* Adds twitter handle for authors that have a matching name as what their feed provides

**Testing**
------------
* Tested in local host to make sure the RSS feed shows it correctly and doesn't cause an exception otherwise
Comment on lines 185 to 204
// This foreach section is responsible for adding the Twitter handles
foreach (var item in feed.Items)
{
string newTitleText = string.Empty;
if (item.Authors.Count > 0)
newTitleText = Tamarins.Where(x => x.FirstName + " " + x.LastName == item.Authors[0].Name).FirstOrDefault()?.TwitterHandle ?? string.Empty;
newTitleText = String.IsNullOrEmpty(newTitleText) ? item.Title.Text : item.Title.Text + " @" + newTitleText;

if (!string.IsNullOrWhiteSpace(item.Title.Type))
{
string type = item.Title.Type == "text" ? "Plaintext" : item.Title.Type;
TextSyndicationContentKind textKind = (TextSyndicationContentKind)
Enum.Parse(typeof(TextSyndicationContentKind), type, ignoreCase: true);
item.Title = new TextSyndicationContent(newTitleText, textKind);
}
else
{
item.Title = new TextSyndicationContent(newTitleText);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please encapsulate in a method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encapsulated! Good point

Comment on lines 188 to 203
string newTitleText = string.Empty;
if (item.Authors.Count > 0)
newTitleText = Tamarins.Where(x => x.FirstName + " " + x.LastName == item.Authors[0].Name).FirstOrDefault()?.TwitterHandle ?? string.Empty;
newTitleText = String.IsNullOrEmpty(newTitleText) ? item.Title.Text : item.Title.Text + " @" + newTitleText;

if (!string.IsNullOrWhiteSpace(item.Title.Type))
{
string type = item.Title.Type == "text" ? "Plaintext" : item.Title.Type;
TextSyndicationContentKind textKind = (TextSyndicationContentKind)
Enum.Parse(typeof(TextSyndicationContentKind), type, ignoreCase: true);
item.Title = new TextSyndicationContent(newTitleText, textKind);
}
else
{
item.Title = new TextSyndicationContent(newTitleText);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This improves readability a bit. Fitting everything into newTitleText is a bit of a smell to me.

Also, I prefer not to use ternaries if possible.

Suggested change
string newTitleText = string.Empty;
if (item.Authors.Count > 0)
newTitleText = Tamarins.Where(x => x.FirstName + " " + x.LastName == item.Authors[0].Name).FirstOrDefault()?.TwitterHandle ?? string.Empty;
newTitleText = String.IsNullOrEmpty(newTitleText) ? item.Title.Text : item.Title.Text + " @" + newTitleText;
if (!string.IsNullOrWhiteSpace(item.Title.Type))
{
string type = item.Title.Type == "text" ? "Plaintext" : item.Title.Type;
TextSyndicationContentKind textKind = (TextSyndicationContentKind)
Enum.Parse(typeof(TextSyndicationContentKind), type, ignoreCase: true);
item.Title = new TextSyndicationContent(newTitleText, textKind);
}
else
{
item.Title = new TextSyndicationContent(newTitleText);
}
var twitterHandle = string.Empty;
if (item.Authors.Count > 0)
{
twitterHandle = Tamarins.FirstOrDefault(x =>
$"{x.FirstName} {x.LastName}" == item.Authors[0].Name)?.TwitterHandle ?? string.Empty;
}
var title = item.Title.Text;
if (!string.IsNullOrEmpty(twitterHandle))
title += $"@{twitterHandle}";
if (!string.IsNullOrWhiteSpace(item.Title.Type))
{
string type = item.Title.Type == "text" ? "Plaintext" : item.Title.Type;
var textKind = (TextSyndicationContentKind)
Enum.Parse(typeof(TextSyndicationContentKind), type, ignoreCase: true);
item.Title = new TextSyndicationContent(title, textKind);
}
else
{
item.Title = new TextSyndicationContent(title);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed newTitleText and got rid of ternaries

{
string newTitleText = string.Empty;
if (item.Authors.Count > 0)
newTitleText = Tamarins.Where(x => x.FirstName + " " + x.LastName == item.Authors[0].Name).FirstOrDefault()?.TwitterHandle ?? string.Empty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matching code seems to be a bit fragile and could perhaps lead to many misses depending on what the author has set up their feed to return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, @Cheesebaron, I couldn't find a different way to find a match except by comparing with item.Authors[0].Name. Do you have any other ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Significantly changed this, and just ended up using the function used by the Preview page

@saamerm
Copy link
Contributor Author

saamerm commented Apr 15, 2021

@Cheesebaron made the requested changes. Could you please review again?

@Cheesebaron
Copy link
Member

@saamerm it doesn't build

src\Firehose.Web\Infrastructure\NewCombinedFeedSource.cs(79,61): Error CS0103: The name 'GetFilterFunction' does not exist in the current context

@saamerm
Copy link
Contributor Author

saamerm commented Apr 18, 2021

@Cheesebaron forgot to commit the function. Please review now

{
var host = itemUrl.Host.ToLowerInvariant();

if (host.Contains("medium.com"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some pattern matching and splitting out into separate methods would do very good here

private bool MatchesAuthorUrls(IAmACommunityMember author, IEnumerable<Uri> urls, SyndicationItem item)
{
var authorHosts = author.FeedUris.Select(au => au.Host.ToLowerInvariant()).Concat(new[] { author.WebSite.Host.ToLowerInvariant() }).ToArray();
var feedBurnerAuthors = author.FeedUris.Where(au => au.Host.Contains("feeds.feedburner")).ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these fields don't seem relevant to all cases. Move closer to actual usage.

Also calling to list isn't necessary here.

TextSyndicationContentKind GetTextKind(string type)
{
if (type == "text")
return (TextSyndicationContentKind)Enum.Parse(typeof(TextSyndicationContentKind), "Plaintext", true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it possible to return the actual Kind here instead of having to parse an enum?

if (type == "text")
return (TextSyndicationContentKind)Enum.Parse(typeof(TextSyndicationContentKind), "Plaintext", true);
else
return (TextSyndicationContentKind)Enum.Parse(typeof(TextSyndicationContentKind), type, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TryParse, not all types will match an enum

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants