parent
4534f130b3
commit
817306b1ad
3 changed files with 30 additions and 1 deletions
|
|
@ -16,5 +16,5 @@ func New(id discordgateway.Identifier) *discordgateway.Gateway {
|
||||||
|
|
||||||
conn := ws.NewConnWithDialer(codec, NewDialer())
|
conn := ws.NewConnWithDialer(codec, NewDialer())
|
||||||
socket := ws.NewCustomWebsocket(conn, discordgateway.AddGatewayParams(gatewayURL))
|
socket := ws.NewCustomWebsocket(conn, discordgateway.AddGatewayParams(gatewayURL))
|
||||||
return discordgateway.FromWebsocketGateway(ws.NewGateway(socket, nil), discordgateway.State{Identifier: id})
|
return discordgateway.FromWebsocketGateway(ws.NewGateway(socket, &discordgateway.DefaultGatewayOpts), discordgateway.State{Identifier: id})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
internal/gateway/gateway_test.go
Normal file
17
internal/gateway/gateway_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
discordgateway "github.com/ayn2op/arikawa/v3/gateway"
|
||||||
|
"github.com/ayn2op/arikawa/v3/utils/ws"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
t.Run("authentication failed", func(t *testing.T) {
|
||||||
|
opts := New(discordgateway.DefaultIdentifier("token")).Opts()
|
||||||
|
if !opts.ErrorIsFatalClose(&ws.CloseEvent{Code: 4004}) {
|
||||||
|
t.Error("gateway reconnects after the token is rejected")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -2,22 +2,34 @@ package chat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ayn2op/arikawa/v3/discord"
|
"github.com/ayn2op/arikawa/v3/discord"
|
||||||
"github.com/ayn2op/arikawa/v3/gateway"
|
"github.com/ayn2op/arikawa/v3/gateway"
|
||||||
|
"github.com/ayn2op/arikawa/v3/utils/ws"
|
||||||
"github.com/ayn2op/discordo/internal/ui/chat/composer"
|
"github.com/ayn2op/discordo/internal/ui/chat/composer"
|
||||||
"github.com/ayn2op/discordo/internal/ui/chat/guildstree"
|
"github.com/ayn2op/discordo/internal/ui/chat/guildstree"
|
||||||
"github.com/ayn2op/ningen/v3"
|
"github.com/ayn2op/ningen/v3"
|
||||||
"github.com/ayn2op/tview"
|
"github.com/ayn2op/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// closeCodeAuthenticationFailed is the close code of the gateway for a token it rejects.
|
||||||
|
const closeCodeAuthenticationFailed = 4004
|
||||||
|
|
||||||
func openState(state *ningen.State) tview.Cmd {
|
func openState(state *ningen.State) tview.Cmd {
|
||||||
return func() tview.Msg {
|
return func() tview.Msg {
|
||||||
if err := state.Open(context.Background()); err != nil {
|
if err := state.Open(context.Background()); err != nil {
|
||||||
slog.Error("failed to open chat state", "err", err)
|
slog.Error("failed to open chat state", "err", err)
|
||||||
|
|
||||||
|
// A rejected token never works again, so ask for a new one.
|
||||||
|
var event *ws.CloseEvent
|
||||||
|
if errors.As(err, &event) && event.Code == closeCodeAuthenticationFailed {
|
||||||
|
return LogoutMsg{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue