Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination to connection listed by state #21

Merged
merged 1 commit into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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