Skip to content

Commit

Permalink
[AC-99] enrichment
Browse files Browse the repository at this point in the history
  • Loading branch information
Link512 committed Dec 11, 2018
1 parent d2970a6 commit 4bfa9a2
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 47 deletions.
17 changes: 11 additions & 6 deletions activity.go
Expand Up @@ -77,12 +77,17 @@ func (a *Activity) decode(data map[string]interface{}) error {
return nil
}

// baseActivityGroup is the common part of responses obtained from reading normal or enriched aggregated feeds.
type baseActivityGroup struct {
ActivityCount int `json:"activity_count,omitempty"`
ActorCount int `json:"actor_count,omitempty"`
Group string `json:"group,omitempty"`
ID string `json:"id,omitempty"`
Verb string `json:"verb,omitempty"`
}

// ActivityGroup is a group of Activity obtained from aggregated feeds.
type ActivityGroup struct {
Activities []Activity `json:"activities,omitempty"`
ActivityCount int `json:"activity_count,omitempty"`
ActorCount int `json:"actor_count,omitempty"`
Group string `json:"group,omitempty"`
ID string `json:"id,omitempty"`
Verb string `json:"verb,omitempty"`
baseActivityGroup
Activities []Activity `json:"activities,omitempty"`
}
24 changes: 24 additions & 0 deletions aggregated_feed.go
Expand Up @@ -33,3 +33,27 @@ func (f *AggregatedFeed) GetNextPageActivities(resp *AggregatedFeedResponse) (*A
}
return f.GetActivities(opts...)
}

// GetEnrichedActivities requests and retrieves the enriched activities and groups for the
// aggregated feed.
func (f *AggregatedFeed) GetEnrichedActivities(opts ...GetActivitiesOption) (*EnrichedAggregatedFeedResponse, error) {
body, err := f.client.getEnrichedActivities(f, opts...)
if err != nil {
return nil, err
}
var resp EnrichedAggregatedFeedResponse
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
return &resp, nil
}

// GetNextPageActivities returns the enriched activities for the given AggregatedFeed at the "next" page
// of a previous *AggregatedFeedResponse response, if any.
func (f *AggregatedFeed) GetNextPageEnrichedActivities(resp *EnrichedAggregatedFeedResponse) (*EnrichedAggregatedFeedResponse, error) {
opts, err := resp.parseNext()
if err != nil {
return nil, err
}
return f.GetEnrichedActivities(opts...)
}
30 changes: 22 additions & 8 deletions aggregated_feed_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"testing"

"github.com/GetStream/stream-go2"
stream "github.com/GetStream/stream-go2"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -14,26 +14,33 @@ func TestAggregatedFeedGetActivities(t *testing.T) {
client, requester := newClient(t)
aggregated := newAggregatedFeedWithUserID(client, "123")
testCases := []struct {
opts []stream.GetActivitiesOption
url string
opts []stream.GetActivitiesOption
url string
enrichedURL string
}{
{
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key",
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/aggregated/123/?api_key=key",
},
{
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key&limit=42",
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key&limit=42",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/aggregated/123/?api_key=key&limit=42",
},
{
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42), stream.WithActivitiesOffset(11), stream.WithActivitiesIDGT("aabbcc")},
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42), stream.WithActivitiesOffset(11), stream.WithActivitiesIDGT("aabbcc")},
url: "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/aggregated/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
},
}

for _, tc := range testCases {
_, err := aggregated.GetActivities(tc.opts...)
assert.NoError(t, err)
testRequest(t, requester.req, http.MethodGet, tc.url, "")
_, err = aggregated.GetEnrichedActivities(tc.opts...)
assert.NoError(t, err)
testRequest(t, requester.req, http.MethodGet, tc.enrichedURL, "")
}
}

