Skip to content

Commit

Permalink
Fix metadata fetching for new Facebook contacts (#97)
Browse files Browse the repository at this point in the history
* Fix: Fetch metadata for new contacts

* Change comparison by Scheme from URN to ChannelType
  • Loading branch information
Robi9 authored Feb 17, 2022
1 parent ab998e7 commit b0278f5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
36 changes: 25 additions & 11 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,20 +817,34 @@ func (h *handler) DescribeURN(ctx context.Context, channel courier.Channel, urn
base, _ := url.Parse(graphURL)
path, _ := url.Parse(fmt.Sprintf("/%s", urn.Path()))
u := base.ResolveReference(path)

query := url.Values{}
query.Set("access_token", accessToken)
u.RawQuery = query.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
rr, err := utils.MakeHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("unable to look up contact data:%s\n%s", err, rr.Response)
}

// read our name
name, _ := jsonparser.GetString(rr.Body, "name")
if fmt.Sprint(channel.ChannelType()) == "FBA" {
query.Set("fields", "first_name,last_name")
query.Set("access_token", accessToken)

return map[string]string{"name": name}, nil
u.RawQuery = query.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
rr, err := utils.MakeHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("unable to look up contact data:%s\n%s", err, rr.Response)
}
// read our first and last name
firstName, _ := jsonparser.GetString(rr.Body, "first_name")
lastName, _ := jsonparser.GetString(rr.Body, "last_name")
return map[string]string{"name": utils.JoinNonEmpty(" ", firstName, lastName)}, nil
} else {
query.Set("access_token", accessToken)
u.RawQuery = query.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
rr, err := utils.MakeHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("unable to look up contact data:%s\n%s", err, rr.Response)
}
// read our name
name, _ := jsonparser.GetString(rr.Body, "name")
return map[string]string{"name": name}, nil
}
}

// see https://developers.facebook.com/docs/messenger-platform/webhook#security
Expand Down
34 changes: 29 additions & 5 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,31 @@ func addInvalidSignature(r *http.Request) {
}

// mocks the call to the Facebook graph API
func buildMockFBGraph(testCases []ChannelHandleTestCase) *httptest.Server {
func buildMockFBGraphFBA(testCases []ChannelHandleTestCase) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
accessToken := r.URL.Query().Get("access_token")
defer r.Body.Close()

// invalid auth token
if accessToken != "a123" {
http.Error(w, "invalid auth token", 403)
}

// user has a name
if strings.HasSuffix(r.URL.Path, "1337") {
w.Write([]byte(`{ "first_name": "John", "last_name": "Doe"}`))
return
}
// no name
w.Write([]byte(`{ "first_name": "", "last_name": ""}`))
}))
graphURL = server.URL

return server
}

// mocks the call to the Facebook graph API
func buildMockFBGraphIG(testCases []ChannelHandleTestCase) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
accessToken := r.URL.Query().Get("access_token")
defer r.Body.Close()
Expand All @@ -835,7 +859,7 @@ func buildMockFBGraph(testCases []ChannelHandleTestCase) *httptest.Server {
}

func TestDescribeFBA(t *testing.T) {
fbGraph := buildMockFBGraph(testCasesFBA)
fbGraph := buildMockFBGraphFBA(testCasesFBA)
defer fbGraph.Close()

handler := newHandler("FBA", "Facebook", false).(courier.URNDescriber)
Expand All @@ -853,7 +877,7 @@ func TestDescribeFBA(t *testing.T) {
}

func TestDescribeIG(t *testing.T) {
fbGraph := buildMockFBGraph(testCasesIG)
fbGraph := buildMockFBGraphIG(testCasesIG)
defer fbGraph.Close()

handler := newHandler("IG", "Instagram", false).(courier.URNDescriber)
Expand All @@ -876,12 +900,12 @@ func TestHandler(t *testing.T) {
}

func BenchmarkHandler(b *testing.B) {
fbService := buildMockFBGraph(testCasesFBA)
fbService := buildMockFBGraphFBA(testCasesFBA)

RunChannelBenchmarks(b, testChannelsFBA, newHandler("FBA", "Facebook", false), testCasesFBA)
fbService.Close()

fbServiceIG := buildMockFBGraph(testCasesIG)
fbServiceIG := buildMockFBGraphIG(testCasesIG)

RunChannelBenchmarks(b, testChannelsIG, newHandler("IG", "Instagram", false), testCasesIG)
fbServiceIG.Close()
Expand Down

0 comments on commit b0278f5

Please sign in to comment.