Skip to content

Commit

Permalink
conversation list and user list test server methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 8, 2023
1 parent 57b7670 commit 44d1d25
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 19 deletions.
84 changes: 82 additions & 2 deletions internal/chunk/chunktest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"strconv"

"github.com/rusq/dlog"
"github.com/rusq/slackdump/v2/internal/chunk"
"github.com/slack-go/slack"

"github.com/rusq/slackdump/v2/internal/chunk"
)

// Server is a test server for testing the chunk package.
Expand Down Expand Up @@ -55,6 +56,8 @@ func router(p *chunk.Player) *http.ServeMux {
mux.HandleFunc("/api/conversations.info", handleConversationsInfo(p))
mux.HandleFunc("/api/conversations.history", handleConversationsHistory(p))
mux.HandleFunc("/api/conversations.replies", handleConversationsReplies(p))
mux.HandleFunc("/api/conversations.list", handleConversationsList(p))
mux.HandleFunc("/api/users.list", handleUsersList(p))
return mux
}

Expand Down Expand Up @@ -119,7 +122,7 @@ func handleConversationsReplies(p *chunk.Player) http.HandlerFunc {
return
}

var slackResp = slack.SlackResponse{
slackResp := slack.SlackResponse{
Ok: true,
}
msg, err := p.Thread(channel, timestamp)
Expand Down Expand Up @@ -189,3 +192,80 @@ func handleConversationsInfo(p *chunk.Player) http.HandlerFunc {
}
}
}

type channelResponse struct {
Channels []slack.Channel `json:"channels"`
slack.SlackResponse
Metadata slack.ResponseMetadata `json:"response_metadata"`
}

func handleConversationsList(p *chunk.Player) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, task := trace.NewTask(r.Context(), "conversation.list")
defer task.End()

c, err := p.Channels()
sr := slack.SlackResponse{
Ok: true,
ResponseMetadata: slack.ResponseMetadata{
Cursor: "moar",
},
}
if err != nil {
if errors.Is(err, io.EOF) {
sr.Ok = false
sr.ResponseMetadata.Cursor = ""
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
resp := channelResponse{
Channels: c,
SlackResponse: sr,
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

type userResponseFull struct {
Users []slack.User `json:"users,omitempty"`
User slack.User `json:"user,omitempty"`
Members []slack.User `json:"members"`
slack.SlackResponse
slack.UserPresence
Metadata slack.ResponseMetadata `json:"response_metadata"`
}

func handleUsersList(p *chunk.Player) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, task := trace.NewTask(r.Context(), "users.list")
defer task.End()

sr := slack.SlackResponse{
Ok: true,
}
u, err := p.Users()
if err != nil {
if errors.Is(err, io.EOF) {
sr.Ok = false
sr.Error = "pagination complete"
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

resp := userResponseFull{
Users: u,
SlackResponse: sr,
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
60 changes: 43 additions & 17 deletions internal/chunk/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (p *Player) Offset() int64 {
return p.lastOffset.Load()
}

// tryGetChunk tries to get the chunk for the given id. It returns io.EOF if
// there are no more chunks for the given id.
// tryGetChunk tries to get the next chunk for the given id. It returns
// io.EOF if there are no more chunks for the given id.
func (p *Player) tryGetChunk(id string) (*Chunk, error) {
offsets, ok := p.idx[id]
if !ok {
Expand Down Expand Up @@ -116,6 +116,39 @@ func (p *Player) tryGetChunk(id string) (*Chunk, error) {
return &chunk, nil
}

// Messages returns the next message chunk for the given channel.
func (p *Player) Messages(channelID string) ([]slack.Message, error) {
chunk, err := p.tryGetChunk(channelID)
if err != nil {
return nil, err
}
return chunk.Messages, nil
}

// Users returns the next users chunk.
func (p *Player) Users() ([]slack.User, error) {
chunk, err := p.tryGetChunk(userChunkID)
if err != nil {
return nil, err
}
return chunk.Users, nil
}

// Channels returns the next channels chunk.
func (p *Player) Channels() ([]slack.Channel, error) {
chunk, err := p.tryGetChunk(channelChunkID)
if err != nil {
return nil, err
}
return chunk.Channels, nil
}

// HasMoreMessages returns true if there are more messages to be read for the
// channel.
func (p *Player) HasMoreMessages(channelID string) bool {
return p.hasMore(channelID)
}

// hasMore returns true if there are more chunks for the given id.
func (p *Player) hasMore(id string) bool {
offsets, ok := p.idx[id]
Expand All @@ -130,19 +163,16 @@ func (p *Player) hasMore(id string) bool {
return ptr < len(offsets)
}

// Messages returns the messages for the given channel.
func (p *Player) Messages(channelID string) ([]slack.Message, error) {
chunk, err := p.tryGetChunk(channelID)
if err != nil {
return nil, err
}
return chunk.Messages, nil
func (p *Player) HasMoreThreads(channelID string, threadTS string) bool {
return p.hasMore(threadID(channelID, threadTS))
}

// HasMoreMessages returns true if there are more messages to be read for the
// channel.
func (p *Player) HasMoreMessages(channelID string) bool {
return p.hasMore(channelID)
func (p *Player) HasMoreChannels() bool {
return p.hasMore(channelChunkID)
}

func (p *Player) HasMoreUsers() bool {
return p.hasMore(userChunkID)
}

// Thread returns the messages for the given thread.
Expand All @@ -155,10 +185,6 @@ func (p *Player) Thread(channelID string, threadTS string) ([]slack.Message, err
return chunk.Messages, nil
}

func (p *Player) HasMoreThreads(channelID string, threadTS string) bool {
return p.hasMore(threadID(channelID, threadTS))
}

// Reset resets the state of the Player.
func (p *Player) Reset() error {
p.pointer = make(offsets)
Expand Down

0 comments on commit 44d1d25

Please sign in to comment.