feat: SO/SE converter
refactor: rework some author logic
This commit is contained in:
parent
63a30ab1cf
commit
abfd0cfb6f
@ -3,6 +3,7 @@ package routes
|
|||||||
import (
|
import (
|
||||||
"anonymousoverflow/config"
|
"anonymousoverflow/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -19,6 +20,8 @@ type urlConversionRequest struct {
|
|||||||
URL string `form:"url" binding:"required"`
|
URL string `form:"url" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var stackExchangeRegex = regexp.MustCompile(`https://(.+).stackexchange.com/questions/`)
|
||||||
|
|
||||||
func PostHome(c *gin.Context) {
|
func PostHome(c *gin.Context) {
|
||||||
body := urlConversionRequest{}
|
body := urlConversionRequest{}
|
||||||
|
|
||||||
@ -33,15 +36,24 @@ func PostHome(c *gin.Context) {
|
|||||||
soLink := body.URL
|
soLink := body.URL
|
||||||
|
|
||||||
// validate 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{
|
c.HTML(400, "home.html", gin.H{
|
||||||
"errorMessage": "Invalid stack overflow URL",
|
"errorMessage": "Invalid stack overflow/exchange URL",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// redirect to the proxied thread
|
// if stack overflow, trim https://stackoverflow.com
|
||||||
c.Redirect(302, fmt.Sprintf("/questions/%s", strings.TrimPrefix(soLink, "https://stackoverflow.com/questions/")))
|
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))))
|
||||||
}
|
}
|
||||||
|
@ -178,25 +178,34 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
userDetails := questionMetadata.Find("div.user-details")
|
userDetails := questionMetadata.Find("div.user-details")
|
||||||
|
|
||||||
questionAuthor := ""
|
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) {
|
userDetails.Find("a").Each(func(i int, s *goquery.Selection) {
|
||||||
// get the second
|
// if question has been edited, the author is the second element.
|
||||||
if i == 0 {
|
|
||||||
if s.Text() != "" {
|
|
||||||
// if it's not been edited, it means it's the first
|
|
||||||
questionAuthor = s.Text()
|
|
||||||
questionAuthorURL, _ = s.Attr("href")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise it's the second element
|
if isQuestionEdited {
|
||||||
if i == 1 {
|
if i == 1 {
|
||||||
questionAuthor = s.Text()
|
questionAuthor = s.Text()
|
||||||
questionAuthorURL, _ = s.Attr("href")
|
questionAuthorURL += s.AttrOr("href", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// otherwise it's the first element
|
||||||
|
if i == 0 {
|
||||||
|
questionAuthor = s.Text()
|
||||||
|
questionAuthorURL += s.AttrOr("href", "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
newFilteredQuestion.AuthorName = questionAuthor
|
newFilteredQuestion.AuthorName = questionAuthor
|
||||||
@ -221,17 +230,17 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
|
|
||||||
answerFooter := s.Find("div.mt24")
|
answerFooter := s.Find("div.mt24")
|
||||||
|
|
||||||
answerAuthorURL := ""
|
answerAuthorURL := fmt.Sprintf("https://%s", domain)
|
||||||
answerAuthorName := ""
|
answerAuthorName := ""
|
||||||
answerTimestamp := ""
|
answerTimestamp := ""
|
||||||
|
|
||||||
answerFooter.Find("div.post-signature").Each(func(i int, s *goquery.Selection) {
|
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 {
|
if answerAuthorDetails.Length() > 0 {
|
||||||
questionAuthor := questionAuthorDetails.Find("a").First()
|
questionAuthor := answerAuthorDetails.Find("a").First()
|
||||||
answerAuthorName = html.EscapeString(questionAuthor.Text())
|
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())
|
answerTimestamp = html.EscapeString(s.Find("span.relativetime").Text())
|
||||||
|
@ -8,5 +8,4 @@ type FilteredComment struct {
|
|||||||
AuthorName string
|
AuthorName string
|
||||||
AuthorURL string
|
AuthorURL string
|
||||||
Upvotes string
|
Upvotes string
|
||||||
Domain string
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"anonymousoverflow/src/types"
|
"anonymousoverflow/src/types"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
@ -34,7 +35,7 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commentAuthorURL := ""
|
commentAuthorURL := fmt.Sprintf("https://%s", domain)
|
||||||
|
|
||||||
commentAuthor := commentBody.Find("span.comment-user")
|
commentAuthor := commentBody.Find("span.comment-user")
|
||||||
if commentAuthor.Length() == 0 {
|
if commentAuthor.Length() == 0 {
|
||||||
@ -43,7 +44,7 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commentAuthorURL = commentAuthor.AttrOr("href", "")
|
commentAuthorURL += commentAuthor.AttrOr("href", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
commentTimestamp := commentBody.Find("span.relativetime-clean").Text()
|
commentTimestamp := commentBody.Find("span.relativetime-clean").Text()
|
||||||
@ -54,7 +55,6 @@ func FindAndReturnComments(inHtml, domain string, postLayout *goquery.Selection)
|
|||||||
AuthorName: commentAuthor.Text(),
|
AuthorName: commentAuthor.Text(),
|
||||||
AuthorURL: commentAuthorURL,
|
AuthorURL: commentAuthorURL,
|
||||||
Upvotes: commentScore,
|
Upvotes: commentScore,
|
||||||
Domain: domain,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
comments = append(comments, newFilteredComment)
|
comments = append(comments, newFilteredComment)
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
<div class="comment-author">
|
<div class="comment-author">
|
||||||
Commented {{ $comment.Timestamp }} by
|
Commented {{ $comment.Timestamp }} by
|
||||||
<a
|
<a
|
||||||
href="https://{{ $comment.Domain }}{{ $comment.AuthorURL }}"
|
href="{{ $comment.AuthorURL }}"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
>{{ $comment.AuthorName }}</a
|
>{{ $comment.AuthorName }}</a
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<p class="timestamp">
|
<p class="timestamp">
|
||||||
Asked {{ .question.Timestamp }} by
|
Asked {{ .question.Timestamp }} by
|
||||||
<a
|
<a
|
||||||
href="https://{{ .domain }}{{ .question.AuthorURL }}"
|
href="{{ .question.AuthorURL }}"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
>{{ .question.AuthorName }}</a
|
>{{ .question.AuthorName }}</a
|
||||||
@ -87,7 +87,7 @@
|
|||||||
<div class="answer-author">
|
<div class="answer-author">
|
||||||
Answered {{ $answer.Timestamp }} by
|
Answered {{ $answer.Timestamp }} by
|
||||||
<a
|
<a
|
||||||
href="https://{{ .domain }}{{ $answer.AuthorURL }}"
|
href="{{ $answer.AuthorURL }}"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
>{{ $answer.AuthorName }}</a
|
>{{ $answer.AuthorName }}</a
|
||||||
|
Loading…
x
Reference in New Issue
Block a user