get post ID from context

This commit is contained in:
Andre Medeiros 2021-07-12 15:51:02 -04:00
parent 147f1e924c
commit 06465ebf19
2 changed files with 17 additions and 1 deletions

View file

@ -460,7 +460,7 @@ func payloadFromMessage(acct *data.Account, msg *reddit.MessageData, badgeCount
break break
case (msg.Kind == "t1" && msg.Type == "comment_reply"): case (msg.Kind == "t1" && msg.Type == "comment_reply"):
title := fmt.Sprintf(`%s in “%s”`, msg.Author, postTitle) title := fmt.Sprintf(`%s in “%s”`, msg.Author, postTitle)
_, postID := reddit.SplitID(msg.ParentID) postID := reddit.PostIDFromContext(msg.Context)
payload = payload. payload = payload.
AlertTitle(title). AlertTitle(title).
Category("inbox-comment-reply"). Category("inbox-comment-reply").

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptrace" "net/http/httptrace"
"regexp"
"strings" "strings"
"time" "time"
@ -34,6 +35,21 @@ func SplitID(id string) (string, string) {
return "", "" 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 { func NewClient(id, secret string, statsd *statsd.Client) *Client {
tracer := &httptrace.ClientTrace{ tracer := &httptrace.ClientTrace{
GotConn: func(info httptrace.GotConnInfo) { GotConn: func(info httptrace.GotConnInfo) {