Skip to content

Commit

Permalink
Fixed invalid links with text
Browse files Browse the repository at this point in the history
Bumped version
  • Loading branch information
HirbodBehnam committed Aug 3, 2024
1 parent 0c0c8e0 commit 49026c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
12 changes: 6 additions & 6 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RunBot(token string, allowedUsers AllowedUsers) {

// fetchPostDetailsAndSend gets the basic info about the post being sent to us
func fetchPostDetailsAndSend(text string, chatID int64, messageID int) {
result, fetchErr := RedditOauth.StartFetch(text)
result, realPostUrl, fetchErr := RedditOauth.StartFetch(text)
if fetchErr != nil {
msg := tgbotapi.NewMessage(chatID, fetchErr.BotError)
msg.ReplyToMessageID = messageID
Expand All @@ -78,9 +78,9 @@ func fetchPostDetailsAndSend(text string, chatID int64, messageID int) {
msg.ParseMode = MarkdownV2
switch data := result.(type) {
case reddit.FetchResultText:
msg.Text = addLinkIfNeeded(data.Title+"\n"+data.Text, text)
msg.Text = addLinkIfNeeded(data.Title+"\n"+data.Text, realPostUrl)
case reddit.FetchResultComment:
msg.Text = addLinkIfNeeded(data.Text, text)
msg.Text = addLinkIfNeeded(data.Text, realPostUrl)
case reddit.FetchResultMedia:
if len(data.Medias) == 0 {
msg.Text = "No media found!"
Expand All @@ -91,13 +91,13 @@ func fetchPostDetailsAndSend(text string, chatID int64, messageID int) {
if len(data.Medias) == 1 && data.Type != reddit.FetchResultMediaTypePhoto {
switch data.Type {
case reddit.FetchResultMediaTypeGif:
handleGifUpload(data.Medias[0].Link, data.Title, data.ThumbnailLink, text, chatID)
handleGifUpload(data.Medias[0].Link, data.Title, data.ThumbnailLink, realPostUrl, chatID)
return
case reddit.FetchResultMediaTypeVideo:
// If the video does have an audio, ask user if they want the audio
if _, hasAudio := data.HasAudio(); !hasAudio {
// Otherwise, just download the video
handleVideoUpload(data.Medias[0].Link, "", data.Title, data.ThumbnailLink, text, data.Duration, chatID)
handleVideoUpload(data.Medias[0].Link, "", data.Title, data.ThumbnailLink, realPostUrl, data.Duration, chatID)
return
}
}
Expand All @@ -116,7 +116,7 @@ func fetchPostDetailsAndSend(text string, chatID int64, messageID int) {
}
// Insert the id in cache
err := CallbackCache.SetMediaCache(idString, cache.CallbackDataCached{
PostLink: text,
PostLink: realPostUrl,
Links: data.Medias.ToLinkMap(),
Title: data.Title,
ThumbnailLink: data.ThumbnailLink,
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Version is the version of this program :|
const Version = "3.2.0"
const Version = "3.2.1"

// GlobalHttpClient is a http client which all request must be done through it
var GlobalHttpClient = &http.Client{
Expand Down
23 changes: 13 additions & 10 deletions pkg/reddit/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var giphyCommentRegex = regexp.MustCompile(`!\[gif]\(giphy\|(\w+)(?:\|downsized)
// FetchResultComment
// FetchResultMedia
// FetchResultAlbum
func (o *Oauth) StartFetch(postUrl string) (fetchResult interface{}, fetchError *FetchError) {
func (o *Oauth) StartFetch(postUrl string) (fetchResult interface{}, realPostUrl string, fetchError *FetchError) {
// Don't crash the whole application
defer func() {
if r := recover(); r != nil {
Expand All @@ -42,19 +42,19 @@ func (o *Oauth) StartFetch(postUrl string) (fetchResult interface{}, fetchError
}
}()
// Get the post ID
postId, isComment, fetchError := o.getPostID(postUrl)
postId, realPostUrl, isComment, fetchError := o.getPostID(postUrl)
if fetchError != nil {
return
}
if isComment {
root, err := o.GetComment(postId)
if err != nil {
return nil, &FetchError{
return nil, "", &FetchError{
NormalError: "cannot download comment: " + err.Error(),
BotError: "Cannot download comment",
}
}
return getCommentFromRoot(root), nil
return getCommentFromRoot(root), realPostUrl, nil
}
// Now download the json
root, err := o.GetPost(postId)
Expand All @@ -65,12 +65,13 @@ func (o *Oauth) StartFetch(postUrl string) (fetchResult interface{}, fetchError
}
return
}
return getPost(postUrl, root)
fetchResult, fetchError = getPost(postUrl, root)
return
}

// Gets the post ID from a post URL.
// If you use this function, pass false for secondPass.
func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *FetchError) {
func (o *Oauth) getPostID(postUrl string) (postID, realPostUrl string, isComment bool, err *FetchError) {
var u *url.URL = nil
// Check all lines for links. In new reddit update, sharing via Telegram adds the post title at its first
lines := strings.Split(postUrl, "\n")
Expand All @@ -79,6 +80,7 @@ func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *F
line = "https://" + line
}
u, _ = url.Parse(line)
realPostUrl = line
if u == nil {
continue
}
Expand All @@ -91,7 +93,7 @@ func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *F
continue
}
// redd.it links are never comments
return p, false, nil
return p, realPostUrl, false, nil
}
if u.Host == "v.redd.it" {
followedUrl, err := o.FollowRedirect(line)
Expand All @@ -107,6 +109,7 @@ func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *F
u = nil // this is for last loop. If u is nil after that final loop, it means that there is no reddit url in text
}
if u == nil {
realPostUrl = ""
err = &FetchError{
NormalError: "",
BotError: "Cannot parse reddit the url. Does your text contain a reddit url?",
Expand All @@ -115,7 +118,7 @@ func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *F
}
split := strings.Split(u.Path, "/")
if len(split) == 2 { // www.reddit.com/x
return split[1], false, nil
return split[1], realPostUrl, false, nil
}
if len(split) < 5 {
err = &FetchError{
Expand Down Expand Up @@ -143,9 +146,9 @@ func (o *Oauth) getPostID(postUrl string) (postID string, isComment bool, err *F
return o.getPostID(followedUrl)
}
if len(split) >= 7 && split[6] != "" {
return split[6], true, nil
return split[6], realPostUrl, true, nil
}
return split[4], false, nil
return split[4], realPostUrl, false, nil
}

// getCommentFromRoot gets the comment content from root of the json API.
Expand Down
11 changes: 10 additions & 1 deletion pkg/reddit/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func TestGetPostId(t *testing.T) {
NeedsInternet bool
ExpectedID string
ExpectedIsComment bool
ExpectedRealUrl string
ExpectedError string // is empty if no error must be thrown
}{
{
Expand Down Expand Up @@ -251,6 +252,7 @@ func TestGetPostId(t *testing.T) {
Url: "Prop Hunt Was Fun\nhttps://www.reddit.com/r/Unexpected/comments/wul62b/prop_hunt_was_fun/\nhttps://google.com",
NeedsInternet: false,
ExpectedID: "wul62b",
ExpectedRealUrl: "https://www.reddit.com/r/Unexpected/comments/wul62b/prop_hunt_was_fun/",
ExpectedIsComment: false,
ExpectedError: "",
},
Expand Down Expand Up @@ -299,6 +301,7 @@ func TestGetPostId(t *testing.T) {
Url: "reddit.com/r/dankmemes/comments/kmi4d3/invest_in_sliding_gif_memes/?utm_medium=android_app&utm_source=share",
NeedsInternet: false,
ExpectedID: "kmi4d3",
ExpectedRealUrl: "https://reddit.com/r/dankmemes/comments/kmi4d3/invest_in_sliding_gif_memes/?utm_medium=android_app&utm_source=share",
ExpectedIsComment: false,
ExpectedError: "",
},
Expand All @@ -307,6 +310,7 @@ func TestGetPostId(t *testing.T) {
Url: "https://reddit.com/r/UkraineWarVideoReport/s/AKk56RlMN6",
NeedsInternet: true,
ExpectedID: "15ma9tp",
ExpectedRealUrl: "https://www.reddit.com/r/UkraineWarVideoReport/comments/15ma9tp/zagorsk_opticalmechanical_plant_in_sergiev_posad/?share_id=v1sVrE120zX-Zw6mWnAID&utm_content=1&utm_medium=android_app&utm_name=androidcss&utm_source=share&utm_term=2",
ExpectedIsComment: false,
ExpectedError: "",
},
Expand Down Expand Up @@ -341,10 +345,15 @@ func TestGetPostId(t *testing.T) {
}
}
// Get the id
id, isComment, err := oauth.getPostID(test.Url)
id, realPostUrl, isComment, err := oauth.getPostID(test.Url)
if err != nil {
assert.Equal(t, test.ExpectedError, err.BotError)
}
if test.ExpectedRealUrl == "" {
assert.Equal(t, test.Url, realPostUrl)
} else {
assert.Equal(t, test.ExpectedRealUrl, realPostUrl)
}
assert.Equal(t, test.ExpectedIsComment, isComment)
assert.Equal(t, test.ExpectedID, id)
})
Expand Down

0 comments on commit 49026c4

Please sign in to comment.