From 06465ebf19729517f4a8deb82f21ec6d45db770e Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 12 Jul 2021 15:51:02 -0400 Subject: [PATCH] get post ID from context --- cmd/apollo-worker-notifications/main.go | 2 +- internal/reddit/client.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/apollo-worker-notifications/main.go b/cmd/apollo-worker-notifications/main.go index 890e266..9678610 100644 --- a/cmd/apollo-worker-notifications/main.go +++ b/cmd/apollo-worker-notifications/main.go @@ -460,7 +460,7 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount break case (msg.Kind == "t1" && msg.Type == "comment_reply"): title := fmt.Sprintf(`%s in ā€œ%sā€`, msg.Author, postTitle) - _, postID := reddit.SplitID(msg.ParentID) + postID := reddit.PostIDFromContext(msg.Context) payload = payload. AlertTitle(title). Category("inbox-comment-reply"). diff --git a/internal/reddit/client.go b/internal/reddit/client.go index 4b7c1c8..c9d7b4f 100644 --- a/internal/reddit/client.go +++ b/internal/reddit/client.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "net/http/httptrace" + "regexp" "strings" "time" @@ -34,6 +35,21 @@ func SplitID(id string) (string, string) { return "", "" } +func PostIDFromContext(context string) string { + exps := []*regexp.Regexp{ + regexp.MustCompile(`\/r\/[^\/]*\/comments\/([^\/]*)\/.*`), + } + + for _, exp := range exps { + matches := exp.FindStringSubmatch(context) + if len(matches) != 2 { + continue + } + return matches[1] + } + return "" +} + func NewClient(id, secret string, statsd *statsd.Client) *Client { tracer := &httptrace.ClientTrace{ GotConn: func(info httptrace.GotConnInfo) {