Expand All @@ -48,6 +55,13 @@ func TestAggregatedFeedGetNextPageActivities(t *testing.T) {
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/feed/aggregated/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":"/api/v1.0/enrich/feed/aggregated/123/?id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25"}`
enrichedResp, err := aggregated.GetEnrichedActivities()
require.NoError(t, err)
_, err = aggregated.GetNextPageEnrichedActivities(enrichedResp)
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/enrich/feed/aggregated/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":123}`
_, err = aggregated.GetActivities()
require.Error(t, err)
Expand Down
9 changes: 9 additions & 0 deletions client.go
Expand Up @@ -443,6 +443,15 @@ func (c *Client) removeActivityByForeignID(feed Feed, foreignID string) error {

func (c *Client) getActivities(feed Feed, opts ...GetActivitiesOption) ([]byte, error) {
endpoint := c.makeEndpoint("feed/%s/%s/", feed.Slug(), feed.UserID())
return c.getActivitiesInternal(endpoint, feed, opts...)
}

func (c *Client) getEnrichedActivities(feed Feed, opts ...GetActivitiesOption) ([]byte, error) {
endpoint := c.makeEndpoint("enrich/feed/%s/%s/", feed.Slug(), feed.UserID())
return c.getActivitiesInternal(endpoint, feed, opts...)
}

func (c *Client) getActivitiesInternal(endpoint endpoint, feed Feed, opts ...GetActivitiesOption) ([]byte, error) {
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
Expand Down
16 changes: 16 additions & 0 deletions enriched_activities.go
@@ -0,0 +1,16 @@
package stream

type EnrichedActivity map[string]interface{}

// EnrichedActivityGroup is a group of enriched Activities obtained from aggregated feeds.
type EnrichedActivityGroup struct {
baseActivityGroup
Activities []EnrichedActivity `json:"activities,omitempty"`
}

// EnrichedNotificationFeedResult is a notification-feed specific response, containing
// the list of enriched activities in the group, plus the extra fields about the group read+seen status.
type EnrichedNotificationFeedResult struct {
baseNotificationFeedResult
Activities []EnrichedActivity `json:"activities"`
}
30 changes: 30 additions & 0 deletions flat_feed.go
Expand Up @@ -41,3 +41,33 @@ func (f *FlatFeed) GetActivitiesWithRanking(ranking string, opts ...GetActivitie
func (f *FlatFeed) GetFollowers(opts ...FollowersOption) (*FollowersResponse, error) {
return f.client.getFollowers(f, opts...)
}

// GetActivities returns the activities for the given FlatFeed, filtering
// results with the provided GetActivitiesOption parameters.
func (f *FlatFeed) GetEnrichedActivities(opts ...GetActivitiesOption) (*EnrichedFlatFeedResponse, error) {
body, err := f.client.getEnrichedActivities(f, opts...)
if err != nil {
return nil, err
}
var resp EnrichedFlatFeedResponse
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
return &resp, nil
}

// GetNextPageActivities returns the activities for the given FlatFeed at the "next" page
// of a previous *EnrichedFlatFeedResponse response, if any.
func (f *FlatFeed) GetNextPageEnrichedActivities(resp *EnrichedFlatFeedResponse) (*EnrichedFlatFeedResponse, error) {
opts, err := resp.parseNext()
if err != nil {
return nil, err
}
return f.GetEnrichedActivities(opts...)
}

// GetActivitiesWithRanking returns the activities (filtered) for the given FlatFeed,
// using the provided ranking method.
func (f *FlatFeed) GetEnrichedActivitiesWithRanking(ranking string, opts ...GetActivitiesOption) (*EnrichedFlatFeedResponse, error) {
return f.GetEnrichedActivities(append(opts, withActivitiesRanking(ranking))...)
}
35 changes: 28 additions & 7 deletions flat_feed_test.go
Expand Up @@ -14,15 +14,18 @@ func TestFlatFeedGetActivities(t *testing.T) {
client, requester := newClient(t)
flat := newFlatFeedWithUserID(client, "123")
testCases := []struct {
opts []stream.GetActivitiesOption
url string
opts []stream.GetActivitiesOption
url string
enrichedURL string
}{
{
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key",
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/flat/123/?api_key=key",
},
{
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key&limit=42",
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key&limit=42",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/flat/123/?api_key=key&limit=42",
},
{
opts: []stream.GetActivitiesOption{
Expand All @@ -33,13 +36,15 @@ func TestFlatFeedGetActivities(t *testing.T) {
stream.WithActivitiesIDLT("ffgghh"),
stream.WithActivitiesIDLTE("iijjkk"),
},
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key&id_gt=aabbcc&id_gte=ccddee&id_lt=ffgghh&id_lte=iijjkk&limit=42&offset=11",
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key&id_gt=aabbcc&id_gte=ccddee&id_lt=ffgghh&id_lte=iijjkk&limit=42&offset=11",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/flat/123/?api_key=key&id_gt=aabbcc&id_gte=ccddee&id_lt=ffgghh&id_lte=iijjkk&limit=42&offset=11",
},
{
opts: []stream.GetActivitiesOption{
stream.WithCustomParam("aaa", "bbb"),
},
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?aaa=bbb&api_key=key",
url: "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?aaa=bbb&api_key=key",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/flat/123/?aaa=bbb&api_key=key",
},
}

Expand All @@ -51,6 +56,14 @@ func TestFlatFeedGetActivities(t *testing.T) {
_, err = flat.GetActivitiesWithRanking("popularity", tc.opts...)
testRequest(t, requester.req, http.MethodGet, fmt.Sprintf("%s&ranking=popularity", tc.url), "")
assert.NoError(t, err)

_, err = flat.GetEnrichedActivities(tc.opts...)
testRequest(t, requester.req, http.MethodGet, tc.enrichedURL, "")
assert.NoError(t, err)

_, err = flat.GetEnrichedActivitiesWithRanking("popularity", tc.opts...)
testRequest(t, requester.req, http.MethodGet, fmt.Sprintf("%s&ranking=popularity", tc.enrichedURL), "")
assert.NoError(t, err)
}
}

Expand All @@ -66,6 +79,14 @@ func TestFlatFeedGetNextPageActivities(t *testing.T) {
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/feed/flat/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":"/api/v1.0/enrich/feed/flat/123/?id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25"}`
enrichedResp, err := flat.GetEnrichedActivities()
require.NoError(t, err)

_, err = flat.GetNextPageEnrichedActivities(enrichedResp)
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/enrich/feed/flat/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":123}`
_, err = flat.GetActivities()
require.Error(t, err)
Expand Down
24 changes: 24 additions & 0 deletions notification_feed.go
Expand Up @@ -30,3 +30,27 @@ func (f *NotificationFeed) GetNextPageActivities(resp *NotificationFeedResponse)
}
return f.GetActivities(opts...)
}

// GetEnrichedActivities returns the enriched activities for the given NotificationFeed, filtering
// results with the provided GetActivitiesOption parameters.
func (f *NotificationFeed) GetEnrichedActivities(opts ...GetActivitiesOption) (*EnrichedNotificationFeedResponse, error) {
body, err := f.client.getEnrichedActivities(f, opts...)
if err != nil {
return nil, err
}
var resp EnrichedNotificationFeedResponse
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
return &resp, nil
}

// GetNextPageEnrichedActivities returns the enriched activities for the given NotificationFeed at the "next" page
// of a previous *NotificationFeedResponse response, if any.
func (f *NotificationFeed) GetNextPageEnrichedActivities(resp *EnrichedNotificationFeedResponse) (*EnrichedNotificationFeedResponse, error) {
opts, err := resp.parseNext()
if err != nil {
return nil, err
}
return f.GetEnrichedActivities(opts...)
}
50 changes: 35 additions & 15 deletions notification_feed_test.go
Expand Up @@ -13,42 +13,54 @@ func TestGetNotificationActivities(t *testing.T) {
client, requester := newClient(t)
notification := newNotificationFeedWithUserID(client, "123")
testCases := []struct {
opts []stream.GetActivitiesOption
url string
opts []stream.GetActivitiesOption
url string
enrichedURL string
}{
{
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key",
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key",
},
{
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&limit=42",
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42)},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&limit=42",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&limit=42",
},
{
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42), stream.WithActivitiesOffset(11), stream.WithActivitiesIDGT("aabbcc")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
opts: []stream.GetActivitiesOption{stream.WithActivitiesLimit(42), stream.WithActivitiesOffset(11), stream.WithActivitiesIDGT("aabbcc")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&id_gt=aabbcc&limit=42&offset=11",
},
{
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkRead(false, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_read=f1%2Cf2%2Cf3",
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkRead(false, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_read=f1%2Cf2%2Cf3",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&mark_read=f1%2Cf2%2Cf3",
},
{
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkRead(true, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_read=true",
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkRead(true, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_read=true",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&mark_read=true",
},
{
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkSeen(false, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_seen=f1%2Cf2%2Cf3",
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkSeen(false, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_seen=f1%2Cf2%2Cf3",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&mark_seen=f1%2Cf2%2Cf3",
},
{
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkSeen(true, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_seen=true",
opts: []stream.GetActivitiesOption{stream.WithNotificationsMarkSeen(true, "f1", "f2", "f3")},
url: "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&mark_seen=true",
enrichedURL: "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&mark_seen=true",
},
}

for _, tc := range testCases {
_, err := notification.GetActivities(tc.opts...)
testRequest(t, requester.req, http.MethodGet, tc.url, "")
assert.NoError(t, err)

_, err = notification.GetEnrichedActivities(tc.opts...)
testRequest(t, requester.req, http.MethodGet, tc.enrichedURL, "")
assert.NoError(t, err)
}
}

Expand All @@ -64,6 +76,14 @@ func TestNotificationFeedGetNextPageActivities(t *testing.T) {
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/feed/notification/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":"/api/v1.0/enrich/feed/notification/123/?id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25"}`
enrichedResp, err := notification.GetEnrichedActivities()
require.NoError(t, err)

_, err = notification.GetNextPageEnrichedActivities(enrichedResp)
testRequest(t, requester.req, http.MethodGet, "https://api.stream-io-api.com/api/v1.0/enrich/feed/notification/123/?api_key=key&id_lt=78c1a709-aff2-11e7-b3a7-a45e60be7d3b&limit=25", "")
require.NoError(t, err)

requester.resp = `{"next":123}`
_, err = notification.GetActivities()
require.Error(t, err)
Expand Down

0 comments on commit 4bfa9a2

Please sign in to comment.