Skip to content

Commit

Permalink
Include only posts (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b authored Aug 17, 2023
1 parent 887c579 commit d61f60b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
9 changes: 7 additions & 2 deletions db/gen/coredb/recommend.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/queries/core/recommend.sql
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ from feed_entity_scores f1
where f1.created_at > @window_end::timestamptz
and (@include_viewer::bool or f1.actor_id != @viewer_id)
and (@include_posts::bool or f1.feed_entity_type != @post_entity_type)
and (@include_events::bool or f1.feed_entity_type != @feed_entity_type)
and not (f1.action = any(@excluded_feed_actions::varchar[]))
union
select *
Expand Down
59 changes: 46 additions & 13 deletions publicapi/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,41 @@ func (api FeedAPI) GlobalFeed(ctx context.Context, before *string, after *string
return paginator.paginate(before, after, first, last)
}

type feedParams struct {
ExcludeUserID persist.DBID
IncludePosts bool
IncludeEvents bool
ExcludeActions []persist.Action
FetchFrom time.Duration
}

func fetchFeedEntities(ctx context.Context, queries *db.Queries, p feedParams) ([]db.FeedEntityScore, error) {
var q db.GetFeedEntityScoresParams

q.IncludeViewer = true
q.IncludePosts = true
q.IncludeEvents = true
q.WindowEnd = time.Now().Add(-p.FetchFrom)
q.ExcludedFeedActions = util.MapWithoutError(p.ExcludeActions, func(a persist.Action) string { return string(a) })

if !p.IncludePosts {
q.IncludePosts = false
q.PostEntityType = int32(persist.PostTypeTag)
}

if !p.IncludeEvents {
q.IncludeEvents = false
q.FeedEntityType = int32(persist.FeedEventTypeTag)
}

if p.ExcludeUserID != "" {
q.IncludeViewer = false
q.ViewerID = p.ExcludeUserID
}

return queries.GetFeedEntityScores(ctx, q)
}

func (api FeedAPI) TrendingFeed(ctx context.Context, before *string, after *string, first *int, last *int, includePosts bool) ([]any, PageInfo, error) {
// Validate
if err := validatePaginationParams(api.validator, first, last); err != nil {
Expand All @@ -334,12 +369,11 @@ func (api FeedAPI) TrendingFeed(ctx context.Context, before *string, after *stri

if !hasCursors {
calcFunc := func(ctx context.Context) ([]persist.FeedEntityType, []persist.DBID, error) {
trendData, err := api.queries.GetFeedEntityScores(ctx, db.GetFeedEntityScoresParams{
WindowEnd: time.Now().Add(-time.Duration(3 * 24 * time.Hour)),
PostEntityType: int32(persist.PostTypeTag),
ExcludedFeedActions: []string{string(persist.ActionUserCreated), string(persist.ActionUserFollowedUsers)},
IncludeViewer: true,
IncludePosts: includePosts,
trendData, err := fetchFeedEntities(ctx, api.queries, feedParams{
IncludePosts: includePosts,
IncludeEvents: true,
ExcludeActions: []persist.Action{persist.ActionUserCreated, persist.ActionUserFollowedUsers},
FetchFrom: time.Duration(3 * 24 * time.Hour),
})
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -438,13 +472,12 @@ func (api FeedAPI) CuratedFeed(ctx context.Context, before, after *string, first
}

if !hasCursors {
trendData, err := api.queries.GetFeedEntityScores(ctx, db.GetFeedEntityScoresParams{
WindowEnd: time.Now().Add(-time.Duration(7 * 24 * time.Hour)),
PostEntityType: int32(persist.PostTypeTag),
ExcludedFeedActions: []string{string(persist.ActionUserCreated), string(persist.ActionUserFollowedUsers)},
IncludeViewer: false,
ViewerID: userID,
IncludePosts: includePosts,
trendData, err := fetchFeedEntities(ctx, api.queries, feedParams{
IncludePosts: includePosts,
IncludeEvents: !includePosts,
ExcludeUserID: userID,
ExcludeActions: []persist.Action{persist.ActionUserCreated, persist.ActionUserFollowedUsers},
FetchFrom: time.Duration(7 * 24 * time.Hour),
})
if err != nil {
return nil, PageInfo{}, err
Expand Down

0 comments on commit d61f60b

Please sign in to comment.