From 099b464f84b13d8b8446d2631de8c2689f3be989 Mon Sep 17 00:00:00 2001 From: Skip R Date: Thu, 18 Dec 2025 18:28:07 -0800 Subject: [PATCH] client: refactor boot logic into `SetUp` method This method only has heartbeat session population logic for now, so it's actually a no-op during provisioning. However, there's probably some value in "mandating" that clients call this shortly after construction, so we have a chance to run any setup logic that we might need in the future. This _feels_ unidiomatic...? --- pkg/connector/client.go | 29 ++++++++++++++++++++++------- pkg/connector/login_token.go | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index cb92dd5..66d3a91 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -46,16 +46,9 @@ type DiscordClient struct { } func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error { - log := login.Log meta := login.Metadata.(*UserLoginMetadata) session, err := discordgo.New(meta.Token) - if meta.HeartbeatSession.IsExpired() { - log.Info().Msg("Heartbeat session expired, creating a new one") - meta.HeartbeatSession = discordgo.NewHeartbeatSession() - } - meta.HeartbeatSession.BumpLastUsed() - session.HeartbeatSession = meta.HeartbeatSession login.Save(ctx) if err != nil { @@ -71,6 +64,8 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us ReuploadMedia: d.ReuploadMedia, }, } + cl.SetUp(ctx, meta) + login.Client = &cl return nil @@ -78,6 +73,26 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us var _ bridgev2.NetworkAPI = (*DiscordClient)(nil) +// SetUp performs basic bookkeeping and initialization that should be done +// immediately after a DiscordClient has been created. +// +// nil may be passed for meta, especially during provisioning where we need to +// connect to the Discord gateway, but don't have a UserLogin yet. +func (d *DiscordClient) SetUp(ctx context.Context, meta *UserLoginMetadata) { + log := zerolog.Ctx(ctx) + + // We'll have UserLogin metadata if this UserLogin is being loaded from the + // database, i.e. it hasn't just been provisioned. + if meta != nil { + if meta.HeartbeatSession.IsExpired() { + log.Info().Msg("Heartbeat session expired, creating a new one") + meta.HeartbeatSession = discordgo.NewHeartbeatSession() + } + meta.HeartbeatSession.BumpLastUsed() + d.Session.HeartbeatSession = meta.HeartbeatSession + } +} + func (d *DiscordClient) Connect(ctx context.Context) { log := zerolog.Ctx(ctx) diff --git a/pkg/connector/login_token.go b/pkg/connector/login_token.go index fd479fa..d499343 100644 --- a/pkg/connector/login_token.go +++ b/pkg/connector/login_token.go @@ -84,6 +84,7 @@ func (dl *DiscordTokenLogin) SubmitUserInput(ctx context.Context, input map[stri connector: dl.connector, Session: session, } + client.SetUp(ctx, nil) err = client.connect(ctx) if err != nil { dl.softlyCloseSession()