Skip to content

Commit

Permalink
Add handling of parsing the post ID from threadmark links that don't …
Browse files Browse the repository at this point in the history
…include

the post ID at all (ie: the very first post in the thread).
If no ID is provided when requesting a post from a page, return the first post.
Fixes issue where the only threadmark of a thread is the very first post of
the thread.
  • Loading branch information
Kinematics committed Sep 21, 2015
1 parent df19112 commit e55e894
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions TallyCore/Adapters/XenForoAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,14 @@ private HtmlNode GetPostFromPageById(HtmlDocument page, string id)
if (id == null)
throw new ArgumentNullException(nameof(id));

var postsList = GetPostsFromPage(page);

if (id == "")
return postsList.FirstOrDefault();

if (!id.StartsWith("post-"))
id = "post-" + id;

var postsList = GetPostsFromPage(page);

return postsList.FirstOrDefault(a => a.Id == id);
}
#endregion
Expand Down Expand Up @@ -576,18 +579,28 @@ private string GetPostIdFromUrl(string url)
if (url == null)
throw new ArgumentNullException(nameof(url));

// For links directly to post ID
// Format: https://forums.sufficientvelocity.com/posts/4062860/
Regex postLinkRegex = new Regex(@"posts/(?<postId>\d+)/");
var m = postLinkRegex.Match(url);
if (m.Success)
return m.Groups["postId"].Value;

// For long-form links. May not include page number if the post is on the first page of the thread.
// Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/page-109#post-4062860
// Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/#post-4062860
postLinkRegex = new Regex(@"/(page-\d+)?#post-(?<postId>\d+)");
m = postLinkRegex.Match(url);
if (m.Success)
return m.Groups["postId"].Value;

// Long form link for the very first post in the thread.
// Format: https://forums.sufficientvelocity.com/threads/a-villain-in-a-world-of-heroes.18001/
postLinkRegex = new Regex(@"/threads/[^\/]+/$");
m = postLinkRegex.Match(url);
if (m.Success)
return "";

throw new ArgumentException("Unable to extract post ID from link:\n" + url, nameof(url));
}

Expand Down

0 comments on commit e55e894

Please sign in to comment.