Skip to content

Commit

Permalink
reddit: handle crossposts
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Feb 28, 2020
1 parent f15f142 commit 29e8e69
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/google/safebrowsing v0.0.0-20190624211811-bbf0d20d26b3
github.com/gorilla/schema v1.1.0
github.com/jarcoal/httpmock v1.0.4 // indirect
Expand All @@ -36,7 +36,7 @@ require (
github.com/jonas747/dshardorchestrator/v2 v2.0.1-0.20200203181047-f9a62c68f600
github.com/jonas747/dstate v1.0.5
github.com/jonas747/dutil v0.0.3
github.com/jonas747/go-reddit v0.0.0-20181219005523-28353ff8e846
github.com/jonas747/go-reddit v0.1.1
github.com/jonas747/jdshardmanager v1.0.1
github.com/jonas747/ogg v0.0.0-20161220051205-b4f6f4cf3757 // indirect
github.com/jonas747/retryableredis v1.0.2
Expand Down Expand Up @@ -74,8 +74,8 @@ require (
goji.io v2.0.2+incompatible
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
golang.org/x/image v0.0.0-20190902063713-cb417be4ba39
golang.org/x/net v0.0.0-20200202094626-16171245cfb2
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
Expand Down Expand Up @@ -210,6 +212,10 @@ github.com/jonas747/dutil v0.0.3 h1:Qe4Qls2sBjAvBOqregFLyDuWG/gonK8BiqT9DHRhIg0=
github.com/jonas747/dutil v0.0.3/go.mod h1:oMJ++hvPrAb7Kl2aAHntvjiMXQWfja5t8ZvX13RMbuE=
github.com/jonas747/go-reddit v0.0.0-20181219005523-28353ff8e846 h1:qIj0ZTvzN3W4QFmf3Xw7vBwbLfXnqoepH6ma8hOEWD0=
github.com/jonas747/go-reddit v0.0.0-20181219005523-28353ff8e846/go.mod h1:9HN8PKMjEmDmJE3C71GsR6piXo1AxPrkxjQ/nZOe8Lo=
github.com/jonas747/go-reddit v0.1.0 h1:vL88sUkdDDYv3qtW+OQtTuwBF+WVhXHWqZPgozmFvNM=
github.com/jonas747/go-reddit v0.1.0/go.mod h1:9HN8PKMjEmDmJE3C71GsR6piXo1AxPrkxjQ/nZOe8Lo=
github.com/jonas747/go-reddit v0.1.1 h1:WObqltOmk97cbBAZ3HTaH8fZeri4ZxUrdW1YBFRXzMk=
github.com/jonas747/go-reddit v0.1.1/go.mod h1:9HN8PKMjEmDmJE3C71GsR6piXo1AxPrkxjQ/nZOe8Lo=
github.com/jonas747/gojay v0.0.0-20190906102056-b3bd5c8fcd50 h1:wsd5JxAZeqlRlj2tWklZmlWB7ojU7ksPR2X0rk4VzWI=
github.com/jonas747/gojay v0.0.0-20190906102056-b3bd5c8fcd50/go.mod h1:e5tzLnqryDTnlHxS8vbZRvoMiO851Qb02WY2EoC6Dzs=
github.com/jonas747/jdshardmanager v1.0.1 h1:cO2Ivj5pReBCQc2iPaPIqdAr413Yq0Rj0O5Bw1NhFK0=
Expand Down Expand Up @@ -437,12 +443,16 @@ golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/perf v0.0.0-20180704124530-6e6d33e29852/go.mod h1:JLpeXjPJfIyPr5TlbXLkXWLhP8nz10XfvxElABhCtcw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
45 changes: 43 additions & 2 deletions reddit/redditbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,28 @@ func CreatePostMessage(post *reddit.Link) (string, *discordgo.MessageEmbed) {
html.UnescapeString(post.Title), post.Author, "https://redd.it/"+post.ID)

plainBody := ""
parentSpoiler := false
if post.IsSelf {
plainBody = common.CutStringShort(html.UnescapeString(post.Selftext), 250)
} else if post.CrosspostParent != "" && len(post.CrosspostParentList) > 0 {
// Handle cross posts
parent := post.CrosspostParentList[0]
plainBody += "**" + html.UnescapeString(parent.Title) + "**\n"

if parent.IsSelf {
plainBody += common.CutStringShort(html.UnescapeString(parent.Selftext), 250)
} else {
plainBody += parent.URL
}

if parent.Spoiler {
parentSpoiler = true
}
} else {
plainBody = post.URL
}

if post.Spoiler {
if post.Spoiler || parentSpoiler {
plainMessage += "|| " + plainBody + " ||"
} else {
plainMessage += plainBody
Expand All @@ -264,6 +279,7 @@ func CreatePostMessage(post *reddit.Link) (string, *discordgo.MessageEmbed) {
embed.URL = "https://redd.it/" + post.ID

if post.IsSelf {
// Handle Self posts
embed.Title = "New self post"
if post.Spoiler {
embed.Description += "|| " + common.CutStringShort(html.UnescapeString(post.Selftext), 250) + " ||"
Expand All @@ -272,7 +288,32 @@ func CreatePostMessage(post *reddit.Link) (string, *discordgo.MessageEmbed) {
}

embed.Color = 0xc3fc7e
} else if post.CrosspostParent != "" && len(post.CrosspostParentList) > 0 {
// Handle crossposts
embed.Title = "New Crosspost"

parent := post.CrosspostParentList[0]
embed.Description += "**" + html.UnescapeString(parent.Title) + "**\n"
if parent.IsSelf {
// Cropsspost was a self post
embed.Color = 0xc3fc7e
if parent.Spoiler {
embed.Description += "|| " + common.CutStringShort(html.UnescapeString(parent.Selftext), 250) + " ||"
} else {
embed.Description += common.CutStringShort(html.UnescapeString(parent.Selftext), 250)
}
} else {
// cross post was a link most likely
embed.Color = 0x718aed
embed.Description += parent.URL
if parent.Media.Type == "" && !parent.Spoiler {
embed.Image = &discordgo.MessageEmbedImage{
URL: parent.URL,
}
}
}
} else {
// Handle Link posts
embed.Color = 0x718aed
embed.Title = "New link post"
embed.Description += post.URL
Expand All @@ -288,7 +329,7 @@ func CreatePostMessage(post *reddit.Link) (string, *discordgo.MessageEmbed) {
embed.Title += " [spoiler]"
}

plainMessage = common.EscapeSpecialMentions(plainMessage)
plainMessage = common.EscapeMentionsFromOutsideSource(plainMessage)
return plainMessage, embed
}

Expand Down

0 comments on commit 29e8e69

Please sign in to comment.