Skip to content

Commit

Permalink
Merge pull request #21 from tapglue/connection-pagination
Browse files Browse the repository at this point in the history
Add pagination to connection listed by state
  • Loading branch information
xla authored Nov 23, 2016
2 parents 4fc137c + d66ec1d commit 89ad687
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
36 changes: 34 additions & 2 deletions core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ConnectionByStateFunc func(
currentApp *app.App,
originID uint64,
state connection.State,
opts connection.QueryOptions,
) (*ConnectionFeed, error)

// ConnectionByState returns all connections for the given origin and state.
Expand All @@ -32,6 +33,7 @@ func ConnectionByState(
currentApp *app.App,
originID uint64,
state connection.State,
opts connection.QueryOptions,
) (*ConnectionFeed, error) {
switch state {
case connection.StatePending, connection.StateConfirmed, connection.StateRejected:
Expand All @@ -41,34 +43,61 @@ func ConnectionByState(
}

ics, err := connections.Query(currentApp.Namespace(), connection.QueryOptions{
Before: opts.Before,
Enabled: &defaultEnabled,
FromIDs: []uint64{originID},
Limit: opts.Limit,
States: []connection.State{state},
})
if err != nil {
return nil, err
}

ocs, err := connections.Query(currentApp.Namespace(), connection.QueryOptions{
Before: opts.Before,
Enabled: &defaultEnabled,
Limit: opts.Limit,
States: []connection.State{state},
ToIDs: []uint64{originID},
})
if err != nil {
return nil, err
}

cons := append(ics, ocs...)

if len(cons) == 0 {
return &ConnectionFeed{
Connections: connection.List{},
UserMap: user.Map{},
}, nil
}

if len(cons) > opts.Limit {
cons = cons[:opts.Limit-1]
}

ids := []uint64{}

for _, c := range cons {
if c.FromID == originID {
ids = append(ids, c.ToID)
} else {
ids = append(ids, c.FromID)
}
}

um, err := user.MapFromIDs(
users,
currentApp.Namespace(),
append(ics.ToIDs(), ocs.FromIDs()...)...,
ids...,
)
if err != nil {
return nil, err
}

return &ConnectionFeed{
Connections: append(ics, ocs...),
Connections: cons,
UserMap: um,
}, nil
}
Expand Down Expand Up @@ -185,6 +214,7 @@ type ConnectionFollowerIDsFunc func(
origin uint64,
) ([]uint64, error)

// ConnectionFollowerIDs returns the list of ids of users who follow origin.
func ConnectionFollowerIDs(
connections connection.Service,
) ConnectionFollowerIDsFunc {
Expand Down Expand Up @@ -326,6 +356,8 @@ type ConnectionFriendIDsFunc func(
origin uint64,
) ([]uint64, error)

// ConnectionFriendIDs returns the list of ids of users who are friends with
// origin.
func ConnectionFriendIDs(connections connection.Service) ConnectionFriendIDsFunc {
return func(currentApp *app.App, origin uint64) ([]uint64, error) {
fs, err := connections.Query(currentApp.Namespace(), connection.QueryOptions{
Expand Down
35 changes: 31 additions & 4 deletions handler/http/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,25 @@ func ConnectionByState(fn core.ConnectionByStateFunc) Handler {
state = extractState(r)
)

feed, err := fn(app, currentUser.ID, state)
opts, err := extractConnectionOpts(r)
if err != nil {
respondError(w, 0, wrapError(ErrBadRequest, err.Error()))
return
}

opts.Before, err = extractTimeCursorBefore(r)
if err != nil {
respondError(w, 0, wrapError(ErrBadRequest, err.Error()))
return
}

opts.Limit, err = extractLimit(r)
if err != nil {
respondError(w, 0, wrapError(ErrBadRequest, err.Error()))
return
}

feed, err := fn(app, currentUser.ID, state, opts)
if err != nil {
respondError(w, 0, err)
return
Expand All @@ -36,6 +54,12 @@ func ConnectionByState(fn core.ConnectionByStateFunc) Handler {
}

respondJSON(w, http.StatusOK, &payloadConnections{
pagination: pagination(
r,
opts.Limit,
connectionCursorAfter(feed.Connections, opts.Limit),
connectionCursorBefore(feed.Connections, opts.Limit),
),
cons: feed.Connections,
origin: currentUser.ID,
userMap: feed.UserMap,
Expand Down Expand Up @@ -571,9 +595,10 @@ func (p *payloadConnection) UnmarshalJSON(raw []byte) error {
}

type payloadConnections struct {
cons connection.List
origin uint64
userMap user.Map
cons connection.List
origin uint64
pagination *payloadPagination
userMap user.Map
}

func (p *payloadConnections) MarshalJSON() ([]byte, error) {
Expand All @@ -582,11 +607,13 @@ func (p *payloadConnections) MarshalJSON() ([]byte, error) {
IncomingCount int `json:"incoming_connections_count"`
Outgoing []*payloadConnection `json:"outgoing"`
OutgoingCount int `json:"outgoing_connections_count"`
Pagination *payloadPagination `json:"paging"`
Users []*payloadUser `json:"users"`
UsersCount int `json:"users_count"`
}{
Incoming: []*payloadConnection{},
Outgoing: []*payloadConnection{},
Pagination: p.pagination,
Users: []*payloadUser{},
UsersCount: len(p.userMap),
}
Expand Down

0 comments on commit 89ad687

Please sign in to comment.