Skip to content

Commit

Permalink
add DMs, but nothing works
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 26, 2024
1 parent 9a25abb commit 2aacd2e
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 63 deletions.
13 changes: 7 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Provider interface {
// retrieved.
Validate() error
// Test tests if credentials are valid.
Test(ctx context.Context) error
Test(ctx context.Context) (*slack.AuthTestResponse, error)
// Client returns an authenticated HTTP client
HTTPClient() (*http.Client, error)
}
Expand Down Expand Up @@ -107,22 +107,23 @@ func IsClientToken(tok string) bool {

// TestAuth attempts to authenticate with the given provider. It will return
// AuthError if failed.
func (s simpleProvider) Test(ctx context.Context) error {
func (s simpleProvider) Test(ctx context.Context) (*slack.AuthTestResponse, error) {
ctx, task := trace.NewTask(ctx, "TestAuth")
defer task.End()

httpCl, err := s.HTTPClient()
if err != nil {
return &Error{Err: err}
return nil, &Error{Err: err}
}
cl := slack.New(s.Token, slack.OptionHTTPClient(httpCl))

region := trace.StartRegion(ctx, "simpleProvider.Test")
defer region.End()
if _, err := cl.AuthTestContext(ctx); err != nil {
return &Error{Err: err}
ai, err := cl.AuthTestContext(ctx)
if err != nil {
return ai, &Error{Err: err}
}
return nil
return ai, nil
}

func (s simpleProvider) HTTPClient() (*http.Client, error) {
Expand Down
33 changes: 14 additions & 19 deletions cmd/slackdump/internal/diag/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package diag
import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/rusq/slackdump/v3"
Expand Down Expand Up @@ -43,35 +42,24 @@ func runEdge(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SAuthError)
return err
}
ai, err := prov.Test(ctx)
if err != nil {
return err
}
lg.Printf("auth test: %+v", ai)

lg.Print("connected")
sd, err := slackdump.New(ctx, prov)
if err != nil {
base.SetExitStatus(base.SAuthError)
return err
}
cl, err := edge.New(cfg.Workspace, sd.Info().TeamID, prov.SlackToken(), prov.Cookies())

cl, err := edge.NewWithProvider(cfg.Workspace, sd.Info().TeamID, prov)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
req := edge.UsersListRequest{
Channels: []string{edgeParams.channel},
Filter: "everyone AND NOT bots AND NOT apps",
Count: 20,
}
resp, err := cl.Post(ctx, "/users/list", &req)
if err != nil {
return err
}
var ur edge.UsersListResponse
if err := cl.ParseResponse(&ur, resp); err != nil {
return err
}
if !ur.Ok {
return fmt.Errorf("error: %s", ur.Error)
}
lg.Printf("%+v\n", ur)

lg.Printf("*** Search for Channels test ***")
channels, err := cl.SearchChannels(ctx, "")
Expand All @@ -82,5 +70,12 @@ func runEdge(ctx context.Context, cmd *base.Command, args []string) error {
enc.SetIndent("", " ")
enc.Encode(channels)

lg.Printf("*** DMs test ***")
dms, err := cl.DMs(ctx)
if err != nil {
return err
}
enc.Encode(dms)

return nil
}
2 changes: 1 addition & 1 deletion internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func tryLoad(ctx context.Context, filename string) (auth.Provider, error) {
return nil, err
}
// test the loaded credentials
if err := authTester(prov, ctx); err != nil {
if _, err := authTester(prov, ctx); err != nil {
return nil, err
}
return prov, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/cache/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/internal/fixtures"
"github.com/rusq/slackdump/v3/internal/mocks/mock_appauth"
Expand Down Expand Up @@ -240,9 +241,9 @@ func TestInitProvider(t *testing.T) {
}
}

func fakeAuthTester(err error) func(_ auth.Provider, ctx context.Context) error {
return func(_ auth.Provider, ctx context.Context) error {
return err
func fakeAuthTester(err error) func(_ auth.Provider, ctx context.Context) (*slack.AuthTestResponse, error) {
return func(_ auth.Provider, ctx context.Context) (*slack.AuthTestResponse, error) {
return nil, err
}
}

Expand Down
19 changes: 11 additions & 8 deletions internal/chunk/chunktest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package chunktest
import (
"context"
"net/http"

"github.com/rusq/slack"
)

// TestAuth to use with the chunktest server.
type TestAuth struct {
FakeToken string
FakeCookies []*http.Cookie
WantValidateError error
WantTestError error
WantHTTPClient *http.Client
WantHTTPClientErr error
FakeToken string
FakeCookies []*http.Cookie
WantValidateError error
WantTestError error
WantHTTPClient *http.Client
WantHTTPClientErr error
WantAuthTestResponse *slack.AuthTestResponse
}

// SlackToken should return the Slack Token value.
Expand All @@ -33,8 +36,8 @@ func (a *TestAuth) Validate() error {
}

// Test tests if credentials are valid.
func (a *TestAuth) Test(ctx context.Context) error {
return a.WantTestError
func (a *TestAuth) Test(ctx context.Context) (*slack.AuthTestResponse, error) {
return a.WantAuthTestResponse, a.WantTestError
}

// HTTPClient returns an authenticated HTTP client
Expand Down
7 changes: 7 additions & 0 deletions internal/edge/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ type UserMembershipResponse struct {
BaseResponse
}

type WebClientFields struct {
XReason string `json:"_x_reason"`
XMode string `json:"_x_mode"`
XSonic bool `json:"_x_sonic"`
XAppName string `json:"_x_app_name"`
}

var ErrNotOK = errors.New("server returned NOT OK")

// GetUsers returns users from the slack edge api for the channel. User IDs
Expand Down
89 changes: 89 additions & 0 deletions internal/edge/dms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package edge

import (
"context"
"log/slog"
"net/url"
"time"
)

type dmsForm struct {
Token string `json:"token"`
Count int `json:"count"`
IncludeClosed bool `json:"include_closed"`
IncludeChannel bool `json:"include_channel"`
ExcludeBots bool `json:"exclude_bots"`
Cursor string `json:"cursor,omitempty"`
WebClientFields
}

func (d dmsForm) Values() url.Values {
return values(d, true)
}

type dmsResponse struct {
BaseResponse
IMs []DM `json:"ims,omitempty"`
MPIMs []DM `json:"mpims,omitempty"` //TODO

}

type DM struct {
ID string `json:"id"`
// Message slack.Message `json:"message,omitempty"`
Channel Channel `json:"channel,omitempty"`
Latest string `json:"latest,omitempty"` // i.e. "1710632873.037269"
}

type Channel struct {
ID string `json:"id"`
Created int64 `json:"created"`
IsFrozen bool `json:"is_frozen"`
IsArchived bool `json:"is_archived"`
IsIM bool `json:"is_im"`
IsOrgShared bool `json:"is_org_shared"`
ContextTeamID string `json:"context_team_id"`
Updated int64 `json:"updated"`
User string `json:"user"`
LastRead string `json:"last_read"`
Latest string `json:"latest"`
IsOpen bool `json:"is_open"`
}

func (cl *Client) DMs(ctx context.Context) ([]DM, error) {
form := dmsForm{
Token: cl.token,
Count: 250,
IncludeClosed: true,
IncludeChannel: true,
ExcludeBots: false,
Cursor: "",
WebClientFields: WebClientFields{
XReason: "dms-tab-populate",
XMode: "online",
XSonic: true,
XAppName: "client",
},
}

var IMs []DM
var url = cl.webapiURL("client.dms")
slog.Info("url", "url", url)
for range 3 {
resp, err := cl.PostFormRaw(ctx, url, form.Values())
if err != nil {
return nil, err
}
r := dmsResponse{}
if err := cl.ParseResponse(&r, resp); err != nil {
return nil, err
}
if r.ResponseMetadata.NextCursor == "" {
break
}
IMs = append(IMs, r.IMs...)
time.Sleep(300 * time.Millisecond) //TODO: hax
form.Cursor = r.ResponseMetadata.NextCursor
}
return IMs, nil
}
13 changes: 11 additions & 2 deletions internal/edge/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -36,6 +38,7 @@ func NewWithClient(workspaceName string, teamID string, token string, cl *http.C
workspaceName: workspaceName,
cl: cl,
token: token,
teamID: teamID,
apiPath: fmt.Sprintf("https://edgeapi.slack.com/cache/%s/", teamID),
}, nil
}
Expand Down Expand Up @@ -71,7 +74,8 @@ type BaseResponse struct {
}

type ResponseMetadata struct {
Messages []string `json:"messages,omitempty"`
Messages []string `json:"messages,omitempty"`
NextCursor string `json:"next_cursor,omitempty"`
}

func (r *BaseRequest) SetToken(token string) {
Expand Down Expand Up @@ -105,7 +109,12 @@ func (cl *Client) Post(ctx context.Context, path string, req PostRequest) (*http

func (cl *Client) ParseResponse(req any, resp *http.Response) error {
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
slog.Info("response", "body", string(data))
dec := json.NewDecoder(bytes.NewReader(data))
return dec.Decode(req)
}

Expand Down
Loading

0 comments on commit 2aacd2e

Please sign in to comment.