From abfd0cfb6f2e934ebeec3fe44761ed380d57fc71 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Mon, 20 Feb 2023 14:17:24 -0500 Subject: [PATCH] feat: SO/SE converter refactor: rework some author logic --- src/routes/home.go | 20 ++++++++++++++---- src/routes/question.go | 45 ++++++++++++++++++++++++----------------- src/types/comment.go | 1 - src/utils/comments.go | 6 +++--- templates/comments.html | 2 +- templates/question.html | 4 ++-- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/routes/home.go b/src/routes/home.go index b56e164..c129e2f 100644 --- a/src/routes/home.go +++ b/src/routes/home.go @@ -3,6 +3,7 @@ package routes import ( "anonymousoverflow/config" "fmt" + "regexp" "strings" "github.com/gin-gonic/gin" @@ -19,6 +20,8 @@ type urlConversionRequest struct { URL string `form:"url" binding:"required"` } +var stackExchangeRegex = regexp.MustCompile(`https://(.+).stackexchange.com/questions/`) + func PostHome(c *gin.Context) { body := urlConversionRequest{} @@ -33,15 +36,24 @@ func PostHome(c *gin.Context) { soLink := body.URL // validate URL - if !strings.HasPrefix(soLink, "https://stackoverflow.com/questions/") { + isStackOverflow := strings.HasPrefix(soLink, "https://stackoverflow.com/questions/") + isStackExchange := stackExchangeRegex.MatchString(soLink) + if !isStackExchange && !isStackOverflow { c.HTML(400, "home.html", gin.H{ - "errorMessage": "Invalid stack overflow URL", + "errorMessage": "Invalid stack overflow/exchange URL", "theme": c.MustGet("theme").(string), }) return } - // redirect to the proxied thread - c.Redirect(302, fmt.Sprintf("/questions/%s", strings.TrimPrefix(soLink, "https://stackoverflow.com/questions/"))) + // if stack overflow, trim https://stackoverflow.com + if isStackOverflow { + c.Redirect(302, strings.TrimPrefix(soLink, "https://stackoverflow.com")) + return + } + // if stack exchange, extract the subdomain + sub := stackExchangeRegex.FindStringSubmatch(soLink)[1] + + c.Redirect(302, fmt.Sprintf("/exchange/%s/%s", sub, strings.TrimPrefix(soLink, fmt.Sprintf("https://%s.stackexchange.com", sub)))) } diff --git a/src/routes/question.go b/src/routes/question.go index 0eb01ec..9a81bc0 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -178,24 +178,33 @@ func ViewQuestion(c *gin.Context) { userDetails := questionMetadata.Find("div.user-details") questionAuthor := "" - questionAuthorURL := "" + questionAuthorURL := fmt.Sprintf("https://%s", domain) + + // check if the question has been edited + isQuestionEdited := false + questionMetadata.Find("a.js-gps-track").Each(func(i int, s *goquery.Selection) { + if strings.Contains(s.Text(), "edited") { + isQuestionEdited = true + return + } + }) userDetails.Find("a").Each(func(i int, s *goquery.Selection) { - // get the second - if i == 0 { - if s.Text() != "" { - // if it's not been edited, it means it's the first + // if question has been edited, the author is the second element. + + if isQuestionEdited { + if i == 1 { questionAuthor = s.Text() - questionAuthorURL, _ = s.Attr("href") + questionAuthorURL += s.AttrOr("href", "") + return + } + } else { + // otherwise it's the first element + if i == 0 { + questionAuthor = s.Text() + questionAuthorURL += s.AttrOr("href", "") return } - } - - // otherwise it's the second element - if i == 1 { - questionAuthor = s.Text() - questionAuthorURL, _ = s.Attr("href") - return } }) @@ -221,17 +230,17 @@ func ViewQuestion(c *gin.Context) { answerFooter := s.Find("div.mt24") - answerAuthorURL := "" + answerAuthorURL := fmt.Sprintf("https://%s", domain) answerAuthorName := "" answerTimestamp := "" answerFooter.Find("div.post-signature").Each(func(i int, s *goquery.Selection) { - questionAuthorDetails := s.Find("div.user-details") + answerAuthorDetails := s.Find("div.user-details") - if questionAuthorDetails.Length() > 0 { - questionAuthor := questionAuthorDetails.Find("a").First() + if answerAuthorDetails.Length() > 0 { + questionAuthor := answerAuthorDetails.Find("a").First() answerAuthorName = html.EscapeString(questionAuthor.Text()) - answerAuthorURL = html.EscapeString(questionAuthor.AttrOr("href", "")) + answerAuthorURL += html.EscapeString(questionAuthor.AttrOr("href", "")) } answerTimestamp = html.EscapeString(s.Find("span.relativetime").Text()) diff --git a/src/types/comment.go b/src/types/comment.go index 8213b4b..b1a6b68 100644 --- a/src/types/comment.go +++ b/src/types/comment.go @@ -8,5 +8,4 @@ type FilteredComment struct { AuthorName string AuthorURL string Upvotes string - Domain string } diff --git a/src/utils/comments.go b/src/utils/comments.go index 3847fc7..d668115 100644 --- a/src/utils/comments.go +++ b/src/utils/comments.go @@ -2,6 +2,7 @@ package utils import ( "anonymousoverflow/src/types" + "fmt" "html/template" "github.com/PuerkitoBio/goquery" @@ -34,7 +35,7 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection) return } - commentAuthorURL := "" + commentAuthorURL := fmt.Sprintf("https://%s", domain) commentAuthor := commentBody.Find("span.comment-user") if commentAuthor.Length() == 0 { @@ -43,7 +44,7 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection) return } - commentAuthorURL = commentAuthor.AttrOr("href", "") + commentAuthorURL += commentAuthor.AttrOr("href", "") } commentTimestamp := commentBody.Find("span.relativetime-clean").Text() @@ -54,7 +55,6 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection) AuthorName: commentAuthor.Text(), AuthorURL: commentAuthorURL, Upvotes: commentScore, - Domain: domain, } comments = append(comments, newFilteredComment) diff --git a/templates/comments.html b/templates/comments.html index 3b56b69..345f733 100644 --- a/templates/comments.html +++ b/templates/comments.html @@ -19,7 +19,7 @@
Commented {{ $comment.Timestamp }} by {{ $comment.AuthorName }} Asked {{ .question.Timestamp }} by {{ .question.AuthorName }} Answered {{ $answer.Timestamp }} by {{ $answer.AuthorName }}