features/AddTwitterHandle #721
base: master
Are you sure you want to change the base?
Conversation
* 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
// 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); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Encapsulated! Good point
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); | ||
} |
There was a problem hiding this comment.
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.
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); | |
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@Cheesebaron made the requested changes. Could you please review again? |
@saamerm it doesn't build
|
@Cheesebaron forgot to commit the function. Please review now |
{ | ||
var host = itemUrl.Host.ToLowerInvariant(); | ||
|
||
if (host.Contains("medium.com")) |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
Testing
Live in action:
Note that this also causes the titles in the Preview to have the twitter handle as you can see: